@@ -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,12 @@ 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
+
145
163
let parser ~debug ~emitter ~path =
146
164
let processTypeArg (coreType : Parsetree.core_type ) =
147
165
if debug then Printf. printf " TypeArg: %s\n " (locToString coreType.ptyp_loc)
@@ -167,6 +185,10 @@ let parser ~debug ~emitter ~path =
167
185
| Ppat_var {loc; txt = id } ->
168
186
if isLowercaseId id then emitter |> emitVariable ~id ~debug ~loc ;
169
187
Ast_mapper. default_mapper.pat mapper p
188
+ | Ppat_record (cases , _ ) ->
189
+ cases
190
+ |> List. iter (fun (label , _ ) -> emitter |> emitRecordLabel ~label ~debug );
191
+ Ast_mapper. default_mapper.pat mapper p
170
192
| _ -> Ast_mapper. default_mapper.pat mapper p
171
193
in
172
194
let expr (mapper : Ast_mapper.mapper ) (e : Parsetree.expression ) =
@@ -212,6 +234,13 @@ let parser ~debug ~emitter ~path =
212
234
->
213
235
if debug then Printf. printf " BinaryExp: %s\n " (locToString pexp_loc);
214
236
Ast_mapper. default_mapper.expr mapper e
237
+ | Pexp_record (cases , _ ) ->
238
+ cases
239
+ |> List. iter (fun (label , _ ) -> emitter |> emitRecordLabel ~label ~debug );
240
+ Ast_mapper. default_mapper.expr mapper e
241
+ | Pexp_field (_ , label ) | Pexp_setfield (_ , label , _ ) ->
242
+ emitter |> emitRecordLabel ~label ~debug ;
243
+ Ast_mapper. default_mapper.expr mapper e
215
244
| _ -> Ast_mapper. default_mapper.expr mapper e
216
245
in
217
246
let module_expr (mapper : Ast_mapper.mapper ) (me : Parsetree.module_expr ) =
@@ -242,7 +271,7 @@ let parser ~debug ~emitter ~path =
242
271
match mt.pmty_desc with
243
272
| Pmty_ident {txt = lid ; loc} ->
244
273
emitter
245
- |> emitLongident ~module Token :Token. Type
274
+ |> emitLongident ~upper CaseToken :Token. Type
246
275
~pos: (Utils. tupleOfLexing loc.loc_start)
247
276
~lid ~debug ;
248
277
Ast_mapper. default_mapper.module_type mapper mt
@@ -251,7 +280,7 @@ let parser ~debug ~emitter ~path =
251
280
let module_type_declaration (mapper : Ast_mapper.mapper )
252
281
(mtd : Parsetree.module_type_declaration ) =
253
282
emitter
254
- |> emitLongident ~module Token :Token. Type
283
+ |> emitLongident ~upper CaseToken :Token. Type
255
284
~pos: (Utils. tupleOfLexing mtd.pmtd_name.loc.loc_start)
256
285
~lid: (Longident. Lident mtd.pmtd_name.txt) ~debug ;
257
286
Ast_mapper. default_mapper.module_type_declaration mapper mtd
@@ -264,11 +293,20 @@ let parser ~debug ~emitter ~path =
264
293
~lid: od.popen_lid.txt ~debug ;
265
294
Ast_mapper. default_mapper.open_description mapper od
266
295
in
296
+ let label_declaration (mapper : Ast_mapper.mapper )
297
+ (ld : Parsetree.label_declaration ) =
298
+ emitter
299
+ |> emitRecordLabel
300
+ ~label: {loc = ld.pld_name.loc; txt = Longident. Lident ld.pld_name.txt}
301
+ ~debug ;
302
+ Ast_mapper. default_mapper.label_declaration mapper ld
303
+ in
267
304
268
305
let mapper =
269
306
{
270
307
Ast_mapper. default_mapper with
271
308
expr;
309
+ label_declaration;
272
310
module_declaration;
273
311
module_binding;
274
312
module_expr;
0 commit comments