Skip to content

Commit

Permalink
Highlight fixes (#241)
Browse files Browse the repository at this point in the history
* 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 <aspeddro@gmail.com>
  • Loading branch information
Emilios1995 and aspeddro authored Jul 25, 2024
1 parent 5e2a44a commit d77653c
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 90 deletions.
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)

(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)

; 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"
"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

0 comments on commit d77653c

Please sign in to comment.