-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathDocExtraction.ml
346 lines (335 loc) · 12 KB
/
DocExtraction.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
type fieldDoc = {
fieldName: string;
docstrings: string list;
signature: string;
optional: bool;
deprecated: string option;
}
type constructorDoc = {
constructorName: string;
docstrings: string list;
signature: string;
deprecated: string option;
inlineRecordFields: fieldDoc list option;
}
type docItemDetail =
| Record of {fieldDocs: fieldDoc list}
| Variant of {constructorDocs: constructorDoc list}
type docItem =
| Value of {
id: string;
docstring: string list;
signature: string;
name: string;
deprecated: string option;
}
| Type of {
id: string;
docstring: string list;
signature: string;
name: string;
deprecated: string option;
detail: docItemDetail option;
(** Additional documentation for constructors and record fields, if available. *)
}
| Module of docsForModule
| ModuleAlias of {
id: string;
docstring: string list;
name: string;
items: docItem list;
}
and docsForModule = {
id: string;
docstring: string list;
deprecated: string option;
name: string;
items: docItem list;
}
let stringifyDocstrings docstrings =
let open Protocol in
docstrings
|> List.map (fun docstring -> docstring |> String.trim |> wrapInQuotes)
|> array
let stringifyFieldDoc ~indentation (fieldDoc : fieldDoc) =
let open Protocol in
stringifyObject ~indentation:(indentation + 1)
[
("name", Some (wrapInQuotes fieldDoc.fieldName));
( "deprecated",
match fieldDoc.deprecated with
| Some d -> Some (wrapInQuotes d)
| None -> None );
("optional", Some (string_of_bool fieldDoc.optional));
("docstrings", Some (stringifyDocstrings fieldDoc.docstrings));
("signature", Some (wrapInQuotes fieldDoc.signature));
]
let stringifyDetail ?(indentation = 0) (detail : docItemDetail) =
let open Protocol in
match detail with
| Record {fieldDocs} ->
stringifyObject ~startOnNewline:true ~indentation
[
("kind", Some (wrapInQuotes "record"));
( "items",
Some (fieldDocs |> List.map (stringifyFieldDoc ~indentation) |> array)
);
]
| Variant {constructorDocs} ->
stringifyObject ~startOnNewline:true ~indentation
[
("kind", Some (wrapInQuotes "variant"));
( "items",
Some
(constructorDocs
|> List.map (fun constructorDoc ->
stringifyObject ~startOnNewline:true
~indentation:(indentation + 1)
[
( "name",
Some (wrapInQuotes constructorDoc.constructorName) );
( "deprecated",
match constructorDoc.deprecated with
| Some d -> Some (wrapInQuotes d)
| None -> None );
( "docstrings",
Some (stringifyDocstrings constructorDoc.docstrings) );
( "signature",
Some (wrapInQuotes constructorDoc.signature) );
( "inlineRecordFields",
match constructorDoc.inlineRecordFields with
| None -> None
| Some fieldDocs ->
Some
(fieldDocs
|> List.map
(stringifyFieldDoc
~indentation:(indentation + 1))
|> array) );
])
|> array) );
]
let rec stringifyDocItem ?(indentation = 0) ~originalEnv (item : docItem) =
let open Protocol in
match item with
| Value {id; docstring; signature; name; deprecated} ->
stringifyObject ~startOnNewline:true ~indentation
[
("id", Some (wrapInQuotes id));
("kind", Some (wrapInQuotes "value"));
("name", Some (name |> Json.escape |> wrapInQuotes));
( "deprecated",
match deprecated with
| Some d -> Some (wrapInQuotes d)
| None -> None );
( "signature",
Some (signature |> String.trim |> Json.escape |> wrapInQuotes) );
("docstrings", Some (stringifyDocstrings docstring));
]
| Type {id; docstring; signature; name; deprecated; detail} ->
stringifyObject ~startOnNewline:true ~indentation
[
("id", Some (wrapInQuotes id));
("kind", Some (wrapInQuotes "type"));
("name", Some (name |> Json.escape |> wrapInQuotes));
( "deprecated",
match deprecated with
| Some d -> Some (wrapInQuotes d)
| None -> None );
("signature", Some (signature |> Json.escape |> wrapInQuotes));
("docstrings", Some (stringifyDocstrings docstring));
( "detail",
match detail with
| None -> None
| Some detail ->
Some (stringifyDetail ~indentation:(indentation + 1) detail) );
]
| Module m ->
stringifyObject ~startOnNewline:true ~indentation
[
("id", Some (wrapInQuotes m.id));
("name", Some (wrapInQuotes m.name));
("kind", Some (wrapInQuotes "module"));
( "items",
Some
(m.items
|> List.map
(stringifyDocItem ~originalEnv ~indentation:(indentation + 1))
|> array) );
]
| ModuleAlias m ->
stringifyObject ~startOnNewline:true ~indentation
[
("id", Some (wrapInQuotes m.id));
("kind", Some (wrapInQuotes "moduleAlias"));
("name", Some (wrapInQuotes m.name));
("docstrings", Some (stringifyDocstrings m.docstring));
( "items",
Some
(m.items
|> List.map
(stringifyDocItem ~originalEnv ~indentation:(indentation + 1))
|> array) );
]
and stringifyDocsForModule ?(indentation = 0) ~originalEnv (d : docsForModule) =
let open Protocol in
stringifyObject ~startOnNewline:true ~indentation
[
("name", Some (wrapInQuotes d.name));
( "deprecated",
match d.deprecated with
| Some d -> Some (wrapInQuotes d)
| None -> None );
("docstrings", Some (stringifyDocstrings d.docstring));
( "items",
Some
(d.items
|> List.map
(stringifyDocItem ~originalEnv ~indentation:(indentation + 1))
|> array) );
]
let fieldToFieldDoc (field : SharedTypes.field) : fieldDoc =
{
fieldName = field.fname.txt;
docstrings = field.docstring;
optional = field.optional;
signature = Shared.typeToString field.typ;
deprecated = field.deprecated;
}
let typeDetail typ ~env ~full =
let open SharedTypes in
match TypeUtils.extractTypeFromResolvedType ~env ~full typ with
| Some (Trecord {fields}) ->
Some (Record {fieldDocs = fields |> List.map fieldToFieldDoc})
| Some (Tvariant {constructors}) ->
Some
(Variant
{
constructorDocs =
constructors
|> List.map (fun (c : Constructor.t) ->
{
constructorName = c.cname.txt;
docstrings = c.docstring;
signature = CompletionBackEnd.showConstructor c;
deprecated = c.deprecated;
inlineRecordFields =
(match c.args with
| InlineRecord fields ->
Some (fields |> List.map fieldToFieldDoc)
| _ -> None);
});
})
| _ -> None
let makeId modulePath ~identifier =
identifier :: modulePath |> List.rev |> SharedTypes.ident
let extractDocs ~path ~debug =
if debug then Printf.printf "extracting docs for %s\n" path;
if
FindFiles.isImplementation path = false
&& FindFiles.isInterface path = false
then (
Printf.eprintf "error: failed to read %s, expected an .res or .resi file\n"
path;
exit 1);
let path =
if FindFiles.isImplementation path then
let pathAsResi =
(path |> Filename.dirname) ^ "/"
^ (path |> Filename.basename |> Filename.chop_extension)
^ ".resi"
in
if Sys.file_exists pathAsResi then (
if debug then
Printf.printf "preferring found resi file for impl: %s\n" pathAsResi;
pathAsResi)
else path
else path
in
match Cmt.loadFullCmtFromPath ~path with
| None ->
Printf.eprintf
"error: failed to generate doc for %s, try to build the project\n" path;
exit 1
| Some full ->
let file = full.file in
let structure = file.structure in
let open SharedTypes in
let env = QueryEnv.fromFile file in
let rec extractDocsForModule ?(modulePath = [env.file.moduleName])
(structure : Module.structure) =
{
id = modulePath |> List.rev |> ident;
docstring = structure.docstring |> List.map String.trim;
name = structure.name;
deprecated = structure.deprecated;
items =
structure.items
|> List.filter_map (fun (item : Module.item) ->
match item.kind with
| Value typ ->
Some
(Value
{
id = modulePath |> makeId ~identifier:item.name;
docstring = item.docstring |> List.map String.trim;
signature =
"let " ^ item.name ^ ": " ^ Shared.typeToString typ;
name = item.name;
deprecated = item.deprecated;
})
| Type (typ, _) ->
Some
(Type
{
id = modulePath |> makeId ~identifier:item.name;
docstring = item.docstring |> List.map String.trim;
signature = typ.decl |> Shared.declToString item.name;
name = item.name;
deprecated = item.deprecated;
detail = typeDetail typ ~full ~env;
})
| Module (Ident p) ->
(* module Whatever = OtherModule *)
let aliasToModule = p |> pathIdentToString in
let id =
(modulePath |> List.rev |> List.hd) ^ "." ^ item.name
in
let items, internalDocstrings =
match
ProcessCmt.fileForModule ~package:full.package
aliasToModule
with
| None -> ([], [])
| Some file ->
let docs =
extractDocsForModule ~modulePath:[id] file.structure
in
(docs.items, docs.docstring)
in
Some
(ModuleAlias
{
id;
name = item.name;
items;
docstring = item.docstring @ internalDocstrings |> List.map String.trim;
})
| Module (Structure m) ->
(* module Whatever = {} in res or module Whatever: {} in resi. *)
Some
(Module
(extractDocsForModule ~modulePath:(m.name :: modulePath)
m))
| Module (Constraint (Structure _impl, Structure interface)) ->
(* module Whatever: { <interface> } = { <impl> }. Prefer the interface. *)
Some
(Module
(extractDocsForModule
~modulePath:(interface.name :: modulePath)
interface))
| _ -> None);
}
in
let docs = extractDocsForModule structure in
print_endline (stringifyDocsForModule ~originalEnv:env docs)