Skip to content

Commit 8950909

Browse files
committed
Sort modules and dependencies alphabetically in output_dot function for improved readability
- Updated the output_dot function to sort both modules and their dependencies alphabetically before outputting. - Enhanced the clarity of cyclic dependency representation by sorting modules within cycles. - These changes aim to improve the overall organization and readability of the generated DOT output.
1 parent a0327c2 commit 8950909

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

lib/formatter.ml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,11 @@ and output_dot graph out_channel =
7979
List.iter (fun dep -> Hashtbl.replace all_modules_set dep true) deps)
8080
modules;
8181

82-
(* Convert set to list *)
83-
let all_modules = Hashtbl.fold (fun k _ acc -> k :: acc) all_modules_set [] in
82+
(* Convert set to list and sort alphabetically *)
83+
let all_modules =
84+
Hashtbl.fold (fun k _ acc -> k :: acc) all_modules_set []
85+
|> List.sort String.compare
86+
in
8487

8588
(* Output nodes with metadata *)
8689
List.iter
@@ -106,16 +109,21 @@ and output_dot graph out_channel =
106109

107110
output_string out_channel "\n";
108111

112+
(* Sort modules alphabetically before outputting edges *)
113+
let sorted_modules = List.sort String.compare modules in
114+
109115
(* Output edges *)
110116
List.iter
111117
(fun module_name ->
112118
let deps = Dependency_graph.get_dependencies graph module_name in
119+
(* Sort dependencies alphabetically as well *)
120+
let sorted_deps = List.sort String.compare deps in
113121
List.iter
114122
(fun dep ->
115123
output_string out_channel
116124
(" \"" ^ module_name ^ "\" -> \"" ^ dep ^ "\";\n"))
117-
deps)
118-
modules;
125+
sorted_deps)
126+
sorted_modules;
119127

120128
(* Find cycles and highlight them *)
121129
let sccs = Dependency_graph.find_strongly_connected_components graph in
@@ -129,9 +137,11 @@ and output_dot graph out_channel =
129137
output_string out_channel " style=filled;\n";
130138
output_string out_channel " color=pink;\n";
131139
output_string out_channel " label=\"Cyclic dependency\";\n";
140+
(* Sort modules in the cycle alphabetically *)
141+
let sorted_scc = List.sort String.compare scc in
132142
List.iter
133143
(fun m -> output_string out_channel (" \"" ^ m ^ "\";\n"))
134-
scc;
144+
sorted_scc;
135145
output_string out_channel " }\n"))
136146
sccs);
137147

0 commit comments

Comments
 (0)