zig.rs

  1use std::fs;
  2use zed_extension_api::{self as zed, serde_json, settings::LspSettings, LanguageServerId, Result};
  3
  4struct ZigExtension {
  5    cached_binary_path: Option<String>,
  6}
  7
  8#[derive(Clone)]
  9struct ZlsBinary {
 10    path: String,
 11    args: Option<Vec<String>>,
 12    environment: Option<Vec<(String, String)>>,
 13}
 14
 15impl ZigExtension {
 16    fn language_server_binary(
 17        &mut self,
 18        language_server_id: &LanguageServerId,
 19        worktree: &zed::Worktree,
 20    ) -> Result<ZlsBinary> {
 21        let mut args: Option<Vec<String>> = None;
 22        let environment = Some(worktree.shell_env());
 23
 24        if let Ok(lsp_settings) = LspSettings::for_worktree("zls", worktree) {
 25            if let Some(binary) = lsp_settings.binary {
 26                args = binary.arguments;
 27                if let Some(path) = binary.path {
 28                    return Ok(ZlsBinary {
 29                        path: path.clone(),
 30                        args,
 31                        environment,
 32                    });
 33                }
 34            }
 35        }
 36
 37        if let Some(path) = worktree.which("zls") {
 38            return Ok(ZlsBinary {
 39                path,
 40                args,
 41                environment,
 42            });
 43        }
 44
 45        if let Some(path) = &self.cached_binary_path {
 46            if fs::metadata(&path).map_or(false, |stat| stat.is_file()) {
 47                return Ok(ZlsBinary {
 48                    path: path.clone(),
 49                    args,
 50                    environment,
 51                });
 52            }
 53        }
 54
 55        zed::set_language_server_installation_status(
 56            &language_server_id,
 57            &zed::LanguageServerInstallationStatus::CheckingForUpdate,
 58        );
 59        // We're pinning ZLS to a release that has `.tar.gz` assets, since the latest release does not have
 60        // them, at time of writing.
 61        //
 62        // ZLS tracking issue: https://github.com/zigtools/zls/issues/1879
 63        let release = zed::github_release_by_tag_name("zigtools/zls", "0.11.0")?;
 64
 65        let (platform, arch) = zed::current_platform();
 66        let asset_name = format!(
 67            "zls-{arch}-{os}.{extension}",
 68            arch = match arch {
 69                zed::Architecture::Aarch64 => "aarch64",
 70                zed::Architecture::X86 => "x86",
 71                zed::Architecture::X8664 => "x86_64",
 72            },
 73            os = match platform {
 74                zed::Os::Mac => "macos",
 75                zed::Os::Linux => "linux",
 76                zed::Os::Windows => "windows",
 77            },
 78            extension = match platform {
 79                zed::Os::Mac | zed::Os::Linux => "tar.gz",
 80                zed::Os::Windows => "zip",
 81            }
 82        );
 83
 84        let asset = release
 85            .assets
 86            .iter()
 87            .find(|asset| asset.name == asset_name)
 88            .ok_or_else(|| format!("no asset found matching {:?}", asset_name))?;
 89
 90        let version_dir = format!("zls-{}", release.version);
 91        let binary_path = format!("{version_dir}/bin/zls");
 92
 93        if !fs::metadata(&binary_path).map_or(false, |stat| stat.is_file()) {
 94            zed::set_language_server_installation_status(
 95                &language_server_id,
 96                &zed::LanguageServerInstallationStatus::Downloading,
 97            );
 98
 99            zed::download_file(
100                &asset.download_url,
101                &version_dir,
102                match platform {
103                    zed::Os::Mac | zed::Os::Linux => zed::DownloadedFileType::GzipTar,
104                    zed::Os::Windows => zed::DownloadedFileType::Zip,
105                },
106            )
107            .map_err(|e| format!("failed to download file: {e}"))?;
108
109            zed::make_file_executable(&binary_path)?;
110
111            let entries =
112                fs::read_dir(".").map_err(|e| format!("failed to list working directory {e}"))?;
113            for entry in entries {
114                let entry = entry.map_err(|e| format!("failed to load directory entry {e}"))?;
115                if entry.file_name().to_str() != Some(&version_dir) {
116                    fs::remove_dir_all(&entry.path()).ok();
117                }
118            }
119        }
120
121        self.cached_binary_path = Some(binary_path.clone());
122        Ok(ZlsBinary {
123            path: binary_path,
124            args,
125            environment,
126        })
127    }
128}
129
130impl zed::Extension for ZigExtension {
131    fn new() -> Self {
132        Self {
133            cached_binary_path: None,
134        }
135    }
136
137    fn language_server_command(
138        &mut self,
139        language_server_id: &LanguageServerId,
140        worktree: &zed::Worktree,
141    ) -> Result<zed::Command> {
142        let zls_binary = self.language_server_binary(language_server_id, worktree)?;
143        Ok(zed::Command {
144            command: zls_binary.path,
145            args: zls_binary.args.unwrap_or_default(),
146            env: zls_binary.environment.unwrap_or_default(),
147        })
148    }
149
150    fn language_server_workspace_configuration(
151        &mut self,
152        _language_server_id: &zed::LanguageServerId,
153        worktree: &zed::Worktree,
154    ) -> Result<Option<serde_json::Value>> {
155        let settings = LspSettings::for_worktree("zls", worktree)
156            .ok()
157            .and_then(|lsp_settings| lsp_settings.settings.clone())
158            .unwrap_or_default();
159        Ok(Some(settings))
160    }
161}
162
163zed::register_extension!(ZigExtension);