diff --git a/extensions/zig/extension.toml b/extensions/zig/extension.toml index 16210c5ba2441a8383a9c496e369e0c319439809..bcd4f5855530662d1752e64ef4d753e63c7d7c8e 100644 --- a/extensions/zig/extension.toml +++ b/extensions/zig/extension.toml @@ -11,5 +11,5 @@ name = "zls" language = "Zig" [grammars.zig] -repository = "https://github.com/maxxnino/tree-sitter-zig" -commit = "0d08703e4c3f426ec61695d7617415fff97029bd" +repository = "https://github.com/tree-sitter-grammars/tree-sitter-zig" +commit = "eb7d58c2dc4fbeea4745019dee8df013034ae66b" diff --git a/extensions/zig/languages/zig/config.toml b/extensions/zig/languages/zig/config.toml index 1ce1b215a01c474137c876b70c93c6c176d4fae6..838b4f2116aca5be902908b662f03e289bad1e54 100644 --- a/extensions/zig/languages/zig/config.toml +++ b/extensions/zig/languages/zig/config.toml @@ -1,7 +1,7 @@ name = "Zig" grammar = "zig" path_suffixes = ["zig", "zon"] -line_comments = ["// "] +line_comments = ["// ", "/// ", "//! "] autoclose_before = ";:.,=}])" brackets = [ { start = "{", end = "}", close = true, newline = true }, diff --git a/extensions/zig/languages/zig/highlights.scm b/extensions/zig/languages/zig/highlights.scm index aea2d34addb822b2578bfcf6134136baf25c000f..20c256eb6342839a007993f0a39f3f7ba69743d7 100644 --- a/extensions/zig/languages/zig/highlights.scm +++ b/extensions/zig/languages/zig/highlights.scm @@ -1,198 +1,271 @@ -[ - (container_doc_comment) - (doc_comment) +; Variables -] @comment.doc +(identifier) @variable -[ - (line_comment) -] @comment +; Parameters -[ - variable: (IDENTIFIER) - variable_type_function: (IDENTIFIER) -] @variable +(parameter + name: (identifier) @variable.parameter) -;; func parameter -parameter: (IDENTIFIER) @property +; Types -[ - field_member: (IDENTIFIER) - field_access: (IDENTIFIER) -] @property +(parameter + type: (identifier) @type) -;; 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 -( +((identifier) @type + (#match? @type "^[A-Z_][a-zA-Z0-9_]*")) + +(variable_declaration + (identifier) @type + "=" [ - variable_type_function: (IDENTIFIER) - field_access: (IDENTIFIER) - ] @constant - (#match? @constant "^[A-Z][A-Z_0-9]+$") -) + (struct_declaration) + (enum_declaration) + (union_declaration) + (opaque_declaration) + ]) + +[ + (builtin_type) + "anyframe" +] @type.builtin + +; Constants + +((identifier) @constant + (#match? @constant "^[A-Z][A-Z_0-9]+$")) [ - function_call: (IDENTIFIER) - function: (IDENTIFIER) -] @function + "null" + "unreachable" + "undefined" +] @constant.builtin -exception: "!" @keyword +(field_expression + . + member: (identifier) @constant) -( - (IDENTIFIER) @variable.special - (#eq? @variable.special "_") -) +(enum_declaration + (container_field + type: (identifier) @constant)) -(PtrTypeStart "c" @variable.special) +; Labels -( - (ContainerDeclType - [ - (ErrorUnionExpr) - "enum" - ] - ) - (ContainerField (IDENTIFIER) @constant) -) +(block_label (identifier) @label) -field_constant: (IDENTIFIER) @constant +(break_label (identifier) @label) -(BUILTINIDENTIFIER) @keyword +; Fields -((BUILTINIDENTIFIER) @function - (#any-of? @function "@import" "@cImport")) +(field_initializer + . + (identifier) @variable.member) -(INTEGER) @number +(field_expression + (_) + member: (identifier) @property) -(FLOAT) @number +(field_expression + (_) + member: (identifier) @type (#match? @type "^[A-Z_][a-zA-Z0-9_]*")) -[ - "true" - "false" -] @boolean +(container_field + name: (identifier) @property) + +(initializer_list + (assignment_expression + left: (field_expression + . + member: (identifier) @property))) + +; 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 [ - (LINESTRING) - (STRINGLITERALSINGLE) -] @string + "c" + "..." +] @variable.builtin + +((identifier) @variable.builtin + (#eq? @variable.builtin "_")) -(CHAR_LITERAL) @string.special.symbol -(EscapeSequence) @string.escape -(FormatSequence) @string.special +(calling_convention + (identifier) @variable.builtin) -(BreakLabel (IDENTIFIER) @tag) -(BlockLabel (IDENTIFIER) @tag) +; Keywords [ - "fn" "asm" "defer" "errdefer" "test" + "error" + "const" + "var" +] @keyword + +[ "struct" "union" "enum" "opaque" - "error" - "try" - "catch" - "for" - "while" - "break" - "continue" - "const" - "var" - "volatile" - "allowzero" - "noalias" - "addrspace" - "align" - "callconv" - "linksection" - "comptime" - "export" - "extern" - "inline" - "noinline" - "packed" - "pub" - "threadlocal" +] @keyword.type + +[ "async" "await" "suspend" "nosuspend" "resume" +] @keyword.coroutine + +"fn" @keyword.function + +[ "and" "or" "orelse" - "return" +] @keyword.operator + +"return" @keyword.return + +[ "if" "else" "switch" -] @keyword +] @keyword.conditional + +[ + "for" + "while" + "break" + "continue" +] @keyword.repeat [ "usingnamespace" -] @constant + "export" +] @keyword.import [ - "anytype" - "anyframe" - (BuildinTypeExpr) -] @type + "try" + "catch" +] @keyword.exception [ - "null" - "unreachable" - "undefined" -] @constant + "volatile" + "allowzero" + "noalias" + "addrspace" + "align" + "callconv" + "linksection" + "pub" + "inline" + "noinline" + "extern" + "comptime" + "packed" + "threadlocal" +] @keyword.modifier + +; Operator [ - (CompareOp) - (BitwiseOp) - (BitShiftOp) - (AdditionOp) - (AssignOp) - (MultiplyOp) - (PrefixOp) + "=" + "*=" + "*%=" + "*|=" + "/=" + "%=" + "+=" + "+%=" + "+|=" + "-=" + "-%=" + "-|=" + "<<=" + "<<|=" + ">>=" + "&=" + "^=" + "|=" + "!" + "~" + "-" + "-%" + "&" + "==" + "!=" + ">" + ">=" + "<=" + "<" + "&" + "^" + "|" + "<<" + ">>" + "<<|" + "+" + "++" + "+%" + "-%" + "+|" + "-|" "*" + "/" + "%" "**" - "->" - ".?" + "*%" + "*|" + "||" ".*" + ".?" "?" + ".." ] @operator -[ - ";" - "." - "," - ":" -] @punctuation.delimiter +; Literals -[ - ".." - "..." -] @punctuation.special +(character) @character + +([ + (string) + (multiline_string) +] @string + (#set! "priority" 95)) + +(integer) @number + +(float) @number.float + +(boolean) @boolean + +(escape_sequence) @string.escape + +; Punctuation [ "[" @@ -201,10 +274,22 @@ field_constant: (IDENTIFIER) @constant ")" "{" "}" - (Payload "|") - (PtrPayload "|") - (PtrIndexPayload "|") ] @punctuation.bracket -; Error -(ERROR) @error +[ + ";" + "." + "," + ":" + "=>" + "->" +] @punctuation.delimiter + +(payload "|" @punctuation.bracket) + +; Comments + +(comment) @comment + +((comment) @comment.documentation + (#match? @comment.documentation "^//(/|!)")) diff --git a/extensions/zig/languages/zig/indents.scm b/extensions/zig/languages/zig/indents.scm index ca4940fc0967916b091b234a5f7baf22f204d45f..aba33b9ae71f6a1dba46f83084df2075cc279963 100644 --- a/extensions/zig/languages/zig/indents.scm +++ b/extensions/zig/languages/zig/indents.scm @@ -1,22 +1,17 @@ [ - (AsmExpr) - (AssignExpr) - (Block) - (BlockExpr) - (ContainerDecl) - (ErrorUnionExpr) - (InitList) - (SwitchExpr) - (TestDecl) + (block) + (switch_expression) + (initializer_list) ] @indent.begin +(block + "}" @indent.end) + (_ "[" "]" @end) @indent (_ "{" "}" @end) @indent (_ "(" ")" @end) @indent [ - (line_comment) - (container_doc_comment) - (doc_comment) - (LINESTRING) + (comment) + (multiline_string) ] @indent.ignore diff --git a/extensions/zig/languages/zig/injections.scm b/extensions/zig/languages/zig/injections.scm index e3ff406d36666f00d77e6348df7c711c19e80f8b..48a1b44c9bd758882b6f9d31485b1447877d3b61 100644 --- a/extensions/zig/languages/zig/injections.scm +++ b/extensions/zig/languages/zig/injections.scm @@ -1,5 +1,10 @@ -[ - (container_doc_comment) - (doc_comment) - (line_comment) -] @comment +((comment) @injection.content + (#set! injection.language "comment")) + +; TODO: add when asm is added +; (asm_output_item (string) @injection.content +; (#set! injection.language "asm")) +; (asm_input_item (string) @injection.content +; (#set! injection.language "asm")) +; (asm_clobbers (string) @injection.content +; (#set! injection.language "asm")) diff --git a/extensions/zig/languages/zig/outline.scm b/extensions/zig/languages/zig/outline.scm index 7ae683a876e2e7743614fed1f4767875a7d6036c..acb2cc6986df788414a7be81fc583474ecf411fa 100644 --- a/extensions/zig/languages/zig/outline.scm +++ b/extensions/zig/languages/zig/outline.scm @@ -1,27 +1,50 @@ -(Decl ( - FnProto( - "fn" @context - function: (_) @name - ) -) - ) @item +(test_declaration + "test" @context + [ + (string) + (identifier) + ] @name) @item -( - Decl ( - VarDecl ( - "const" - variable_type_function: (_) @name - (ErrorUnionExpr) @context - ) - ) -) @item +(function_declaration + "pub"? @context + [ + "extern" + "export" + "inline" + "noinline" + ]? @context + "fn" @context + name: (_) @name) @item -( - TestDecl ( - "test" @context - [ - (STRINGLITERALSINGLE) - (IDENTIFIER) - ]? @name - ) -) @item +(source_file + (variable_declaration + "pub"? @context + (identifier) @name + "=" (_) @context) @item) + +(struct_declaration + (variable_declaration + "pub"? @context + (identifier) @name + "=" (_) @context) @item) + +(union_declaration + (variable_declaration + "pub"? @context + (identifier) @name + "=" (_) @context) @item) + +(enum_declaration + (variable_declaration + "pub"? @context + (identifier) @name + "=" (_) @context) @item) + +(opaque_declaration + (variable_declaration + "pub"? @context + (identifier) @name + "=" (_) @context) @item) + +(container_field + . (_) @name) @item