Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
csharp: Use lsp settings to locate binary if present (#15885)
Jonathan Dickinson864c443f4b93
diff --git a/docs/src/languages/csharp.md b/docs/src/languages/csharp.mdindex 4d7342c260d..5dbeb19f6f0 100644--- a/docs/src/languages/csharp.md+++ b/docs/src/languages/csharp.md@@ -1,3 +1,20 @@# C#C# support is available through the [C# extension](https://github.com/zed-industries/zed/tree/main/extensions/csharp).++## Configuration++The `OmniSharp` binary can be configured in a Zed settings file with:++```jsonc+{+ "lsp": {+ "omnisharp": {+ "binary": {+ "path": "/path/to/OmniSharp",+ "args": ["optional", "additional", "args", "-lsp"],+ },+ },+ },+}+```diff --git a/extensions/csharp/src/csharp.rs b/extensions/csharp/src/csharp.rsindex 422b472d220..61f1edb1eb2 100644--- a/extensions/csharp/src/csharp.rs+++ b/extensions/csharp/src/csharp.rs@@ -1,23 +1,48 @@use std::fs;-use zed_extension_api::{self as zed, Result};+use zed_extension_api::{self as zed, settings::LspSettings, LanguageServerId, Result};++struct OmnisharpBinary {+ path: String,+ args: Option<Vec<String>>,+}struct CsharpExtension {cached_binary_path: Option<String>,}impl CsharpExtension {- fn language_server_binary_path(+ fn language_server_binary(&mut self,- language_server_id: &zed::LanguageServerId,+ language_server_id: &LanguageServerId,worktree: &zed::Worktree,- ) -> Result<String> {+ ) -> Result<OmnisharpBinary> {+ let binary_settings = LspSettings::for_worktree("omnisharp", worktree)+ .ok()+ .and_then(|lsp_settings| lsp_settings.binary);+ let binary_args = binary_settings+ .as_ref()+ .and_then(|binary_settings| binary_settings.arguments.clone());++ if let Some(path) = binary_settings.and_then(|binary_settings| binary_settings.path) {+ return Ok(OmnisharpBinary {+ path,+ args: binary_args,+ });+ }+if let Some(path) = worktree.which("OmniSharp") {- return Ok(path);+ return Ok(OmnisharpBinary {+ path,+ args: binary_args,+ });}if let Some(path) = &self.cached_binary_path {- if fs::metadata(path).map_or(false, |stat| stat.is_file()) {- return Ok(path.clone());+ if fs::metadata(&path).map_or(false, |stat| stat.is_file()) {+ return Ok(OmnisharpBinary {+ path: path.clone(),+ args: binary_args,+ });}}@@ -88,7 +113,10 @@ impl CsharpExtension {}self.cached_binary_path = Some(binary_path.clone());- Ok(binary_path)+ Ok(OmnisharpBinary {+ path: binary_path,+ args: binary_args,+ })}}@@ -104,9 +132,10 @@ impl zed::Extension for CsharpExtension {language_server_id: &zed::LanguageServerId,worktree: &zed::Worktree,) -> Result<zed::Command> {+ let omnisharp_binary = self.language_server_binary(language_server_id, worktree)?;Ok(zed::Command {- command: self.language_server_binary_path(language_server_id, worktree)?,- args: vec!["-lsp".to_string()],+ command: omnisharp_binary.path,+ args: omnisharp_binary.args.unwrap_or_else(|| vec!["-lsp".into()]),env: Default::default(),})}