Extract Vue extension (#10486)

This PR extracts Vue support into an extension and removes the built-in
C# support from Zed.

Release Notes:

- Removed built-in support for Vue, in favor of making it available as
an extension. The Vue extension will be suggested for download when you
open a `.vue` file.

---------

Co-authored-by: Max <max@zed.dev>
This commit is contained in:
Marshall Bowers 2024-04-12 14:39:27 -04:00 committed by GitHub
parent 8bca9cea26
commit f3a78f613a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 196 additions and 275 deletions

16
extensions/vue/Cargo.toml Normal file
View file

@ -0,0 +1,16 @@
[package]
name = "zed_vue"
version = "0.0.1"
edition = "2021"
publish = false
license = "Apache-2.0"
[lints]
workspace = true
[lib]
path = "src/vue.rs"
crate-type = ["cdylib"]
[dependencies]
zed_extension_api = "0.0.6"

View file

@ -0,0 +1 @@
../../LICENSE-APACHE

View file

@ -0,0 +1,18 @@
id = "vue"
name = "Vue"
description = "Vue support."
version = "0.0.1"
schema_version = 1
authors = ["Piotr Osiewicz <piotr@zed.dev>"]
repository = "https://github.com/zed-industries/zed"
[language_servers.vue-language-server]
name = "Vue Language Server"
language = "Vue.js"
# REFACTOR is explicitly disabled, as vue-lsp does not adhere to LSP protocol for code actions with these - it
# sends back a CodeAction with neither `command` nor `edits` fields set, which is against the spec.
code_action_kinds = ["", "quickfix", "refactor.rewrite"]
[grammars.vue]
repository = "https://github.com/tree-sitter-grammars/tree-sitter-vue"
commit = "7e48557b903a9db9c38cea3b7839ef7e1f36c693"

View file

@ -0,0 +1,2 @@
("<" @open ">" @close)
("\"" @open "\"" @close)

View file

@ -0,0 +1,22 @@
name = "Vue.js"
code_fence_block_name = "vue"
grammar = "vue"
path_suffixes = ["vue"]
block_comment = ["<!-- ", " -->"]
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 = true, not_in = ["string", "comment"] },
{ start = "\"", end = "\"", close = true, newline = false, not_in = ["string"] },
{ start = "'", end = "'", close = true, newline = false, not_in = ["string", "comment"] },
{ start = "`", end = "`", close = true, newline = false, not_in = ["string"] },
]
word_characters = ["-"]
scope_opt_in_language_servers = ["tailwindcss-language-server"]
prettier_parser_name = "vue"
[overrides.string]
word_characters = ["-"]
opt_into_language_servers = ["tailwindcss-language-server"]

View file

@ -0,0 +1,15 @@
(attribute) @property
(directive_attribute) @property
(quoted_attribute_value) @string
(interpolation) @punctuation.special
(raw_text) @embedded
((tag_name) @type
(#match? @type "^[A-Z]"))
((directive_name) @keyword
(#match? @keyword "^v-"))
(start_tag) @tag
(end_tag) @tag
(self_closing_tag) @tag

View file

@ -0,0 +1,7 @@
(script_element
(raw_text) @content
(#set! "language" "javascript"))
(style_element
(raw_text) @content
(#set! "language" "css"))

View file

@ -0,0 +1,7 @@
(comment) @comment
[
(raw_text)
(attribute_value)
(quoted_attribute_value)
] @string

135
extensions/vue/src/vue.rs Normal file
View file

@ -0,0 +1,135 @@
use std::{env, fs};
use zed::lsp::{Completion, CompletionKind};
use zed::CodeLabelSpan;
use zed_extension_api::{self as zed, serde_json, Result};
struct VueExtension {
did_find_server: bool,
}
const SERVER_PATH: &str = "node_modules/@vue/language-server/bin/vue-language-server.js";
const PACKAGE_NAME: &str = "@vue/language-server";
impl VueExtension {
fn server_exists(&self) -> bool {
fs::metadata(SERVER_PATH).map_or(false, |stat| stat.is_file())
}
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(
id,
&zed::LanguageServerInstallationStatus::CheckingForUpdate,
);
// We hardcode the version to 1.8 since we do not support @vue/language-server 2.0 yet.
let version = "1.8".to_string();
if !server_exists
|| zed::npm_package_installed_version(PACKAGE_NAME)?.as_ref() != Some(&version)
{
zed::set_language_server_installation_status(
id,
&zed::LanguageServerInstallationStatus::Downloading,
);
let result = zed::npm_install_package(PACKAGE_NAME, &version);
match result {
Ok(()) => {
if !self.server_exists() {
Err(format!(
"installed package '{PACKAGE_NAME}' did not contain expected path '{SERVER_PATH}'",
))?;
}
}
Err(error) => {
if !self.server_exists() {
Err(error)?;
}
}
}
}
self.did_find_server = true;
Ok(SERVER_PATH.to_string())
}
}
impl zed::Extension for VueExtension {
fn new() -> Self {
Self {
did_find_server: false,
}
}
fn language_server_command(
&mut self,
id: &zed::LanguageServerId,
_: &zed::Worktree,
) -> Result<zed::Command> {
let server_path = self.server_script_path(id)?;
Ok(zed::Command {
command: zed::node_binary_path()?,
args: vec![
env::current_dir()
.unwrap()
.join(&server_path)
.to_string_lossy()
.to_string(),
"--stdio".to_string(),
],
env: Default::default(),
})
}
fn language_server_initialization_options(
&mut self,
_: &zed::LanguageServerId,
_: &zed::Worktree,
) -> Result<Option<serde_json::Value>> {
Ok(Some(serde_json::json!({
"typescript": {
"tsdk": "node_modules/typescript/lib"
}
})))
}
fn label_for_completion(
&self,
_language_server_id: &zed::LanguageServerId,
completion: Completion,
) -> Option<zed::CodeLabel> {
let highlight_name = match completion.kind? {
CompletionKind::Class | CompletionKind::Interface => "type",
CompletionKind::Constructor => "type",
CompletionKind::Constant => "constant",
CompletionKind::Function | CompletionKind::Method => "function",
CompletionKind::Property | CompletionKind::Field => "tag",
CompletionKind::Variable => "type",
CompletionKind::Keyword => "keyword",
CompletionKind::Value => "tag",
_ => return None,
};
let len = completion.label.len();
let name_span = CodeLabelSpan::literal(completion.label, Some(highlight_name.to_string()));
Some(zed::CodeLabel {
code: Default::default(),
spans: if let Some(detail) = completion.detail {
vec![
name_span,
CodeLabelSpan::literal(" ", None),
CodeLabelSpan::literal(detail, None),
]
} else {
vec![name_span]
},
filter_range: (0..len).into(),
})
}
}
zed::register_extension!(VueExtension);