Skip to content

Commit 7c5087b

Browse files
committed
Enhance module path resolution and output generation in formatter.ml
- Introduced a lookup table for module paths to optimize dependency resolution. - Implemented a helper function to resolve paths for modules, including external dependencies. - Updated output generation to include all modules, ensuring comprehensive visualization in the dependency graph.
1 parent b180c92 commit 7c5087b

File tree

1 file changed

+58
-3
lines changed

1 file changed

+58
-3
lines changed

lib/formatter.ml

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,73 @@ and output_dot graph out_channel =
2727
output_string out_channel
2828
" node [shape=box, style=filled, fillcolor=lightblue];\n\n";
2929

30+
(* Create a lookup table for all module paths *)
31+
let path_map = Hashtbl.create (List.length modules) in
32+
33+
(* Get all source directories from module paths for dependency resolution *)
34+
let source_dirs = ref [] in
35+
List.iter
36+
(fun m ->
37+
match Dependency_graph.get_module_path graph m with
38+
| Some path ->
39+
Hashtbl.add path_map m path;
40+
let dir = Filename.dirname path in
41+
if not (List.mem dir !source_dirs) then
42+
source_dirs := dir :: !source_dirs
43+
| None -> ())
44+
modules;
45+
46+
(* Helper function to resolve path for a module *)
47+
let resolve_path m =
48+
try Some (Hashtbl.find path_map m)
49+
with Not_found -> (
50+
match Parse_utils.find_implementation_file_by_name m !source_dirs with
51+
| Some p ->
52+
Hashtbl.add path_map m p;
53+
Some p
54+
| None -> (
55+
(* If not found, try to resolve from node_modules *)
56+
let node_path_opt =
57+
if List.length !source_dirs > 0 then
58+
Parse_utils.find_external_module_path m
59+
(Filename.dirname (List.hd !source_dirs))
60+
else None
61+
in
62+
match node_path_opt with
63+
| Some p ->
64+
Hashtbl.add path_map m p;
65+
Some p
66+
| None -> None))
67+
in
68+
69+
(* Collect all modules including external dependencies *)
70+
let all_modules_set = Hashtbl.create (List.length modules * 2) in
71+
72+
(* Add all known modules *)
73+
List.iter (fun m -> Hashtbl.replace all_modules_set m true) modules;
74+
75+
(* Add all dependencies *)
76+
List.iter
77+
(fun m ->
78+
let deps = Dependency_graph.get_dependencies graph m in
79+
List.iter (fun dep -> Hashtbl.replace all_modules_set dep true) deps)
80+
modules;
81+
82+
(* Convert set to list *)
83+
let all_modules = Hashtbl.fold (fun k _ acc -> k :: acc) all_modules_set [] in
84+
3085
(* Output nodes with metadata *)
3186
List.iter
3287
(fun module_name ->
33-
let path = Dependency_graph.get_module_path graph module_name in
88+
let path_opt = resolve_path module_name in
3489
let label_parts = [ module_name ] in
3590

3691
(* Create label with metadata *)
3792
let label = String.concat "\\n" label_parts in
3893

3994
(* Create tooltip with file path if available *)
4095
let tooltip =
41-
match path with
96+
match path_opt with
4297
| Some path_str -> "tooltip=\"" ^ path_str ^ "\""
4398
| None -> ""
4499
in
@@ -47,7 +102,7 @@ and output_dot graph out_channel =
47102
(" \"" ^ module_name ^ "\" [label=\"" ^ label ^ "\""
48103
^ (if tooltip <> "" then ", " ^ tooltip else "")
49104
^ "];\n"))
50-
modules;
105+
all_modules;
51106

52107
output_string out_channel "\n";
53108

0 commit comments

Comments
 (0)