@@ -3,7 +3,15 @@ module Token = struct
3
3
4
4
(* This needs to stay synced with the same legend in `server.ts` *)
5
5
(* See https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_semanticTokens *)
6
- type tokenType = Keyword | Variable | Type | JsxTag | Namespace
6
+ type tokenType =
7
+ | Keyword
8
+ | Variable
9
+ | Type
10
+ | JsxTag
11
+ | Namespace
12
+ | EnumMember
13
+ | Property
14
+
7
15
type tokenModifiers = NoModifier
8
16
9
17
let tokenTypeToString = function
@@ -12,13 +20,17 @@ module Token = struct
12
20
| Type -> " 2"
13
21
| JsxTag -> " 3"
14
22
| Namespace -> " 4"
23
+ | EnumMember -> " 5"
24
+ | Property -> " 6"
15
25
16
26
let tokenTypeDebug = function
17
27
| Keyword -> " Keyword"
18
28
| Variable -> " Variable"
19
29
| Type -> " Type"
20
30
| JsxTag -> " JsxTag"
21
31
| Namespace -> " Namespace"
32
+ | EnumMember -> " EnumMember"
33
+ | Property -> " Property"
22
34
23
35
let tokenModifiersToString = function NoModifier -> " 0"
24
36
@@ -87,8 +99,8 @@ let emitFromLoc ~loc ~type_ emitter =
87
99
emitter |> emitFromPos posStart posEnd ~type_
88
100
89
101
let emitLongident ?(backwards = false ) ?(jsx = false )
90
- ?(moduleToken = Token. Namespace ) ~ pos ~ lid ~ debug emitter =
91
- let variableToken = if jsx then Token. JsxTag else Variable in
102
+ ?(lowerCaseToken = if jsx then Token. JsxTag else Variable )
103
+ ?( upperCaseToken = Token. Namespace ) ~ pos ~ lid ~ debug emitter =
92
104
let rec flatten acc lid =
93
105
match lid with
94
106
| Longident. Lident txt -> txt :: acc
@@ -100,13 +112,13 @@ let emitLongident ?(backwards = false) ?(jsx = false)
100
112
let rec loop pos segments =
101
113
match segments with
102
114
| [id] when isUppercaseId id || isLowercaseId id ->
103
- let type_ = if isUppercaseId id then moduleToken else variableToken in
115
+ let type_ = if isUppercaseId id then upperCaseToken else lowerCaseToken in
104
116
if debug then
105
117
Printf. printf " Lident: %s %s %s\n " id (posToString pos)
106
118
(Token. tokenTypeDebug type_);
107
119
emitter |> emitFromPos pos (fst pos, snd pos + String. length id) ~type_
108
120
| id :: segments when isUppercaseId id || isLowercaseId id ->
109
- let type_ = if isUppercaseId id then moduleToken else variableToken in
121
+ let type_ = if isUppercaseId id then upperCaseToken else lowerCaseToken in
110
122
if debug then
111
123
Printf. printf " Ldot: %s %s %s\n " id (posToString pos)
112
124
(Token. tokenTypeDebug type_);
@@ -142,6 +154,18 @@ let emitType ~id ~debug ~loc emitter =
142
154
if debug then Printf. printf " Type: %s %s\n " id (locToString loc);
143
155
emitter |> emitFromLoc ~loc ~type_: Token. Type
144
156
157
+ let emitRecordLabel ~(label : Longident.t Location.loc ) ~debug emitter =
158
+ emitter
159
+ |> emitLongident ~lower CaseToken:Token. Property
160
+ ~pos: (Utils. tupleOfLexing label.loc.loc_start)
161
+ ~lid: label.txt ~debug
162
+
163
+ let emitVariant ~(name : Longident.t Location.loc ) ~debug emitter =
164
+ emitter
165
+ |> emitLongident ~upper CaseToken:Token. EnumMember
166
+ ~pos: (Utils. tupleOfLexing name.loc.loc_start)
167
+ ~lid: name.txt ~debug
168
+
145
169
let parser ~debug ~emitter ~path =
146
170
let processTypeArg (coreType : Parsetree.core_type ) =
147
171
if debug then Printf. printf " TypeArg: %s\n " (locToString coreType.ptyp_loc)
@@ -167,6 +191,13 @@ let parser ~debug ~emitter ~path =
167
191
| Ppat_var {loc; txt = id } ->
168
192
if isLowercaseId id then emitter |> emitVariable ~id ~debug ~loc ;
169
193
Ast_mapper. default_mapper.pat mapper p
194
+ | Ppat_record (cases , _ ) ->
195
+ cases
196
+ |> List. iter (fun (label , _ ) -> emitter |> emitRecordLabel ~label ~debug );
197
+ Ast_mapper. default_mapper.pat mapper p
198
+ | Ppat_construct (name , _ ) ->
199
+ emitter |> emitVariant ~name ~debug ;
200
+ Ast_mapper. default_mapper.pat mapper p
170
201
| _ -> Ast_mapper. default_mapper.pat mapper p
171
202
in
172
203
let expr (mapper : Ast_mapper.mapper ) (e : Parsetree.expression ) =
@@ -212,6 +243,16 @@ let parser ~debug ~emitter ~path =
212
243
->
213
244
if debug then Printf. printf " BinaryExp: %s\n " (locToString pexp_loc);
214
245
Ast_mapper. default_mapper.expr mapper e
246
+ | Pexp_record (cases , _ ) ->
247
+ cases
248
+ |> List. iter (fun (label , _ ) -> emitter |> emitRecordLabel ~label ~debug );
249
+ Ast_mapper. default_mapper.expr mapper e
250
+ | Pexp_field (_ , label ) | Pexp_setfield (_ , label , _ ) ->
251
+ emitter |> emitRecordLabel ~label ~debug ;
252
+ Ast_mapper. default_mapper.expr mapper e
253
+ | Pexp_construct (name , _ ) ->
254
+ emitter |> emitVariant ~name ~debug ;
255
+ Ast_mapper. default_mapper.expr mapper e
215
256
| _ -> Ast_mapper. default_mapper.expr mapper e
216
257
in
217
258
let module_expr (mapper : Ast_mapper.mapper ) (me : Parsetree.module_expr ) =
@@ -242,7 +283,7 @@ let parser ~debug ~emitter ~path =
242
283
match mt.pmty_desc with
243
284
| Pmty_ident {txt = lid ; loc} ->
244
285
emitter
245
- |> emitLongident ~module Token :Token. Type
286
+ |> emitLongident ~upper CaseToken :Token. Type
246
287
~pos: (Utils. tupleOfLexing loc.loc_start)
247
288
~lid ~debug ;
248
289
Ast_mapper. default_mapper.module_type mapper mt
@@ -251,7 +292,7 @@ let parser ~debug ~emitter ~path =
251
292
let module_type_declaration (mapper : Ast_mapper.mapper )
252
293
(mtd : Parsetree.module_type_declaration ) =
253
294
emitter
254
- |> emitLongident ~module Token :Token. Type
295
+ |> emitLongident ~upper CaseToken :Token. Type
255
296
~pos: (Utils. tupleOfLexing mtd.pmtd_name.loc.loc_start)
256
297
~lid: (Longident. Lident mtd.pmtd_name.txt) ~debug ;
257
298
Ast_mapper. default_mapper.module_type_declaration mapper mtd
@@ -264,11 +305,29 @@ let parser ~debug ~emitter ~path =
264
305
~lid: od.popen_lid.txt ~debug ;
265
306
Ast_mapper. default_mapper.open_description mapper od
266
307
in
308
+ let label_declaration (mapper : Ast_mapper.mapper )
309
+ (ld : Parsetree.label_declaration ) =
310
+ emitter
311
+ |> emitRecordLabel
312
+ ~label: {loc = ld.pld_name.loc; txt = Longident. Lident ld.pld_name.txt}
313
+ ~debug ;
314
+ Ast_mapper. default_mapper.label_declaration mapper ld
315
+ in
316
+ let constructor_declaration (mapper : Ast_mapper.mapper )
317
+ (cd : Parsetree.constructor_declaration ) =
318
+ emitter
319
+ |> emitVariant
320
+ ~name: {loc = cd.pcd_name.loc; txt = Longident. Lident cd.pcd_name.txt}
321
+ ~debug ;
322
+ Ast_mapper. default_mapper.constructor_declaration mapper cd
323
+ in
267
324
268
325
let mapper =
269
326
{
270
327
Ast_mapper. default_mapper with
328
+ constructor_declaration;
271
329
expr;
330
+ label_declaration;
272
331
module_declaration;
273
332
module_binding;
274
333
module_expr;
0 commit comments