Skip to content

Commit cfa5e3e

Browse files
cknittclaude
andcommitted
Keep line comments in operands followed by a block
Why: A line comment inside the operand of switch, try, if, while or a loop was printed as a line suffix in a flat group, so it moved behind the opening brace, and the next format attached it to the block. The operand now breaks the groups around such a comment, and a comment trailing the whole operand goes into parentheses with it, as if conditions already did. Format modified (line comment in a switch operand): Example: let x = switch [ a, // c b, ] { | _ => 1 } Before (rescript): let x = switch [a, b] { // c | _ => 1 } After (rescript): let x = switch [ a, // c b, ] { | _ => 1 } Review: Why not make every same-line line comment break its group? That changes code that formats stably today, such as foo(v :> b) // comment, which would split over three lines; the drift only happens before a block. Area: printer tests Output: unchanged Formatting: changed operands of switch, try, if, while and loops that contain a line comment; 80 operand/comment combinations format idempotently (48 did not before), and 330 spread contexts format as before apart from those Formats: none PPX: unchanged Editor: unchanged Tests: +tests/syntax_tests/data/printer/comments/operandBeforeBlock.res Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EUpL6dKCiSwAeriDGRHqNr Claude-Session: https://claude.ai/code/session_01Kr4cjoUi2f5VpMvE6wT9bo Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent 798978d commit cfa5e3e

6 files changed

Lines changed: 235 additions & 27 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fix the formatter moving a line comment in the operand of `switch`, `try`, `if`, `while` or a loop into the block that follows, which changed it again on every format.

‎compiler/syntax/src/res_doc.ml‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,35 @@ let rec will_break doc =
122122
| IfBreaks {yes; no} -> will_break yes || will_break no
123123
| _ -> false
124124

125+
(* See documentation in interface file *)
126+
let rec break_before_line_suffixes doc =
127+
match doc with
128+
| LineSuffix _ -> Concat [doc; BreakParent]
129+
| Concat docs -> Concat (List.map break_before_line_suffixes docs)
130+
| Indent doc -> Indent (break_before_line_suffixes doc)
131+
| Group {should_break; doc} ->
132+
Group {should_break; doc = break_before_line_suffixes doc}
133+
| IfBreaks {yes; no; broken} ->
134+
IfBreaks
135+
{
136+
yes = break_before_line_suffixes yes;
137+
no = break_before_line_suffixes no;
138+
broken;
139+
}
140+
| CustomLayout docs -> CustomLayout (List.map break_before_line_suffixes docs)
141+
| Nil | Text _ | LineBreak _ | BreakParent -> doc
142+
143+
(* See documentation in interface file *)
144+
let rec has_ungrouped_line_suffix doc =
145+
match doc with
146+
| LineSuffix _ -> true
147+
| Concat docs | CustomLayout docs ->
148+
List.exists has_ungrouped_line_suffix docs
149+
| Indent doc -> has_ungrouped_line_suffix doc
150+
| IfBreaks {yes; no} ->
151+
has_ungrouped_line_suffix yes || has_ungrouped_line_suffix no
152+
| Group _ | Nil | Text _ | LineBreak _ | BreakParent -> false
153+
125154
let join ~sep docs =
126155
let rec loop acc sep docs =
127156
match docs with

‎compiler/syntax/src/res_doc.mli‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ val tilde : t
4141
val equal : t
4242
val trailing_comma : t
4343

44+
val break_before_line_suffixes : t -> t
45+
(** [break_before_line_suffixes doc] makes every line suffix in [doc] break the
46+
groups around it, so that a trailing line comment stays where it was written
47+
instead of moving to the end of the line. Use it for a doc that is followed
48+
by an opening brace on the same line, where the comment would move into the
49+
block. *)
50+
51+
val has_ungrouped_line_suffix : t -> bool
52+
(** [has_ungrouped_line_suffix doc] checks whether [doc] contains a line suffix
53+
outside of any group, which no group break can move to its own line. *)
54+
4455
val will_break : t -> bool
4556
(** [will_break doc] checks whether [doc] contains forced line breaks. For each
4657
[custom_layout], it only inspects the first alternative.

‎compiler/syntax/src/res_printer.ml‎

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3165,6 +3165,22 @@ and print_expression_args ~state (args : Parsetree.expression list) cmt_tbl =
31653165
Doc.rparen;
31663166
]
31673167

3168+
(* The operand of [switch], [try], [if], [while] or a loop, which is followed
3169+
by a block on the same line. A line comment in it breaks the groups around
3170+
it, or, when it trails the whole operand, goes into parentheses with it,
3171+
rather than moving into the block. *)
3172+
and print_operand_before_brace doc =
3173+
let doc = Doc.break_before_line_suffixes doc in
3174+
if Doc.has_ungrouped_line_suffix doc then
3175+
Doc.concat
3176+
[
3177+
Doc.lparen;
3178+
Doc.indent (Doc.concat [Doc.hard_line; doc]);
3179+
Doc.hard_line;
3180+
Doc.rparen;
3181+
]
3182+
else doc
3183+
31683184
and print_if_chain ~state pexp_attributes ifs else_expr cmt_tbl =
31693185
let if_docs =
31703186
Doc.join ~sep:Doc.space
@@ -3187,7 +3203,7 @@ and print_if_chain ~state pexp_attributes ifs else_expr cmt_tbl =
31873203
Doc.concat
31883204
[
31893205
if_txt;
3190-
Doc.group condition;
3206+
print_operand_before_brace (Doc.group condition);
31913207
Doc.space;
31923208
(let then_expr =
31933209
match Parsetree_viewer.process_braces then_expr with
@@ -3661,8 +3677,9 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
36613677
(Doc.concat
36623678
[
36633679
Doc.text "while ";
3664-
(if Parsetree_viewer.is_block_expr expr1 then condition
3665-
else Doc.group (Doc.if_breaks (add_parens condition) condition));
3680+
print_operand_before_brace
3681+
(if Parsetree_viewer.is_block_expr expr1 then condition
3682+
else Doc.group (Doc.if_breaks (add_parens condition) condition));
36663683
Doc.space;
36673684
print_expression_block ~state ~braces:true expr2 cmt_tbl;
36683685
])
@@ -3676,16 +3693,18 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
36763693
(let doc =
36773694
print_expression_with_comments ~state from_expr cmt_tbl
36783695
in
3679-
match Parens.expr from_expr with
3680-
| Parens.Parenthesized -> add_parens doc
3681-
| Braced braces -> print_braces doc from_expr braces
3682-
| Nothing -> doc);
3696+
print_operand_before_brace
3697+
(match Parens.expr from_expr with
3698+
| Parens.Parenthesized -> add_parens doc
3699+
| Braced braces -> print_braces doc from_expr braces
3700+
| Nothing -> doc));
36833701
print_direction_flag direction_flag;
36843702
(let doc = print_expression_with_comments ~state to_expr cmt_tbl in
3685-
match Parens.expr to_expr with
3686-
| Parens.Parenthesized -> add_parens doc
3687-
| Braced braces -> print_braces doc to_expr braces
3688-
| Nothing -> doc);
3703+
print_operand_before_brace
3704+
(match Parens.expr to_expr with
3705+
| Parens.Parenthesized -> add_parens doc
3706+
| Braced braces -> print_braces doc to_expr braces
3707+
| Nothing -> doc));
36893708
Doc.space;
36903709
print_expression_block ~state ~braces:true body cmt_tbl;
36913710
])
@@ -3699,10 +3718,11 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
36993718
(let doc =
37003719
print_expression_with_comments ~state array_expr cmt_tbl
37013720
in
3702-
match Parens.expr array_expr with
3703-
| Parens.Parenthesized -> add_parens doc
3704-
| Braced braces -> print_braces doc array_expr braces
3705-
| Nothing -> doc);
3721+
print_operand_before_brace
3722+
(match Parens.expr array_expr with
3723+
| Parens.Parenthesized -> add_parens doc
3724+
| Braced braces -> print_braces doc array_expr braces
3725+
| Nothing -> doc));
37063726
Doc.space;
37073727
print_expression_block ~state ~braces:true body cmt_tbl;
37083728
])
@@ -3716,10 +3736,11 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
37163736
(let doc =
37173737
print_expression_with_comments ~state iterable_expr cmt_tbl
37183738
in
3719-
match Parens.expr iterable_expr with
3720-
| Parens.Parenthesized -> add_parens doc
3721-
| Braced braces -> print_braces doc iterable_expr braces
3722-
| Nothing -> doc);
3739+
print_operand_before_brace
3740+
(match Parens.expr iterable_expr with
3741+
| Parens.Parenthesized -> add_parens doc
3742+
| Braced braces -> print_braces doc iterable_expr braces
3743+
| Nothing -> doc));
37233744
Doc.space;
37243745
print_expression_block ~state ~braces:true body cmt_tbl;
37253746
])
@@ -3801,10 +3822,11 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
38013822
| Pexp_try (expr, cases) ->
38023823
let expr_doc =
38033824
let doc = print_expression_with_comments ~state expr cmt_tbl in
3804-
match Parens.expr expr with
3805-
| Parens.Parenthesized -> add_parens doc
3806-
| Braced braces -> print_braces doc expr braces
3807-
| Nothing -> doc
3825+
print_operand_before_brace
3826+
(match Parens.expr expr with
3827+
| Parens.Parenthesized -> add_parens doc
3828+
| Braced braces -> print_braces doc expr braces
3829+
| Nothing -> doc)
38083830
in
38093831
Doc.concat
38103832
[
@@ -3816,10 +3838,11 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
38163838
| Pexp_match (expr, cases) ->
38173839
let expr_doc =
38183840
let doc = print_expression_with_comments ~state expr cmt_tbl in
3819-
match Parens.expr expr with
3820-
| Parens.Parenthesized -> add_parens doc
3821-
| Braced braces -> print_braces doc expr braces
3822-
| Nothing -> doc
3841+
print_operand_before_brace
3842+
(match Parens.expr expr with
3843+
| Parens.Parenthesized -> add_parens doc
3844+
| Braced braces -> print_braces doc expr braces
3845+
| Nothing -> doc)
38233846
in
38243847
Doc.concat
38253848
[
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Line comments in an operand followed by a block stay in the operand
2+
let x = switch [
3+
a, // c
4+
b,
5+
] {
6+
| _ => 1
7+
}
8+
9+
let x = switch list{
10+
a, // c
11+
...b,
12+
...c,
13+
} {
14+
| _ => 1
15+
}
16+
17+
let x = switch f(
18+
a, // c
19+
b,
20+
) {
21+
| _ => 1
22+
}
23+
24+
let x = switch (
25+
a // c
26+
) {
27+
| _ => 1
28+
}
29+
30+
let x = switch a /* c */ {
31+
| _ => 1
32+
}
33+
34+
let x = try [
35+
...a, // c
36+
b,
37+
] catch {
38+
| _ => 1
39+
}
40+
41+
let x = try (
42+
a // c
43+
) catch {
44+
| _ => 1
45+
}
46+
47+
let x = if (
48+
a // c
49+
) {
50+
1
51+
} else {
52+
2
53+
}
54+
55+
let x = if c {
56+
1
57+
} else if (
58+
a // c
59+
) {
60+
2
61+
} else {
62+
3
63+
}
64+
65+
let x = while (
66+
a // c
67+
) {
68+
()
69+
}
70+
71+
let x = for i in (
72+
a // c
73+
) to (
74+
b // d
75+
) {
76+
()
77+
}
78+
79+
let x = for y of [
80+
a, // c
81+
b,
82+
] {
83+
()
84+
}
85+
86+
let x = async () => {
87+
for await y of (
88+
a // c
89+
) {
90+
()
91+
}
92+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Line comments in an operand followed by a block stay in the operand
2+
let x = switch [
3+
a, // c
4+
b,
5+
] { | _ => 1 }
6+
7+
let x = switch list{
8+
a, // c
9+
...b,
10+
...c,
11+
} { | _ => 1 }
12+
13+
let x = switch f(
14+
a, // c
15+
b,
16+
) { | _ => 1 }
17+
18+
let x = switch a // c
19+
{ | _ => 1 }
20+
21+
let x = switch a /* c */ { | _ => 1 }
22+
23+
let x = try [
24+
...a, // c
25+
b,
26+
] catch { | _ => 1 }
27+
28+
let x = try a // c
29+
catch { | _ => 1 }
30+
31+
let x = if a // c
32+
{ 1 } else { 2 }
33+
34+
let x = if c { 1 } else if a // c
35+
{ 2 } else { 3 }
36+
37+
let x = while a // c
38+
{ () }
39+
40+
let x = for i in a // c
41+
to b // d
42+
{ () }
43+
44+
let x = for y of [
45+
a, // c
46+
b,
47+
] { () }
48+
49+
let x = async () => {
50+
for await y of a // c
51+
{ () }
52+
}

0 commit comments

Comments
 (0)