From e5b71cc6ac81494de9114286f97696f1272c5a38 Mon Sep 17 00:00:00 2001 From: Allan Calix Date: Wed, 24 Jan 2024 20:07:18 -0800 Subject: [PATCH 1/3] Add zig support --- Cargo.lock | 10 + Cargo.toml | 1 + crates/zed/Cargo.toml | 1 + crates/zed/src/languages.rs | 6 + crates/zed/src/languages/zig.rs | 125 +++++++++++ crates/zed/src/languages/zig/config.toml | 10 + crates/zed/src/languages/zig/folds.scm | 16 ++ crates/zed/src/languages/zig/highlights.scm | 234 ++++++++++++++++++++ crates/zed/src/languages/zig/indents.scm | 22 ++ crates/zed/src/languages/zig/injections.scm | 5 + 10 files changed, 430 insertions(+) create mode 100644 crates/zed/src/languages/zig.rs create mode 100644 crates/zed/src/languages/zig/config.toml create mode 100644 crates/zed/src/languages/zig/folds.scm create mode 100644 crates/zed/src/languages/zig/highlights.scm create mode 100644 crates/zed/src/languages/zig/indents.scm create mode 100644 crates/zed/src/languages/zig/injections.scm diff --git a/Cargo.lock b/Cargo.lock index fc1217161ab796bff024b95e39722e98bee75eea..24b3633753d7802542cbcd95f65c81f0ac1a48fc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8716,6 +8716,15 @@ dependencies = [ "tree-sitter", ] +[[package]] +name = "tree-sitter-zig" +version = "0.0.1" +source = "git+https://github.com/maxxnino/tree-sitter-zig?rev=0d08703e4c3f426ec61695d7617415fff97029bd#0d08703e4c3f426ec61695d7617415fff97029bd" +dependencies = [ + "cc", + "tree-sitter", +] + [[package]] name = "try-lock" version = "0.2.4" @@ -9812,6 +9821,7 @@ dependencies = [ "tree-sitter-uiua", "tree-sitter-vue", "tree-sitter-yaml", + "tree-sitter-zig", "unindent", "url", "urlencoding", diff --git a/Cargo.toml b/Cargo.toml index 4c3658b1a320f7e32624b73b66beb427cc629319..bad9e149bc50d96abc32b000d4002665f7545685 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -161,6 +161,7 @@ tree-sitter-nix = { git = "https://github.com/nix-community/tree-sitter-nix", re tree-sitter-nu = { git = "https://github.com/nushell/tree-sitter-nu", rev = "26bbaecda0039df4067861ab38ea8ea169f7f5aa"} tree-sitter-vue = {git = "https://github.com/zed-industries/tree-sitter-vue", rev = "6608d9d60c386f19d80af7d8132322fa11199c42"} tree-sitter-uiua = {git = "https://github.com/shnarazk/tree-sitter-uiua", rev = "9260f11be5900beda4ee6d1a24ab8ddfaf5a19b2"} +tree-sitter-zig = { git = "https://github.com/maxxnino/tree-sitter-zig", rev = "0d08703e4c3f426ec61695d7617415fff97029bd" } [patch.crates-io] tree-sitter = { git = "https://github.com/tree-sitter/tree-sitter", rev = "31c40449749c4263a91a43593831b82229049a4c" } diff --git a/crates/zed/Cargo.toml b/crates/zed/Cargo.toml index d1a83e0b3fa9f3589f6d494f3c2097909ddbb62b..a4084a7bd9437b14502843e61c7cff8ccf3a8326 100644 --- a/crates/zed/Cargo.toml +++ b/crates/zed/Cargo.toml @@ -142,6 +142,7 @@ tree-sitter-nix.workspace = true tree-sitter-nu.workspace = true tree-sitter-vue.workspace = true tree-sitter-uiua.workspace = true +tree-sitter-zig.workspace = true url = "2.2" urlencoding = "2.1.2" diff --git a/crates/zed/src/languages.rs b/crates/zed/src/languages.rs index bba9137708fa40c5881990c909ed2627da639c71..cb557ced8dd1eee42d24ba6eb306d048587b8bc8 100644 --- a/crates/zed/src/languages.rs +++ b/crates/zed/src/languages.rs @@ -31,6 +31,7 @@ mod typescript; mod uiua; mod vue; mod yaml; +mod zig; // 1. Add tree-sitter-{language} parser to zed crate // 2. Create a language directory in zed/crates/zed/src/languages and add the language to init function below @@ -112,6 +113,11 @@ pub fn init( tree_sitter_go::language(), vec![Arc::new(go::GoLspAdapter)], ); + language( + "zig", + tree_sitter_zig::language(), + vec![Arc::new(zig::ZlsAdapter)], + ); language( "heex", tree_sitter_heex::language(), diff --git a/crates/zed/src/languages/zig.rs b/crates/zed/src/languages/zig.rs new file mode 100644 index 0000000000000000000000000000000000000000..734d21e2eabf9f68a23267f8eca770edfc3ad795 --- /dev/null +++ b/crates/zed/src/languages/zig.rs @@ -0,0 +1,125 @@ +use anyhow::{anyhow, Context, Result}; +use async_compression::futures::bufread::GzipDecoder; +use async_tar::Archive; +use async_trait::async_trait; +use futures::{io::BufReader, StreamExt}; +use language::{LanguageServerName, LspAdapter, LspAdapterDelegate}; +use lsp::LanguageServerBinary; +use smol::fs; +use std::env::consts::ARCH; +use std::{any::Any, path::PathBuf}; +use util::async_maybe; +use util::github::latest_github_release; +use util::{github::GitHubLspBinaryVersion, ResultExt}; + +pub struct ZlsAdapter; + +#[async_trait] +impl LspAdapter for ZlsAdapter { + fn name(&self) -> LanguageServerName { + LanguageServerName("zls".into()) + } + + fn short_name(&self) -> &'static str { + "zls" + } + + async fn fetch_latest_server_version( + &self, + delegate: &dyn LspAdapterDelegate, + ) -> Result> { + let release = latest_github_release("zigtools/zls", false, delegate.http_client()).await?; + let asset_name = format!("zls-{}-macos.tar.gz", ARCH); + let asset = release + .assets + .iter() + .find(|asset| asset.name == asset_name) + .ok_or_else(|| anyhow!("no asset found matching {:?}", asset_name))?; + let version = GitHubLspBinaryVersion { + name: release.name, + url: asset.browser_download_url.clone(), + }; + + Ok(Box::new(version) as Box<_>) + } + + async fn fetch_server_binary( + &self, + version: Box, + container_dir: PathBuf, + delegate: &dyn LspAdapterDelegate, + ) -> Result { + let version = version.downcast::().unwrap(); + let binary_path = container_dir.join("bin/zls"); + + if fs::metadata(&binary_path).await.is_err() { + let mut response = delegate + .http_client() + .get(&version.url, Default::default(), true) + .await + .context("error downloading release")?; + let decompressed_bytes = GzipDecoder::new(BufReader::new(response.body_mut())); + let archive = Archive::new(decompressed_bytes); + archive.unpack(container_dir).await?; + } + + fs::set_permissions( + &binary_path, + ::from_mode(0o755), + ) + .await?; + Ok(LanguageServerBinary { + path: binary_path, + arguments: vec![], + }) + } + + async fn cached_server_binary( + &self, + container_dir: PathBuf, + _: &dyn LspAdapterDelegate, + ) -> Option { + get_cached_server_binary(container_dir).await + } + + async fn installation_test_binary( + &self, + container_dir: PathBuf, + ) -> Option { + get_cached_server_binary(container_dir) + .await + .map(|mut binary| { + binary.arguments = vec!["--help".into()]; + binary + }) + } +} + +async fn get_cached_server_binary(container_dir: PathBuf) -> Option { + async_maybe!({ + let mut last_binary_path = 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_file() + && entry + .file_name() + .to_str() + .map_or(false, |name| name == "zls") + { + last_binary_path = Some(entry.path()); + } + } + + if let Some(path) = last_binary_path { + Ok(LanguageServerBinary { + path, + arguments: Vec::new(), + }) + } else { + Err(anyhow!("no cached binary")) + } + }) + .await + .log_err() +} diff --git a/crates/zed/src/languages/zig/config.toml b/crates/zed/src/languages/zig/config.toml new file mode 100644 index 0000000000000000000000000000000000000000..2ac71fd5c07a5e60ec72c445a4fe6c8eaa157507 --- /dev/null +++ b/crates/zed/src/languages/zig/config.toml @@ -0,0 +1,10 @@ +name = "Zig" +path_suffixes = ["zig"] +line_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 }, +] diff --git a/crates/zed/src/languages/zig/folds.scm b/crates/zed/src/languages/zig/folds.scm new file mode 100644 index 0000000000000000000000000000000000000000..9659874395e11c01a4b517ca195cfcb6ed86232d --- /dev/null +++ b/crates/zed/src/languages/zig/folds.scm @@ -0,0 +1,16 @@ +[ + (Block) + (ContainerDecl) + (SwitchExpr) + (InitList) + (AsmExpr) + (ErrorSetDecl) + (LINESTRING) + ( + [ + (IfPrefix) + (WhilePrefix) + (ForPrefix) + ] + ) +] @fold diff --git a/crates/zed/src/languages/zig/highlights.scm b/crates/zed/src/languages/zig/highlights.scm new file mode 100644 index 0000000000000000000000000000000000000000..189d6aa468f181b104d0467f162b90883acbba01 --- /dev/null +++ b/crates/zed/src/languages/zig/highlights.scm @@ -0,0 +1,234 @@ +[ + (container_doc_comment) + (doc_comment) + (line_comment) +] @comment + +[ + variable: (IDENTIFIER) + variable_type_function: (IDENTIFIER) +] @variable + +parameter: (IDENTIFIER) @parameter + +[ + field_member: (IDENTIFIER) + field_access: (IDENTIFIER) +] @field + +;; assume TitleCase is a type +( + [ + variable_type_function: (IDENTIFIER) + field_access: (IDENTIFIER) + parameter: (IDENTIFIER) + ] @type + (#match? @type "^[A-Z]([a-z]+[A-Za-z0-9]*)*$") +) +;; assume camelCase is a function +( + [ + variable_type_function: (IDENTIFIER) + field_access: (IDENTIFIER) + parameter: (IDENTIFIER) + ] @function + (#match? @function "^[a-z]+([A-Z][a-z0-9]*)+$") +) + +;; assume all CAPS_1 is a constant +( + [ + variable_type_function: (IDENTIFIER) + field_access: (IDENTIFIER) + ] @constant + (#match? @constant "^[A-Z][A-Z_0-9]+$") +) + +[ + function_call: (IDENTIFIER) + function: (IDENTIFIER) +] @function + +exception: "!" @exception + +( + (IDENTIFIER) @variable.builtin + (#eq? @variable.builtin "_") +) + +(PtrTypeStart "c" @variable.builtin) + +( + (ContainerDeclType + [ + (ErrorUnionExpr) + "enum" + ] + ) + (ContainerField (IDENTIFIER) @constant) +) + +field_constant: (IDENTIFIER) @constant + +(BUILTINIDENTIFIER) @keyword + +; No idea why this doesnt work +; ((BUILTINIDENTIFIER) @include +; (#any-of? @include "@import" "@cImport")) + +(INTEGER) @number + +(FLOAT) @float + +[ + "true" + "false" +] @boolean + +[ + (LINESTRING) + (STRINGLITERALSINGLE) +] @string + +(CHAR_LITERAL) @character +(EscapeSequence) @string.escape +(FormatSequence) @string.special + +(BreakLabel (IDENTIFIER) @label) +(BlockLabel (IDENTIFIER) @label) + +[ + "asm" + "defer" + "errdefer" + "test" + "struct" + "union" + "enum" + "opaque" + "error" +] @keyword + +[ + "async" + "await" + "suspend" + "nosuspend" + "resume" +] @keyword.coroutine + +[ + "fn" +] @keyword.function + +[ + "and" + "or" + "orelse" +] @keyword.operator + +[ + "return" +] @keyword.return + +[ + "if" + "else" + "switch" +] @conditional + +[ + "for" + "while" + "break" + "continue" +] @keyword + +[ + "usingnamespace" +] @include + +[ + "try" + "catch" +] @keyword + +[ + "anytype" + (BuildinTypeExpr) +] @type.builtin + +[ + "const" + "var" + "volatile" + "allowzero" + "noalias" +] @type.qualifier + +[ + "addrspace" + "align" + "callconv" + "linksection" +] @storageclass + +[ + "comptime" + "export" + "extern" + "inline" + "noinline" + "packed" + "pub" + "threadlocal" +] @attribute + +[ + "null" + "unreachable" + "undefined" +] @constant.builtin + +[ + (CompareOp) + (BitwiseOp) + (BitShiftOp) + (AdditionOp) + (AssignOp) + (MultiplyOp) + (PrefixOp) + "*" + "**" + "->" + ".?" + ".*" + "?" +] @operator + +[ + ";" + "." + "," + ":" +] @punctuation.delimiter + +[ + ".." + "..." +] @punctuation.special + +[ + "[" + "]" + "(" + ")" + "{" + "}" + (Payload "|") + (PtrPayload "|") + (PtrIndexPayload "|") +] @punctuation.bracket + +; Error +(ERROR) @error diff --git a/crates/zed/src/languages/zig/indents.scm b/crates/zed/src/languages/zig/indents.scm new file mode 100644 index 0000000000000000000000000000000000000000..a2af44ee406eb6b50bd09866a81588a9db728512 --- /dev/null +++ b/crates/zed/src/languages/zig/indents.scm @@ -0,0 +1,22 @@ +[ + (Block) + (ContainerDecl) + (SwitchExpr) + (InitList) +] @indent + +[ + "(" + ")" + "[" + "]" + "{" + "}" +] @branch + +[ + (line_comment) + (container_doc_comment) + (doc_comment) + (LINESTRING) +] @ignore diff --git a/crates/zed/src/languages/zig/injections.scm b/crates/zed/src/languages/zig/injections.scm new file mode 100644 index 0000000000000000000000000000000000000000..e3ff406d36666f00d77e6348df7c711c19e80f8b --- /dev/null +++ b/crates/zed/src/languages/zig/injections.scm @@ -0,0 +1,5 @@ +[ + (container_doc_comment) + (doc_comment) + (line_comment) +] @comment From ebbfff5ce8329ba83af88c1bbf0e29f820fb3797 Mon Sep 17 00:00:00 2001 From: Allan Calix Date: Thu, 25 Jan 2024 19:17:18 -0800 Subject: [PATCH 2/3] Updates zigs highlight to emit right captures --- crates/zed/src/languages/zig/highlights.scm | 18 +++++++++--------- crates/zed/src/languages/zig/indents.scm | 6 +++--- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/crates/zed/src/languages/zig/highlights.scm b/crates/zed/src/languages/zig/highlights.scm index 189d6aa468f181b104d0467f162b90883acbba01..62bb548401859415a0f5a45ed3b1dbacf27a1c2c 100644 --- a/crates/zed/src/languages/zig/highlights.scm +++ b/crates/zed/src/languages/zig/highlights.scm @@ -25,6 +25,7 @@ parameter: (IDENTIFIER) @parameter ] @type (#match? @type "^[A-Z]([a-z]+[A-Za-z0-9]*)*$") ) + ;; assume camelCase is a function ( [ @@ -32,7 +33,7 @@ parameter: (IDENTIFIER) @parameter field_access: (IDENTIFIER) parameter: (IDENTIFIER) ] @function - (#match? @function "^[a-z]+([A-Z][a-z0-9]*)+$") + (#match? @function "^[a-z]+([A-Z][a-z0-9]+)$") ) ;; assume all CAPS_1 is a constant @@ -49,7 +50,7 @@ parameter: (IDENTIFIER) @parameter function: (IDENTIFIER) ] @function -exception: "!" @exception +exception: "!" @keyword.exception ( (IDENTIFIER) @variable.builtin @@ -72,13 +73,12 @@ field_constant: (IDENTIFIER) @constant (BUILTINIDENTIFIER) @keyword -; No idea why this doesnt work -; ((BUILTINIDENTIFIER) @include -; (#any-of? @include "@import" "@cImport")) +((BUILTINIDENTIFIER) @keyword.import + (#any-of? @keyword.import "@import" "@cImport")) (INTEGER) @number -(FLOAT) @float +(FLOAT) @number.float [ "true" @@ -135,7 +135,7 @@ field_constant: (IDENTIFIER) @constant "if" "else" "switch" -] @conditional +] @keyword [ "for" @@ -146,7 +146,7 @@ field_constant: (IDENTIFIER) @constant [ "usingnamespace" -] @include +] @keyword.import [ "try" @@ -171,7 +171,7 @@ field_constant: (IDENTIFIER) @constant "align" "callconv" "linksection" -] @storageclass +] @keyword.storage [ "comptime" diff --git a/crates/zed/src/languages/zig/indents.scm b/crates/zed/src/languages/zig/indents.scm index a2af44ee406eb6b50bd09866a81588a9db728512..9c27ddcba3758fabfdf17a4e391bdeb4eb98159d 100644 --- a/crates/zed/src/languages/zig/indents.scm +++ b/crates/zed/src/languages/zig/indents.scm @@ -3,7 +3,7 @@ (ContainerDecl) (SwitchExpr) (InitList) -] @indent +] @indent.begin [ "(" @@ -12,11 +12,11 @@ "]" "{" "}" -] @branch +] @indent.branch [ (line_comment) (container_doc_comment) (doc_comment) (LINESTRING) -] @ignore +] @indent.ignore From 742329ee8f25c932f1b61adbcd5709e8bf7c41da Mon Sep 17 00:00:00 2001 From: Allan Calix Date: Thu, 25 Jan 2024 20:30:07 -0800 Subject: [PATCH 3/3] Highlight a broader range of zig functions --- crates/zed/src/languages/zig/highlights.scm | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/crates/zed/src/languages/zig/highlights.scm b/crates/zed/src/languages/zig/highlights.scm index 62bb548401859415a0f5a45ed3b1dbacf27a1c2c..53b08aef8562aef2eb5c6c2639ef7e8c58de54ac 100644 --- a/crates/zed/src/languages/zig/highlights.scm +++ b/crates/zed/src/languages/zig/highlights.scm @@ -26,14 +26,10 @@ parameter: (IDENTIFIER) @parameter (#match? @type "^[A-Z]([a-z]+[A-Za-z0-9]*)*$") ) -;; assume camelCase is a function ( - [ - variable_type_function: (IDENTIFIER) - field_access: (IDENTIFIER) - parameter: (IDENTIFIER) - ] @function - (#match? @function "^[a-z]+([A-Z][a-z0-9]+)$") + (_ + variable_type_function: (IDENTIFIER) @function + (FnCallArguments)) ) ;; assume all CAPS_1 is a constant