Skip to content

Commit 781b36c

Browse files
zthGabriel Nordeborn
authored and
Gabriel Nordeborn
committed
linkables in constructor and record field details
1 parent 85f7a2b commit 781b36c

File tree

2 files changed

+83
-45
lines changed

2 files changed

+83
-45
lines changed

analysis/src/DocExtraction.ml

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

8-
type fieldDoc = {fieldName: string; docstrings: string list; signature: string}
8+
type fieldDoc = {
9+
fieldName: string;
10+
docstrings: string list;
11+
signature: string;
12+
linkables: linkableType list;
13+
}
914

1015
type constructorDoc = {
1116
constructorName: string;
1217
docstrings: string list;
1318
signature: string;
19+
linkables: linkableType list;
1420
}
1521

1622
type docItemDetail =
@@ -83,7 +89,9 @@ module Linkables = struct
8389
| Some path -> (
8490
match References.digConstructor ~env ~package:full.package path with
8591
| None -> (env, [typ])
86-
| Some (env1, {item = {decl}}) -> linkablesFromDecl decl ~env:env1 ~full)
92+
| Some (env1, {item = {decl}}) ->
93+
let env, types = linkablesFromDecl decl ~env:env1 ~full in
94+
(env, typ :: types))
8795
| None -> (env, [typ])
8896

8997
type linkableSource =
@@ -138,7 +146,7 @@ let stringifyLinkables ?(indentation = 0)
138146
])
139147
|> array
140148

141-
let stringifyDetail ?(indentation = 0) (detail : docItemDetail) =
149+
let stringifyDetail ?(indentation = 0) ~originalEnv (detail : docItemDetail) =
142150
let open Protocol in
143151
match detail with
144152
| Record {fieldDocs} ->
@@ -155,6 +163,10 @@ let stringifyDetail ?(indentation = 0) (detail : docItemDetail) =
155163
( "docstrings",
156164
Some (stringifyDocstrings fieldDoc.docstrings) );
157165
("signature", Some (wrapInQuotes fieldDoc.signature));
166+
( "linkables",
167+
Some
168+
(stringifyLinkables ~indentation:(indentation + 1)
169+
~originalEnv fieldDoc.linkables) );
158170
])
159171
|> array) );
160172
]
@@ -175,6 +187,10 @@ let stringifyDetail ?(indentation = 0) (detail : docItemDetail) =
175187
Some (stringifyDocstrings constructorDoc.docstrings) );
176188
( "signature",
177189
Some (wrapInQuotes constructorDoc.signature) );
190+
( "linkables",
191+
Some
192+
(stringifyLinkables ~indentation:(indentation + 1)
193+
~originalEnv constructorDoc.linkables) );
178194
])
179195
|> array) );
180196
]
@@ -212,7 +228,9 @@ let rec stringifyDocItem ?(indentation = 0) ~originalEnv (item : docItem) =
212228
match detail with
213229
| None -> None
214230
| Some detail ->
215-
Some (stringifyDetail ~indentation:(indentation + 1) detail) );
231+
Some
232+
(stringifyDetail ~originalEnv ~indentation:(indentation + 1)
233+
detail) );
216234
]
217235
| Module m ->
218236
stringifyObject ~startOnNewline:true ~indentation
@@ -239,6 +257,49 @@ and stringifyDocsForModule ?(indentation = 0) ~originalEnv (d : docsForModule) =
239257
|> array) );
240258
]
241259

