Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Extract C# support into an extension (#9971)
Marshall Bowersdf3050dac131
diff --git a/Cargo.lock b/Cargo.lockindex 865965ea7c8..868f5ee1c14 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -5435,7 +5435,6 @@ dependencies = ["tree-sitter","tree-sitter-bash","tree-sitter-c",- "tree-sitter-c-sharp","tree-sitter-clojure","tree-sitter-cpp","tree-sitter-css",@@ -10365,15 +10364,6 @@ dependencies = ["tree-sitter",]-[[package]]-name = "tree-sitter-c-sharp"-version = "0.20.0"-source = "git+https://github.com/tree-sitter/tree-sitter-c-sharp?rev=dd5e59721a5f8dae34604060833902b882023aaf#dd5e59721a5f8dae34604060833902b882023aaf"-dependencies = [- "cc",- "tree-sitter",-]-[[package]]name = "tree-sitter-clojure"version = "0.0.9"@@ -12667,6 +12657,13 @@ dependencies = ["zed_extension_api 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)",]+[[package]]+name = "zed_csharp"+version = "0.0.1"+dependencies = [+ "zed_extension_api 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)",+]+[[package]]name = "zed_extension_api"version = "0.0.4"diff --git a/Cargo.toml b/Cargo.tomlindex db4ea2e7b04..485daad4b60 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -98,6 +98,7 @@ members = ["crates/zed_actions","extensions/astro",+ "extensions/csharp","extensions/gleam","extensions/haskell","extensions/php",@@ -297,7 +298,6 @@ tree-sitter = { version = "0.20", features = ["wasm"] }tree-sitter-bash = { git = "https://github.com/tree-sitter/tree-sitter-bash", rev = "7331995b19b8f8aba2d5e26deb51d2195c18bc94" }tree-sitter-c = "0.20.1"tree-sitter-clojure = { git = "https://github.com/prcastro/tree-sitter-clojure", branch = "update-ts" }-tree-sitter-c-sharp = { git = "https://github.com/tree-sitter/tree-sitter-c-sharp", rev = "dd5e59721a5f8dae34604060833902b882023aaf" }tree-sitter-cpp = { git = "https://github.com/tree-sitter/tree-sitter-cpp", rev = "f44509141e7e483323d2ec178f2d2e6c0fc041c1" }tree-sitter-css = { git = "https://github.com/tree-sitter/tree-sitter-css", rev = "769203d0f9abe1a9a691ac2b9fe4bb4397a73c51" }tree-sitter-dart = { git = "https://github.com/agent3bood/tree-sitter-dart", rev = "48934e3bf757a9b78f17bdfaa3e2b4284656fdc7" }diff --git a/crates/extensions_ui/src/extension_suggest.rs b/crates/extensions_ui/src/extension_suggest.rsindex 704c0584c8f..f000368352e 100644--- a/crates/extensions_ui/src/extension_suggest.rs+++ b/crates/extensions_ui/src/extension_suggest.rs@@ -16,6 +16,7 @@ fn suggested_extensions() -> &'static HashMap<&'static str, Arc<str>> {[("astro", "astro"),("beancount", "beancount"),+ ("csharp", "cs"),("dockerfile", "Dockerfile"),("elisp", "el"),("fish", "fish"),diff --git a/crates/languages/Cargo.toml b/crates/languages/Cargo.tomlindex be06d2266f0..0ecbc2d3fee 100644--- a/crates/languages/Cargo.toml+++ b/crates/languages/Cargo.toml@@ -37,7 +37,6 @@ smol.workspace = truetask.workspace = truetoml.workspace = truetree-sitter-bash.workspace = true-tree-sitter-c-sharp.workspace = truetree-sitter-c.workspace = truetree-sitter-clojure.workspace = truetree-sitter-cpp.workspace = truediff --git a/crates/languages/src/csharp.rs b/crates/languages/src/csharp.rsdeleted file mode 100644index 82fec8468a1..00000000000--- a/crates/languages/src/csharp.rs+++ /dev/null@@ -1,148 +0,0 @@-use anyhow::{anyhow, Context, Result};-use async_compression::futures::bufread::GzipDecoder;-use async_tar::Archive;-use async_trait::async_trait;-use futures::{io::BufReader, StreamExt};-use language::{LanguageServerName, LspAdapterDelegate};-use lsp::LanguageServerBinary;-use smol::fs;-use std::env::consts::ARCH;-use std::ffi::OsString;-use std::{any::Any, path::PathBuf};-use util::{github::latest_github_release, maybe};-use util::{github::GitHubLspBinaryVersion, ResultExt};--pub struct OmniSharpAdapter;--#[async_trait(?Send)]-impl super::LspAdapter for OmniSharpAdapter {- fn name(&self) -> LanguageServerName {- LanguageServerName("OmniSharp".into())- }-- async fn fetch_latest_server_version(- &self,- delegate: &dyn LspAdapterDelegate,- ) -> Result<Box<dyn 'static + Send + Any>> {- let mapped_arch = match ARCH {- "aarch64" => Some("arm64"),- "x86_64" => Some("x64"),- _ => None,- };-- match mapped_arch {- None => Ok(Box::new(())),- Some(arch) => {- let release = latest_github_release(- "OmniSharp/omnisharp-roslyn",- true,- false,- delegate.http_client(),- )- .await?;- let asset_name = format!("omnisharp-osx-{}-net6.0.tar.gz", arch);- let asset = release- .assets- .iter()- .find(|asset| asset.name == asset_name)- .ok_or_else(|| anyhow!("no asset found matching {:?}", asset_name))?;- let version = GitHubLspBinaryVersion {- name: release.tag_name,- url: asset.browser_download_url.clone(),- };-- Ok(Box::new(version) as Box<_>)- }- }- }-- async fn fetch_server_binary(- &self,- version: Box<dyn 'static + Send + Any>,- container_dir: PathBuf,- delegate: &dyn LspAdapterDelegate,- ) -> Result<LanguageServerBinary> {- let version = version.downcast::<GitHubLspBinaryVersion>().unwrap();- let binary_path = container_dir.join("omnisharp");-- if fs::metadata(&binary_path).await.is_err() {- let mut response = delegate- .http_client()- .get(&version.url, Default::default(), true)- .await- .context("error downloading release")?;- let decompressed_bytes = GzipDecoder::new(BufReader::new(response.body_mut()));- let archive = Archive::new(decompressed_bytes);- archive.unpack(container_dir).await?;- }-- // todo("windows")- #[cfg(not(windows))]- {- fs::set_permissions(- &binary_path,- <fs::Permissions as fs::unix::PermissionsExt>::from_mode(0o755),- )- .await?;- }- Ok(LanguageServerBinary {- path: binary_path,- env: None,- arguments: server_binary_arguments(),- })- }-- async fn cached_server_binary(- &self,- container_dir: PathBuf,- _: &dyn LspAdapterDelegate,- ) -> Option<LanguageServerBinary> {- get_cached_server_binary(container_dir).await- }-- async fn installation_test_binary(- &self,- container_dir: PathBuf,- ) -> Option<LanguageServerBinary> {- get_cached_server_binary(container_dir)- .await- .map(|mut binary| {- binary.arguments = vec!["--help".into()];- binary- })- }-}--async fn get_cached_server_binary(container_dir: PathBuf) -> Option<LanguageServerBinary> {- maybe!(async {- let mut last_binary_path = None;- let mut entries = fs::read_dir(&container_dir).await?;- while let Some(entry) = entries.next().await {- let entry = entry?;- if entry.file_type().await?.is_file()- && entry- .file_name()- .to_str()- .map_or(false, |name| name == "omnisharp")- {- last_binary_path = Some(entry.path());- }- }-- if let Some(path) = last_binary_path {- Ok(LanguageServerBinary {- path,- env: None,- arguments: server_binary_arguments(),- })- } else {- Err(anyhow!("no cached binary"))- }- })- .await- .log_err()-}--fn server_binary_arguments() -> Vec<OsString> {- vec!["-lsp".into()]-}diff --git a/crates/languages/src/lib.rs b/crates/languages/src/lib.rsindex 90e3f181b82..d772ee7f21b 100644--- a/crates/languages/src/lib.rs+++ b/crates/languages/src/lib.rs@@ -13,7 +13,6 @@ use self::{deno::DenoSettings, elixir::ElixirSettings};mod c;mod clojure;-mod csharp;mod css;mod dart;mod deno;@@ -60,7 +59,6 @@ pub fn init(languages.register_native_grammars([("bash", tree_sitter_bash::language()),("c", tree_sitter_c::language()),- ("c_sharp", tree_sitter_c_sharp::language()),("clojure", tree_sitter_clojure::language()),("cpp", tree_sitter_cpp::language()),("css", tree_sitter_css::language()),@@ -164,7 +162,6 @@ pub fn init(language!("c", vec![Arc::new(c::CLspAdapter) as Arc<dyn LspAdapter>]);language!("clojure", vec![Arc::new(clojure::ClojureLspAdapter)]);language!("cpp", vec![Arc::new(c::CLspAdapter)]);- language!("csharp", vec![Arc::new(csharp::OmniSharpAdapter {})]);language!("css",vec![diff --git a/extensions/csharp/Cargo.toml b/extensions/csharp/Cargo.tomlnew file mode 100644index 00000000000..c5ff9868c6a--- /dev/null+++ b/extensions/csharp/Cargo.toml@@ -0,0 +1,16 @@+[package]+name = "zed_csharp"+version = "0.0.1"+edition = "2021"+publish = false+license = "Apache-2.0"++[lints]+workspace = true++[lib]+path = "src/csharp.rs"+crate-type = ["cdylib"]++[dependencies]+zed_extension_api = "0.0.4"diff --git a/extensions/csharp/LICENSE-APACHE b/extensions/csharp/LICENSE-APACHEnew file mode 120000index 00000000000..1cd601d0a3a--- /dev/null+++ b/extensions/csharp/LICENSE-APACHE@@ -0,0 +1 @@+../../LICENSE-APACHE\ No newline at end of filediff --git a/extensions/csharp/extension.toml b/extensions/csharp/extension.tomlnew file mode 100644index 00000000000..31c9d328c07--- /dev/null+++ b/extensions/csharp/extension.toml@@ -0,0 +1,15 @@+id = "csharp"+name = "C#"+description = "C# support."+version = "0.0.1"+schema_version = 1+authors = ["fminkowski <[email protected]>"]+repository = "https://github.com/zed-industries/zed"++[language_servers.omnisharp]+name = "OmniSharp"+language = "CSharp"++[grammars.c_sharp]+repository = "https://github.com/tree-sitter/tree-sitter-c-sharp"+commit = "dd5e59721a5f8dae34604060833902b882023aaf"diff --git a/crates/languages/src/csharp/config.toml b/extensions/csharp/languages/csharp/config.tomlsimilarity index 100%rename from crates/languages/src/csharp/config.tomlrename to extensions/csharp/languages/csharp/config.tomldiff --git a/crates/languages/src/csharp/highlights.scm b/extensions/csharp/languages/csharp/highlights.scmsimilarity index 100%rename from crates/languages/src/csharp/highlights.scmrename to extensions/csharp/languages/csharp/highlights.scmdiff --git a/crates/languages/src/csharp/injections.scm b/extensions/csharp/languages/csharp/injections.scmsimilarity index 100%rename from crates/languages/src/csharp/injections.scmrename to extensions/csharp/languages/csharp/injections.scmdiff --git a/crates/languages/src/csharp/outline.scm b/extensions/csharp/languages/csharp/outline.scmsimilarity index 100%rename from crates/languages/src/csharp/outline.scmrename to extensions/csharp/languages/csharp/outline.scmdiff --git a/extensions/csharp/src/csharp.rs b/extensions/csharp/src/csharp.rsnew file mode 100644index 00000000000..72a2e69aeec--- /dev/null+++ b/extensions/csharp/src/csharp.rs@@ -0,0 +1,116 @@+use std::fs;+use zed_extension_api::{self as zed, Result};++struct CsharpExtension {+ cached_binary_path: Option<String>,+}++impl CsharpExtension {+ fn language_server_binary_path(+ &mut self,+ config: zed::LanguageServerConfig,+ worktree: &zed::Worktree,+ ) -> Result<String> {+ if let Some(path) = &self.cached_binary_path {+ if fs::metadata(path).map_or(false, |stat| stat.is_file()) {+ return Ok(path.clone());+ }+ }++ if let Some(path) = worktree.which("OmniSharp") {+ self.cached_binary_path = Some(path.clone());+ return Ok(path);+ }++ zed::set_language_server_installation_status(+ &config.name,+ &zed::LanguageServerInstallationStatus::CheckingForUpdate,+ );+ let release = zed::latest_github_release(+ "OmniSharp/omnisharp-roslyn",+ zed::GithubReleaseOptions {+ require_assets: true,+ pre_release: false,+ },+ )?;++ let (platform, arch) = zed::current_platform();+ let asset_name = format!(+ "omnisharp-{os}-{arch}-net6.0.{extension}",+ os = match platform {+ zed::Os::Mac => "osx",+ zed::Os::Linux => "linux",+ zed::Os::Windows => "win",+ },+ arch = match arch {+ zed::Architecture::Aarch64 => "arm64",+ zed::Architecture::X86 => "x86",+ zed::Architecture::X8664 => "x64",+ },+ extension = match platform {+ zed::Os::Mac | zed::Os::Linux => "tar.gz",+ zed::Os::Windows => "zip",+ }+ );++ let asset = release+ .assets+ .iter()+ .find(|asset| asset.name == asset_name)+ .ok_or_else(|| format!("no asset found matching {:?}", asset_name))?;++ let version_dir = format!("omnisharp-{}", release.version);+ let binary_path = format!("{version_dir}/OmniSharp");++ if !fs::metadata(&binary_path).map_or(false, |stat| stat.is_file()) {+ zed::set_language_server_installation_status(+ &config.name,+ &zed::LanguageServerInstallationStatus::Downloading,+ );++ zed::download_file(+ &asset.download_url,+ &version_dir,+ match platform {+ zed::Os::Mac | zed::Os::Linux => zed::DownloadedFileType::GzipTar,+ zed::Os::Windows => zed::DownloadedFileType::Zip,+ },+ )+ .map_err(|e| format!("failed to download file: {e}"))?;++ let entries =+ fs::read_dir(".").map_err(|e| format!("failed to list working directory {e}"))?;+ for entry in entries {+ let entry = entry.map_err(|e| format!("failed to load directory entry {e}"))?;+ if entry.file_name().to_str() != Some(&version_dir) {+ fs::remove_dir_all(&entry.path()).ok();+ }+ }+ }++ self.cached_binary_path = Some(binary_path.clone());+ Ok(binary_path)+ }+}++impl zed::Extension for CsharpExtension {+ fn new() -> Self {+ Self {+ cached_binary_path: None,+ }+ }++ fn language_server_command(+ &mut self,+ config: zed::LanguageServerConfig,+ worktree: &zed::Worktree,+ ) -> Result<zed::Command> {+ Ok(zed::Command {+ command: self.language_server_binary_path(config, worktree)?,+ args: vec!["-lsp".to_string()],+ env: Default::default(),+ })+ }+}++zed::register_extension!(CsharpExtension);