From db10520755ec2a95eee0ce716e2307fca106a738 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Thu, 13 Mar 2025 21:36:13 +0000 Subject: [PATCH] Align queries with 'nvim-treesitter' I fear the upstream queries may be broken, somehow, as they don't work with `--apply-all-captures` which should match the "last query wins" matching behaviour of `nvim-treesitter`. I also removed `locals.scm`, as `tree-sitter` and `nvim-treesitter` don't agree on how to write it, and I don't really care about it. It does mean that the highlights can't tell the difference between `int` the built-in and `int` the type alias anymore, but that's a small edge-case (and `nvim-treesitter` didn't support it anyway). --- queries/folds.scm | 2 - queries/highlights.scm | 91 +++++++++++---------- queries/indents.scm | 127 ++++++++++++++++++----------- queries/injections.scm | 3 +- queries/locals.scm | 35 -------- test/highlight/built-ins.tig | 6 +- test/highlight/functions.tig | 6 +- test/highlight/identifiers.tig | 12 +-- test/highlight/imports.tig | 2 +- test/highlight/keywords.tig | 2 +- test/highlight/object-oriented.tig | 12 +-- 11 files changed, 149 insertions(+), 149 deletions(-) delete mode 100644 queries/locals.scm diff --git a/queries/folds.scm b/queries/folds.scm index 24332a0..3b076bc 100644 --- a/queries/folds.scm +++ b/queries/folds.scm @@ -8,9 +8,7 @@ (let_expression) (function_declaration) (primitive_declaration) - (record_type) - (class_declaration) (class_type) (method_declaration) diff --git a/queries/highlights.scm b/queries/highlights.scm index 6299d5b..a256d0e 100644 --- a/queries/highlights.scm +++ b/queries/highlights.scm @@ -1,23 +1,22 @@ ; Built-ins {{{ ((function_call function: (identifier) @function.builtin) - (#match? @function.builtin "^(chr|concat|exit|flush|getchar|not|ord|print|print_err|print_int|size|strcmp|streq|substring)$") - (#is-not? local)) + (#any-of? @function.builtin + "chr" "concat" "exit" "flush" "getchar" "not" "ord" "print" "print_err" "print_int" "size" + "strcmp" "streq" "substring")) ((type_identifier) @type.builtin - (#match? @type.builtin "^(int|string|Object)$") - (#is-not? local)) + (#any-of? @type.builtin "int" "string" "Object")) ((identifier) @variable.builtin - (#match? @variable.builtin "^self$") - (#is-not? local)) -; }}} + (#eq? @variable.builtin "self")) +; }}} ; Keywords {{{ [ - "function" - "method" - "primitive" + "function" + "primitive" + "method" ] @keyword.function [ @@ -27,14 +26,9 @@ "while" ] @keyword.repeat -[ - "new" -] @keyword.constructor +"new" @keyword.operator - -[ - "import" -] @include +"import" @keyword.import [ "array" @@ -48,73 +42,80 @@ "then" "type" "var" - - "class" "extends" - "_cast" "_chunks" "_exp" "_lvalue" "_namety" ] @keyword -; }}} +"class" @keyword.type + +; }}} ; Operators {{{ (operator) @operator [ - "," - ";" - ":" - "." + "," + ";" + ":" + "." ] @punctuation.delimiter [ - "(" - ")" - "[" - "]" - "{" - "}" + "(" + ")" + "[" + "]" + "{" + "}" ] @punctuation.bracket -; }}} +; }}} ; Functions and methods {{{ (function_call function: (identifier) @function) + (function_declaration name: (identifier) @function) + (primitive_declaration name: (identifier) @function) (method_call - method: (identifier) @method) + method: (identifier) @function.method) + (method_declaration - name: (identifier) @method) + name: (identifier) @function.method) (parameters - name: (identifier) @parameter) -; }}} + name: (identifier) @variable.parameter) +; }}} ; Declarations {{{ (import_declaration file: (string_literal) @string.special.path) -; }}} +; }}} ; Literals {{{ (nil_literal) @constant.builtin -(integer_literal) @number -(string_literal) @string -(escape_sequence) @string.escape -; }}} +(integer_literal) @number + +(string_literal) @string + +(escape_sequence) @string.escape + +; }}} ; Misc {{{ -(comment) @comment +(comment) @comment ; @spell (type_identifier) @type -(field_identifier) @property -(identifier) @variable -; }}} +(field_identifier) @variable.member + +(identifier) @variable + +; }}} ; vim: sw=2 foldmethod=marker diff --git a/queries/indents.scm b/queries/indents.scm index 4ac8d24..686bfde 100644 --- a/queries/indents.scm +++ b/queries/indents.scm @@ -1,69 +1,104 @@ ; Control flow {{{ -(if_expression) @indent -"then" @branch -"else" @branch +(if_expression) @indent.begin -(while_expression) @indent -"do" @branch +"then" @indent.branch + +"else" @indent.branch + +(while_expression) @indent.begin + +"do" @indent.branch + +(for_expression) @indent.begin + +"to" @indent.branch -(for_expression) @indent -"to" @branch ; }}} - ; Class {{{ -(class_declaration) @indent -(class_declaration "}" @indent_end) +(class_declaration) @indent.begin + +(class_declaration + "}" @indent.end) + +(class_type) @indent.begin + +(class_type + "}" @indent.end) -(class_type) @indent -(class_type "}" @indent_end) ; }}} - ; Groups {{{ -(let_expression) @indent -"in" @branch -"end" @branch -(let_expression "end" @indent_end) +(let_expression) @indent.begin + +"in" @indent.branch + +"end" @indent.branch + +(let_expression + "end" @indent.end) + +(sequence_expression) @indent.begin + +")" @indent.branch + +(sequence_expression + ")" @indent.end) -(sequence_expression) @indent -")" @branch -(sequence_expression ")" @indent_end) ; }}} - ; Functions and methods {{{ -(parameters) @indent -(parameters ")" @indent_end) +(parameters) @indent.begin -(function_call) @indent -(method_call) @indent -")" @branch +(parameters + ")" @indent.end) + +(function_call) @indent.begin + +(function_call + ")" @indent.end) + +(method_call) @indent.begin + +")" @indent.branch + +(function_declaration) @indent.begin + +(primitive_declaration) @indent.begin + +(method_declaration) @indent.begin -(function_declaration) @indent -(primitive_declaration) @indent -(method_declaration) @indent ; }}} - ; Values and expressions {{{ -(array_value) @indent -"]" @branch -(array_value "]" @indent_end) +(array_value) @indent.begin -(array_expression) @indent -"of" @branch +"]" @indent.branch -(record_expression) @indent -"}" @branch -(record_expression "}" @indent_end) +(array_value + "]" @indent.end) -(record_type) @indent -"}" @branch -(record_type "}" @indent_end) +(array_expression) @indent.begin + +"of" @indent.branch + +(record_expression) @indent.begin + +"}" @indent.branch + +(record_expression + "}" @indent.end) + +(record_type) @indent.begin + +"}" @indent.branch + +(record_type + "}" @indent.end) + +(variable_declaration) @indent.begin -(variable_declaration) @indent ; }}} - ; Misc{{{ -(comment) @ignore -(string_literal) @ignore -; }}} +(comment) @indent.ignore +(string_literal) @indent.ignore + +; }}} ; vim: sw=2 foldmethod=marker diff --git a/queries/injections.scm b/queries/injections.scm index 2135031..9735c59 100644 --- a/queries/injections.scm +++ b/queries/injections.scm @@ -1,3 +1,4 @@ -(comment) @comment +((comment) @injection.content + (#set! injection.language "comment")) ; vim: sw=2 foldmethod=marker diff --git a/queries/locals.scm b/queries/locals.scm deleted file mode 100644 index 70baf87..0000000 --- a/queries/locals.scm +++ /dev/null @@ -1,35 +0,0 @@ -; See this issue [1] for support for "lazy scoping" which is somewhat needed -; for Tiger semantics (e.g: one can call a function before it has been defined -; top-to-bottom). -; -; [1]: https://github.com/tree-sitter/tree-sitter/issues/918 - -; Scopes {{{ -[ - (for_expression) - (let_expression) - (function_declaration) -] @local.scope -; }}} - -; Definitions {{{ -(type_declaration - name: (identifier) @local.definition) - -(parameters - name: (identifier) @local.definition) - -(function_declaration - name: (identifier) @local.definition) -(primitive_declaration - name: (identifier) @local.definition) - -(variable_declaration - name: (identifier) @local.definition) -; }}} - -; References {{{ -(identifier) @local.reference -; }}} - -; vim: sw=2 foldmethod=marker diff --git a/test/highlight/built-ins.tig b/test/highlight/built-ins.tig index 6879054..f359959 100644 --- a/test/highlight/built-ins.tig +++ b/test/highlight/built-ins.tig @@ -6,7 +6,7 @@ let /* ^ type.builtin */ var b := exit(0) - /* ^ function */ + /* ^ function.builtin */ type int = string /* Shadowing the built-in type */ /* ^ type.builtin */ @@ -30,13 +30,13 @@ in end; exit(1); - /* <- function */ + /* <- function.builtin */ print("shadowing is fun"); /* <- function.builtin */ self; - /* <- variable */ + /* <- variable.builtin */ b := print /* ^ variable */ diff --git a/test/highlight/functions.tig b/test/highlight/functions.tig index 1b210dd..635dff9 100644 --- a/test/highlight/functions.tig +++ b/test/highlight/functions.tig @@ -1,8 +1,8 @@ primitive print(s: string) /* ^ function */ -/* ^ parameter */ +/* ^ variable.parameter */ function func(a: int) : int = (print("Hello World!"); a) /* ^ function */ -/* ^ parameter */ -/* ^ function */ +/* ^ variable.parameter */ +/* ^ function.builtin */ diff --git a/test/highlight/identifiers.tig b/test/highlight/identifiers.tig index 2d52edd..d583320 100644 --- a/test/highlight/identifiers.tig +++ b/test/highlight/identifiers.tig @@ -6,24 +6,24 @@ type int_array = array of int /* ^ type.builtin */ type record = {a: int, b: string} -/* ^ property */ +/* ^ variable.member */ /* ^ type.builtin */ -/* ^ property */ +/* ^ variable.member */ /* ^ type.builtin */ var record := record {a = 12, b = "27"} /* ^ variable */ /* ^ type */ -/* ^ property */ -/* ^ property */ +/* ^ variable.member */ +/* ^ variable.member */ var array := int_array[12] of 27; /* ^ variable */ /* ^ type */ primitive func(a: int, b: string) : array -/* ^ parameter */ +/* ^ variable.parameter */ /* ^ type.builtin */ -/* ^ parameter */ +/* ^ variable.parameter */ /* ^ type.builtin */ /* ^ type */ diff --git a/test/highlight/imports.tig b/test/highlight/imports.tig index 6addf12..44a2c2b 100644 --- a/test/highlight/imports.tig +++ b/test/highlight/imports.tig @@ -1,3 +1,3 @@ import "lib.tih" -/* <- include */ +/* <- keyword.import */ /* ^ string.special.path */ diff --git a/test/highlight/keywords.tig b/test/highlight/keywords.tig index 6704c42..5a90a33 100644 --- a/test/highlight/keywords.tig +++ b/test/highlight/keywords.tig @@ -10,7 +10,7 @@ let /* <- keyword.function */ import "lib.tih" - /* <- include */ + /* <- keyword.import */ type array_of_int = array of int /* <- keyword */ diff --git a/test/highlight/object-oriented.tig b/test/highlight/object-oriented.tig index 2a71476..895e1b3 100644 --- a/test/highlight/object-oriented.tig +++ b/test/highlight/object-oriented.tig @@ -1,11 +1,11 @@ let class A extends Object {} - /* <- keyword */ + /* <- keyword.type */ /* ^ keyword */ /* ^ type.builtin */ type B = class extends A { - /* ^ keyword */ + /* ^ keyword.type */ /* ^ keyword */ /* ^ type */ @@ -13,16 +13,16 @@ let method meth() : int = self.a /* <- keyword.function */ - /* ^ method */ + /* ^ function.method */ /* ^ variable.builtin */ } var object := new B - /* ^ keyword.constructor */ + /* ^ keyword.operator */ in object.a := 27; - /* ^ property */ + /* ^ variable.member */ object.meth() - /* ^ method */ + /* ^ function.method */ end