260+
let typeDetail typ ~env ~full =
261+
let open SharedTypes in
262+
match TypeUtils.extractTypeFromResolvedType ~env ~full typ with
263+
| Some (Trecord {fields}) ->
264+
Some
265+
(Record
266+
{
267+
fieldDocs =
268+
fields
269+
|> List.map (fun (field : field) ->
270+
{
271+
fieldName = field.fname.txt;
272+
docstrings = field.docstring;
273+
signature = Shared.typeToString field.typ;
274+
linkables =
275+
TypeExpr field.typ |> Linkables.findLinkables ~env ~full;
276+
});
277+
})
278+
| Some (Tvariant {constructors}) ->
279+
Some
280+
(Variant
281+
{
282+
constructorDocs =
283+
constructors
284+
|> List.map (fun (c : Constructor.t) ->
285+
let linkables =
286+
(match c.args with
287+
| Args args -> args |> List.map (fun (t, _) -> t)
288+
| InlineRecord fields ->
289+
fields |> List.map (fun f -> f.typ))
290+
|> List.map (fun t ->
291+
TypeExpr t |> Linkables.findLinkables ~env ~full)
292+
|> List.flatten
293+
in
294+
{
295+
constructorName = c.cname.txt;
296+
docstrings = c.docstring;
297+
signature = CompletionBackEnd.showConstructor c;
298+
linkables;
299+
});
300+
})
301+
| _ -> None
302+
242303
exception Invalid_file_type
243304

244305
let makeId modulePath ~identifier =
@@ -307,41 +368,7 @@ let extractDocs ~path ~debug =
307368
|> Shared.declToString item.name
308369
|> formatCode;
309370
name = item.name;
310-
detail =
311-
(match
312-
TypeUtils.extractTypeFromResolvedType ~env ~full
313-
typ
314-
with
315-
| Some (Trecord {fields}) ->
316-
Some
317-
(Record
318-
{
319-
fieldDocs =
320-
fields
321-
|> List.map (fun (field : field) ->
322-
{
323-
fieldName = field.fname.txt;
324-
docstrings = field.docstring;
325-
signature =
326-
Shared.typeToString field.typ;
327-
});
328-
})
329-
| Some (Tvariant {constructors}) ->
330-
Some
331-
(Variant
332-
{
333-
constructorDocs =
334-
constructors
335-
|> List.map (fun (c : Constructor.t) ->
336-
{
337-
constructorName = c.cname.txt;
338-
docstrings = c.docstring;
339-
signature =
340-
CompletionBackEnd
341-
.showConstructor c;
342-
});
343-
})
344-
| _ -> None);
371+
detail = typeDetail typ ~full ~env;
345372
})
346373
| Module (Structure m) ->
347374
(* module Whatever = {} in res or module Whatever: {} in resi. *)

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ extracting docs for src/DocExtractionRes.res
1818
"fieldDocs": [{
1919
"fieldName": "name",
2020
"docstrings": ["The name of the stuff."],
21-
"signature": "string"
21+
"signature": "string",
22+
"linkables": []
2223
}, {
2324
"fieldName": "online",
2425
"docstrings": ["Whether stuff is online."],
25-
"signature": "bool"
26+
"signature": "bool",
27+
"linkables": []
2628
}]
2729
}
2830
},
@@ -79,17 +81,25 @@ extracting docs for src/DocExtractionRes.res
7981
{
8082
"constructorName": "Started",
8183
"docstrings": ["If this is started or not"],
82-
"signature": "Started(t)"
84+
"signature": "Started(t)",
85+
"linkables": [{
86+
"linkId": "DocExtractionRes.t",
87+
"path": "t",
88+
"moduleName": "DocExtractionRes",
89+
"external": false
90+
}]
8391
},
8492
{
8593
"constructorName": "Stopped",
8694
"docstrings": ["Stopped?"],
87-
"signature": "Stopped"
95+
"signature": "Stopped",
96+
"linkables": []
8897
},
8998
{
9099
"constructorName": "Idle",
91100
"docstrings": ["Now idle."],
92-
"signature": "Idle"
101+
"signature": "Idle",
102+
"linkables": []
93103
}]
94104
}
95105
},
@@ -169,7 +179,8 @@ extracting docs for src/DocExtractionRes.res
169179
{
170180
"constructorName": "SomeStuff",
171181
"docstrings": ["This has inline records..."],
172-
"signature": "SomeStuff"
182+
"signature": "SomeStuff",
183+
"linkables": []
173184
}]
174185
}
175186
},

0 commit comments

Comments
 (0)