From 684d9dde5681504d8fe53ffe450c94902b3fa45b Mon Sep 17 00:00:00 2001 From: llogick <16590917+llogick@users.noreply.github.com> Date: Mon, 15 Jul 2024 00:35:03 -0700 Subject: [PATCH] zig: Wire up LSP `settings` and `binary.{path/arguments}` for `zls` (#14379) Enables the LSP `settings` and `binary.{path/arguments}` functionality Example: ``` "lsp": { "zls": { "settings": { "semantic_tokens": "none" }, "binary": { "path": "/home/user/zls/zig-out/bin/zls", "arguments": ["--enable-debug-log"] } } }, ``` Release Notes: - N/A --- extensions/zig/src/zig.rs | 44 ++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/extensions/zig/src/zig.rs b/extensions/zig/src/zig.rs index a42d62be29..755834fd19 100644 --- a/extensions/zig/src/zig.rs +++ b/extensions/zig/src/zig.rs @@ -1,6 +1,5 @@ use std::fs; -use zed::LanguageServerId; -use zed_extension_api::{self as zed, Result}; +use zed_extension_api::{self as zed, serde_json, settings::LspSettings, LanguageServerId, Result}; struct ZigExtension { cached_binary_path: Option, @@ -9,6 +8,7 @@ struct ZigExtension { #[derive(Clone)] struct ZlsBinary { path: String, + args: Option>, environment: Option>, } @@ -18,11 +18,27 @@ impl ZigExtension { language_server_id: &LanguageServerId, worktree: &zed::Worktree, ) -> Result { + let mut args: Option> = None; + let environment = Some(worktree.shell_env()); + + if let Ok(lsp_settings) = LspSettings::for_worktree("zls", worktree) { + if let Some(binary) = lsp_settings.binary { + args = binary.arguments; + if let Some(path) = binary.path { + return Ok(ZlsBinary { + path: path.clone(), + args, + environment, + }); + } + } + } + if let Some(path) = worktree.which("zls") { - let environment = worktree.shell_env(); return Ok(ZlsBinary { path, - environment: Some(environment), + args, + environment, }); } @@ -30,7 +46,8 @@ impl ZigExtension { if fs::metadata(&path).map_or(false, |stat| stat.is_file()) { return Ok(ZlsBinary { path: path.clone(), - environment: None, + args, + environment, }); } } @@ -104,7 +121,8 @@ impl ZigExtension { self.cached_binary_path = Some(binary_path.clone()); Ok(ZlsBinary { path: binary_path, - environment: None, + args, + environment, }) } } @@ -124,10 +142,22 @@ impl zed::Extension for ZigExtension { let zls_binary = self.language_server_binary(language_server_id, worktree)?; Ok(zed::Command { command: zls_binary.path, - args: vec![], + args: zls_binary.args.unwrap_or_default(), env: zls_binary.environment.unwrap_or_default(), }) } + + fn language_server_workspace_configuration( + &mut self, + _language_server_id: &zed::LanguageServerId, + worktree: &zed::Worktree, + ) -> Result> { + let settings = LspSettings::for_worktree("zls", worktree) + .ok() + .and_then(|lsp_settings| lsp_settings.settings.clone()) + .unwrap_or_default(); + Ok(Some(settings)) + } } zed::register_extension!(ZigExtension);