Piotr/z 651 vue support (#3123)

Release Notes:

- Added Vue language support.
This commit is contained in:
Piotr Osiewicz 2023-10-13 18:58:59 +02:00 committed by GitHub
parent 16d9d77d88
commit bfbe4ae4b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 286 additions and 18 deletions

10
Cargo.lock generated
View File

@ -8792,6 +8792,15 @@ dependencies = [
"tree-sitter",
]
[[package]]
name = "tree-sitter-vue"
version = "0.0.1"
source = "git+https://github.com/zed-industries/tree-sitter-vue?rev=95b2890#95b28908d90e928c308866f7631e73ef6e1d4b5f"
dependencies = [
"cc",
"tree-sitter",
]
[[package]]
name = "tree-sitter-yaml"
version = "0.0.1"
@ -10161,6 +10170,7 @@ dependencies = [
"tree-sitter-svelte",
"tree-sitter-toml",
"tree-sitter-typescript",
"tree-sitter-vue",
"tree-sitter-yaml",
"unindent",
"url",

View File

@ -149,7 +149,7 @@ tree-sitter-yaml = { git = "https://github.com/zed-industries/tree-sitter-yaml",
tree-sitter-lua = "0.0.14"
tree-sitter-nix = { git = "https://github.com/nix-community/tree-sitter-nix", rev = "66e3e9ce9180ae08fc57372061006ef83f0abde7" }
tree-sitter-nu = { git = "https://github.com/nushell/tree-sitter-nu", rev = "786689b0562b9799ce53e824cb45a1a2a04dc673"}
tree-sitter-vue = {git = "https://github.com/zed-industries/tree-sitter-vue", rev = "95b2890"}
[patch.crates-io]
tree-sitter = { git = "https://github.com/tree-sitter/tree-sitter", rev = "35a6052fbcafc5e5fc0f9415b8652be7dcaf7222" }
async-task = { git = "https://github.com/zed-industries/async-task", rev = "341b57d6de98cdfd7b418567b8de2022ca993a6e" }

View File

@ -110,7 +110,6 @@ pub struct LanguageServerName(pub Arc<str>);
pub struct CachedLspAdapter {
pub name: LanguageServerName,
pub short_name: &'static str,
pub initialization_options: Option<Value>,
pub disk_based_diagnostic_sources: Vec<String>,
pub disk_based_diagnostics_progress_token: Option<String>,
pub language_ids: HashMap<String, String>,
@ -121,7 +120,6 @@ impl CachedLspAdapter {
pub async fn new(adapter: Arc<dyn LspAdapter>) -> Arc<Self> {
let name = adapter.name().await;
let short_name = adapter.short_name();
let initialization_options = adapter.initialization_options().await;
let disk_based_diagnostic_sources = adapter.disk_based_diagnostic_sources().await;
let disk_based_diagnostics_progress_token =
adapter.disk_based_diagnostics_progress_token().await;
@ -130,7 +128,6 @@ impl CachedLspAdapter {
Arc::new(CachedLspAdapter {
name,
short_name,
initialization_options,
disk_based_diagnostic_sources,
disk_based_diagnostics_progress_token,
language_ids,

View File

@ -2751,15 +2751,6 @@ impl Project {
let lsp = project_settings.lsp.get(&adapter.name.0);
let override_options = lsp.map(|s| s.initialization_options.clone()).flatten();
let mut initialization_options = adapter.initialization_options.clone();
match (&mut initialization_options, override_options) {
(Some(initialization_options), Some(override_options)) => {
merge_json_value_into(override_options, initialization_options);
}
(None, override_options) => initialization_options = override_options,
_ => {}
}
let server_id = pending_server.server_id;
let container_dir = pending_server.container_dir.clone();
let state = LanguageServerState::Starting({
@ -2771,7 +2762,7 @@ impl Project {
cx.spawn_weak(|this, mut cx| async move {
let result = Self::setup_and_insert_language_server(
this,
initialization_options,
override_options,
pending_server,
adapter.clone(),
language.clone(),
@ -2874,7 +2865,7 @@ impl Project {
async fn setup_and_insert_language_server(
this: WeakModelHandle<Self>,
initialization_options: Option<serde_json::Value>,
override_initialization_options: Option<serde_json::Value>,
pending_server: PendingLanguageServer,
adapter: Arc<CachedLspAdapter>,
language: Arc<Language>,
@ -2884,7 +2875,7 @@ impl Project {
) -> Result<Option<Arc<LanguageServer>>> {
let setup = Self::setup_pending_language_server(
this,
initialization_options,
override_initialization_options,
pending_server,
adapter.clone(),
server_id,
@ -2916,7 +2907,7 @@ impl Project {
async fn setup_pending_language_server(
this: WeakModelHandle<Self>,
initialization_options: Option<serde_json::Value>,
override_options: Option<serde_json::Value>,
pending_server: PendingLanguageServer,
adapter: Arc<CachedLspAdapter>,
server_id: LanguageServerId,
@ -3062,6 +3053,14 @@ impl Project {
}
})
.detach();
let mut initialization_options = adapter.adapter.initialization_options().await;
match (&mut initialization_options, override_options) {
(Some(initialization_options), Some(override_options)) => {
merge_json_value_into(override_options, initialization_options);
}
(None, override_options) => initialization_options = override_options,
_ => {}
}
let language_server = language_server.initialize(initialization_options).await?;

View File

@ -16,6 +16,7 @@ pub struct GithubRelease {
pub pre_release: bool,
pub assets: Vec<GithubReleaseAsset>,
pub tarball_url: String,
pub zipball_url: String,
}
#[derive(Deserialize, Debug)]

View File

@ -138,6 +138,7 @@ tree-sitter-yaml.workspace = true
tree-sitter-lua.workspace = true
tree-sitter-nix.workspace = true
tree-sitter-nu.workspace = true
tree-sitter-vue.workspace = true
url = "2.2"
urlencoding = "2.1.2"

View File

@ -24,6 +24,7 @@ mod rust;
mod svelte;
mod tailwind;
mod typescript;
mod vue;
mod yaml;
// 1. Add tree-sitter-{language} parser to zed crate
@ -190,13 +191,20 @@ pub fn init(
language(
"php",
tree_sitter_php::language(),
vec![Arc::new(php::IntelephenseLspAdapter::new(node_runtime))],
vec![Arc::new(php::IntelephenseLspAdapter::new(
node_runtime.clone(),
))],
);
language("elm", tree_sitter_elm::language(), vec![]);
language("glsl", tree_sitter_glsl::language(), vec![]);
language("nix", tree_sitter_nix::language(), vec![]);
language("nu", tree_sitter_nu::language(), vec![]);
language(
"vue",
tree_sitter_vue::language(),
vec![Arc::new(vue::VueLspAdapter::new(node_runtime))],
);
}
#[cfg(any(test, feature = "test-support"))]

View File

@ -0,0 +1,214 @@
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use futures::StreamExt;
pub use language::*;
use lsp::{CodeActionKind, LanguageServerBinary};
use node_runtime::NodeRuntime;
use parking_lot::Mutex;
use serde_json::Value;
use smol::fs::{self};
use std::{
any::Any,
ffi::OsString,
path::{Path, PathBuf},
sync::Arc,
};
use util::ResultExt;
pub struct VueLspVersion {
vue_version: String,
ts_version: String,
}
pub struct VueLspAdapter {
node: Arc<dyn NodeRuntime>,
typescript_install_path: Mutex<Option<PathBuf>>,
}
impl VueLspAdapter {
const SERVER_PATH: &'static str =
"node_modules/@vue/language-server/bin/vue-language-server.js";
// TODO: this can't be hardcoded, yet we have to figure out how to pass it in initialization_options.
const TYPESCRIPT_PATH: &'static str = "node_modules/typescript/lib";
pub fn new(node: Arc<dyn NodeRuntime>) -> Self {
let typescript_install_path = Mutex::new(None);
Self {
node,
typescript_install_path,
}
}
}
#[async_trait]
impl super::LspAdapter for VueLspAdapter {
async fn name(&self) -> LanguageServerName {
LanguageServerName("vue-language-server".into())
}
fn short_name(&self) -> &'static str {
"vue-language-server"
}
async fn fetch_latest_server_version(
&self,
_: &dyn LspAdapterDelegate,
) -> Result<Box<dyn 'static + Send + Any>> {
Ok(Box::new(VueLspVersion {
vue_version: self
.node
.npm_package_latest_version("@vue/language-server")
.await?,
ts_version: self.node.npm_package_latest_version("typescript").await?,
}) as Box<_>)
}
async fn initialization_options(&self) -> Option<Value> {
let typescript_sdk_path = self.typescript_install_path.lock();
let typescript_sdk_path = typescript_sdk_path
.as_ref()
.expect("initialization_options called without a container_dir for typescript");
Some(serde_json::json!({
"typescript": {
"tsdk": typescript_sdk_path
}
}))
}
fn code_action_kinds(&self) -> Option<Vec<CodeActionKind>> {
// 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.
Some(vec![
CodeActionKind::EMPTY,
CodeActionKind::QUICKFIX,
CodeActionKind::REFACTOR_REWRITE,
])
}
async fn fetch_server_binary(
&self,
version: Box<dyn 'static + Send + Any>,
container_dir: PathBuf,
_: &dyn LspAdapterDelegate,
) -> Result<LanguageServerBinary> {
let version = version.downcast::<VueLspVersion>().unwrap();
let server_path = container_dir.join(Self::SERVER_PATH);
let ts_path = container_dir.join(Self::TYPESCRIPT_PATH);
if fs::metadata(&server_path).await.is_err() {
self.node
.npm_install_packages(
&container_dir,
&[("@vue/language-server", version.vue_version.as_str())],
)
.await?;
}
assert!(fs::metadata(&server_path).await.is_ok());
if fs::metadata(&ts_path).await.is_err() {
self.node
.npm_install_packages(
&container_dir,
&[("typescript", version.ts_version.as_str())],
)
.await?;
}
assert!(fs::metadata(&ts_path).await.is_ok());
*self.typescript_install_path.lock() = Some(ts_path);
Ok(LanguageServerBinary {
path: self.node.binary_path().await?,
arguments: vue_server_binary_arguments(&server_path),
})
}
async fn cached_server_binary(
&self,
container_dir: PathBuf,
_: &dyn LspAdapterDelegate,
) -> Option<LanguageServerBinary> {
let (server, ts_path) = get_cached_server_binary(container_dir, self.node.clone()).await?;
*self.typescript_install_path.lock() = Some(ts_path);
Some(server)
}
async fn installation_test_binary(
&self,
container_dir: PathBuf,
) -> Option<LanguageServerBinary> {
let (server, ts_path) = get_cached_server_binary(container_dir, self.node.clone())
.await
.map(|(mut binary, ts_path)| {
binary.arguments = vec!["--help".into()];
(binary, ts_path)
})?;
*self.typescript_install_path.lock() = Some(ts_path);
Some(server)
}
async fn label_for_completion(
&self,
item: &lsp::CompletionItem,
language: &Arc<language::Language>,
) -> Option<language::CodeLabel> {
use lsp::CompletionItemKind as Kind;
let len = item.label.len();
let grammar = language.grammar()?;
let highlight_id = match item.kind? {
Kind::CLASS | Kind::INTERFACE => grammar.highlight_id_for_name("type"),
Kind::CONSTRUCTOR => grammar.highlight_id_for_name("type"),
Kind::CONSTANT => grammar.highlight_id_for_name("constant"),
Kind::FUNCTION | Kind::METHOD => grammar.highlight_id_for_name("function"),
Kind::PROPERTY | Kind::FIELD => grammar.highlight_id_for_name("tag"),
Kind::VARIABLE => grammar.highlight_id_for_name("type"),
Kind::KEYWORD => grammar.highlight_id_for_name("keyword"),
Kind::VALUE => grammar.highlight_id_for_name("tag"),
_ => None,
}?;
let text = match &item.detail {
Some(detail) => format!("{} {}", item.label, detail),
None => item.label.clone(),
};
Some(language::CodeLabel {
text,
runs: vec![(0..len, highlight_id)],
filter_range: 0..len,
})
}
}
fn vue_server_binary_arguments(server_path: &Path) -> Vec<OsString> {
vec![server_path.into(), "--stdio".into()]
}
type TypescriptPath = PathBuf;
async fn get_cached_server_binary(
container_dir: PathBuf,
node: Arc<dyn NodeRuntime>,
) -> Option<(LanguageServerBinary, TypescriptPath)> {
(|| async move {
let mut last_version_dir = None;
let mut entries = fs::read_dir(&container_dir).await?;
while let Some(entry) = entries.next().await {
let entry = entry?;
if entry.file_type().await?.is_dir() {
last_version_dir = Some(entry.path());
}
}
let last_version_dir = last_version_dir.ok_or_else(|| anyhow!("no cached binary"))?;
let server_path = last_version_dir.join(VueLspAdapter::SERVER_PATH);
let typescript_path = last_version_dir.join(VueLspAdapter::TYPESCRIPT_PATH);
if server_path.exists() && typescript_path.exists() {
Ok((
LanguageServerBinary {
path: node.binary_path().await?,
arguments: vue_server_binary_arguments(&server_path),
},
typescript_path,
))
} else {
Err(anyhow!(
"missing executable in directory {:?}",
last_version_dir
))
}
})()
.await
.log_err()
}

View File

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

View File

@ -0,0 +1,14 @@
name = "Vue.js"
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 = ["-"]

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"))