Skip to content

Commit 4af55e0

Browse files
committed
Use the grammar to highlight when to/downto are used as labels.
Labels are not handled by semantic highlighting (because there is no precise location information in the AST). But the grammar defines to/downto as keywords, in order to handle for loops. There are 3 cases where to/downto can be labels: 1) function definition `(~to as x) =>...` 2) function application `foo(~to=...)` 3) JSX props `<foo to=...` (punned cases are not problematic as they are also variables, and variables are handled via semantic highlighting). The grammar now recognises patterns of the form `to = ` and `to as` and highlights `to` as a variable. The correctness of this relies on an observation: in `to =` and `to as`, it's not possible that `to` is the keyword of a syntactically valid for loop, as for that to be the case it must be followed by an expression.
1 parent 5de5022 commit 4af55e0

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

analysis/tests/src/Parser.res

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,22 @@ module SomeComponent = {
8484

8585
let _ = <SomeComponent.Nested> <div /> </SomeComponent.Nested>
8686

87-
let _ = true
87+
// true/false
88+
let _ = true || false
89+
90+
// to/downto as label
91+
let toAs = (~to as x) => x
92+
let _toEquals = toAs(~to=10)
93+
94+
let to = 1
95+
for _ in to + to to to + to {
96+
()
97+
}
98+
99+
module ToAsProp = {
100+
@react.component
101+
let make = (~to) => {
102+
<> {React.int(to)} </>
103+
}
104+
}
105+
let _ = <ToAsProp to=3 />

analysis/tests/src/expected/Parser.res.txt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Parse tests/src/Parser.res
2-
structure items:23 diagnostics:0
2+
structure items:29 diagnostics:0
33
Lident: M (0,7) Namespace
44
Lident: C (1,9) Namespace
55
Lident: Component (1,13) Namespace
@@ -103,4 +103,22 @@ JsxTag >: (84,29)
103103
JsxTag >: (84,61)
104104
JsxTag <: (84,31)
105105
Lident: div (84,32) JsxLowercase
106+
Lident: x (90,25) Variable
107+
Variable: x (90,19)->(90,20)
108+
Variable: toAs (90,4)->(90,8)
109+
Lident: toAs (91,16) Variable
110+
Variable: _toEquals (91,4)->(91,13)
111+
Variable: to (93,4)->(93,6)
112+
Lident: to (94,20) Variable
113+
Lident: to (94,25) Variable
114+
Lident: to (94,9) Variable
115+
Lident: to (94,14) Variable
116+
Lident: ToAsProp (98,7) Namespace
117+
Lident: to (101,18) Variable
118+
Ldot: React (101,8) Namespace
119+
Lident: int (101,14) Variable
120+
Variable: to (100,14)->(100,17)
121+
Variable: make (100,6)->(100,10)
122+
JsxTag <: (104,8)
123+
Lident: ToAsProp (104,9) Namespace
106124

grammars/rescript.tmLanguage.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,32 @@
1717
"name": "keyword.control",
1818
"match": "\\b(and|as|assert|constraint|downto|else|exception|external|for|if|in|include|lazy|module|mutable|of|open|rec|switch|to|try|type|when|while|with)\\b"
1919
},
20+
"RE_TO_DOWNTO_AS_LABELS": {
21+
"patterns": [
22+
{
23+
"match": "(to|downto)\\s*(=)",
24+
"captures": {
25+
"1": {
26+
"name": "variable"
27+
},
28+
"2": {
29+
"name": "keyword.operator keyword"
30+
}
31+
}
32+
},
33+
{
34+
"match": "(to|downto)\\s*(as)",
35+
"captures": {
36+
"1": {
37+
"name": "variable"
38+
},
39+
"2": {
40+
"name": "keyword.control"
41+
}
42+
}
43+
}
44+
]
45+
},
2046
"RE_CONSTANTS_BOOL": {
2147
"name": "constant.language.boolean",
2248
"match": "\\b(false|true)\\b"
@@ -93,6 +119,9 @@
93119
},
94120
"keyword": {
95121
"patterns": [
122+
{
123+
"include": "#RE_TO_DOWNTO_AS_LABELS"
124+
},
96125
{
97126
"include": "#RE_KEYWORDS"
98127
},

0 commit comments

Comments
 (0)