Skip to content

Commit d77653c

Browse files
Highlight fixes (#241)
* Highlight variants as constructors * Add set of built-in types * Update @namespace to @module It's what's in the neovim docs right now * Update @parameter to @variable.parameter * Update string interpolation and char * Update parameter to @variable.parameter * Update annotation to attribute * Complete change of polyvar as constructors * Specialize some of the keywords * Move the => arrow to operators This is how it's in the ecma parser (JS) * Specialize ternary operator * Highlight function name in bindings * Highlight function calls * Highlight members of records and modules * Highlight unit as built-in constant * Highlight labeled parameters names as properties * Fix existing tests * Fix pipe operator call highlighting to work with simple values too * Add highlight tests for function names and calls * Add tests for module and record members * Change record field expression to member * Add test for labeled argument as property * Add test for unit highlight * Adapt to new tree-sitter last match wins * Move template highlight rule * Add string interpolation test * Remove member highlight as property * Change highlight of variable from module * Change highlight of destructured variables * Change highlight of labeled arguments * Fix highlight of variables in string interpolation * Fix parameter highlights * Move "as" to keyword.operator * Move async/await to coroutine * Fix test * Add missing punctutation highlights * Remove metadata from unit highlight * (true) and (false) as boolean --------- Co-authored-by: Pedro Castro <[email protected]>
1 parent 5e2a44a commit d77653c

File tree

7 files changed

+179
-90
lines changed

7 files changed

+179
-90
lines changed

queries/rescript/highlights.scm

+104-40
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,54 @@
77
((value_identifier) @constant.macro
88
(#match? @constant.macro "^\\.*$"))
99

10+
11+
((value_identifier) @variable)
12+
1013
[
1114
(type_identifier)
1215
(unit_type)
1316
(list)
1417
(list_pattern)
1518
] @type
1619

20+
((type_identifier) @type.builtin
21+
(#any-of? @type.builtin
22+
"int" "char" "string" "float" "bool" "unit"))
23+
1724
[
1825
(variant_identifier)
1926
(polyvar_identifier)
20-
] @constant
27+
] @constructor
2128

2229
(record_type_field (property_identifier) @property)
2330
(record_field (property_identifier) @property)
2431
(object (field (property_identifier) @property))
2532
(object_type (field (property_identifier) @property))
26-
(member_expression (property_identifier) @property)
27-
(module_identifier) @namespace
33+
(module_identifier) @module
34+
35+
(member_expression (property_identifier) @variable.member)
36+
37+
(value_identifier_path
38+
(module_identifier)
39+
(value_identifier) @variable)
40+
41+
42+
(record_pattern
43+
(value_identifier_path
44+
(value_identifier) @variable.member))
45+
46+
(record_pattern
47+
(value_identifier) @variable)
48+
49+
(labeled_argument
50+
label: (value_identifier) @variable.parameter)
51+
2852

2953
; Parameters
3054
;----------------
3155

32-
(list_pattern (value_identifier) @parameter)
33-
(spread_pattern (value_identifier) @parameter)
56+
(list_pattern (value_identifier) @variable.parameter)
57+
(spread_pattern (value_identifier) @variable.parameter)
3458

3559
; String literals
3660
;----------------
@@ -40,11 +64,8 @@
4064
(template_string)
4165
] @string
4266

43-
(template_substitution
44-
"${" @punctuation.bracket
45-
"}" @punctuation.bracket) @embedded
4667

47-
(character) @string.special
68+
(character) @character
4869
(escape_sequence) @string.escape
4970

5071
; Other literals
@@ -53,68 +74,102 @@
5374
[
5475
(true)
5576
(false)
56-
] @constant.builtin
77+
] @boolean
5778

5879
(number) @number
59-
(polyvar) @constant
60-
(polyvar_string) @constant
80+
(polyvar) @constructor
81+
(polyvar_string) @constructor
6182

6283
; Functions
6384
;----------
6485

