1use std::fs;
2use zed_extension_api::{self as zed, Result};
3
4struct CsharpExtension {
5 cached_binary_path: Option<String>,
6}
7
8impl CsharpExtension {
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("OmniSharp") {
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 "OmniSharp/omnisharp-roslyn",
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 "omnisharp-{os}-{arch}-net6.0.{extension}",
40 os = match platform {
41 zed::Os::Mac => "osx",
42 zed::Os::Linux => "linux",
43 zed::Os::Windows => "win",
44 },
45 arch = match arch {
46 zed::Architecture::Aarch64 => "arm64",
47 zed::Architecture::X86 => "x86",
48 zed::Architecture::X8664 => "x64",
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!("omnisharp-{}", release.version);
63 let binary_path = format!("{version_dir}/OmniSharp");
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 let entries =
82 fs::read_dir(".").map_err(|e| format!("failed to list working directory {e}"))?;
83 for entry in entries {
84 let entry = entry.map_err(|e| format!("failed to load directory entry {e}"))?;
85 if entry.file_name().to_str() != Some(&version_dir) {
86 fs::remove_dir_all(&entry.path()).ok();
87 }
88 }
89 }
90
91 self.cached_binary_path = Some(binary_path.clone());
92 Ok(binary_path)
93 }
94}
95
96impl zed::Extension for CsharpExtension {
97 fn new() -> Self {
98 Self {
99 cached_binary_path: None,
100 }
101 }
102
103 fn language_server_command(
104 &mut self,
105 config: zed::LanguageServerConfig,
106 worktree: &zed::Worktree,
107 ) -> Result<zed::Command> {
108 Ok(zed::Command {
109 command: self.language_server_binary_path(config, worktree)?,
110 args: vec!["-lsp".to_string()],
111 env: Default::default(),
112 })
113 }
114}
115
116zed::register_extension!(CsharpExtension);