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