Skip to content

Commit 60e0f13

Browse files
committed
some refactor
1 parent a187e44 commit 60e0f13

File tree

2 files changed

+133
-124
lines changed

2 files changed

+133
-124
lines changed

tools/bin/main.ml

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,39 @@ reanalyze Reanalyze
1919
-v, --version Print version
2020
-h, --help Print help|}
2121

22-
type exitCode = [`Ok | `Error]
23-
24-
let logAndExit ~log ~code =
25-
print_endline log |> ignore;
26-
let code =
27-
match code with
28-
| `Ok -> 0
29-
| `Error -> 1
30-
in
31-
exit code
22+
let logAndExit = function
23+
| Ok log ->
24+
Printf.printf "%s\n" log;
25+
exit 0
26+
| Error log ->
27+
Printf.eprintf "%s\n" log;
28+
exit 1
3229

3330
let version = Version.version
3431

3532
let main () =
3633
match Sys.argv |> Array.to_list |> List.tl with
3734
| "doc" :: rest -> (
3835
match rest with
39-
| ["-h"] | ["--help"] -> logAndExit ~log:docHelp ~code:`Ok
36+
| ["-h"] | ["--help"] -> logAndExit (Ok docHelp)
4037
| [path] ->
4138
(* NOTE: Internal use to generate docs from compiler *)
4239
let () =
4340
match Sys.getenv_opt "FROM_COMPILER" with
4441
| Some "true" -> Analysis.Cfg.isDocGenFromCompiler := true
4542
| _ -> ()
4643
in
47-
Tools.extractDocs ~path ~debug:false
48-
| _ -> logAndExit ~log:docHelp ~code:`Error)
44+
logAndExit (Tools.extractDocs ~path ~debug:false)
45+
| _ -> logAndExit (Error docHelp))
4946
| "reanalyze" :: _ ->
5047
let len = Array.length Sys.argv in
5148
for i = 1 to len - 2 do
5249
Sys.argv.(i) <- Sys.argv.(i + 1)
5350
done;
5451
Sys.argv.(len - 1) <- "";
5552
Reanalyze.cli ()
56-
| ["-h"] | ["--help"] -> logAndExit ~log:help ~code:`Ok
57-
| ["-v"] | ["--version"] -> logAndExit ~log:version ~code:`Ok
58-
| _ -> logAndExit ~log:help ~code:`Error
53+
| ["-h"] | ["--help"] -> logAndExit (Ok help)
54+
| ["-v"] | ["--version"] -> logAndExit (Ok version)
55+
| _ -> logAndExit (Error help)
5956

6057
let () = main ()

tools/src/tools.ml

Lines changed: 120 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -225,113 +225,125 @@ let makeId modulePath ~identifier =
225225

226226
let extractDocs ~path ~debug =
227227
if debug then Printf.printf "extracting docs for %s\n" path;
228-
if
229-
FindFiles.isImplementation path = false
230-
&& FindFiles.isInterface path = false
231-
then (
232-
Printf.eprintf "error: failed to read %s, expected an .res or .resi file\n"
233-
path;
234-
exit 1);
235-
let path =
236-
if FindFiles.isImplementation path then
237-
let pathAsResi =
238-
(path |> Filename.dirname) ^ "/"
239-
^ (path |> Filename.basename |> Filename.chop_extension)
240-
^ ".resi"
228+
let result =
229+
match
230+
FindFiles.isImplementation path = false
231+
&& FindFiles.isInterface path = false
232+
with
233+
| false -> (
234+
let path =
235+
if FindFiles.isImplementation path then
236+
let pathAsResi =
237+
(path |> Filename.dirname) ^ "/"
238+
^ (path |> Filename.basename |> Filename.chop_extension)
239+
^ ".resi"
240+
in
241+
if Sys.file_exists pathAsResi then (
242+
if debug then
243+
Printf.printf "preferring found resi file for impl: %s\n"
244+
pathAsResi;
245+
pathAsResi)
246+
else path
247+
else path
241248
in
242-
if Sys.file_exists pathAsResi then (
243-
if debug then
244-
Printf.printf "preferring found resi file for impl: %s\n" pathAsResi;
245-
pathAsResi)
246-
else path
247-
else path
248-
in
249-
match Cmt.loadFullCmtFromPath ~path with
250-
| None ->
251-
Printf.eprintf
252-
"error: failed to generate doc for %s, try to build the project\n" path;
253-
exit 1
254-
| Some full ->
255-
let file = full.file in
256-
let structure = file.structure in
257-
let open SharedTypes in
258-
let env = QueryEnv.fromFile file in
259-
let rec extractDocsForModule ?(modulePath = [env.file.moduleName])
260-
(structure : Module.structure) =
261-
{
262-
id = modulePath |> List.rev |> ident;
263-
docstring = structure.docstring |> List.map String.trim;
264-
name = structure.name;
265-
deprecated = structure.deprecated;
266-
items =
267-
structure.items
268-
|> List.filter_map (fun (item : Module.item) ->
269-
match item.kind with
270-
| Value typ ->
271-
Some
272-
(Value
273-
{
274-
id = modulePath |> makeId ~identifier:item.name;
275-
docstring = item.docstring |> List.map String.trim;
276-
signature =
277-
"let " ^ item.name ^ ": " ^ Shared.typeToString typ;
278-
name = item.name;
279-
deprecated = item.deprecated;
280-
})
281-
| Type (typ, _) ->
282-
Some
283-
(Type
284-
{
285-
id = modulePath |> makeId ~identifier:item.name;
286-
docstring = item.docstring |> List.map String.trim;
287-
signature = typ.decl |> Shared.declToString item.name;
288-
name = item.name;
289-
deprecated = item.deprecated;
290-
detail = typeDetail typ ~full ~env;
291-
})
292-
| Module (Ident p) ->
293-
(* module Whatever = OtherModule *)
294-
let aliasToModule = p |> pathIdentToString in
295-
let id =
296-
(modulePath |> List.rev |> List.hd) ^ "." ^ item.name
297-
in
298-
let items, internalDocstrings =
299-
match
300-
ProcessCmt.fileForModule ~package:full.package
301-
aliasToModule
302-
with
303-
| None -> ([], [])
304-
| Some file ->
305-
let docs =
306-
extractDocsForModule ~modulePath:[id] file.structure
249+
match Cmt.loadFullCmtFromPath ~path with
250+
| None ->
251+
Error
252+
(Printf.sprintf
253+
"error: failed to generate doc for %s, try to build the project"
254+
path)
255+
| Some full ->
256+
let file = full.file in
257+
let structure = file.structure in
258+
let open SharedTypes in
259+
let env = QueryEnv.fromFile file in
260+
let rec extractDocsForModule ?(modulePath = [env.file.moduleName])
261+
(structure : Module.structure) =
262+
{
263+
id = modulePath |> List.rev |> ident;
264+
docstring = structure.docstring |> List.map String.trim;
265+
name = structure.name;
266+
deprecated = structure.deprecated;
267+
items =
268+
structure.items
269+
|> List.filter_map (fun (item : Module.item) ->
270+
match item.kind with
271+
| Value typ ->
272+
Some
273+
(Value
274+
{
275+
id = modulePath |> makeId ~identifier:item.name;
276+
docstring = item.docstring |> List.map String.trim;
277+
signature =
278+
"let " ^ item.name ^ ": "
279+
^ Shared.typeToString typ;
280+
name = item.name;
281+
deprecated = item.deprecated;
282+
})
283+
| Type (typ, _) ->
284+
Some
285+
(Type
286+
{
287+
id = modulePath |> makeId ~identifier:item.name;
288+
docstring = item.docstring |> List.map String.trim;
289+
signature =
290+
typ.decl |> Shared.declToString item.name;
291+
name = item.name;
292+
deprecated = item.deprecated;
293+
detail = typeDetail typ ~full ~env;
294+
})
295+
| Module (Ident p) ->
296+
(* module Whatever = OtherModule *)
297+
let aliasToModule = p |> pathIdentToString in
298+
let id =
299+
(modulePath |> List.rev |> List.hd) ^ "." ^ item.name
300+
in
301+
let items, internalDocstrings =
302+
match
303+
ProcessCmt.fileForModule ~package:full.package
304+
aliasToModule
305+
with
306+
| None -> ([], [])
307+
| Some file ->
308+
let docs =
309+
extractDocsForModule ~modulePath:[id]
310+
file.structure
311+
in
312+
(docs.items, docs.docstring)
307313
in
308-
(docs.items, docs.docstring)
309-
in
310-
Some
311-
(ModuleAlias
312-
{
313-
id;
314-
name = item.name;
315-
items;
316-
docstring =
317-
item.docstring @ internalDocstrings
318-
|> List.map String.trim;
319-
})
320-
| Module (Structure m) ->
321-
(* module Whatever = {} in res or module Whatever: {} in resi. *)
322-
Some
323-
(Module
324-
(extractDocsForModule ~modulePath:(m.name :: modulePath)
325-
m))
326-
| Module (Constraint (Structure _impl, Structure interface)) ->
327-
(* module Whatever: { <interface> } = { <impl> }. Prefer the interface. *)
328-
Some
329-
(Module
330-
(extractDocsForModule
331-
~modulePath:(interface.name :: modulePath)
332-
interface))
333-
| _ -> None);
334-
}
335-
in
336-
let docs = extractDocsForModule structure in
337-
print_endline (stringifyDocsForModule ~originalEnv:env docs)
314+
Some
315+
(ModuleAlias
316+
{
317+
id;
318+
name = item.name;
319+
items;
320+
docstring =
321+
item.docstring @ internalDocstrings
322+
|> List.map String.trim;
323+
})
324+
| Module (Structure m) ->
325+
(* module Whatever = {} in res or module Whatever: {} in resi. *)
326+
Some
327+
(Module
328+
(extractDocsForModule
329+
~modulePath:(m.name :: modulePath) m))
330+
| Module
331+
(Constraint (Structure _impl, Structure interface)) ->
332+
(* module Whatever: { <interface> } = { <impl> }. Prefer the interface. *)
333+
Some
334+
(Module
335+
(extractDocsForModule
336+
~modulePath:(interface.name :: modulePath)
337+
interface))
338+
| _ -> None);
339+
}
340+
in
341+
let docs = extractDocsForModule structure in
342+
Ok (stringifyDocsForModule ~originalEnv:env docs))
343+
| true ->
344+
Error
345+
(Printf.sprintf
346+
"error: failed to read %s, expected an .res or .resi file" path)
347+
in
348+
349+
result

0 commit comments

Comments
 (0)