Skip to content

Commit b149637

Browse files
cknittclaude
andcommitted
Keep line comments in operands followed by a block
Why: #9061 keeps a line comment in the operand of switch, try, if, while or a loop by putting the operand into parentheses when Doc.has_pending_line_suffix finds the comment still queued at its end. A comment after an item or argument then moves to the end of the operand, away from the item it describes. The operand now first breaks the groups around such a comment (Doc.break_before_line_suffixes), so the comment stays after its item and the parentheses are only needed when it is still pending. A binary expression is left flat: breaking it would flush the comment after the operator, where the next format attaches it to the right operand, so it keeps #9061's parentheses. 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. Review: Why are binary operands exempt? With their group broken, `switch a // c\n+ b {` printed `a + // c\nb` and then `a +\n// c\nb` on the next format; flat, it prints `switch (\n a + b // c\n) {` and stays. Area: printer tests Output: unchanged Formatting: operands of switch, try, if, while and loops with a line comment after an item or argument keep it there; binary operands keep #9061's parentheses 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 2693742 commit b149637

6 files changed

Lines changed: 221 additions & 12 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Keep a line comment after an item or argument in the operand of `switch`, `try`, `if`, `while` or a loop next to that item when formatting, instead of moving it to the end of the operand.

‎compiler/syntax/src/res_doc.ml‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,24 @@ 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+
125143
(* See documentation in interface file *)
126144
let has_pending_line_suffix doc =
127145
(* [pending]: a line suffix is queued; [broken]: the innermost enclosing

‎compiler/syntax/src/res_doc.mli‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ 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+
4451
val will_break : t -> bool
4552
(** [will_break doc] checks whether [doc] contains forced line breaks. For each
4653
[custom_layout], it only inspects the first alternative.

‎compiler/syntax/src/res_printer.ml‎

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3166,10 +3166,18 @@ and print_expression_args ~state (args : Parsetree.expression list) cmt_tbl =
31663166
]
31673167

31683168
(* The operand of [switch], [try], [if], [while] or a loop, which is followed
3169-
by more code on the same line. A line comment still pending at its end
3170-
would move after that code, into the block, so the operand goes into
3171-
parentheses with the comment before the [)]. *)
3172-
and print_operand_before_brace doc =
3169+
by a block on the same line. A line comment in it breaks the groups around
3170+
it, so that it stays where it was written; one still pending at the end of
3171+
the operand would move into the block, so the operand goes into
3172+
parentheses with the comment before the [)]. A binary expression is left
3173+
flat: breaking it would print the comment after the operator, where the
3174+
next format attaches it to the right operand, so its comment goes into the
3175+
parentheses instead. *)
3176+
and print_operand_before_brace ~expr doc =
3177+
let doc =
3178+
if Parsetree_viewer.is_binary_expression expr then doc
3179+
else Doc.break_before_line_suffixes doc
3180+
in
31733181
if Doc.has_pending_line_suffix doc then
31743182
Doc.concat
31753183
[
@@ -3202,7 +3210,7 @@ and print_if_chain ~state pexp_attributes ifs else_expr cmt_tbl =
32023210
Doc.concat
32033211
[
32043212
if_txt;
3205-
print_operand_before_brace (Doc.group condition);
3213+
print_operand_before_brace ~expr:if_expr (Doc.group condition);
32063214
Doc.space;
32073215
(let then_expr =
32083216
match Parsetree_viewer.process_braces then_expr with
@@ -3676,7 +3684,7 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
36763684
(Doc.concat
36773685
[
36783686
Doc.text "while ";
3679-
print_operand_before_brace
3687+
print_operand_before_brace ~expr:expr1
36803688
(if Parsetree_viewer.is_block_expr expr1 then condition
36813689
else Doc.group (Doc.if_breaks (add_parens condition) condition));
36823690
Doc.space;
@@ -3692,14 +3700,14 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
36923700
(let doc =
36933701
print_expression_with_comments ~state from_expr cmt_tbl
36943702
in
3695-
print_operand_before_brace
3703+
print_operand_before_brace ~expr:from_expr
36963704
(match Parens.expr from_expr with
36973705
| Parens.Parenthesized -> add_parens doc
36983706
| Braced braces -> print_braces doc from_expr braces
36993707
| Nothing -> doc));
37003708
print_direction_flag direction_flag;
37013709
(let doc = print_expression_with_comments ~state to_expr cmt_tbl in
3702-
print_operand_before_brace
3710+
print_operand_before_brace ~expr:to_expr
37033711
(match Parens.expr to_expr with
37043712
| Parens.Parenthesized -> add_parens doc
37053713
| Braced braces -> print_braces doc to_expr braces
@@ -3717,7 +3725,7 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
37173725
(let doc =
37183726
print_expression_with_comments ~state array_expr cmt_tbl
37193727
in
3720-
print_operand_before_brace
3728+
print_operand_before_brace ~expr:array_expr
37213729
(match Parens.expr array_expr with
37223730
| Parens.Parenthesized -> add_parens doc
37233731
| Braced braces -> print_braces doc array_expr braces
@@ -3735,7 +3743,7 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
37353743
(let doc =
37363744
print_expression_with_comments ~state iterable_expr cmt_tbl
37373745
in
3738-
print_operand_before_brace
3746+
print_operand_before_brace ~expr:iterable_expr
37393747
(match Parens.expr iterable_expr with
37403748
| Parens.Parenthesized -> add_parens doc
37413749
| Braced braces -> print_braces doc iterable_expr braces
@@ -3821,7 +3829,7 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
38213829
| Pexp_try (expr, cases, finally) ->
38223830
let expr_doc =
38233831
let doc = print_expression_with_comments ~state expr cmt_tbl in
3824-
print_operand_before_brace
3832+
print_operand_before_brace ~expr
38253833
(match Parens.expr expr with
38263834
| Parens.Parenthesized -> add_parens doc
38273835
| Braced braces -> print_braces doc expr braces
@@ -3847,7 +3855,7 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
38473855
| Pexp_match (expr, cases) ->
38483856
let expr_doc =
38493857
let doc = print_expression_with_comments ~state expr cmt_tbl in
3850-
print_operand_before_brace
3858+
print_operand_before_brace ~expr
38513859
(match Parens.expr expr with
38523860
| Parens.Parenthesized -> add_parens doc
38533861
| Braced braces -> print_braces doc expr braces
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
}
93+
94+
let x = switch (
95+
a + b // c
96+
) {
97+
| _ => 1
98+
}
99+
100+
let x = if (
101+
a && b // c
102+
) {
103+
1
104+
} else {
105+
2
106+
}
107+
108+
let x = while (
109+
f(a, b) + 1 > 0 // c
110+
) {
111+
()
112+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
}
53+
54+
let x = switch a // c
55+
+ b { | _ => 1 }
56+
57+
let x = if a // c
58+
&& b { 1 } else { 2 }
59+
60+
let x = while f(
61+
a, // c
62+
b,
63+
) + 1 > 0 { () }

0 commit comments

Comments
 (0)