Skip to content

Highlight fixes #241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 38 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
419bd37
Highlight variants as constructors
Emilios1995 Mar 21, 2024
1ad1493
Add set of built-in types
Emilios1995 Mar 21, 2024
32944ca
Update @namespace to @module
Emilios1995 Mar 21, 2024
3e82ffc
Update @parameter to @variable.parameter
Emilios1995 Mar 21, 2024
4fb7aeb
Update string interpolation and char
Emilios1995 Mar 21, 2024
a72871e
Update parameter to @variable.parameter
Emilios1995 Mar 21, 2024
ef8fa56
Update annotation to attribute
Emilios1995 Mar 21, 2024
b5a655d
Complete change of polyvar as constructors
Emilios1995 Mar 21, 2024
53eba40
Specialize some of the keywords
Emilios1995 Mar 21, 2024
37dcedd
Move the => arrow to operators
Emilios1995 Mar 21, 2024
07dcf9b
Specialize ternary operator
Emilios1995 Mar 21, 2024
88d6a90
Highlight function name in bindings
Emilios1995 Mar 21, 2024
b3ba626
Highlight function calls
Emilios1995 Mar 21, 2024
5479086
Highlight members of records and modules
Emilios1995 Mar 21, 2024
20f0b4a
Highlight unit as built-in constant
Emilios1995 Mar 24, 2024
bfa3fae
Highlight labeled parameters names as properties
Emilios1995 Mar 24, 2024
6680403
Fix existing tests
Emilios1995 Mar 24, 2024
08e1ecd
Fix pipe operator call highlighting to work with simple values too
Emilios1995 Mar 24, 2024
9a7d2cc
Add highlight tests for function names and calls
Emilios1995 Mar 24, 2024
0c5f544
Add tests for module and record members
Emilios1995 Mar 24, 2024
d5bd57f
Change record field expression to member
Emilios1995 Mar 24, 2024
83f7209
Add test for labeled argument as property
Emilios1995 Mar 24, 2024
777ddf5
Add test for unit highlight
Emilios1995 Mar 25, 2024
dc8abb8
Adapt to new tree-sitter last match wins
Emilios1995 Mar 25, 2024
8196309
Move template highlight rule
Emilios1995 Mar 25, 2024
ac21b00
Add string interpolation test
Emilios1995 May 26, 2024
bf62a2d
Remove member highlight as property
Emilios1995 May 26, 2024
85a4f41
Change highlight of variable from module
Emilios1995 May 26, 2024
3a20909
Change highlight of destructured variables
Emilios1995 May 26, 2024
d5a3946
Change highlight of labeled arguments
Emilios1995 May 27, 2024
1fe1bbd
Fix highlight of variables in string interpolation
Emilios1995 May 27, 2024
996bde6
Fix parameter highlights
Emilios1995 May 27, 2024
74f3f3a
Move "as" to keyword.operator
Emilios1995 May 27, 2024
7473a29
Move async/await to coroutine
Emilios1995 May 27, 2024
736c0bb
Fix test
Emilios1995 May 27, 2024
bc3b707
Add missing punctutation highlights
Emilios1995 May 27, 2024
f81c39d
Remove metadata from unit highlight
Emilios1995 Jun 24, 2024
e4bed23
(true) and (false) as boolean
aspeddro Jul 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 104 additions & 40 deletions queries/rescript/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,54 @@
((value_identifier) @constant.macro
(#match? @constant.macro "^\\.*$"))


((value_identifier) @variable)

[
(type_identifier)
(unit_type)
(list)
(list_pattern)
] @type

((type_identifier) @type.builtin
(#any-of? @type.builtin
"int" "char" "string" "float" "bool" "unit"))

[
(variant_identifier)
(polyvar_identifier)
] @constant
] @constructor

(record_type_field (property_identifier) @property)
(record_field (property_identifier) @property)
(object (field (property_identifier) @property))
(object_type (field (property_identifier) @property))
(member_expression (property_identifier) @property)
(module_identifier) @namespace
(module_identifier) @module

(member_expression (property_identifier) @variable.member)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was @property and was changed to @variable.member. I looked at other languages and this is how it's usually handled: @property is for places such as record definition when both the property and value are explicitly written, and member is for extracting a value out of that property. (which is why it belongs to the variable group.)


(value_identifier_path
(module_identifier)
(value_identifier) @variable)


(record_pattern
(value_identifier_path
(value_identifier) @variable.member))

(record_pattern
(value_identifier) @variable)

(labeled_argument
label: (value_identifier) @variable.parameter)


; Parameters
;----------------

(list_pattern (value_identifier) @parameter)
(spread_pattern (value_identifier) @parameter)
(list_pattern (value_identifier) @variable.parameter)
(spread_pattern (value_identifier) @variable.parameter)

; String literals
;----------------
Expand All @@ -40,11 +64,8 @@
(template_string)
] @string