6586
; parameter(s) in parens
66-
[
67-
(parameter (value_identifier))
68-
(labeled_parameter (value_identifier))
69-
] @parameter
87+
88+
(parameter (value_identifier) @variable.parameter)
89+
(labeled_parameter (value_identifier) @variable.parameter)
7090

7191
; single parameter with no parens
72-
(function parameter: (value_identifier) @parameter)
92+
(function parameter: (value_identifier) @variable.parameter)
7393

7494
; first-level descructuring (required for nvim-tree-sitter as it only matches direct
7595
; children and the above patterns do not match destructuring patterns in NeoVim)
76-
(parameter (tuple_pattern (tuple_item_pattern (value_identifier) @parameter)))
77-
(parameter (array_pattern (value_identifier) @parameter))
78-
(parameter (record_pattern (value_identifier) @parameter))
96+
(parameter (tuple_pattern (tuple_item_pattern (value_identifier) @variable.parameter)))
97+
(parameter (array_pattern (value_identifier) @variable.parameter))
98+
(parameter (record_pattern (value_identifier) @variable.parameter))
99+
100+
; function identifier in let binding
101+
(let_binding
102+
pattern: (value_identifier) @function
103+
body: (function))
104+
105+
; function calls
106+
107+
(call_expression
108+
function: (value_identifier_path
109+
_
110+
(value_identifier) @function.call))
111+
112+
(call_expression
113+
function: (value_identifier) @function.call)
114+
115+
; highlight the right-hand side of a pipe operator as a function call
116+
(pipe_expression
117+
_
118+
[(value_identifier_path
119+
_
120+
(value_identifier) @function.call)
121+
(value_identifier) @function.call])
122+
79123

80124
; Meta
81125
;-----
82126

83-
(decorator_identifier) @annotation
127+
(decorator_identifier) @attribute
84128

85129
(extension_identifier) @keyword
86130
("%") @keyword
87131

88132
; Misc
89133
;-----
90134

91-
(subscript_expression index: (string) @property)
92-
(polyvar_type_pattern "#" @constant)
135+
(polyvar_type_pattern "#" @constructor)
93136

94137
[
95-
("include")
96-
("open")
97-
] @include
138+
"include"
139+
"open"
140+
] @keyword.import
141+
142+
143+
[
144+
"private"
145+
"mutable"
146+
"rec"
147+
] @keyword.modifier
148+
149+
[
150+
"type"
151+
] @keyword.type
98152

99153
[
154+
"and"
155+
"with"
100156
"as"
157+
] @keyword.operator
158+
159+
[
101160
"export"
102161
"external"
103162
"let"
104163
"module"
105-
"mutable"
106-
"private"
107-
"rec"
108-
"type"
109-
"and"
110164
"assert"
111165
"await"
112-
"with"
113166
"lazy"
114167
"constraint"
115168
] @keyword
116169

117-
((function "async" @keyword))
170+
(("await") @keyword.coroutine)
171+
172+
((function "async" @keyword.coroutine))
118173

119174
(module_unpack "unpack" @keyword)
120175

@@ -123,30 +178,31 @@
123178
"else"
124179
"switch"
125180
"when"
126-
] @conditional
181+
] @keyword.conditional
127182

128183
[
129184
"exception"
130185
"try"
131186
"catch"
132-
] @exception
187+
] @keyword.exception
133188

