zed_odin/
odin.rs

1use std::fs;
2use zed::LanguageServerId;
3use zed_extension_api::{self as zed, Result};
4
5struct OdinExtension {
6    cached_binary_path: Option<String>,
7}
8
9impl OdinExtension {
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("ols") {
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            "DanielGavin/ols",
32            zed::GithubReleaseOptions {
33                require_assets: true,
34                pre_release: true,
35            },
36        )?;
37
38        let (platform, arch) = zed::current_platform();
39        let asset_name = format!(
40            "ols-{arch}-{os}.{extension}",
41            arch = match arch {
42                zed::Architecture::Aarch64 => "arm64",
43                zed::Architecture::X86 => "x86",
44                zed::Architecture::X8664 => "x86_64",
45            },
46            os = match platform {
47                zed::Os::Mac => "darwin",
48                zed::Os::Linux => "unknown-linux-gnu",
49                zed::Os::Windows => "windows-msvc",
50            },
51            extension = match platform {
52                zed::Os::Mac | zed::Os::Linux => "zip",
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!("ols-{}", release.version);
64        fs::create_dir_all(&version_dir)
65            .map_err(|err| format!("failed to create directory '{version_dir}': {err}"))?;
66        let binary_path = format!(
67            "{version_dir}/ols-{arch}-{os}",
68            arch = match arch {
69                zed::Architecture::Aarch64 => "arm64",
70                zed::Architecture::X86 => "x86",
71                zed::Architecture::X8664 => "x86_64",
72            },
73            os = match platform {
74                zed::Os::Mac => "darwin",
75                zed::Os::Linux => "unknown-linux-gnu",
76                zed::Os::Windows => "windows-msvc",
77            },
78        );
79
80        if !fs::metadata(&binary_path).map_or(false, |stat| stat.is_file()) {
81            zed::set_language_server_installation_status(
82                &language_server_id,
83                &zed::LanguageServerInstallationStatus::Downloading,
84            );
85
86            zed::download_file(
87                &asset.download_url,
88                &version_dir,
89                match platform {
90                    zed::Os::Mac | zed::Os::Linux => zed::DownloadedFileType::Zip,
91                    zed::Os::Windows => zed::DownloadedFileType::Zip,
92                },
93            )
94            .map_err(|e| format!("failed to download file: {e}"))?;
95
96            zed::make_file_executable(&binary_path)?;
97
98            let entries =
99                fs::read_dir(".").map_err(|e| format!("failed to list working directory {e}"))?;
100            for entry in entries {
101                let entry = entry.map_err(|e| format!("failed to load directory entry {e}"))?;
102                if entry.file_name().to_str() != Some(&version_dir) {
103                    fs::remove_dir_all(&entry.path()).ok();
104                }
105            }
106        }
107
108        self.cached_binary_path = Some(binary_path.clone());
109        Ok(binary_path)
110    }
111}
112
113impl zed::Extension for OdinExtension {
114    fn new() -> Self {
115        Self {
116            cached_binary_path: None,
117        }
118    }
119
120    fn language_server_command(
121        &mut self,
122        language_server_id: &LanguageServerId,
123        worktree: &zed::Worktree,
124    ) -> Result<zed::Command> {
125        Ok(zed::Command {
126            command: self.language_server_binary_path(language_server_id, worktree)?,
127            args: vec![],
128            env: Default::default(),
129        })
130    }
131}
132
133zed::register_extension!(OdinExtension);