(template_substitution
"${" @punctuation.bracket
"}" @punctuation.bracket) @embedded

(character) @string.special
(character) @character
(escape_sequence) @string.escape

; Other literals
Expand All @@ -53,68 +74,102 @@
[
(true)
(false)
] @constant.builtin
] @boolean

(number) @number
(polyvar) @constant
(polyvar_string) @constant
(polyvar) @constructor
(polyvar_string) @constructor

; Functions
;----------

; parameter(s) in parens
[
(parameter (value_identifier))
(labeled_parameter (value_identifier))
] @parameter

(parameter (value_identifier) @variable.parameter)
(labeled_parameter (value_identifier) @variable.parameter)

; single parameter with no parens
(function parameter: (value_identifier) @parameter)
(function parameter: (value_identifier) @variable.parameter)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and other similar changes conform to to Neovim's new tags with fallbacks. Meaning, if a certain theme specifies a color for @variable.parameter, it will be used, but otherwise it will fall back to the color for @variable.


; first-level descructuring (required for nvim-tree-sitter as it only matches direct
; children and the above patterns do not match destructuring patterns in NeoVim)
(parameter (tuple_pattern (tuple_item_pattern (value_identifier) @parameter)))
(parameter (array_pattern (value_identifier) @parameter))
(parameter (record_pattern (value_identifier) @parameter))
(parameter (tuple_pattern (tuple_item_pattern (value_identifier) @variable.parameter)))
(parameter (array_pattern (value_identifier) @variable.parameter))
(parameter (record_pattern (value_identifier) @variable.parameter))

; function identifier in let binding
(let_binding
pattern: (value_identifier) @function
body: (function))

; function calls

(call_expression
function: (value_identifier_path
_
(value_identifier) @function.call))

(call_expression
function: (value_identifier) @function.call)

; highlight the right-hand side of a pipe operator as a function call
(pipe_expression
_
[(value_identifier_path
_
(value_identifier) @function.call)
(value_identifier) @function.call])


; Meta
;-----

(decorator_identifier) @annotation
(decorator_identifier) @attribute

(extension_identifier) @keyword
("%") @keyword

; Misc
;-----

(subscript_expression index: (string) @property)
(polyvar_type_pattern "#" @constant)
(polyvar_type_pattern "#" @constructor)

[
("include")
("open")
] @include
"include"
"open"
] @keyword.import


[
"private"
"mutable"
"rec"
] @keyword.modifier

[
"type"
] @keyword.type

[
"and"
"with"
"as"
] @keyword.operator

[
"export"
"external"
"let"
"module"
"mutable"
"private"
"rec"
"type"
"and"
"assert"
"await"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind moving this one to @keyword.coroutine?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I moved both async and await

"with"
"lazy"
"constraint"
] @keyword

((function "async" @keyword))
(("await") @keyword.coroutine)

((function "async" @keyword.coroutine))

