Skip to content

Commit

Permalink
toml: Respect language server binary settings (#25276)
Browse files Browse the repository at this point in the history
This PR updates the TOML extension to respect the `binary` settings for
the language server.

Related to #22775.

Release Notes:

- N/A
  • Loading branch information
maxdeviant authored Feb 20, 2025
1 parent a1223e0 commit 2581f8b
Showing 1 changed file with 44 additions and 7 deletions.
51 changes: 44 additions & 7 deletions extensions/toml/src/toml.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,50 @@
use std::fs;
use zed::LanguageServerId;
use zed_extension_api::settings::LspSettings;
use zed_extension_api::{self as zed, Result};

struct TaploBinary {
path: String,
args: Option<Vec<String>>,
}

struct TomlExtension {
cached_binary_path: Option<String>,
}

impl TomlExtension {
fn language_server_binary_path(
fn language_server_binary(
&mut self,
language_server_id: &LanguageServerId,
) -> Result<String> {
worktree: &zed::Worktree,
) -> Result<TaploBinary> {
let binary_settings = LspSettings::for_worktree("taplo", 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(TaploBinary {
path,
args: binary_args,
});
}

if let Some(path) = worktree.which("taplo") {
return Ok(TaploBinary {
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());
return Ok(TaploBinary {
path: path.clone(),
args: binary_args,
});
}
}

Expand Down Expand Up @@ -88,7 +119,10 @@ impl TomlExtension {
}

self.cached_binary_path = Some(binary_path.clone());
Ok(binary_path)
Ok(TaploBinary {
path: binary_path,
args: binary_args,
})
}
}

Expand All @@ -102,11 +136,14 @@ impl zed::Extension for TomlExtension {
fn language_server_command(
&mut self,
language_server_id: &LanguageServerId,
_worktree: &zed::Worktree,
worktree: &zed::Worktree,
) -> Result<zed::Command> {
let taplo_binary = self.language_server_binary(language_server_id, worktree)?;
Ok(zed::Command {
command: self.language_server_binary_path(language_server_id)?,
args: vec!["lsp".to_string(), "stdio".to_string()],
command: taplo_binary.path,
args: taplo_binary
.args
.unwrap_or_else(|| vec!["lsp".to_string(), "stdio".to_string()]),
env: Default::default(),
})
}
Expand Down

0 comments on commit 2581f8b

Please sign in to comment.