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