Add language_server_workspace_configuration
to extension API (#10212)
This PR adds the ability for extensions to implement `language_server_workspace_configuration` to provide workspace configuration to the language server. We've used the Dart extension as a motivating example for this, pulling it out into an extension in the process. Release Notes: - Removed built-in support for Dart, in favor of making it available as an extension. The Dart extension will be suggested for download when you open a `.dart` file. --------- Co-authored-by: Max <max@zed.dev> Co-authored-by: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
parent
4aaf3459c4
commit
c851e6edba
36 changed files with 586 additions and 187 deletions
16
extensions/dart/Cargo.toml
Normal file
16
extensions/dart/Cargo.toml
Normal file
|
@ -0,0 +1,16 @@
|
|||
[package]
|
||||
name = "zed_dart"
|
||||
version = "0.0.1"
|
||||
edition = "2021"
|
||||
publish = false
|
||||
license = "Apache-2.0"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[lib]
|
||||
path = "src/dart.rs"
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
zed_extension_api = { path = "../../crates/extension_api" }
|
1
extensions/dart/LICENSE-APACHE
Symbolic link
1
extensions/dart/LICENSE-APACHE
Symbolic link
|
@ -0,0 +1 @@
|
|||
../../LICENSE-APACHE
|
15
extensions/dart/extension.toml
Normal file
15
extensions/dart/extension.toml
Normal file
|
@ -0,0 +1,15 @@
|
|||
id = "dart"
|
||||
name = "Dart"
|
||||
description = "Dart support."
|
||||
version = "0.0.1"
|
||||
schema_version = 1
|
||||
authors = ["Abdullah Alsigar <abdullah.alsigar@gmail.com>", "Flo <flo80@users.noreply.github.com>"]
|
||||
repository = "https://github.com/zed-industries/zed"
|
||||
|
||||
[language_servers.dart]
|
||||
name = "Dart LSP"
|
||||
language = "Dart"
|
||||
|
||||
[grammars.dart]
|
||||
repository = "https://github.com/agent3bood/tree-sitter-dart"
|
||||
commit = "48934e3bf757a9b78f17bdfaa3e2b4284656fdc7"
|
6
extensions/dart/languages/dart/brackets.scm
Normal file
6
extensions/dart/languages/dart/brackets.scm
Normal file
|
@ -0,0 +1,6 @@
|
|||
("(" @open ")" @close)
|
||||
("[" @open "]" @close)
|
||||
("{" @open "}" @close)
|
||||
("<" @open ">" @close)
|
||||
("\"" @open "\"" @close)
|
||||
("'" @open "'" @close)
|
13
extensions/dart/languages/dart/config.toml
Normal file
13
extensions/dart/languages/dart/config.toml
Normal file
|
@ -0,0 +1,13 @@
|
|||
name = "Dart"
|
||||
grammar = "dart"
|
||||
path_suffixes = ["dart"]
|
||||
line_comments = ["// "]
|
||||
autoclose_before = ";:.,=}])>"
|
||||
brackets = [
|
||||
{ start = "{", end = "}", close = true, newline = true },
|
||||
{ start = "[", end = "]", close = true, newline = true },
|
||||
{ start = "(", end = ")", close = true, newline = true },
|
||||
{ start = "\"", end = "\"", close = true, newline = false, not_in = ["string"] },
|
||||
{ start = "'", end = "'", close = true, newline = false, not_in = ["string"] },
|
||||
{ start = "/*", end = " */", close = true, newline = false, not_in = ["string", "comment"] },
|
||||
]
|
209
extensions/dart/languages/dart/highlights.scm
Normal file
209
extensions/dart/languages/dart/highlights.scm
Normal file
|
@ -0,0 +1,209 @@
|
|||
(dotted_identifier_list) @string
|
||||
|
||||
; Methods
|
||||
; --------------------
|
||||
(function_type
|
||||
name: (identifier) @function)
|
||||
(super) @function
|
||||
|
||||
; Annotations
|
||||
; --------------------
|
||||
(annotation
|
||||
name: (identifier) @attribute)
|
||||
|
||||
; Operators and Tokens
|
||||
; --------------------
|
||||
(template_substitution
|
||||
"$" @punctuation.special
|
||||
"{" @punctuation.special
|
||||
"}" @punctuation.special
|
||||
) @none
|
||||
|
||||
(template_substitution
|
||||
"$" @punctuation.special
|
||||
(identifier_dollar_escaped) @variable
|
||||
) @none
|
||||
|
||||
(escape_sequence) @string.escape
|
||||
|
||||
[
|
||||
"@"
|
||||
"=>"
|
||||
".."
|
||||
"??"
|
||||
"=="
|
||||
"?"
|
||||
":"
|
||||
"&&"
|
||||
"%"
|
||||
"<"
|
||||
">"
|
||||
"="
|
||||
">="
|
||||
"<="
|
||||
"||"
|
||||
(increment_operator)
|
||||
(is_operator)
|
||||
(prefix_operator)
|
||||
(equality_operator)
|
||||
(additive_operator)
|
||||
] @operator
|
||||
|
||||
[
|
||||
"("
|
||||
")"
|
||||
"["
|
||||
"]"
|
||||
"{"
|
||||
"}"
|
||||
"<"
|
||||
">"
|
||||
] @punctuation.bracket
|
||||
|
||||
; Delimiters
|
||||
; --------------------
|
||||
[
|
||||
";"
|
||||
"."
|
||||
","
|
||||
] @punctuation.delimiter
|
||||
|
||||
; Types
|
||||
; --------------------
|
||||
(class_definition
|
||||
name: (identifier) @type)
|
||||
(constructor_signature
|
||||
name: (identifier) @type)
|
||||
(scoped_identifier
|
||||
scope: (identifier) @type)
|
||||
(function_signature
|
||||
name: (identifier) @function)
|
||||
(getter_signature
|
||||
(identifier) @function)
|
||||
(setter_signature
|
||||
name: (identifier) @function)
|
||||
(enum_declaration
|
||||
name: (identifier) @type)
|
||||
(enum_constant
|
||||
name: (identifier) @type)
|
||||
(type_identifier) @type
|
||||
(void_type) @type
|
||||
|
||||
((scoped_identifier
|
||||
scope: (identifier) @type
|
||||
name: (identifier) @type)
|
||||
(#match? @type "^[a-zA-Z]"))
|
||||
|
||||
(type_identifier) @type
|
||||
|
||||
; Variables
|
||||
; --------------------
|
||||
; var keyword
|
||||
(inferred_type) @keyword
|
||||
|
||||
(const_builtin) @constant.builtin
|
||||
(final_builtin) @constant.builtin
|
||||
|
||||
((identifier) @type
|
||||
(#match? @type "^_?[A-Z]"))
|
||||
|
||||
("Function" @type)
|
||||
|
||||
; properties
|
||||
; TODO: add method/call_expression to grammar and
|
||||
; distinguish method call from variable access
|
||||
(unconditional_assignable_selector
|
||||
(identifier) @property)
|
||||
|
||||
; assignments
|
||||
(assignment_expression
|
||||
left: (assignable_expression) @variable)
|
||||
|
||||
(this) @variable.builtin
|
||||
|
||||
; Literals
|
||||
; --------------------
|
||||
[
|
||||
(hex_integer_literal)
|
||||
(decimal_integer_literal)
|
||||
(decimal_floating_point_literal)
|
||||
; TODO: inaccessible nodes
|
||||
; (octal_integer_literal)
|
||||
; (hex_floating_point_literal)
|
||||
] @number
|
||||
|
||||
(symbol_literal) @symbol
|
||||
(string_literal) @string
|
||||
(true) @boolean
|
||||
(false) @boolean
|
||||
(null_literal) @constant.builtin
|
||||
|
||||
(documentation_comment) @comment
|
||||
(comment) @comment
|
||||
|
||||
; Keywords
|
||||
; --------------------
|
||||
["import" "library" "export"] @keyword.include
|
||||
|
||||
; Reserved words (cannot be used as identifiers)
|
||||
; TODO: "rethrow" @keyword
|
||||
[
|
||||
; "assert"
|
||||
(case_builtin)
|
||||
"extension"
|
||||
"on"
|
||||
"class"
|
||||
"enum"
|
||||
"extends"
|
||||
"in"
|
||||
"is"
|
||||
"new"
|
||||
"return"
|
||||
"super"
|
||||
"with"
|
||||
] @keyword
|
||||
|
||||
|
||||
; Built in identifiers:
|
||||
; alone these are marked as keywords
|
||||
[
|
||||
"abstract"
|
||||
"as"
|
||||
"async"
|
||||
"async*"
|
||||
"yield"
|
||||
"sync*"
|
||||
"await"
|
||||
"covariant"
|
||||
"deferred"
|
||||
"dynamic"
|
||||
"external"
|
||||
"factory"
|
||||
"get"
|
||||
"implements"
|
||||
"interface"
|
||||
"library"
|
||||
"operator"
|
||||
"mixin"
|
||||
"part"
|
||||
"set"
|
||||
"show"
|
||||
"static"
|
||||
"typedef"
|
||||
] @keyword
|
||||
|
||||
; when used as an identifier:
|
||||
((identifier) @variable.builtin
|
||||
(#vim-match? @variable.builtin "^(abstract|as|covariant|deferred|dynamic|export|external|factory|Function|get|implements|import|interface|library|operator|mixin|part|set|static|typedef)$"))
|
||||
|
||||
["if" "else" "switch" "default"] @keyword
|
||||
|
||||
[
|
||||
"try"
|
||||
"throw"
|
||||
"catch"
|
||||
"finally"
|
||||
(break_statement)
|
||||
] @keyword
|
||||
|
||||
["do" "while" "continue" "for"] @keyword
|
7
extensions/dart/languages/dart/indents.scm
Normal file
7
extensions/dart/languages/dart/indents.scm
Normal file
|
@ -0,0 +1,7 @@
|
|||
[
|
||||
(if_statement)
|
||||
(for_statement)
|
||||
] @indent
|
||||
|
||||
(_ "{" "}" @end) @indent
|
||||
(_ "(" ")" @end) @indent
|
18
extensions/dart/languages/dart/outline.scm
Normal file
18
extensions/dart/languages/dart/outline.scm
Normal file
|
@ -0,0 +1,18 @@
|
|||
(class_definition
|
||||
"class" @context
|
||||
name: (_) @name) @item
|
||||
|
||||
(function_signature
|
||||
name: (_) @name) @item
|
||||
|
||||
(getter_signature
|
||||
"get" @context
|
||||
name: (_) @name) @item
|
||||
|
||||
(setter_signature
|
||||
"set" @context
|
||||
name: (_) @name) @item
|
||||
|
||||
(enum_declaration
|
||||
"enum" @context
|
||||
name: (_) @name) @item
|
123
extensions/dart/src/dart.rs
Normal file
123
extensions/dart/src/dart.rs
Normal file
|
@ -0,0 +1,123 @@
|
|||
use zed::lsp::CompletionKind;
|
||||
use zed::settings::LspSettings;
|
||||
use zed::{CodeLabel, CodeLabelSpan};
|
||||
use zed_extension_api::{self as zed, serde_json, Result};
|
||||
|
||||
struct DartExtension;
|
||||
|
||||
impl zed::Extension for DartExtension {
|
||||
fn new() -> Self {
|
||||
Self
|
||||
}
|
||||
|
||||
fn language_server_command(
|
||||
&mut self,
|
||||
_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())?;
|
||||
|
||||
Ok(zed::Command {
|
||||
command: path,
|
||||
args: vec!["language-server".to_string(), "--protocol=lsp".to_string()],
|
||||
env: Default::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("dart", worktree)
|
||||
.ok()
|
||||
.and_then(|lsp_settings| lsp_settings.settings.clone())
|
||||
.unwrap_or_default();
|
||||
|
||||
Ok(Some(serde_json::json!({
|
||||
"dart": settings
|
||||
})))
|
||||
}
|
||||
|
||||
fn label_for_completion(
|
||||
&self,
|
||||
_language_server_id: &zed::LanguageServerId,
|
||||
completion: zed::lsp::Completion,
|
||||
) -> Option<CodeLabel> {
|
||||
let arrow = " → ";
|
||||
|
||||
match completion.kind? {
|
||||
CompletionKind::Class => Some(CodeLabel {
|
||||
filter_range: (0..completion.label.len()).into(),
|
||||
spans: vec![CodeLabelSpan::literal(
|
||||
completion.label,
|
||||
Some("type".into()),
|
||||
)],
|
||||
code: String::new(),
|
||||
}),
|
||||
CompletionKind::Function | CompletionKind::Constructor | CompletionKind::Method => {
|
||||
let mut parts = completion.detail.as_ref()?.split(arrow);
|
||||
let (name, _) = completion.label.split_once('(')?;
|
||||
let parameter_list = parts.next()?;
|
||||
let return_type = parts.next()?;
|
||||
let fn_name = " a";
|
||||
let fat_arrow = " => ";
|
||||
let call_expr = "();";
|
||||
|
||||
let code =
|
||||
format!("{return_type}{fn_name}{parameter_list}{fat_arrow}{name}{call_expr}");
|
||||
|
||||
let parameter_list_start = return_type.len() + fn_name.len();
|
||||
|
||||
Some(CodeLabel {
|
||||
spans: vec![
|
||||
CodeLabelSpan::code_range(
|
||||
code.len() - call_expr.len() - name.len()..code.len() - call_expr.len(),
|
||||
),
|
||||
CodeLabelSpan::code_range(
|
||||
parameter_list_start..parameter_list_start + parameter_list.len(),
|
||||
),
|
||||
CodeLabelSpan::literal(arrow, None),
|
||||
CodeLabelSpan::code_range(0..return_type.len()),
|
||||
],
|
||||
filter_range: (0..name.len()).into(),
|
||||
code,
|
||||
})
|
||||
}
|
||||
CompletionKind::Property => {
|
||||
let class_start = "class A {";
|
||||
let get = " get ";
|
||||
let property_end = " => a; }";
|
||||
let ty = completion.detail?;
|
||||
let name = completion.label;
|
||||
|
||||
let code = format!("{class_start}{ty}{get}{name}{property_end}");
|
||||
let name_start = class_start.len() + ty.len() + get.len();
|
||||
|
||||
Some(CodeLabel {
|
||||
spans: vec![
|
||||
CodeLabelSpan::code_range(name_start..name_start + name.len()),
|
||||
CodeLabelSpan::literal(arrow, None),
|
||||
CodeLabelSpan::code_range(class_start.len()..class_start.len() + ty.len()),
|
||||
],
|
||||
filter_range: (0..name.len()).into(),
|
||||
code,
|
||||
})
|
||||
}
|
||||
CompletionKind::Variable => {
|
||||
let name = completion.label;
|
||||
|
||||
Some(CodeLabel {
|
||||
filter_range: (0..name.len()).into(),
|
||||
spans: vec![CodeLabelSpan::literal(name, Some("variable".into()))],
|
||||
code: String::new(),
|
||||
})
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
zed::register_extension!(DartExtension);
|
|
@ -13,4 +13,5 @@ path = "src/svelte.rs"
|
|||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
zed_extension_api = "0.0.4"
|
||||
zed_extension_api = { path = "../../crates/extension_api" }
|
||||
# zed_extension_api = "0.0.4"
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use std::{env, fs};
|
||||
use zed_extension_api::{self as zed, Result};
|
||||
use zed_extension_api::{self as zed, serde_json, Result};
|
||||
|
||||
struct SvelteExtension {
|
||||
did_find_server: bool,
|
||||
|
@ -13,14 +13,14 @@ impl SvelteExtension {
|
|||
fs::metadata(SERVER_PATH).map_or(false, |stat| stat.is_file())
|
||||
}
|
||||
|
||||
fn server_script_path(&mut self, config: zed::LanguageServerConfig) -> Result<String> {
|
||||
fn server_script_path(&mut self, id: &zed::LanguageServerId) -> Result<String> {
|
||||
let server_exists = self.server_exists();
|
||||
if self.did_find_server && server_exists {
|
||||
return Ok(SERVER_PATH.to_string());
|
||||
}
|
||||
|
||||
zed::set_language_server_installation_status(
|
||||
&config.name,
|
||||
id,
|
||||
&zed::LanguageServerInstallationStatus::CheckingForUpdate,
|
||||
);
|
||||
let version = zed::npm_package_latest_version(PACKAGE_NAME)?;
|
||||
|
@ -29,7 +29,7 @@ impl SvelteExtension {
|
|||
|| zed::npm_package_installed_version(PACKAGE_NAME)?.as_ref() != Some(&version)
|
||||
{
|
||||
zed::set_language_server_installation_status(
|
||||
&config.name,
|
||||
id,
|
||||
&zed::LanguageServerInstallationStatus::Downloading,
|
||||
);
|
||||
let result = zed::npm_install_package(PACKAGE_NAME, &version);
|
||||
|
@ -63,10 +63,10 @@ impl zed::Extension for SvelteExtension {
|
|||
|
||||
fn language_server_command(
|
||||
&mut self,
|
||||
config: zed::LanguageServerConfig,
|
||||
id: &zed::LanguageServerId,
|
||||
_: &zed::Worktree,
|
||||
) -> Result<zed::Command> {
|
||||
let server_path = self.server_script_path(config)?;
|
||||
let server_path = self.server_script_path(id)?;
|
||||
Ok(zed::Command {
|
||||
command: zed::node_binary_path()?,
|
||||
args: vec![
|
||||
|
@ -83,10 +83,10 @@ impl zed::Extension for SvelteExtension {
|
|||
|
||||
fn language_server_initialization_options(
|
||||
&mut self,
|
||||
_: zed::LanguageServerConfig,
|
||||
_: &zed::LanguageServerId,
|
||||
_: &zed::Worktree,
|
||||
) -> Result<Option<String>> {
|
||||
let config = r#"{
|
||||
) -> Result<Option<serde_json::Value>> {
|
||||
let config = serde_json::json!({
|
||||
"inlayHints": {
|
||||
"parameterNames": {
|
||||
"enabled": "all",
|
||||
|
@ -109,17 +109,15 @@ impl zed::Extension for SvelteExtension {
|
|||
"enabled": true
|
||||
}
|
||||
}
|
||||
}"#;
|
||||
});
|
||||
|
||||
Ok(Some(format!(
|
||||
r#"{{
|
||||
"provideFormatter": true,
|
||||
"configuration": {{
|
||||
"typescript": {config},
|
||||
"javascript": {config}
|
||||
}}
|
||||
}}"#
|
||||
)))
|
||||
Ok(Some(serde_json::json!({
|
||||
"provideFormatter": true,
|
||||
"configuration": {
|
||||
"typescript": config,
|
||||
"javascript": config
|
||||
}
|
||||
})))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue