Skip to content

Commit 312eee0

Browse files
committed
Add dump command to view full file
1 parent 4d596d8 commit 312eee0

File tree

3 files changed

+152
-23
lines changed

3 files changed

+152
-23
lines changed

tools/bin/main.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ let version = Version.version
3131

3232
let main () =
3333
match Sys.argv |> Array.to_list |> List.tl with
34+
| ["dump"; file] -> Tools.dump file |> logAndExit
3435
| "doc" :: rest -> (
3536
match rest with
3637
| ["-h"] | ["--help"] -> logAndExit (Ok docHelp)

tools/src/print_tast.ml

Lines changed: 111 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
open Analysis
2+
13
(** Transform the AST types to the more generic Oak format *)
24
module Oak = struct
35
type application = {name: string; argument: oak}
@@ -10,12 +12,39 @@ module Oak = struct
1012
| Ident of string
1113
| Tuple of namedField list
1214
| List of oak list
15+
| String of string
16+
let mk_bool (b : bool) : oak = if b then Ident "true" else Ident "false"
17+
18+
let mk_string_option (o : string option) : oak =
19+
match o with
20+
| None -> Ident "None"
21+
| Some s -> Application {name = "Some"; argument = String s}
22+
23+
let mk_string_list (items : string list) : oak =
24+
List (items |> List.map (fun s -> String s))
25+
26+
let path_to_string path =
27+
let buf = Buffer.create 64 in
28+
let rec aux = function
29+
| Path.Pident id -> Buffer.add_string buf (Ident.name id)
30+
| Path.Pdot (p, s, _) ->
31+
aux p;
32+
Buffer.add_char buf '.';
33+
Buffer.add_string buf s
34+
| Path.Papply (p1, p2) ->
35+
aux p1;
36+
Buffer.add_char buf '(';
37+
aux p2;
38+
Buffer.add_char buf ')'
39+
in
40+
aux path;
41+
Buffer.contents buf
1342

14-
let rec path_to_string = function
15-
| Path.Pident id -> Ident.name id
16-
| Path.Pdot (p, s, _) -> path_to_string p ^ "." ^ s
17-
| Path.Papply (p1, p2) -> path_to_string p1 ^ "(" ^ path_to_string p2 ^ ")"
18-
43+
let mk_row_field (row_field : Types.row_field) : oak =
44+
match row_field with
45+
| Rpresent _ -> Ident "row_field.Rpresent"
46+
| Reither _ -> Ident "row_field.Reither"
47+
| Rabsent -> Ident "row_field.Rabsent"
1948
let rec mk_type_desc (desc : Types.type_desc) : oak =
2049
match desc with
2150
| Tvar var -> (
@@ -103,13 +132,57 @@ module Oak = struct
103132
}
104133
:: fields)
105134

106-
and mk_row_field (row_field : Types.row_field) : oak =
107-
match row_field with
108-
| Rpresent _ -> Ident "row_field.Rpresent"
109-
| Reither _ -> Ident "row_field.Reither"
110-
| Rabsent -> Ident "row_field.Rabsent"
135+
let mk_package (package : SharedTypes.package) : oak =
136+
Record
137+
[
138+
{
139+
name = "genericJsxModule";
140+
value = mk_string_option package.genericJsxModule;
141+
};
142+
]
111143

112-
and mk_bool (b : bool) : oak = if b then Ident "true" else Ident "false"
144+
let mk_Uri (uri : Uri.t) : oak = String (Uri.toString uri)
145+
146+
let mk_item (item : SharedTypes.Module.item) : oak =
147+
let kind =
148+
match item.kind with
149+
| SharedTypes.Module.Value v ->
150+
Application
151+
{name = "SharedTypes.Module.Value"; argument = mk_type_desc v.desc}
152+
| SharedTypes.Module.Type _ -> Ident "Type"
153+
| SharedTypes.Module.Module _ -> Ident "Module"
154+
in
155+
Record
156+
[
157+
{name = "kind"; value = kind};
158+
{name = "name"; value = String item.name};
159+
{name = "docstring"; value = mk_string_list item.docstring};
160+
{name = "deprecated"; value = mk_string_option item.deprecated};
161+
]
162+
163+
let mk_structure (structure : SharedTypes.Module.structure) : oak =
164+
Record
165+
[
166+
{name = "name"; value = String structure.name};
167+
{name = "docstring"; value = mk_string_list structure.docstring};
168+
{name = "items"; value = List (List.map mk_item structure.items)};
169+
{name = "deprecated"; value = mk_string_option structure.deprecated};
170+
]
171+
172+
let mk_file (file : SharedTypes.File.t) : oak =
173+
Record
174+
[
175+
{name = "uri"; value = mk_Uri file.uri};
176+
{name = "moduleName"; value = String file.moduleName};
177+
{name = "structure"; value = mk_structure file.structure};
178+
]
179+
180+
let mk_full (full : SharedTypes.full) : oak =
181+
Record
182+
[
183+
{name = "package"; value = mk_package full.package};
184+
{name = "file"; value = mk_file full.file};
185+
]
113186
end
114187

