From ba921c9aef386ad895b0a6551968126ff90a288d Mon Sep 17 00:00:00 2001 From: Amaan Qureshi Date: Thu, 29 Aug 2024 22:37:17 -0400 Subject: [PATCH] feat!: switch upstream Zig parser The new parser is faster, does not lag while editing, correctly parses the entire Zig code base, and is much easier to write queries for. --- README.md | 2 +- lockfile.json | 2 +- lua/nvim-treesitter/parsers.lua | 4 +- queries/zig/folds.scm | 33 ++-- queries/zig/highlights.scm | 310 ++++++++++++++++++-------------- queries/zig/indents.scm | 15 +- queries/zig/injections.scm | 14 +- 7 files changed, 217 insertions(+), 163 deletions(-) diff --git a/README.md b/README.md index 721816dcc..bcce59334 100644 --- a/README.md +++ b/README.md @@ -464,7 +464,7 @@ We are looking for maintainers to add more parsers and to write query files for - [x] [yang](https://github.com/Hubro/tree-sitter-yang) (maintained by @Hubro) - [x] [yuck](https://github.com/Philipp-M/tree-sitter-yuck) (maintained by @Philipp-M, @amaanq) - [x] [zathurarc](https://github.com/Freed-Wu/tree-sitter-zathurarc) (maintained by @Freed-Wu) -- [x] [zig](https://github.com/maxxnino/tree-sitter-zig) (maintained by @maxxnino) +- [x] [zig](https://github.com/tree-sitter-grammars/tree-sitter-zig) (maintained by @amaanq) For related information on the supported languages, including related plugins, see [this wiki page](https://github.com/nvim-treesitter/nvim-treesitter/wiki/Supported-Languages-Information). diff --git a/lockfile.json b/lockfile.json index 4420a8132..33f87d99d 100644 --- a/lockfile.json +++ b/lockfile.json @@ -873,6 +873,6 @@ "revision": "0554b4a5d313244b7fc000cbb41c04afae4f4e31" }, "zig": { - "revision": "2bac4cc6c697d46a193905fef6d003bfa0bfabfd" + "revision": "21e2218e0ec7f4e3c0640d16bf8c67e6f0a61e18" } } diff --git a/lua/nvim-treesitter/parsers.lua b/lua/nvim-treesitter/parsers.lua index 67fdb418c..419f34db0 100644 --- a/lua/nvim-treesitter/parsers.lua +++ b/lua/nvim-treesitter/parsers.lua @@ -2508,10 +2508,10 @@ list.zathurarc = { list.zig = { install_info = { - url = "https://github.com/maxxnino/tree-sitter-zig", + url = "https://github.com/tree-sitter-grammars/tree-sitter-zig", files = { "src/parser.c" }, }, - maintainers = { "@maxxnino" }, + maintainers = { "@amaanq" }, } list.templ = { diff --git a/queries/zig/folds.scm b/queries/zig/folds.scm index 6953091be..6be5c62ba 100644 --- a/queries/zig/folds.scm +++ b/queries/zig/folds.scm @@ -1,14 +1,23 @@ [ - (Block) - (ContainerDecl) - (SwitchExpr) - (InitList) - (AsmExpr) - (ErrorSetDecl) - (LINESTRING) - [ - (IfPrefix) - (WhilePrefix) - (ForPrefix) - ] + (block) + (switch_expression) + (initializer_list) + (asm_expression) + (multiline_string) + (if_statement) + (while_statement) + (for_statement) + (if_expression) + (else_clause) + (for_expression) + (while_expression) + (if_type_expression) + (function_signature) + (parameters) + (call_expression) + (struct_declaration) + (opaque_declaration) + (enum_declaration) + (union_declaration) + (error_set_declaration) ] @fold diff --git a/queries/zig/highlights.scm b/queries/zig/highlights.scm index d0f6e5b79..1f9006781 100644 --- a/queries/zig/highlights.scm +++ b/queries/zig/highlights.scm @@ -1,104 +1,116 @@ -(line_comment) @comment @spell +; Variables +(identifier) @variable + +; Parameters +(parameter + name: (identifier) @variable.parameter) + +(payload + (identifier) @variable.parameter) + +; Types +(parameter + type: (identifier) @type) + +((identifier) @type + (#lua-match? @type "^[A-Z_][a-zA-Z0-9_]*")) + +(variable_declaration + (identifier) @type + "=" + [ + (struct_declaration) + (enum_declaration) + (union_declaration) + (opaque_declaration) + ]) [ - (container_doc_comment) - (doc_comment) -] @comment.documentation @spell + (builtin_type) + "anyframe" +] @type.builtin + +; Constants +((identifier) @constant + (#lua-match? @constant "^[A-Z][A-Z_0-9]+$")) [ - variable: (IDENTIFIER) - variable_type_function: (IDENTIFIER) -] @variable + "null" + "unreachable" + "undefined" +] @constant.builtin -parameter: (IDENTIFIER) @variable.parameter +(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 [ - field_member: (IDENTIFIER) - field_access: (IDENTIFIER) -] @variable.member + "c" + "..." +] @variable.builtin -; assume TitleCase is a type -([ - variable_type_function: (IDENTIFIER) - field_access: (IDENTIFIER) - parameter: (IDENTIFIER) -] @type - (#lua-match? @type "^%u([%l]+[%u%l%d]*)*$")) - -; assume camelCase is a function -([ - variable_type_function: (IDENTIFIER) - field_access: (IDENTIFIER) - parameter: (IDENTIFIER) -] @function - (#lua-match? @function "^%l+([%u][%l%d]*)+$")) - -; assume all CAPS_1 is a constant -([ - variable_type_function: (IDENTIFIER) - field_access: (IDENTIFIER) -] @constant - (#lua-match? @constant "^%u[%u%d_]+$")) - -function: (IDENTIFIER) @function - -function_call: (IDENTIFIER) @function.call - -exception: "!" @keyword.exception - -((IDENTIFIER) @variable.builtin +((identifier) @variable.builtin (#eq? @variable.builtin "_")) -(PtrTypeStart - "c" @variable.builtin) - -(ContainerDecl - (ContainerDeclType - "enum") - (ContainerField - (ErrorUnionExpr - (SuffixExpr - (IDENTIFIER) @constant)))) - -field_constant: (IDENTIFIER) @constant - -(BUILTINIDENTIFIER) @function.builtin - -((BUILTINIDENTIFIER) @keyword.import - (#any-of? @keyword.import "@import" "@cImport")) - -(INTEGER) @number - -(FLOAT) @number.float - -[ - "true" - "false" -] @boolean - -[ - (LINESTRING) - (STRINGLITERALSINGLE) -] @string @spell - -(CHAR_LITERAL) @character - -(EscapeSequence) @string.escape - -(FormatSequence) @string.special - -(BreakLabel - (IDENTIFIER) @label) - -(BlockLabel - (IDENTIFIER) @label) +(calling_convention + (identifier) @variable.builtin) +; Keywords [ "asm" "defer" "errdefer" "test" - "opaque" "error" "const" "var" @@ -108,6 +120,7 @@ field_constant: (IDENTIFIER) @constant "struct" "union" "enum" + "opaque" ] @keyword.type [ @@ -151,11 +164,6 @@ field_constant: (IDENTIFIER) @constant "catch" ] @keyword.exception -[ - "anytype" - (BuildinTypeExpr) -] @type.builtin - [ "volatile" "allowzero" @@ -168,50 +176,85 @@ field_constant: (IDENTIFIER) @constant "inline" "noinline" "extern" -] @keyword.modifier - -[ "comptime" "packed" "threadlocal" -] @attribute +] @keyword.modifier +; Operator [ - "null" - "unreachable" - "undefined" -] @constant.builtin - -[ - (CompareOp) - (BitwiseOp) - (BitShiftOp) - (AdditionOp) - (AssignOp) - (MultiplyOp) - (PrefixOp) "=" + "*=" + "*%=" + "*|=" + "/=" + "%=" + "+=" + "+%=" + "+|=" + "-=" + "-%=" + "-|=" + "<<=" + "<<|=" + ">>=" + "&=" + "^=" + "|=" + "!" + "~" + "-" + "-%" + "&" + "==" + "!=" + ">" + ">=" + "<=" + "<" + "&" + "^" + "|" + "<<" + ">>" + "<<|" + "+" + "++" + "+%" + "-%" + "+|" + "-|" "*" + "/" + "%" "**" - "->" - ".?" + "*%" + "*|" + "||" ".*" + ".?" "?" + ".." ] @operator -[ - ";" - "." - "," - ":" - "=>" -] @punctuation.delimiter +; Literals +(character) @character -[ - ".." - "..." -] @punctuation.special +([ + (string) + (multiline_string) +] @string + (#set! "priority" 95)) +(integer) @number + +(float) @number.float + +(boolean) @boolean + +(escape_sequence) @string.escape + +; Punctuation [ "[" "]" @@ -221,19 +264,20 @@ field_constant: (IDENTIFIER) @constant "}" ] @punctuation.bracket -(Payload +[ + ";" + "." + "," + ":" + "=>" + "->" +] @punctuation.delimiter + +(payload "|" @punctuation.bracket) -(PtrPayload - "|" @punctuation.bracket) +; Comments +(comment) @comment @spell -(PtrIndexPayload - "|" @punctuation.bracket) - -(PtrListPayload - "|" @punctuation.bracket) - -(ParamType - (ErrorUnionExpr - (SuffixExpr - variable_type_function: (IDENTIFIER) @type))) +((comment) @comment.documentation + (#lua-match? @comment.documentation "^//!")) diff --git a/queries/zig/indents.scm b/queries/zig/indents.scm index bf9d3e42a..431aed5b6 100644 --- a/queries/zig/indents.scm +++ b/queries/zig/indents.scm @@ -1,11 +1,10 @@ [ - (Block) - (ContainerDecl) - (SwitchExpr) - (InitList) + (block) + (switch_expression) + (initializer_list) ] @indent.begin -(Block +(block "}" @indent.end) [ @@ -18,8 +17,6 @@ ] @indent.branch [ - (line_comment) - (container_doc_comment) - (doc_comment) - (LINESTRING) + (comment) + (multiline_string) ] @indent.ignore diff --git a/queries/zig/injections.scm b/queries/zig/injections.scm index 9e687ca76..48a1b44c9 100644 --- a/queries/zig/injections.scm +++ b/queries/zig/injections.scm @@ -1,6 +1,10 @@ -([ - (container_doc_comment) - (doc_comment) - (line_comment) -] @injection.content +((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"))