dart: Respect LSP binary settings (#17494)

Enable configuring Dart's LSP from other means of installation types.

Some users don't install the `dart` binary, but uses version manager.

In the example, I uses [FVM](https://fvm.app/) (short for "Flutter
Version Manager").

I have tested this with "Install Dev Extensions".

Release Notes:

- N/A

cc other maintainer: @agent3bood @flo80

---------

Co-authored-by: Marshall Bowers <elliott.codes@gmail.com>
This commit is contained in:
Yohanes Bandung Bondowoso 2024-09-16 22:03:51 +07:00 committed by GitHub
parent 2baa704af7
commit 2cae6f3e08
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 63 additions and 7 deletions

View file

@ -5,6 +5,23 @@ Dart support is available through the [Dart extension](https://github.com/zed-in
- Tree Sitter: [UserNobody14/tree-sitter-dart](https://github.com/UserNobody14/tree-sitter-dart)
- Language Server: [dart language-server](https://github.com/dart-lang/sdk)
## Configuration
The `dart` binary can be configured in a Zed settings file with:
```json
{
"lsp": {
"dart": {
"binary": {
"path": "/opt/homebrew/bin/fvm",
"arguments": ["dart", "language-server", "--protocol=lsp"]
}
}
}
}
```
<!--
TBD: Document Dart. pubspec.yaml
- https://github.com/dart-lang/sdk/blob/main/pkg/analysis_server/tool/lsp_spec/README.md

View file

@ -3,7 +3,7 @@ name = "Dart"
description = "Dart support."
version = "0.0.3"
schema_version = 1
authors = ["Abdullah Alsigar <abdullah.alsigar@gmail.com>", "Flo <flo80@users.noreply.github.com>"]
authors = ["Abdullah Alsigar <abdullah.alsigar@gmail.com>", "Flo <flo80@users.noreply.github.com>", "ybbond <hi@ybbond.id>"]
repository = "https://github.com/zed-industries/zed"
[language_servers.dart]

View file

@ -3,8 +3,47 @@ use zed::settings::LspSettings;
use zed::{CodeLabel, CodeLabelSpan};
use zed_extension_api::{self as zed, serde_json, Result};
struct DartBinary {
pub path: String,
pub args: Option<Vec<String>>,
}
struct DartExtension;
impl DartExtension {
fn language_server_binary(
&mut self,
_language_server_id: &zed::LanguageServerId,
worktree: &zed::Worktree,
) -> Result<DartBinary> {
let binary_settings = LspSettings::for_worktree("dart", 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(DartBinary {
path,
args: binary_args,
});
}
if let Some(path) = worktree.which("dart") {
return Ok(DartBinary {
path,
args: binary_args,
});
}
Err(
"dart must be installed from dart.dev/get-dart or pointed to by the LSP binary settings"
.to_string(),
)
}
}
impl zed::Extension for DartExtension {
fn new() -> Self {
Self
@ -12,16 +51,16 @@ impl zed::Extension for DartExtension {
fn language_server_command(
&mut self,
_language_server_id: &zed::LanguageServerId,
language_server_id: &zed::LanguageServerId,
worktree: &zed::Worktree,
) -> Result<zed::Command> {
let path = worktree
.which("dart")
.ok_or_else(|| "dart must be installed from dart.dev/get-dart".to_string())?;
let dart_binary = self.language_server_binary(language_server_id, worktree)?;
Ok(zed::Command {
command: path,
args: vec!["language-server".to_string(), "--protocol=lsp".to_string()],
command: dart_binary.path,
args: dart_binary.args.unwrap_or_else(|| {
vec!["language-server".to_string(), "--protocol=lsp".to_string()]
}),
env: Default::default(),
})
}