From c17ad645e2352f2011f99885a477e101d1cd6f9d Mon Sep 17 00:00:00 2001 From: reiniscirpons Date: Sun, 18 Aug 2024 12:35:22 +0100 Subject: [PATCH] feat: Add new and improve existing highlight queries, update tests to match. --- queries/highlights.scm | 144 ++++++++++++++++++++++++-------------- test/highlight/keywords.g | 20 +++++- test/highlight/mini.g | 52 ++++++++++---- 3 files changed, 148 insertions(+), 68 deletions(-) diff --git a/queries/highlights.scm b/queries/highlights.scm index b5cff47..44aeb44 100644 --- a/queries/highlights.scm +++ b/queries/highlights.scm @@ -1,16 +1,14 @@ -; Constants - ; NOTE: (reiniscirpons) in case multiple queries match, last query wins. So ; queries should go from least specific to most specific. (This is the default ; behaviour since tree-sitter 0.22.2) (identifier) @variable +; Constants ; convention: constants are of the form ALL_CAPS_AND_UNDERSCORES and have length at least 2 ((identifier) @constant - (#match? @constant "^[A-Z_][A-Z_]+$")) + (#match? @constant "^[A-Z_][A-Z_]+$")) ; Functions - (assignment_statement left: (identifier) @function right: (function)) @@ -28,9 +26,7 @@ ((call function: (identifier) @function.builtin) - (#match? @function.builtin "^(Assert|Info|IsBound|Unbind|TryNextMethod)$")) - -; Function parameters + (#any-of? @function.builtin "Assert" "Info" "IsBound" "Unbind" "TryNextMethod")) (parameters (identifier) @variable.parameter) @@ -45,22 +41,84 @@ (lambda_parameters (identifier) @variable.parameter) +; arg is treated specially when it is the only parameter of a function +((parameters + . + (identifier) @variable.parameter.builtin .) + (#eq? @variable.parameter.builtin "arg")) + +((qualified_parameters + . + (identifier) @variable.parameter.builtin .) + (#eq? @variable.parameter.builtin "arg")) + +((qualified_parameters + . + (qualified_identifier + (identifier) @variable.parameter.builtin) .) + (#eq? @variable.parameter.builtin "arg")) + +((lambda_parameters + . + (identifier) @variable.parameter.builtin .) + (#eq? @variable.parameter.builtin "arg")) + (locals (identifier) @variable.parameter) ; Literals - (bool) @constant.builtin -(tilde) @variable.builtin + (integer) @number + (float) @number.float -(comment) @comment @spell + (string) @string + (char) @character + (escape_sequence) @string.escape -(pragma) @keyword.directive +[ + (help_topic) + (help_book) +] @string.special + +(tilde) @variable.builtin + +; Record selectors +(record_entry + left: [ + (identifier) + (integer) + ] @variable.member) + +(record_selector + selector: [ + (identifier) + (integer) + ] @variable.member) + +(component_selector + selector: [ + (identifier) + (integer) + ] @variable.member) + +(function_call_option + [ + (identifier) + (record_entry ;Record entries specify global properties in function calls + left: [ + (identifier) + (integer) + ]) + ] @property) +(help_statement + (help_selector) @property) + +; Operators [ "+" "-" @@ -82,17 +140,20 @@ (help_statement (help_operator) @operator) - +; Keywords [ - "atomic" (break_statement) (continue_statement) - "readonly" - "readwrite" - "rec" + "atomic" (quit_statement) ] @keyword +[ + "function" + "local" + "end" +] @keyword.function + [ "and" "in" @@ -101,11 +162,15 @@ "or" ] @keyword.operator +"rec" @keyword.type + [ - "function" - "local" - "end" -] @keyword.function + "readonly" + "readwrite" +] @keyword.modifier + +(atomic_function + "atomic" @keyword.modifier) [ "for" @@ -126,6 +191,9 @@ "return" @keyword.return +(pragma) @keyword.directive + +;Punctuation [ "," ";" @@ -134,13 +202,6 @@ ":" ] @punctuation.delimiter -(help_statement "?" @punctuation.special) - -[ - (help_topic) - (help_book) -] @string.special - [ "(" ")" @@ -151,29 +212,8 @@ "}" ] @punctuation.bracket -; Record selectors as properties - -(record_entry - left: [ - (identifier) - (integer) - ] @property) - - -(record_selector - selector: [ - (identifier) - (integer) - ] @property) - -(component_selector - selector: [ - (identifier) - (integer) - ] @property) - -(function_call_option - (identifier) @property) - (help_statement - (help_selector) @property) + "?" @punctuation.special) + +;Comments +(comment) @comment @spell diff --git a/test/highlight/keywords.g b/test/highlight/keywords.g index f694a8e..c10920b 100644 --- a/test/highlight/keywords.g +++ b/test/highlight/keywords.g @@ -13,13 +13,27 @@ fi; atomic readonly x, y, readwrite z do # <- keyword -# ^ keyword -# ^ keyword +# ^ keyword.modifier +# ^ keyword.modifier # ^ keyword.repeat x := 3; od; # <- keyword.repeat +atomic function(x) +# <- keyword.modifier +# ^ keyword.function +# ^ punctuation.bracket +# ^ variable.parameter +# ^ punctuation.bracket + return x; +# ^ keyword.return +# ^ variable.parameter +# ^ punctuation.delimiter +end; +# <- keyword.function +# ^ punctuation.delimiter + for x in [1..10] do # <- keyword.repeat # ^ keyword.repeat @@ -45,7 +59,7 @@ until x > 0; # <- keyword.repeat x := rec(1:=3, a:=2); -# ^ keyword +# ^ keyword.type fun := function(x) # ^ keyword.function diff --git a/test/highlight/mini.g b/test/highlight/mini.g index b2463f8..660a587 100644 --- a/test/highlight/mini.g +++ b/test/highlight/mini.g @@ -176,6 +176,32 @@ od; # <- keyword.repeat # ^ punctuation.delimiter +foo := function(x, y...) +# ^ variable.parameter +# ^ variable.parameter +# ^ operator + return x; +end; + +bar := function(arg) +# ^ variable.parameter.builtin + return arg; +end; + +baz := function(x, arg) +# ^ variable.parameter +# ^ variable.parameter + return arg; +end; + +foobar := atomic function(readonly arg) +# ^ variable.parameter.builtin + return arg; +end; + +foobaz := {arg} -> arg; +# ^ variable.parameter.builtin + BindGlobal("foo", 1); # <- function.call # ^ punctuation.bracket @@ -196,10 +222,10 @@ BIND_GLOBAL("bar", 2); r := rec( # <- variable # ^ operator -# ^ keyword +# ^ keyword.type # ^ punctuation.bracket a := [1.0,2.3e10,3], -# ^ property +# ^ variable.member # ^ operator # ^ punctuation.bracket # ^ number.float @@ -210,12 +236,12 @@ r := rec( # ^ punctuation.bracket # ^ punctuation.delimiter b := true, -# ^ property +# ^ variable.member # ^ operator # ^ constant.builtin # ^ punctuation.delimiter c := fail, -# ^ property +# ^ variable.member # ^ operator # ^ constant.builtin # ^ punctuation.delimiter @@ -225,25 +251,25 @@ r := rec( r.a; # <- variable #^ punctuation.delimiter -# ^ property +# ^ variable.member # ^ punctuation.delimiter p := rec(a:=r, b:=~.a, 1 := ~); # <- variable # ^ operator -# ^ keyword +# ^ keyword.type # ^ punctuation.bracket -# ^ property +# ^ variable.member # ^ operator # ^ variable # ^ punctuation.delimiter -# ^ property +# ^ variable.member # ^ operator # ^ variable.builtin # ^ punctuation.delimiter -# ^ property +# ^ variable.member # ^ punctuation.delimiter -# ^ property +# ^ variable.member # ^ operator # ^ variable.builtin # ^ punctuation.bracket @@ -252,11 +278,11 @@ p := rec(a:=r, b:=~.a, 1 := ~); p.1.a.c; # <- variable #^ punctuation.delimiter -# ^ property +# ^ variable.member # ^ punctuation.delimiter -# ^ property +# ^ variable.member # ^ punctuation.delimiter -# ^ property +# ^ variable.member # ^ punctuation.delimiter InstallImmediateMethod( IsFinitelyGeneratedGroup,