Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Piotr/z 651 vue support (#3123)
Piotr Osiewiczbfbe4ae4b471
diff --git a/Cargo.lock b/Cargo.lockindex 85b80fa487d..2ef86073cea 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -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",diff --git a/Cargo.toml b/Cargo.tomlindex 532610efd63..995cd15edd4 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -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" }diff --git a/crates/language/src/language.rs b/crates/language/src/language.rsindex bd389652a02..eb6f6e89f70 100644--- a/crates/language/src/language.rs+++ b/crates/language/src/language.rs@@ -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,diff --git a/crates/project/src/project.rs b/crates/project/src/project.rsindex e3251d74835..875086a4e39 100644--- a/crates/project/src/project.rs+++ b/crates/project/src/project.rs@@ -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?;diff --git a/crates/util/src/github.rs b/crates/util/src/github.rsindex b1e981ae496..a3df4c996be 100644--- a/crates/util/src/github.rs+++ b/crates/util/src/github.rs@@ -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)]diff --git a/crates/zed/Cargo.toml b/crates/zed/Cargo.tomlindex f9abcc1e91d..aeabd4b4531 100644--- a/crates/zed/Cargo.toml+++ b/crates/zed/Cargo.toml@@ -138,6 +138,7 @@ tree-sitter-yaml.workspace = truetree-sitter-lua.workspace = truetree-sitter-nix.workspace = truetree-sitter-nu.workspace = true+tree-sitter-vue.workspace = trueurl = "2.2"urlencoding = "2.1.2"diff --git a/crates/zed/src/languages.rs b/crates/zed/src/languages.rsindex 04e5292a7df..caf3cbf7c94 100644--- a/crates/zed/src/languages.rs+++ b/crates/zed/src/languages.rs@@ -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"))]diff --git a/crates/zed/src/languages/vue.rs b/crates/zed/src/languages/vue.rsnew file mode 100644index 00000000000..f0374452dfa--- /dev/null+++ b/crates/zed/src/languages/vue.rs@@ -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()+}diff --git a/crates/zed/src/languages/vue/brackets.scm b/crates/zed/src/languages/vue/brackets.scmnew file mode 100644index 00000000000..2d12b17daab--- /dev/null+++ b/crates/zed/src/languages/vue/brackets.scm@@ -0,0 +1,2 @@+("<" @open ">" @close)+("\"" @open "\"" @close)diff --git a/crates/zed/src/languages/vue/config.toml b/crates/zed/src/languages/vue/config.tomlnew file mode 100644index 00000000000..c41a667b752--- /dev/null+++ b/crates/zed/src/languages/vue/config.toml@@ -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 = ["-"]diff --git a/crates/zed/src/languages/vue/highlights.scm b/crates/zed/src/languages/vue/highlights.scmnew file mode 100644index 00000000000..1a80c84f68e--- /dev/null+++ b/crates/zed/src/languages/vue/highlights.scm@@ -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) @tagdiff --git a/crates/zed/src/languages/vue/injections.scm b/crates/zed/src/languages/vue/injections.scmnew file mode 100644index 00000000000..9084e373f21--- /dev/null+++ b/crates/zed/src/languages/vue/injections.scm@@ -0,0 +1,7 @@+(script_element+ (raw_text) @content+ (#set! "language" "javascript"))++(style_element+ (raw_text) @content+ (#set! "language" "css"))