Skip to content

Commit

Permalink
feat: Add new and improve existing highlight queries, update tests to…
Browse files Browse the repository at this point in the history
… match.
  • Loading branch information
reiniscirpons committed Aug 29, 2024
1 parent 6d441c7 commit c17ad64
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 68 deletions.
144 changes: 92 additions & 52 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
@@ -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))
Expand All @@ -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)
Expand All @@ -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
[
"+"
"-"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -126,6 +191,9 @@

"return" @keyword.return

(pragma) @keyword.directive

;Punctuation
[
","
";"
Expand All @@ -134,13 +202,6 @@
":"
] @punctuation.delimiter

(help_statement "?" @punctuation.special)

[
(help_topic)
(help_book)
] @string.special

[
"("
")"
Expand All @@ -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
20 changes: 17 additions & 3 deletions test/highlight/keywords.g
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -45,7 +59,7 @@ until x > 0;
# <- keyword.repeat

x := rec(1:=3, a:=2);
# ^ keyword
# ^ keyword.type

fun := function(x)
# ^ keyword.function
Expand Down
52 changes: 39 additions & 13 deletions test/highlight/mini.g
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand Down

0 comments on commit c17ad64

Please sign in to comment.