Skip to content

Commit

Permalink
Add ability to specify binary path/arguments for the Zig Language Server
Browse files Browse the repository at this point in the history
  • Loading branch information
llogick committed Jul 8, 2024
1 parent 97f3153 commit d40fb6a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion extensions/zig/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "zed_zig"
version = "0.1.3"
version = "0.1.4"
edition = "2021"
publish = false
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion extensions/zig/extension.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
id = "zig"
name = "Zig"
description = "Zig support."
version = "0.1.3"
version = "0.1.4"
schema_version = 1
authors = ["Allan Calix <[email protected]>"]
repository = "https://github.com/zed-industries/zed"
Expand Down
44 changes: 38 additions & 6 deletions extensions/zig/src/zig.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fs;
use zed::LanguageServerId;
use zed_extension_api::{self as zed, Result};
use zed_extension_api::settings::LspSettings;
use zed_extension_api::{self as zed, serde_json, Result};

struct ZigExtension {
cached_binary_path: Option<String>,
Expand All @@ -9,6 +10,7 @@ struct ZigExtension {
#[derive(Clone)]
struct ZlsBinary {
path: String,
args: Option<Vec<String>>,
environment: Option<Vec<(String, String)>>,
}

Expand All @@ -18,19 +20,36 @@ impl ZigExtension {
language_server_id: &LanguageServerId,
worktree: &zed::Worktree,
) -> Result<ZlsBinary> {
let mut args: Option<Vec<String>> = 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,
});
}

if let Some(path) = &self.cached_binary_path {
if fs::metadata(&path).map_or(false, |stat| stat.is_file()) {
return Ok(ZlsBinary {
path: path.clone(),
environment: None,
args,
environment,
});
}
}
Expand Down Expand Up @@ -104,7 +123,8 @@ impl ZigExtension {
self.cached_binary_path = Some(binary_path.clone());
Ok(ZlsBinary {
path: binary_path,
environment: None,
args,
environment,
})
}
}
Expand All @@ -124,10 +144,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<Option<serde_json::Value>> {
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);

0 comments on commit d40fb6a

Please sign in to comment.