From a7505487e107107e8811aceba6295c25c897fc08 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Wed, 11 Jun 2025 16:18:38 +0800 Subject: [PATCH] highlighter: Fix Zig language highlight. (#942) image --- crates/ui/src/highlighter/languages.rs | 4 +- .../highlighter/languages/zig/highlights.scm | 291 ++++++++++++++++++ .../highlighter/languages/zig/injections.scm | 2 + 3 files changed, 295 insertions(+), 2 deletions(-) create mode 100644 crates/ui/src/highlighter/languages/zig/highlights.scm create mode 100644 crates/ui/src/highlighter/languages/zig/injections.scm diff --git a/crates/ui/src/highlighter/languages.rs b/crates/ui/src/highlighter/languages.rs index a52ae918..0b9c3d3e 100644 --- a/crates/ui/src/highlighter/languages.rs +++ b/crates/ui/src/highlighter/languages.rs @@ -250,8 +250,8 @@ impl Language { ), Self::Zig => ( tree_sitter_zig::LANGUAGE, - tree_sitter_zig::HIGHLIGHTS_QUERY, - tree_sitter_zig::INJECTIONS_QUERY, + include_str!("languages/zig/highlights.scm"), + include_str!("languages/zig/injections.scm"), "", ), Self::Java => ( diff --git a/crates/ui/src/highlighter/languages/zig/highlights.scm b/crates/ui/src/highlighter/languages/zig/highlights.scm new file mode 100644 index 00000000..b94dce2a --- /dev/null +++ b/crates/ui/src/highlighter/languages/zig/highlights.scm @@ -0,0 +1,291 @@ +; Variables + +(identifier) @variable + +; Parameters + +(parameter + name: (identifier) @variable.parameter) + +; Types + +(parameter + type: (identifier) @type) + +((identifier) @type + (#match? @type "^[A-Z_][a-zA-Z0-9_]*")) + +(variable_declaration + (identifier) @type + "=" + [ + (struct_declaration) + (enum_declaration) + (union_declaration) + (opaque_declaration) + ]) + +[ + (builtin_type) + "anyframe" +] @type.builtin + +; Constants + +((identifier) @constant + (#match? @constant "^[A-Z][A-Z_0-9]+$")) + +[ + "null" + "unreachable" + "undefined" +] @constant.builtin + +(field_expression + . + member: (identifier) @constant) + +(enum_declaration + (container_field + type: (identifier) @constant)) + +; Labels + +(block_label (identifier) @label) + +(break_label (identifier) @label) + +; Fields + +(field_initializer + . + (identifier) @variable.member) + +(field_expression + (_) + member: (identifier) @variable.member) + +(container_field + name: (identifier) @variable.member) + +(initializer_list + (assignment_expression + left: (field_expression + . + member: (identifier) @variable.member))) + +; Functions + +(builtin_identifier) @function.builtin + +(call_expression + function: (identifier) @function.call) + +(call_expression + function: (field_expression + member: (identifier) @function.call)) + +(function_declaration + name: (identifier) @function) + +; Modules + +(variable_declaration + (identifier) @module + (builtin_function + (builtin_identifier) @keyword.import + (#any-of? @keyword.import "@import" "@cImport"))) + +; Builtins + +[ + "c" + "..." +] @variable.builtin + +((identifier) @variable.builtin + (#eq? @variable.builtin "_")) + +(calling_convention + (identifier) @variable.builtin) + +; Keywords + +[ + "asm" + "defer" + "errdefer" + "test" + "error" + "const" + "var" +] @keyword + +[ + "struct" + "union" + "enum" + "opaque" +] @keyword.type + +[ + "async" + "await" + "suspend" + "nosuspend" + "resume" +] @keyword.coroutine + +"fn" @keyword.function + +[ + "and" + "or" + "orelse" +] @keyword.operator + +"return" @keyword.return + +[ + "if" + "else" + "switch" +] @keyword.conditional + +[ + "for" + "while" + "break" + "continue" +] @keyword.repeat + +[ + "usingnamespace" + "export" +] @keyword.import + +[ + "try" + "catch" +] @keyword.exception + +[ + "volatile" + "allowzero" + "noalias" + "addrspace" + "align" + "callconv" + "linksection" + "pub" + "inline" + "noinline" + "extern" + "comptime" + "packed" + "threadlocal" +] @keyword.modifier + +; Operator + +[ + "=" + "*=" + "*%=" + "*|=" + "/=" + "%=" + "+=" + "+%=" + "+|=" + "-=" + "-%=" + "-|=" + "<<=" + "<<|=" + ">>=" + "&=" + "^=" + "|=" + "!" + "~" + "-" + "-%" + "&" + "==" + "!=" + ">" + ">=" + "<=" + "<" + "&" + "^" + "|" + "<<" + ">>" + "<<|" + "+" + "++" + "+%" + "-%" + "+|" + "-|" + "*" + "/" + "%" + "**" + "*%" + "*|" + "||" + ".*" + ".?" + "?" + ".." +] @operator + +; Literals + +(character) @character + +([ + (string) + (multiline_string) +] @string + (#set! "priority" 95)) + +(integer) @number + +(float) @number.float + +(boolean) @boolean + +(escape_sequence) @string.escape + +; Punctuation + +[ + "[" + "]" + "(" + ")" + "{" + "}" +] @punctuation.bracket + +[ + ";" + "." + "," + ":" + "=>" + "->" +] @punctuation.delimiter + +(payload "|" @punctuation.bracket) + +; Comments + +(comment) @comment @spell + +((comment) @comment.documentation + (#lua-match? @comment.documentation "^//!")) diff --git a/crates/ui/src/highlighter/languages/zig/injections.scm b/crates/ui/src/highlighter/languages/zig/injections.scm new file mode 100644 index 00000000..2f0e58eb --- /dev/null +++ b/crates/ui/src/highlighter/languages/zig/injections.scm @@ -0,0 +1,2 @@ +((comment) @injection.content + (#set! injection.language "comment"))