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        // TODO: Once we're ready to release v0.0.7 of the Zed extension API we want to pin
 60        // ZLS to a specific version with `zed::github_release_by_tag_name`.
 61        // We're pinning ZLS to a release that has `.tar.gz` assets, since the latest release does not have
 62        // them, at time of writing.
 63        //
 64        // ZLS tracking issue: https://github.com/zigtools/zls/issues/1879
 65        // let release = zed::github_release_by_tag_name("zigtools/zls", "0.11.0")?;
 66
 67        let release = zed::latest_github_release(
 68            "zigtools/zls",
 69            zed::GithubReleaseOptions {
 70                require_assets: true,
 71                pre_release: false,
 72            },
 73        )?;
 74
 75        let (platform, arch) = zed::current_platform();
 76        let asset_name = format!(
 77            "zls-{arch}-{os}.{extension}",
 78            arch = match arch {
 79                zed::Architecture::Aarch64 => "aarch64",
 80                zed::Architecture::X86 => "x86",
 81                zed::Architecture::X8664 => "x86_64",
 82            },
 83            os = match platform {
 84                zed::Os::Mac => "macos",
 85                zed::Os::Linux => "linux",
 86                zed::Os::Windows => "windows",
 87            },
 88            extension = match platform {
 89                zed::Os::Mac | zed::Os::Linux => "tar.gz",
 90                zed::Os::Windows => "zip",
 91            }
 92        );
 93
 94        let asset = release
 95            .assets
 96            .iter()
 97            .find(|asset| asset.name == asset_name)
 98            .ok_or_else(|| format!("no asset found matching {:?}", asset_name))?;
 99
100        let version_dir = format!("zls-{}", release.version);
101        let binary_path = format!("{version_dir}/bin/zls");
102
103        if !fs::metadata(&binary_path).map_or(false, |stat| stat.is_file()) {
104            zed::set_language_server_installation_status(
105                &language_server_id,
106                &zed::LanguageServerInstallationStatus::Downloading,
107            );
108
109            zed::download_file(
110                &asset.download_url,
111                &version_dir,
112                match platform {
113                    zed::Os::Mac | zed::Os::Linux => zed::DownloadedFileType::GzipTar,
114                    zed::Os::Windows => zed::DownloadedFileType::Zip,
115                },
116            )
117            .map_err(|e| format!("failed to download file: {e}"))?;
118
119            zed::make_file_executable(&binary_path)?;
120
121            let entries =
122                fs::read_dir(".").map_err(|e| format!("failed to list working directory {e}"))?;
123            for entry in entries {
124                let entry = entry.map_err(|e| format!("failed to load directory entry {e}"))?;
125                if entry.file_name().to_str() != Some(&version_dir) {
126                    fs::remove_dir_all(&entry.path()).ok();
127                }
128            }
129        }
130
131        self.cached_binary_path = Some(binary_path.clone());
132        Ok(ZlsBinary {
133            path: binary_path,
134            args,
135            environment,
136        })
137    }
138}
139
140impl zed::Extension for ZigExtension {
141    fn new() -> Self {
142        Self {
143            cached_binary_path: None,
144        }
145    }
146
147    fn language_server_command(
148        &mut self,
149        language_server_id: &LanguageServerId,
150        worktree: &zed::Worktree,
151    ) -> Result<zed::Command> {
152        let zls_binary = self.language_server_binary(language_server_id, worktree)?;
153        Ok(zed::Command {
154            command: zls_binary.path,
155            args: zls_binary.args.unwrap_or_default(),
156            env: zls_binary.environment.unwrap_or_default(),
157        })
158    }
159
160    fn language_server_workspace_configuration(
161        &mut self,
162        _language_server_id: &zed::LanguageServerId,
163        worktree: &zed::Worktree,
164    ) -> Result<Option<serde_json::Value>> {
165        let settings = LspSettings::for_worktree("zls", worktree)
166            .ok()
167            .and_then(|lsp_settings| lsp_settings.settings.clone())
168            .unwrap_or_default();
169        Ok(Some(settings))
170    }
171}
172
173zed::register_extension!(ZigExtension);