Skip to content

Commit d735fb1

Browse files
committed
Added explanatory comments on top of SemanticTokens.ml.
1 parent 2b96d8f commit d735fb1

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

Diff for: analysis/src/SemanticTokens.ml

+31-13
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
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+
*)
318

19+
module Token = struct
420
(* This needs to stay synced with the same legend in `server.ts` *)
521
(* See https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_semanticTokens *)
622
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> *)
1531

1632
type tokenModifiers = NoModifier
1733

@@ -47,7 +63,8 @@ module Token = struct
4763

4864
let createEmitter () = {tokens = []; lastLine = 0; lastChar = 0}
4965

50-
let add ~line ~char ~length ~type_ ?(modifiers = NoModifier) e =
66+
let add ~line ~char ~length ~type_ e =
67+
let modifiers = NoModifier in
5168
e.tokens <- (line, char, length, type_, modifiers) :: e.tokens
5269

5370
let emitToken buf (line, char, length, type_, modifiers) e =
@@ -264,9 +281,9 @@ let parser ~debug ~emitter ~path =
264281
:: _ ->
265282
Utils.tupleOfLexing loc_start
266283
| _ :: args -> loop args
267-
| [] -> (-1, -1)
268-
(* should not happen *)
284+
| [] -> (* should not happen *) (-1, -1)
269285
in
286+
270287
loop args
271288
in
272289
let posOfFinalGreatherthan =
@@ -276,6 +293,7 @@ let parser ~debug ~emitter ~path =
276293
let selfClosing =
277294
fst posOfGreatherthanAfterProps == fst posOfFinalGreatherthan
278295
&& snd posOfGreatherthanAfterProps + 1 == snd posOfFinalGreatherthan
296+
(* there's an off-by one somehow in the AST *)
279297
in
280298
(if not selfClosing then
281299
let lineStart, colStart = Utils.tupleOfLexing pexp_loc.loc_start in

0 commit comments

Comments
 (0)