Skip to content

Commit bfe7cb6

Browse files
zthGabriel Nordeborn
authored and
Gabriel Nordeborn
committed
add id:s and signatures for constructors/record fields
1 parent 7d8203a commit bfe7cb6

File tree

4 files changed

+100
-22
lines changed

4 files changed

+100
-22
lines changed

analysis/src/DocExtraction.ml

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,28 @@ type linkableType = {
55
loc: Location.t;
66
}
77

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+
816
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}
1119
type docItem =
1220
| Value of {
21+
id: string;
1322
docstring: string list;
1423
signature: string;
1524
name: string;
1625
linkables: linkableType list;
1726
(** Relevant types to link to, found in relation to this value. *)
1827
}
1928
| Type of {
29+
id: string;
2030
docstring: string list;
2131
signature: string;
2232
name: string;
@@ -26,7 +36,12 @@ type docItem =
2636
(** Relevant types to link to, found in relation to this type. *)
2737
}
2838
| 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+
}
3045

3146
let formatCode content =
3247
let {Res_driver.parsetree = signature; comments} =
@@ -130,11 +145,13 @@ let stringifyDetail ?(indentation = 0) (detail : docItemDetail) =
130145
( "fieldDocs",
131146
Some
132147
(fieldDocs
133-
|> List.map (fun (fieldName, docstrings) ->
148+
|> List.map (fun fieldDoc ->
134149
stringifyObject ~indentation:(indentation + 1)
135150
[
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));
138155
])
139156
|> array) );
140157
]
@@ -145,22 +162,27 @@ let stringifyDetail ?(indentation = 0) (detail : docItemDetail) =
145162
( "fieldDocs",
146163
Some
147164
(constructorDocs
148-
|> List.map (fun (constructorName, docstrings) ->
165+
|> List.map (fun constructorDoc ->
149166
stringifyObject ~startOnNewline:true
150167
~indentation:(indentation + 1)
151168
[
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) );
154175
])
155176
|> array) );
156177
]
157178

158179
let rec stringifyDocItem ?(indentation = 0) ~originalEnv (item : docItem) =
159180
let open Protocol in
160181
match item with
161-
| Value {docstring; signature; name; linkables} ->
182+
| Value {id; docstring; signature; name; linkables} ->
162183
stringifyObject ~startOnNewline:true ~indentation
163184
[
185+
("id", Some (wrapInQuotes id));
164186
("kind", Some (wrapInQuotes "value"));
165187
("name", Some (name |> Json.escape |> wrapInQuotes));
166188
( "signature",
@@ -171,9 +193,10 @@ let rec stringifyDocItem ?(indentation = 0) ~originalEnv (item : docItem) =
171193
(stringifyLinkables ~originalEnv ~indentation:(indentation + 1)
172194
linkables) );
173195
]
174-
| Type {docstring; signature; name; detail; linkables} ->
196+
| Type {id; docstring; signature; name; detail; linkables} ->
175197
stringifyObject ~startOnNewline:true ~indentation
176198
[
199+
("id", Some (wrapInQuotes id));
177200
("kind", Some (wrapInQuotes "type"));
178201
("name", Some (name |> Json.escape |> wrapInQuotes));
179202
("signature", Some (signature |> Json.escape |> wrapInQuotes));
@@ -191,6 +214,7 @@ let rec stringifyDocItem ?(indentation = 0) ~originalEnv (item : docItem) =
191214
| Module m ->
192215
stringifyObject ~startOnNewline:true ~indentation
193216
[
217+
("id", Some (wrapInQuotes m.id));
194218
("kind", Some (wrapInQuotes "module"));
195219
( "item",
196220
Some
@@ -214,6 +238,9 @@ and stringifyDocsForModule ?(indentation = 0) ~originalEnv (d : docsForModule) =
214238

215239
exception Invalid_file_type
216240

241+
let makeId modulePath ~identifier =
242+
identifier :: modulePath |> List.rev |> SharedTypes.ident
243+
217244
let extractDocs ~path ~debug =
218245
if debug then Printf.printf "extracting docs for %s\n" path;
219246
if
@@ -241,8 +268,10 @@ let extractDocs ~path ~debug =
241268
let structure = file.structure in
242269
let open SharedTypes in
243270
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) =
245273
{
274+
id = modulePath |> ident;
246275
docstring = structure.docstring |> List.map String.trim;
247276
name = structure.name;
248277
items =
@@ -253,6 +282,7 @@ let extractDocs ~path ~debug =
253282
Some
254283
(Value
255284
{
285+
id = modulePath |> makeId ~identifier:item.name;
256286
docstring = item.docstring |> List.map String.trim;
257287
signature =
258288
"let " ^ item.name ^ ": " ^ Shared.typeToString typ
@@ -265,6 +295,7 @@ let extractDocs ~path ~debug =
265295
Some
266296
(Type
267297
{
298+
id = modulePath |> makeId ~identifier:item.name;
268299
linkables =
269300
Typ typ |> Linkables.findLinkables ~env ~full;
270301
docstring = item.docstring |> List.map String.trim;
@@ -285,7 +316,12 @@ let extractDocs ~path ~debug =
285316
fieldDocs =
286317
fields
287318
|> 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+
});
289325
})
290326
| Some (Tvariant {constructors}) ->
291327
Some
@@ -294,16 +330,27 @@ let extractDocs ~path ~debug =
294330
constructorDocs =
295331
constructors
296332
|> 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+
});
298340
})
299341
| _ -> None);
300342
})
301343
| Module (Structure m) ->
302344
(* module Whatever = {} in res or module Whatever: {} in resi. *)
303-
Some (Module (extractDocs m))
345+
Some
346+
(Module (extractDocs ~modulePath:(m.name :: modulePath) m))
304347
| Module (Constraint (Structure _impl, Structure interface)) ->
305348
(* module Whatever: { <interface> } = { <impl> }. Prefer the interface. *)
306-
Some (Module (extractDocs interface))
349+
Some
350+
(Module
351+
(extractDocs
352+
~modulePath:(interface.name :: modulePath)
353+
interface))
307354
| _ -> None);
308355
}
309356
in