(module_unpack "unpack" @keyword)

Expand All @@ -123,30 +178,31 @@
"else"
"switch"
"when"
] @conditional
] @keyword.conditional

[
"exception"
"try"
"catch"
] @exception
] @keyword.exception

(call_expression
function: (value_identifier) @exception
(#eq? @exception "raise"))
function: (value_identifier) @keyword.exception
(#eq? @keyword.exception "raise"))

[
"for"
"in"
"to"
"downto"
"while"
] @repeat
] @keyword.repeat

[
"."
","
"|"
":"
] @punctuation.delimiter

[
Expand Down Expand Up @@ -174,6 +230,7 @@
"|>"
":>"
"+="
"=>"
(uncurry)
] @operator

Expand All @@ -188,8 +245,16 @@
"}"
"["
"]"
"<"
">"
] @punctuation.bracket

(unit ["(" ")"] @constant.builtin)

(template_substitution
"${" @punctuation.special
"}" @punctuation.special) @none

(polyvar_type
[
"["
Expand All @@ -201,12 +266,11 @@
[
"~"
"?"
"=>"
".."
"..."
] @punctuation.special

(ternary_expression ["?" ":"] @operator)
(ternary_expression ["?" ":"] @keyword.conditional.ternary)

; JSX
;----------
Expand Down
4 changes: 2 additions & 2 deletions test/highlight/decorators.res
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@name
//<- annotation
//<- attribute

@@name
//<- annotation
//<- attribute
45 changes: 34 additions & 11 deletions test/highlight/expressions.res
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,42 @@ foo->bar == +x +. 1.0
// ^ property

switch foo {
// <- conditional
// <- keyword.conditional
| list{1, x, ...rest} =>
//^ type
// ^ number
// ^ parameter
// ^ variable.parameter
// ^ punctuation.special
// ^ parameter
// ^ punctuation.special
// ^ variable.parameter
// ^ operator
42
| list{1, 2, ...list{b, ..._} as rest} => rest
// ^ parameter
// ^ variable.parameter
// ^ variable
| exception Js.Exn.Error(_) => 99
//^ exception
//^ keyword.exception
}

switch bar {
| #...Mod.t => 33
//^ constant
//^ constructor
}

{ foo, bar: baz, qux: 1 }
//^ property
// ^ property

exception InputClosed(string)
//<- exception
//<- keyword.exception

raise(InputClosed("The stream has closed!"))
//<- exception
//<- keyword.exception

try {
//<- exception
//<- keyword.exception
someOtherJSFunctionThatThrows()
} catch {
// ^ exception
// ^ keyword.exception
| Not_found => 1 // catch a ReScript exception
| Invalid_argument(_) => 2 // catch a second ReScript exception
| Js.Exn.Error(obj) => 3 // catch the JS exception
Expand All @@ -55,3 +55,26 @@ let c = list{a, ...list{b}}
// ^ type
// ^ variable
// ^ variable

let x = fn()
// ^ function.call

let y = x->M.f->f
// ^function.call
// ^function.call

let v = M.v
// ^variable

let {x} = y
// ^variable

let {X.x} = y
// ^variable.member

let x = y.x
// ^variable.member

f(~a=b, ())
// ^variable.parameter
// ^constant.builtin
17 changes: 7 additions & 10 deletions test/highlight/functions.res
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
let inc = n => n + 1
// ^ parameter
// ^ variable.parameter
// ^ punctuation.special
// ^ function

let fn = (a, (b, c), {d, e}, [f, g]) => a + b + c + d + e + f + g
// ^ parameter
// ^ parameter
// ^ parameter
// ^ parameter

let uncurry = (. u, .x) => (u, x)
// ^ operator
// ^ operator
// ^ variable.parameter
// ^ variable.parameter
// ^ variable.parameter
// ^ variable.parameter

let get = async (id) => id
// ^ keyword
// ^ keyword.coroutine
Loading
Loading