-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathtools.ml
563 lines (541 loc) · 20 KB
/
tools.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
open Analysis
type fieldDoc = {
fieldName: string;
docstrings: string list;
signature: string;
optional: bool;
deprecated: string option;
}
type constructorPayload = InlineRecord of {fieldDocs: fieldDoc list}
type constructorDoc = {
constructorName: string;
docstrings: string list;
signature: string;
deprecated: string option;
items: constructorPayload option;
}
type source = {filepath: string; line: int; col: int}
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;
source: source;
}
| Type of {
id: string;
docstring: string list;
signature: string;
name: string;
deprecated: string option;
detail: docItemDetail option;
source: source;
(** Additional documentation for constructors and record fields, if available. *)
}
| Module of docsForModule
| ModuleType of {
id: string;
docstring: string list;
deprecated: string option;
name: string;
source: source;
items: docItem list;
}
| ModuleAlias of {
id: string;
docstring: string list;
name: string;
source: source;
items: docItem list;
}
and docsForModule = {
id: string;
docstring: string list;
deprecated: string option;
name: string;
moduletypeid: string option;
source: source;
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 stringifyConstructorPayload ~indentation
(constructorPayload : constructorPayload) =
let open Protocol in
match constructorPayload with
| InlineRecord {fieldDocs} ->
stringifyObject ~indentation:(indentation + 1)
[
("kind", Some (wrapInQuotes "inlineRecord"));
( "fields",
Some
(fieldDocs
|> List.map (stringifyFieldDoc ~indentation:(indentation + 1))
|> array) );
]
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) );
( "payload",
match constructorDoc.items with
| None -> None
| Some constructorPayload ->
Some
(stringifyConstructorPayload
~indentation:(indentation + 1)
constructorPayload) );
])
|> array) );
]
let stringifySource ~indentation source =
let open Protocol in
stringifyObject ~startOnNewline:false ~indentation
[
("filepath", Some (source.filepath |> wrapInQuotes));
("line", Some (source.line |> string_of_int));
("col", Some (source.col |> string_of_int));
]
let rec stringifyDocItem ?(indentation = 0) ~originalEnv (item : docItem) =
let open Protocol in
match item with
| Value {id; docstring; signature; name; deprecated; source} ->
stringifyObject ~startOnNewline:true ~indentation
[
("id", Some (wrapInQuotes id));
("kind", Some (wrapInQuotes "value"));
("name", Some (name |> wrapInQuotes));
( "deprecated",
match deprecated with
| Some d -> Some (wrapInQuotes d)
| None -> None );
("signature", Some (signature |> String.trim |> wrapInQuotes));
("docstrings", Some (stringifyDocstrings docstring));
("source", Some (stringifySource ~indentation:(indentation + 1) source));
]
| Type {id; docstring; signature; name; deprecated; detail; source} ->
stringifyObject ~startOnNewline:true ~indentation
[
("id", Some (wrapInQuotes id));
("kind", Some (wrapInQuotes "type"));
("name", Some (name |> wrapInQuotes));
( "deprecated",
match deprecated with
| Some d -> Some (wrapInQuotes d)
| None -> None );
("signature", Some (signature |> wrapInQuotes));
("docstrings", Some (stringifyDocstrings docstring));
("source", Some (stringifySource ~indentation:(indentation + 1) source));
( "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"));
( "deprecated",
match m.deprecated with
| Some d -> Some (wrapInQuotes d)
| None -> None );
( "moduletypeid",
match m.moduletypeid with
| Some path -> Some (wrapInQuotes path)
| None -> None );
("docstrings", Some (stringifyDocstrings m.docstring));
( "source",
Some (stringifySource ~indentation:(indentation + 1) m.source) );
( "items",
Some
(m.items
|> List.map
(stringifyDocItem ~originalEnv ~indentation:(indentation + 1))
|> array) );
]
| ModuleType m ->
stringifyObject ~startOnNewline:true ~indentation
[
("id", Some (wrapInQuotes m.id));
("name", Some (wrapInQuotes m.name));
("kind", Some (wrapInQuotes "moduleType"));
( "deprecated",
match m.deprecated with
| Some d -> Some (wrapInQuotes d)
| None -> None );
("docstrings", Some (stringifyDocstrings m.docstring));
( "source",
Some (stringifySource ~indentation:(indentation + 1) m.source) );
( "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));
( "source",
Some (stringifySource ~indentation:(indentation + 1) m.source) );
( "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));
("source", Some (stringifySource ~indentation:(indentation + 1) d.source));
( "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;
items =
(match c.args with
| InlineRecord fields ->
Some
(InlineRecord
{fieldDocs = fields |> List.map fieldToFieldDoc})
| _ -> None);
});
})
| _ -> None
let makeId modulePath ~identifier =
identifier :: modulePath |> List.rev |> SharedTypes.ident
let getSource ~rootPath ({loc_start} : Location.t) =
let line, col = Pos.ofLexing loc_start in
let filepath =
Files.relpath rootPath loc_start.pos_fname
|> Files.split Filename.dir_sep
|> String.concat "/"
in
{filepath; line = line + 1; col = col + 1}
let extractDocs ~entryPointFile ~debug =
let path =
match Filename.is_relative entryPointFile with
| true -> Unix.realpath entryPointFile
| false -> entryPointFile
in
if debug then Printf.printf "extracting docs for %s\n" path;
let result =
match
FindFiles.isImplementation path = false
&& FindFiles.isInterface path = false
with
| false -> (
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 ->
Error
(Printf.sprintf
"error: failed to generate doc for %s, try to build the project"
path)
| Some full ->
let file = full.file in
let structure = file.structure in
let rootPath = full.package.rootPath 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;
moduletypeid = None;
deprecated = structure.deprecated;
source =
{
filepath =
(match rootPath = "." with
| true -> file.uri |> Uri.toPath
| false ->
Files.relpath rootPath (file.uri |> Uri.toPath)
|> Files.split Filename.dir_sep
|> String.concat "/");
line = 1;
col = 1;
};
items =
structure.items
|> List.filter_map (fun (item : Module.item) ->
let source = getSource ~rootPath item.loc in
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;
source;
})
| 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;
source;
})
| Module {type_ = Ident p; isModuleType = false} ->
(* 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;
source;
items;
docstring =
item.docstring @ internalDocstrings
|> List.map String.trim;
})
| Module {type_ = Structure m; isModuleType = false} ->
(* module Whatever = {} in res or module Whatever: {} in resi. *)
let modulePath = m.name :: modulePath in
let docs = extractDocsForModule ~modulePath m in
Some
(Module
{
id = modulePath |> List.rev |> ident;
name = m.name;
moduletypeid = None;
docstring = item.docstring @ m.docstring;
deprecated = item.deprecated;
source;
items = docs.items;
})
| Module {type_ = Structure m; isModuleType = true} ->
(* module type Whatever = {} *)
let modulePath = m.name :: modulePath in
let docs = extractDocsForModule ~modulePath m in
Some
(ModuleType
{
id = modulePath |> List.rev |> ident;
name = m.name;
docstring = item.docstring @ m.docstring;
deprecated = item.deprecated;
source;
items = docs.items;
})
| Module
{
type_ =
Constraint (Structure _impl, Structure interface);
} ->
(* module Whatever: { <interface> } = { <impl> }. Prefer the interface. *)
Some
(Module
(extractDocsForModule
~modulePath:(interface.name :: modulePath)
interface))
| Module {type_ = Constraint (Structure m, Ident p)} ->
(* module M: T = { <impl> }. Print M *)
let docs =
extractDocsForModule ~modulePath:(m.name :: modulePath)
m
in
let identModulePath = p |> Path.head |> Ident.name in
let moduleTypeIdPath =
match
ProcessCmt.fileForModule ~package:full.package
identModulePath
|> Option.is_none
with
| false -> []
| true -> [modulePath |> List.rev |> List.hd]
in
Some
(Module
{
docs with
moduletypeid =
Some
(makeId ~identifier:(Path.name p)
moduleTypeIdPath);
})
| _ -> None);
}
in
let docs = extractDocsForModule structure in
Ok (stringifyDocsForModule ~originalEnv:env docs))
| true ->
Error
(Printf.sprintf
"error: failed to read %s, expected an .res or .resi file" path)
in
result
let extractEmbedded ~extensionPoints ~filename =
let {Res_driver.parsetree = structure} =
Res_driver.parsing_engine.parse_implementation ~for_printer:false ~filename
in
let content = ref [] in
let append item = content := item :: !content in
let extension (iterator : Ast_iterator.iterator) (ext : Parsetree.extension) =
(match ext with
| ( {txt},
PStr
[
{
pstr_desc =
Pstr_eval
( {
pexp_loc;
pexp_desc = Pexp_constant (Pconst_string (contents, _));
},
_ );
};
] )
when extensionPoints |> List.exists (fun v -> v = txt) ->
append (pexp_loc, txt, contents)
| _ -> ());
Ast_iterator.default_iterator.extension iterator ext
in
let iterator = {Ast_iterator.default_iterator with extension} in
iterator.structure iterator structure;
let open Analysis.Protocol in
!content
|> List.map (fun (loc, extensionName, contents) ->
stringifyObject
[
("extensionName", Some (wrapInQuotes extensionName));
("contents", Some (wrapInQuotes contents));
("loc", Some (Analysis.Utils.cmtLocToRange loc |> stringifyRange));
])
|> List.rev |> array