analysis/tests/src/expected/DocExtraction2.res.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ preferring found resi file for impl: src/DocExtraction2.resi
77
"docstrings": ["Module level doc here."],
88
"items": [
99
{
10+
"id": "DocExtraction2.t",
1011
"kind": "type",
1112
"name": "t",
1213
"signature": "type t",
1314
"docstrings": ["Type t is pretty cool."],
1415
"linkables": []
1516
},
1617
{
18+
"id": "DocExtraction2.make",
1719
"kind": "value",
1820
"name": "make",
1921
"signature": "let make: unit => t",
@@ -25,20 +27,23 @@ preferring found resi file for impl: src/DocExtraction2.resi
2527
}]
2628
},
2729
{
30+
"id": "InnerModule.DocExtraction2",
2831
"kind": "module",
2932
"item":
3033
{
3134
"name": "InnerModule",
3235
"docstrings": [],
3336
"items": [
3437
{
38+
"id": "DocExtraction2.InnerModule.t",
3539
"kind": "type",
3640
"name": "t",
3741
"signature": "type t",
3842
"docstrings": ["This type is also t."],
3943
"linkables": []
4044
},
4145
{
46+
"id": "DocExtraction2.InnerModule.make",
4247
"kind": "value",
4348
"name": "make",
4449
"signature": "let make: unit => t",

analysis/tests/src/expected/DocExtraction2.resi.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ extracting docs for src/DocExtraction2.resi
66
"docstrings": ["Module level doc here."],
77
"items": [
88
{
9+
"id": "DocExtraction2.t",
910
"kind": "type",
1011
"name": "t",
1112
"signature": "type t",
1213
"docstrings": ["Type t is pretty cool."],
1314
"linkables": []
1415
},
1516
{
17+
"id": "DocExtraction2.make",
1618
"kind": "value",
1719
"name": "make",
1820
"signature": "let make: unit => t",
@@ -24,20 +26,23 @@ extracting docs for src/DocExtraction2.resi
2426
}]
2527
},
2628
{
29+
"id": "InnerModule.DocExtraction2",
2730
"kind": "module",
2831
"item":
2932
{
3033
"name": "InnerModule",
3134
"docstrings": [],
3235
"items": [
3336
{
37+
"id": "DocExtraction2.InnerModule.t",
3438
"kind": "type",
3539
"name": "t",
3640
"signature": "type t",
3741
"docstrings": ["This type is also t."],
3842
"linkables": []
3943
},
4044
{
45+
"id": "DocExtraction2.InnerModule.make",
4146
"kind": "value",
4247
"name": "make",
4348
"signature": "let make: unit => t",

0 commit comments

Comments
 (0)