134189
(call_expression
135-
function: (value_identifier) @exception
136-
(#eq? @exception "raise"))
190+
function: (value_identifier) @keyword.exception
191+
(#eq? @keyword.exception "raise"))
137192

138193
[
139194
"for"
140195
"in"
141196
"to"
142197
"downto"
143198
"while"
144-
] @repeat
199+
] @keyword.repeat
145200

146201
[
147202
"."
148203
","
149204
"|"
205+
":"
150206
] @punctuation.delimiter
151207

152208
[
@@ -174,6 +230,7 @@
174230
"|>"
175231
":>"
176232
"+="
233+
"=>"
177234
(uncurry)
178235
] @operator
179236

@@ -188,8 +245,16 @@
188245
"}"
189246
"["
190247
"]"
248+
"<"
249+
">"
191250
] @punctuation.bracket
192251

252+
(unit ["(" ")"] @constant.builtin)
253+
254+
(template_substitution
255+
"${" @punctuation.special
256+
"}" @punctuation.special) @none
257+
193258
(polyvar_type
194259
[
195260
"["
@@ -201,12 +266,11 @@
201266
[
202267
"~"
203268
"?"
204-
"=>"
205269
".."
206270
"..."
207271
] @punctuation.special
208272

209-
(ternary_expression ["?" ":"] @operator)
273+
(ternary_expression ["?" ":"] @keyword.conditional.ternary)
210274

211275
; JSX
212276
;----------

test/highlight/decorators.res

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@name
2-
//<- annotation
2+
//<- attribute
33

44
@@name
5-
//<- annotation
5+
//<- attribute

test/highlight/expressions.res

+34-11
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,42 @@ foo->bar == +x +. 1.0
99
// ^ property
1010

1111
switch foo {
12-
// <- conditional
12+
// <- keyword.conditional
1313
| list{1, x, ...rest} =>
1414
//^ type
1515
// ^ number
16-
// ^ parameter
16+
// ^ variable.parameter
1717
// ^ punctuation.special
18-
// ^ parameter
19-
// ^ punctuation.special
18+
// ^ variable.parameter
19+
// ^ operator
2020
42
2121
| list{1, 2, ...list{b, ..._} as rest} => rest
22-
// ^ parameter
22+
// ^ variable.parameter
2323
// ^ variable
2424
| exception Js.Exn.Error(_) => 99
25-
//^ exception
25+
//^ keyword.exception
2626
}
2727

2828
switch bar {
2929
| #...Mod.t => 33
30-
//^ constant
30+
//^ constructor
3131
}
3232

3333
{ foo, bar: baz, qux: 1 }
3434
//^ property
3535
// ^ property
3636

3737
exception InputClosed(string)
38-
//<- exception
38+
//<- keyword.exception
3939

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

4343
try {
44-
//<- exception
44+
//<- keyword.exception
4545
someOtherJSFunctionThatThrows()
4646
} catch {
47-
// ^ exception
47+
// ^ keyword.exception
4848
| Not_found => 1 // catch a ReScript exception
4949
| Invalid_argument(_) => 2 // catch a second ReScript exception
5050
| Js.Exn.Error(obj) => 3 // catch the JS exception
@@ -55,3 +55,26 @@ let c = list{a, ...list{b}}
5555
// ^ type
5656
// ^ variable
5757
// ^ variable
58+
59+
let x = fn()
60+
// ^ function.call
61+
62+
let y = x->M.f->f
63+
// ^function.call
64+
// ^function.call
65+
66+
let v = M.v
67+
// ^variable
68+
69+
let {x} = y
70+
// ^variable
71+
72+
let {X.x} = y
73+
// ^variable.member
74+
75+
let x = y.x
76+
// ^variable.member
77+
78+
f(~a=b, ())
79+
// ^variable.parameter
80+
// ^constant.builtin

test/highlight/functions.res

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
let inc = n => n + 1
2-
// ^ parameter
2+
// ^ variable.parameter
33
// ^ punctuation.special
4+
// ^ function
45

56
let fn = (a, (b, c), {d, e}, [f, g]) => a + b + c + d + e + f + g
6-
// ^ parameter
7-
// ^ parameter
8-
// ^ parameter
9-
// ^ parameter
10-
11-
let uncurry = (. u, .x) => (u, x)
12-
// ^ operator
13-
// ^ operator
7+
// ^ variable.parameter
8+
// ^ variable.parameter
9+
// ^ variable.parameter
10+
// ^ variable.parameter
1411

1512
let get = async (id) => id
16-
// ^ keyword
13+
// ^ keyword.coroutine

0 commit comments

Comments
 (0)