1
- module Token = struct
2
- type legend = {tokenTypes : string array ; tokenModifiers : string array }
1
+ (*
2
+ Generally speaking, semantic highlighting here takes care of categorizing identifiers,
3
+ since the kind of an identifier is highly context-specific and hard to catch with a grammar.
4
+
5
+ The big exception is labels, whose location is not represented in the AST
6
+ E.g. function definition such as (~foo as _) =>, application (~foo=3) and prop <div foo=3>.
7
+ Labels are handled in the grammar, not here.
8
+ Punned labels such as (~foo) => are both labels and identifiers. They are overridden here.
9
+
10
+ There are 2 cases where the grammar and semantic highlighting work jointly.
11
+ The styles emitted in the grammar and here need to be kept in sync.
12
+ 1) For jsx angled brackets, the grammar handles basic cases such as />
13
+ whose location is not in the AST.
14
+ Instead < and > are handled here. Those would be difficult to disambiguate in a grammar.
15
+ 2) Most operators are handled in the grammar. Except < and > are handled here.
16
+ The reason is again that < and > would be difficult do disambiguate in a grammar.
17
+ *)
3
18
19
+ module Token = struct
4
20
(* This needs to stay synced with the same legend in `server.ts` *)
5
21
(* See https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_semanticTokens *)
6
22
type tokenType =
7
- | Operator
8
- | Variable
9
- | Type
10
- | JsxTag
11
- | Namespace
12
- | EnumMember
13
- | Property
14
- | JsxLowercase
23
+ | Operator (* * < and > *)
24
+ | Variable (* * let x = *)
25
+ | Type (* * type t = *)
26
+ | JsxTag (* * the < and > in <div> *)
27
+ | Namespace (* * module M = *)
28
+ | EnumMember (* * variant A or poly variant #A *)
29
+ | Property (* * {x:...} *)
30
+ | JsxLowercase (* * div in <div> *)
15
31
16
32
type tokenModifiers = NoModifier
17
33
@@ -47,7 +63,8 @@ module Token = struct
47
63
48
64
let createEmitter () = {tokens = [] ; lastLine = 0 ; lastChar = 0 }
49
65
50
- let add ~line ~char ~length ~type_ ?(modifiers = NoModifier ) e =
66
+ let add ~line ~char ~length ~type_ e =
67
+ let modifiers = NoModifier in
51
68
e.tokens < - (line, char , length, type_, modifiers) :: e.tokens
52
69
53
70
let emitToken buf (line , char , length , type_ , modifiers ) e =
@@ -264,9 +281,9 @@ let parser ~debug ~emitter ~path =
264
281
:: _ ->
265
282
Utils. tupleOfLexing loc_start
266
283
| _ :: args -> loop args
267
- | [] -> (- 1 , - 1 )
268
- (* should not happen *)
284
+ | [] -> (* should not happen *) (- 1 , - 1 )
269
285
in
286
+
270
287
loop args
271
288
in
272
289
let posOfFinalGreatherthan =
@@ -276,6 +293,7 @@ let parser ~debug ~emitter ~path =
276
293
let selfClosing =
277
294
fst posOfGreatherthanAfterProps == fst posOfFinalGreatherthan
278
295
&& snd posOfGreatherthanAfterProps + 1 == snd posOfFinalGreatherthan
296
+ (* there's an off-by one somehow in the AST *)
279
297
in
280
298
(if not selfClosing then
281
299
let lineStart, colStart = Utils. tupleOfLexing pexp_loc.loc_start in
0 commit comments