csharp.rs

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