zig: Switch to official Zig grammar (#20004)

xdBronch created

Closes #20001

the old outline was *weird* for many reasons so ill just show it with a
hodgepodge of zig declarations
before:

![image](https://github.com/user-attachments/assets/87395623-3e28-491c-9693-c1714da6b29f)
after:

![image](https://github.com/user-attachments/assets/1aa3f3b7-18b0-4d39-b1c6-99740e7bcd6a)

why were values shown? why werent `var`s or modifiers like pub, const,
export? it was very odd to me and inconsistent with other languages. i
chose to leave out unnamed tests, it just seemed like noise to me since
they werent distinct but i can easily revert that
unfortunately there seems to be a bug upstream which causes those
`t`/`f` decls to show 2 things
https://github.com/tree-sitter-grammars/tree-sitter-zig/issues/3

im very new to treesitter and queries so i really havent looked over the
rest of the stuff here, other than outline theyre unmodified please lmk
if theres anything wrong

Release Notes:

- Changed upstream treesitter grammar for zig

Change summary

extensions/zig/extension.toml               |   4 
extensions/zig/languages/zig/config.toml    |   2 
extensions/zig/languages/zig/highlights.scm | 365 ++++++++++++++--------
extensions/zig/languages/zig/indents.scm    |  21 
extensions/zig/languages/zig/injections.scm |  15 
extensions/zig/languages/zig/outline.scm    |  73 +++-
6 files changed, 294 insertions(+), 186 deletions(-)

Detailed changes

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"

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 },

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 "^//(/|!)"))

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

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

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