115188
(** Transform the Oak types to string *)
@@ -231,23 +304,31 @@ module CodePrinter = struct
231304

232305
(** Fold all the events in context into text *)
233306
let dump (ctx : context) =
234-
let addSpaces n = String.make n ' ' in
307+
let buf = Buffer.create 1024 in
308+
let addSpaces n = Buffer.add_string buf (String.make n ' ') in
235309

236310
List.fold_right
237-
(fun event (acc, current_indent) ->
311+
(fun event current_indent ->
238312
match event with
239-
| Write str -> (acc ^ str, current_indent)
240-
| WriteLine -> (acc ^ "\n" ^ addSpaces current_indent, current_indent)
241-
| IndentBy n -> (acc, current_indent + n)
242-
| UnindentBy n -> (acc, current_indent - n))
243-
ctx.events ("", 0)
244-
|> fst
313+
| Write str ->
314+
Buffer.add_string buf str;
315+
current_indent
316+
| WriteLine ->
317+
Buffer.add_char buf '\n';
318+
addSpaces current_indent;
319+
current_indent
320+
| IndentBy n -> current_indent + n
321+
| UnindentBy n -> current_indent - n)
322+
ctx.events ctx.current_indent
323+
|> ignore;
324+
Buffer.contents buf
245325

246326
let rec genOak (oak : Oak.oak) : appendEvents =
247327
match oak with
248328
| Oak.Application application -> genApplication application
249329
| Oak.Record record -> genRecord record
250330
| Oak.Ident ident -> genIdent ident
331+
| Oak.String str -> !-(Format.sprintf "\"%s\"" str)
251332
| Oak.Tuple ts -> genTuple ts
252333
| Oak.List xs -> genList xs
253334

@@ -268,9 +349,12 @@ module CodePrinter = struct
268349

269350
and genRecord (recordFields : Oak.namedField list) : appendEvents =
270351
let short =
271-
sepOpenR +> sepSpace
272-
+> col genNamedField sepSemi recordFields
273-
+> sepSpace +> sepCloseR
352+
match recordFields with
353+
| [] -> sepOpenR +> sepCloseR
354+
| fields ->
355+
sepOpenR +> sepSpace
356+
+> col genNamedField sepSemi fields
357+
+> sepSpace +> sepCloseR
274358
in
275359
let long =
276360
sepOpenR
@@ -292,7 +376,7 @@ module CodePrinter = struct
292376
!-(field.name) +> sepEq
293377
+>
294378
match field.value with
295-
| Oak.List _ -> genOak field.value
379+
| Oak.List _ | Oak.Record _ -> genOak field.value
296380
| _ -> indentAndNln (genOak field.value)
297381
in
298382
expressionFitsOnRestOfLine short long
@@ -318,3 +402,7 @@ end
318402
let print_type_expr (typ : Types.type_expr) : string =
319403
CodePrinter.genOak (Oak.mk_type_desc typ.desc) CodePrinter.emptyContext
320404
|> CodePrinter.dump
405+
406+
let print_full (full : SharedTypes.full) : string =
407+
CodePrinter.genOak (Oak.mk_full full) CodePrinter.emptyContext
408+
|> CodePrinter.dump

tools/src/tools.ml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,3 +640,43 @@ let extractEmbedded ~extensionPoints ~filename =
640640
("loc", Some (Analysis.Utils.cmtLocToRange loc |> stringifyRange));
641641
])
642642
|> List.rev |> array
643+
644+
let dump entryPointFile =
645+
let path =
646+
match Filename.is_relative entryPointFile with
647+
| true -> Unix.realpath entryPointFile
648+
| false -> entryPointFile
649+
in
650+
let result =
651+
match
652+
FindFiles.isImplementation path = false
653+
&& FindFiles.isInterface path = false
654+
with
655+
| false -> (
656+
let path =
657+
if FindFiles.isImplementation path then
658+
let pathAsResi =
659+
(path |> Filename.dirname) ^ "/"
660+
^ (path |> Filename.basename |> Filename.chop_extension)
661+
^ ".resi"
662+
in
663+
if Sys.file_exists pathAsResi then (
664+
Printf.printf "preferring found resi file for impl: %s\n" pathAsResi;
665+
pathAsResi)
666+
else path
667+
else path
668+
in
669+
match Cmt.loadFullCmtFromPath ~path with
670+
| None ->
671+
Error
672+
(Printf.sprintf
673+
"error: failed to generate doc for %s, try to build the project"
674+
path)
675+
| Some full -> Ok (Print_tast.print_full full))
676+
| true ->
677+
Error
678+
(Printf.sprintf
679+
"error: failed to read %s, expected an .res or .resi file" path)
680+
in
681+
682+
result

0 commit comments

Comments
 (0)