Skip to content

Commit f351f02

Browse files
committed
Print more variants of type_desc
1 parent e3ff020 commit f351f02

File tree

3 files changed

+140
-9
lines changed

3 files changed

+140
-9
lines changed

tools/bin/main.ml

+5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ let version = Version.version
3131

3232
let main () =
3333
match Sys.argv |> Array.to_list |> List.tl with
34+
| "temp" :: rest -> (
35+
match rest with
36+
| ["-h"] | ["--help"] -> logAndExit (Ok docHelp)
37+
| [path] -> logAndExit (Tools.dump ~entryPointFile:path ~debug:false)
38+
| _ -> logAndExit (Error docHelp))
3439
| "doc" :: rest -> (
3540
match rest with
3641
| ["-h"] | ["--help"] -> logAndExit (Ok docHelp)

tools/src/print_tast.ml

+76-8
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,65 @@ module Oak = struct
5050
| Tobject _ -> Ident "type_desc.Tobject"
5151
| Tfield _ -> Ident "type_desc.Tfield"
5252
| Tnil -> Ident "type_desc.Tnil"
53-
| Tlink {desc} -> Ident "type_desc.Tlink"
53+
| Tlink {desc} ->
54+
Application {name = "type_desc.Tlink"; argument = mk_type_desc desc}
5455
| Tsubst _ -> Ident "type_desc.Tsubst"
55-
| Tvariant row_descr -> Ident "type_desc.Tvariant"
56+
| Tvariant row_descr ->
57+
Application
58+
{name = "type_desc.Tvariant"; argument = mk_row_desc row_descr}
5659
| Tunivar _ -> Ident "type_desc.Tunivar"
5760
| Tpoly _ -> Ident "type_desc.Tpoly"
5861
| Tpackage _ -> Ident "type_desc.Tpackage"
62+
63+
and mk_row_desc (row_desc : Types.row_desc) : oak =
64+
let fields =
65+
[
66+
{
67+
name = "row_fields";
68+
value =
69+
( row_desc.row_fields
70+
|> List.map (fun (label, row_field) ->
71+
Tuple
72+
[
73+
{name = "label"; value = Ident label};
74+
{name = "row_field"; value = mk_row_field row_field};
75+
])
76+
|> fun ts -> List ts );
77+
};
78+
{name = "row_more"; value = mk_type_desc row_desc.row_more.desc};
79+
{name = "row_closed"; value = mk_bool row_desc.row_closed};
80+
{name = "row_fixed"; value = mk_bool row_desc.row_fixed};
81+
]
82+
in
83+
match row_desc.row_name with
84+
| None -> Record fields
85+
| Some (path, ts) ->
86+
Record
87+
({
88+
name = "row_name";
89+
value =
90+
Tuple
91+
[
92+
{name = "Path.t"; value = Ident (path_to_string path)};
93+
{
94+
name = "fields";
95+
value =
96+
List
97+
(ts
98+
|> List.map (fun (t : Types.type_expr) ->
99+
mk_type_desc t.desc));
100+
};
101+
];
102+
}
103+
:: fields)
104+
105+
and mk_row_field (row_field : Types.row_field) : oak =
106+
match row_field with
107+
| Rpresent _ -> Ident "row_field.Rpresent"
108+
| Reither _ -> Ident "row_field.Reither"
109+
| Rabsent -> Ident "row_field.Rabsent"
110+
111+
and mk_bool (b : bool) : oak = if b then Ident "true" else Ident "false"
59112
end
60113

61114
(** Transform the Oak types to string *)
@@ -116,7 +169,7 @@ module CodePrinter = struct
116169

117170
let sepNln ctx =
118171
{ctx with events = WriteLine :: ctx.events; current_line_column = 0}
119-
172+
let sepSpace ctx = !-" " ctx
120173
let sepComma ctx = !-", " ctx
121174
let sepSemi ctx = !-"; " ctx
122175
let sepOpenT ctx = !-"(" ctx
@@ -126,6 +179,7 @@ module CodePrinter = struct
126179
let sepOpenL ctx = !-"[" ctx
127180
let sepCloseL ctx = !-"]" ctx
128181
let sepEq ctx = !-" = " ctx
182+
let wrapInParentheses f = sepOpenT +> f +> sepCloseT
129183
let indent ctx =
130184
let nextIdent = ctx.current_indent + ctx.indent_size in
131185
{
@@ -197,14 +251,18 @@ module CodePrinter = struct
197251
in
198252
let long =
199253
!-(application.name) +> sepOpenT
200-
+> indentAndNln (genOak application.argument)
201-
+> sepNln +> sepCloseT
254+
+> (match application.argument with
255+
| Oak.List _ | Oak.Record _ -> genOak application.argument
256+
| _ -> indentAndNln (genOak application.argument) +> sepNln)
257+
+> sepCloseT
202258
in
203259
expressionFitsOnRestOfLine short long
204260

205261
and genRecord (recordFields : Oak.namedField list) : appendEvents =
206262
let short =
207-
sepOpenR +> col genNamedField sepSemi recordFields +> sepCloseR
263+
sepOpenR +> sepSpace
264+
+> col genNamedField sepSemi recordFields
265+
+> sepSpace +> sepCloseR
208266
in
209267
let long =
210268
sepOpenR
@@ -232,9 +290,19 @@ module CodePrinter = struct
232290
expressionFitsOnRestOfLine short long
233291

234292
and genList (items : Oak.oak list) : appendEvents =
235-
let short = sepOpenL +> col genOak sepSemi items +> sepCloseL in
293+
let genItem = function
294+
| Oak.Tuple _ as item -> wrapInParentheses (genOak item)
295+
| item -> genOak item
296+
in
297+
let short =
298+
match items with
299+
| [] -> sepOpenL +> sepCloseL
300+
| _ ->
301+
sepOpenL +> sepSpace +> col genItem sepSemi items +> sepSpace
302+
+> sepCloseL
303+
in
236304
let long =
237-
sepOpenL +> indentAndNln (col genOak sepNln items) +> sepNln +> sepCloseL
305+
sepOpenL +> indentAndNln (col genItem sepNln items) +> sepNln +> sepCloseL
238306
in
239307
expressionFitsOnRestOfLine short long
240308
end

tools/src/tools.ml

+59-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,6 @@ let typeDetail typ ~env ~full =
322322

323323
let valueDetail (item : SharedTypes.Module.item) (typ : Types.type_expr) =
324324
let s = Print_tast.print_type_expr typ in
325-
Format.printf "%s\n" s;
326325
Some (Signature {parameters = []; returnType = s})
327326

328327
let makeId modulePath ~identifier =
@@ -337,6 +336,65 @@ let getSource ~rootPath ({loc_start} : Location.t) =
337336
in
338337
{filepath; line = line + 1; col = col + 1}
339338

339+
let dump ~entryPointFile ~debug =
340+
let path =
341+
match Filename.is_relative entryPointFile with
342+
| true -> Unix.realpath entryPointFile
343+
| false -> entryPointFile
344+
in
345+
if debug then Printf.printf "extracting docs for %s\n" path;
346+
let result =
347+
match
348+
FindFiles.isImplementation path = false
349+
&& FindFiles.isInterface path = false
350+
with
351+
| false -> (
352+
let path =
353+
if FindFiles.isImplementation path then
354+
let pathAsResi =
355+
(path |> Filename.dirname) ^ "/"
356+
^ (path |> Filename.basename |> Filename.chop_extension)
357+
^ ".resi"
358+
in
359+
if Sys.file_exists pathAsResi then (
360+
if debug then
361+
Printf.printf "preferring found resi file for impl: %s\n"
362+
pathAsResi;
363+
pathAsResi)
364+
else path
365+
else path
366+
in
367+
match Cmt.loadFullCmtFromPath ~path with
368+
| None ->
369+
Error
370+
(Printf.sprintf
371+
"error: failed to generate doc for %s, try to build the project"
372+
path)
373+
| Some full ->
374+
let file = full.file in
375+
let structure = file.structure in
376+
let open SharedTypes in
377+
let extractDocsForModule (structure : Module.structure) =
378+
structure.items
379+
|> List.filter_map (fun (item : Module.item) ->
380+
match item.kind with
381+
| Value typ -> (
382+
match valueDetail item typ with
383+
| Some (Signature {returnType = rt}) -> Some rt
384+
| _ -> None)
385+
| _ -> None)
386+
|> String.concat "\n"
387+
in
388+
let docs = extractDocsForModule structure in
389+
Ok docs)
390+
| true ->
391+
Error
392+
(Printf.sprintf
393+
"error: failed to read %s, expected an .res or .resi file" path)
394+
in
395+
396+
result
397+
340398
let extractDocs ~entryPointFile ~debug =
341399
let path =
342400
match Filename.is_relative entryPointFile with

0 commit comments

Comments
 (0)