Skip to content

Commit 831eaa9

Browse files
committed
Indent children
1 parent 362a1da commit 831eaa9

File tree

1 file changed

+51
-17
lines changed

1 file changed

+51
-17
lines changed

compiler/core/js_dump.ml

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,34 @@ and expression_desc cxt ~(level : int) f x : cxt =
10701070
P.string f "...";
10711071
expression ~level:13 cxt f e)
10721072

1073+
and print_indented_list (f : P.t) (parent_expr_level : int) (cxt : cxt)
1074+
(items : 'a list) (print_item_func : int -> cxt -> P.t -> 'a -> cxt) : cxt =
1075+
if List.length items = 0 then cxt
1076+
else
1077+
P.group f 1 (fun () ->
1078+
(* Increment indent level by 1 for this block of items *)
1079+
P.newline f;
1080+
(* Start the block on a new, fully indented line for the first item *)
1081+
let rec process_items current_cxt_for_fold remaining_items =
1082+
match remaining_items with
1083+
| [] ->
1084+
current_cxt_for_fold
1085+
(* Base case for recursion, though initial check avoids empty items *)
1086+
| [last_item] ->
1087+
(* Print the last item, but DO NOT print a newline after it *)
1088+
print_item_func parent_expr_level current_cxt_for_fold f last_item
1089+
| current_item :: next_items ->
1090+
let cxt_after_current =
1091+
print_item_func parent_expr_level current_cxt_for_fold f
1092+
current_item
1093+
in
1094+
P.newline f;
1095+
(* Add a newline AFTER the current item, to prepare for the NEXT item *)
1096+
process_items cxt_after_current next_items
1097+
in
1098+
(* Initial call to the recursive helper; initial check ensures items is not empty *)
1099+
process_items cxt items)
1100+
10731101
and print_jsx cxt ?(spread_props : J.expression option)
10741102
?(key : J.expression option) ~(level : int) f (fnName : string)
10751103
(tag : J.expression) (fields : (string * J.expression) list) : cxt =
@@ -1134,36 +1162,42 @@ and print_jsx cxt ?(spread_props : J.expression option)
11341162
next))
11351163
cxt props
11361164
in
1165+
1166+
let print_one_child expr_level_for_child current_cxt_for_child f_format
1167+
child_expr =
1168+
let child_is_jsx_itself =
1169+
match child_expr.J.expression_desc with
1170+
| J.Call (_, _, {call_transformed_jsx = is_jsx}) -> is_jsx
1171+
| _ -> false
1172+
in
1173+
if not child_is_jsx_itself then P.string f_format "{";
1174+
let next_cxt =
1175+
expression ~level:expr_level_for_child current_cxt_for_child f_format
1176+
child_expr
1177+
in
1178+
if not child_is_jsx_itself then P.string f_format "}";
1179+
next_cxt
1180+
in
1181+
11371182
match children_opt with
11381183
| None ->
11391184
P.string f "<";
11401185
let cxt = cxt |> print_tag |> print_props in
11411186
P.string f "/>";
11421187
cxt
11431188
| Some children ->
1144-
let child_is_jsx child =
1145-
match child.J.expression_desc with
1146-
| J.Call (_, _, {call_transformed_jsx = is_jsx}) -> is_jsx
1147-
| _ -> false
1148-
in
1149-
11501189
P.string f "<";
11511190
let cxt = cxt |> print_tag |> print_props in
1152-
11531191
P.string f ">";
1154-
if List.length children > 0 then P.newline f;
11551192

1156-
let cxt =
1157-
List.fold_left
1158-
(fun acc e ->
1159-
if not (child_is_jsx e) then P.string f "{";
1160-
let next = expression ~level acc f e in
1161-
if not (child_is_jsx e) then P.string f "}";
1162-
P.newline f;
1163-
next)
1164-
cxt children
1193+
let cxt_after_children =
1194+
print_indented_list f level cxt children print_one_child
11651195
in
1196+
let cxt = cxt_after_children in
1197+
1198+
P.newline f;
11661199

1200+
(* Newline before the closing tag, uses parent's indent level *)
11671201
P.string f "</";
11681202
let cxt = print_tag cxt in
11691203
P.string f ">";

0 commit comments

Comments
 (0)