zig.rs

  1use std::fs;
  2use zed_extension_api::{self as zed, Result};
  3
  4struct ZigExtension {
  5    cached_binary_path: Option<String>,
  6}
  7
  8impl ZigExtension {
  9    fn language_server_binary_path(
 10        &mut self,
 11        config: zed::LanguageServerConfig,
 12        worktree: &zed::Worktree,
 13    ) -> Result<String> {
 14        if let Some(path) = &self.cached_binary_path {
 15            if fs::metadata(path).map_or(false, |stat| stat.is_file()) {
 16                return Ok(path.clone());
 17            }
 18        }
 19
 20        if let Some(path) = worktree.which("zls") {
 21            self.cached_binary_path = Some(path.clone());
 22            return Ok(path);
 23        }
 24
 25        zed::set_language_server_installation_status(
 26            &config.name,
 27            &zed::LanguageServerInstallationStatus::CheckingForUpdate,
 28        );
 29        let release = zed::latest_github_release(
 30            "zigtools/zls",
 31            zed::GithubReleaseOptions {
 32                require_assets: true,
 33                pre_release: false,
 34            },
 35        )?;
 36
 37        let (platform, arch) = zed::current_platform();
 38        let asset_name = format!(
 39            "zls-{arch}-{os}.{extension}",
 40            arch = match arch {
 41                zed::Architecture::Aarch64 => "aarch64",
 42                zed::Architecture::X86 => "x86",
 43                zed::Architecture::X8664 => "x86_64",
 44            },
 45            os = match platform {
 46                zed::Os::Mac => "macos",
 47                zed::Os::Linux => "linux",
 48                zed::Os::Windows => "windows",
 49            },
 50            extension = match platform {
 51                zed::Os::Mac | zed::Os::Linux => "tar.gz",
 52                zed::Os::Windows => "zip",
 53            }
 54        );
 55
 56        let asset = release
 57            .assets
 58            .iter()
 59            .find(|asset| asset.name == asset_name)
 60            .ok_or_else(|| format!("no asset found matching {:?}", asset_name))?;
 61
 62        let version_dir = format!("zls-{}", release.version);
 63        let binary_path = format!("{version_dir}/bin/zls");
 64
 65        if !fs::metadata(&binary_path).map_or(false, |stat| stat.is_file()) {
 66            zed::set_language_server_installation_status(
 67                &config.name,
 68                &zed::LanguageServerInstallationStatus::Downloading,
 69            );
 70
 71            zed::download_file(
 72                &asset.download_url,
 73                &version_dir,
 74                match platform {
 75                    zed::Os::Mac | zed::Os::Linux => zed::DownloadedFileType::GzipTar,
 76                    zed::Os::Windows => zed::DownloadedFileType::Zip,
 77                },
 78            )
 79            .map_err(|e| format!("failed to download file: {e}"))?;
 80
 81            zed::make_file_executable(&binary_path)?;
 82
 83            let entries =
 84                fs::read_dir(".").map_err(|e| format!("failed to list working directory {e}"))?;
 85            for entry in entries {
 86                let entry = entry.map_err(|e| format!("failed to load directory entry {e}"))?;
 87                if entry.file_name().to_str() != Some(&version_dir) {
 88                    fs::remove_dir_all(&entry.path()).ok();
 89                }
 90            }
 91        }
 92
 93        self.cached_binary_path = Some(binary_path.clone());
 94        Ok(binary_path)
 95    }
 96}
 97
 98impl zed::Extension for ZigExtension {
 99    fn new() -> Self {
100        Self {
101            cached_binary_path: None,
102        }
103    }
104
105    fn language_server_command(
106        &mut self,
107        config: zed::LanguageServerConfig,
108        worktree: &zed::Worktree,
109    ) -> Result<zed::Command> {
110        Ok(zed::Command {
111            command: self.language_server_binary_path(config, worktree)?,
112            args: vec![],
113            env: Default::default(),
114        })
115    }
116}
117
118zed::register_extension!(ZigExtension);