Skip to content
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

Highlight fixes #241

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b3edafc
Highlight variants as constructors
Emilios1995 Mar 21, 2024
ec237f2
Add set of built-in types
Emilios1995 Mar 21, 2024
3abe8b4
Update @namespace to @module
Emilios1995 Mar 21, 2024
7a47330
Update @parameter to @variable.parameter
Emilios1995 Mar 21, 2024
186c562
Update string interpolation and char
Emilios1995 Mar 21, 2024
cc4eeb3
Update parameter to @variable.parameter
Emilios1995 Mar 21, 2024
eabca3a
Update annotation to attribute
Emilios1995 Mar 21, 2024
088898e
Complete change of polyvar as constructors
Emilios1995 Mar 21, 2024
2353fb3
Specialize some of the keywords
Emilios1995 Mar 21, 2024
d5be63f
Move the => arrow to operators
Emilios1995 Mar 21, 2024
68e2310
Specialize ternary operator
Emilios1995 Mar 21, 2024
e43ea99
Highlight function name in bindings
Emilios1995 Mar 21, 2024
03b9e65
Highlight function calls
Emilios1995 Mar 21, 2024
ae86960
Highlight members of records and modules
Emilios1995 Mar 21, 2024
411a36f
Highlight unit as built-in constant
Emilios1995 Mar 24, 2024
85b87d3
Highlight labeled parameters names as properties
Emilios1995 Mar 24, 2024
c82c32a
Fix existing tests
Emilios1995 Mar 24, 2024
4f7a49f
Fix pipe operator call highlighting to work with simple values too
Emilios1995 Mar 24, 2024
a9b3dde
Add highlight tests for function names and calls
Emilios1995 Mar 24, 2024
4f84ef6
Add tests for module and record members
Emilios1995 Mar 24, 2024
68f4846
Change record field expression to member
Emilios1995 Mar 24, 2024
2f58542
Add test for labeled argument as property
Emilios1995 Mar 24, 2024
9a7e362
Add test for unit highlight
Emilios1995 Mar 25, 2024
8461c77
Adapt to new tree-sitter last match wins
Emilios1995 Mar 25, 2024
779f4fc
Move template highlight rule
Emilios1995 Mar 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
125 changes: 90 additions & 35 deletions queries/rescript/highlights.scm
Expand Up @@ -14,23 +14,43 @@
(list_pattern)
] @type

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


((unit) @constant.builtin
(#set! "priority" 105))

[
(variant_identifier)
(polyvar_identifier)
] @constant
] @constructor
Comment on lines 25 to +28
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not everyone might agree on this one, since variants, specially polyvars without parameters, are used as "constants". This thinking is common when making JS bindings, since polyvars compile to strings.

However, from a language perspective, I find it more correct to think of them as constructors. This is also how the ocaml parser highlights them.


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


(record_pattern
(value_identifier) @variable.member)

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

(labeled_argument
label: (value_identifier) @property)


; 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 +60,8 @@
(template_string)
] @string

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

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

; Other literals
Expand All @@ -56,8 +73,8 @@
] @constant.builtin

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

; Functions
;----------
Expand All @@ -66,21 +83,45 @@
[
(parameter (value_identifier))
(labeled_parameter (value_identifier))
] @parameter
] @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
Expand All @@ -89,27 +130,37 @@
;-----

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

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


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

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

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

[
"as"
"export"
"external"
"let"
"module"
"mutable"
"private"
"rec"
"type"
"and"
"assert"
"await"
"with"
"lazy"
"constraint"
] @keyword
Expand All @@ -123,25 +174,25 @@
"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

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

Expand All @@ -190,6 +242,10 @@
"]"
] @punctuation.bracket

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

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

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

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

@@name
//<- annotation
//<- attribute
42 changes: 31 additions & 11 deletions test/highlight/expressions.res
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,23 @@ 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.member

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

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

f(~a=b, ())
// ^property
// ^constant.builtin
11 changes: 6 additions & 5 deletions test/highlight/functions.res
@@ -1,12 +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
// ^ variable.parameter
// ^ variable.parameter
// ^ variable.parameter
// ^ variable.parameter

let uncurry = (. u, .x) => (u, x)
// ^ operator
Expand Down
26 changes: 13 additions & 13 deletions test/highlight/literals.res
@@ -1,26 +1,26 @@

/**/ #polyvar
// ^ constant
// ^ constant
// ^ constructor
// ^ constructor

/**/ #"polyvar"
// ^ constant
// ^ constant
// ^ constant
// ^ constructor
// ^ constructor
// ^ constructor

/**/ #\"polyvar"
// ^ constant
// ^ constant
// ^ constant
// ^ constant
// ^ constructor
// ^ constructor
// ^ constructor
// ^ constructor

/**/ #77
// ^ constant
// ^ constant
// ^ constructor
// ^ constructor

/**/ 'R'
// ^ string.special
// ^ string.special
// ^ character
// ^ character

/**/ '\\'
// ^ string.escape