@@ -5,18 +5,28 @@ type linkableType = {
5
5
loc : Location .t ;
6
6
}
7
7
8
+ type fieldDoc = {fieldName : string ; docstrings : string list ; signature : string }
9
+
10
+ type constructorDoc = {
11
+ constructorName : string ;
12
+ docstrings : string list ;
13
+ signature : string ;
14
+ }
15
+
8
16
type docItemDetail =
9
- | Record of {fieldDocs : ( string * string list ) list }
10
- | Variant of {constructorDocs : ( string * string list ) list }
17
+ | Record of {fieldDocs : fieldDoc list }
18
+ | Variant of {constructorDocs : constructorDoc list }
11
19
type docItem =
12
20
| Value of {
21
+ id : string ;
13
22
docstring : string list ;
14
23
signature : string ;
15
24
name : string ;
16
25
linkables : linkableType list ;
17
26
(* * Relevant types to link to, found in relation to this value. *)
18
27
}
19
28
| Type of {
29
+ id : string ;
20
30
docstring : string list ;
21
31
signature : string ;
22
32
name : string ;
@@ -26,7 +36,12 @@ type docItem =
26
36
(* * Relevant types to link to, found in relation to this type. *)
27
37
}
28
38
| Module of docsForModule
29
- and docsForModule = {docstring : string list ; name : string ; items : docItem list }
39
+ and docsForModule = {
40
+ id : string ;
41
+ docstring : string list ;
42
+ name : string ;
43
+ items : docItem list ;
44
+ }
30
45
31
46
let formatCode content =
32
47
let {Res_driver. parsetree = signature; comments} =
@@ -130,11 +145,13 @@ let stringifyDetail ?(indentation = 0) (detail : docItemDetail) =
130
145
( " fieldDocs" ,
131
146
Some
132
147
(fieldDocs
133
- |> List. map (fun ( fieldName , docstrings ) ->
148
+ |> List. map (fun fieldDoc ->
134
149
stringifyObject ~indentation: (indentation + 1 )
135
150
[
136
- (" fieldName" , Some (wrapInQuotes fieldName));
137
- (" docstrings" , Some (stringifyDocstrings docstrings));
151
+ (" fieldName" , Some (wrapInQuotes fieldDoc.fieldName));
152
+ ( " docstrings" ,
153
+ Some (stringifyDocstrings fieldDoc.docstrings) );
154
+ (" signature" , Some (wrapInQuotes fieldDoc.signature));
138
155
])
139
156
|> array ) );
140
157
]
@@ -145,22 +162,27 @@ let stringifyDetail ?(indentation = 0) (detail : docItemDetail) =
145
162
( " fieldDocs" ,
146
163
Some
147
164
(constructorDocs
148
- |> List. map (fun ( constructorName , docstrings ) ->
165
+ |> List. map (fun constructorDoc ->
149
166
stringifyObject ~start OnNewline:true
150
167
~indentation: (indentation + 1 )
151
168
[
152
- (" constructorName" , Some (wrapInQuotes constructorName));
153
- (" docstrings" , Some (stringifyDocstrings docstrings));
169
+ ( " constructorName" ,
170
+ Some (wrapInQuotes constructorDoc.constructorName) );
171
+ ( " docstrings" ,
172
+ Some (stringifyDocstrings constructorDoc.docstrings) );
173
+ ( " signature" ,
174
+ Some (wrapInQuotes constructorDoc.signature) );
154
175
])
155
176
|> array ) );
156
177
]
157
178
158
179
let rec stringifyDocItem ?(indentation = 0 ) ~originalEnv (item : docItem ) =
159
180
let open Protocol in
160
181
match item with
161
- | Value {docstring; signature; name; linkables} ->
182
+ | Value {id; docstring; signature; name; linkables} ->
162
183
stringifyObject ~start OnNewline:true ~indentation
163
184
[
185
+ (" id" , Some (wrapInQuotes id));
164
186
(" kind" , Some (wrapInQuotes " value" ));
165
187
(" name" , Some (name |> Json. escape |> wrapInQuotes));
166
188
( " signature" ,
@@ -171,9 +193,10 @@ let rec stringifyDocItem ?(indentation = 0) ~originalEnv (item : docItem) =
171
193
(stringifyLinkables ~original Env ~indentation: (indentation + 1 )
172
194
linkables) );
173
195
]
174
- | Type {docstring; signature; name; detail; linkables} ->
196
+ | Type {id; docstring; signature; name; detail; linkables} ->
175
197
stringifyObject ~start OnNewline:true ~indentation
176
198
[
199
+ (" id" , Some (wrapInQuotes id));
177
200
(" kind" , Some (wrapInQuotes " type" ));
178
201
(" name" , Some (name |> Json. escape |> wrapInQuotes));
179
202
(" signature" , Some (signature |> Json. escape |> wrapInQuotes));
@@ -191,6 +214,7 @@ let rec stringifyDocItem ?(indentation = 0) ~originalEnv (item : docItem) =
191
214
| Module m ->
192
215
stringifyObject ~start OnNewline:true ~indentation
193
216
[
217
+ (" id" , Some (wrapInQuotes m.id));
194
218
(" kind" , Some (wrapInQuotes " module" ));
195
219
( " item" ,
196
220
Some
@@ -214,6 +238,9 @@ and stringifyDocsForModule ?(indentation = 0) ~originalEnv (d : docsForModule) =
214
238
215
239
exception Invalid_file_type
216
240
241
+ let makeId modulePath ~identifier =
242
+ identifier :: modulePath |> List. rev |> SharedTypes. ident
243
+
217
244
let extractDocs ~path ~debug =
218
245
if debug then Printf. printf " extracting docs for %s\n " path;
219
246
if
@@ -241,8 +268,10 @@ let extractDocs ~path ~debug =
241
268
let structure = file.structure in
242
269
let open SharedTypes in
243
270
let env = QueryEnv. fromFile file in
244
- let rec extractDocs (structure : Module.structure ) =
271
+ let rec extractDocs ?(modulePath = [env.file.moduleName])
272
+ (structure : Module.structure ) =
245
273
{
274
+ id = modulePath |> ident;
246
275
docstring = structure.docstring |> List. map String. trim;
247
276
name = structure.name;
248
277
items =
@@ -253,6 +282,7 @@ let extractDocs ~path ~debug =
253
282
Some
254
283
(Value
255
284
{
285
+ id = modulePath |> makeId ~identifier: item.name;
256
286
docstring = item.docstring |> List. map String. trim;
257
287
signature =
258
288
" let " ^ item.name ^ " : " ^ Shared. typeToString typ
@@ -265,6 +295,7 @@ let extractDocs ~path ~debug =
265
295
Some
266
296
(Type
267
297
{
298
+ id = modulePath |> makeId ~identifier: item.name;
268
299
linkables =
269
300
Typ typ |> Linkables. findLinkables ~env ~full ;
270
301
docstring = item.docstring |> List. map String. trim;
@@ -285,7 +316,12 @@ let extractDocs ~path ~debug =
285
316
fieldDocs =
286
317
fields
287
318
|> List. map (fun (field : field ) ->
288
- (field.fname.txt, field.docstring));
319
+ {
320
+ fieldName = field.fname.txt;
321
+ docstrings = field.docstring;
322
+ signature =
323
+ Shared. typeToString field.typ;
324
+ });
289
325
})
290
326
| Some (Tvariant {constructors} ) ->
291
327
Some
@@ -294,16 +330,27 @@ let extractDocs ~path ~debug =
294
330
constructorDocs =
295
331
constructors
296
332
|> List. map (fun (c : Constructor.t ) ->
297
- (c.cname.txt, c.docstring));
333
+ {
334
+ constructorName = c.cname.txt;
335
+ docstrings = c.docstring;
336
+ signature =
337
+ CompletionBackEnd
338
+ .showConstructor c;
339
+ });
298
340
})
299
341
| _ -> None );
300
342
})
301
343
| Module (Structure m ) ->
302
344
(* module Whatever = {} in res or module Whatever: {} in resi. *)
303
- Some (Module (extractDocs m))
345
+ Some
346
+ (Module (extractDocs ~module Path:(m.name :: modulePath) m))
304
347
| Module (Constraint (Structure _impl , Structure interface )) ->
305
348
(* module Whatever: { <interface> } = { <impl> }. Prefer the interface. *)
306
- Some (Module (extractDocs interface))
349
+ Some
350
+ (Module
351
+ (extractDocs
352
+ ~module Path:(interface.name :: modulePath)
353
+ interface))
307
354
| _ -> None );
308
355
}
309
356
in
0 commit comments