Skip to content

Commit 30f5cc3

Browse files
authored
fix(grainfmt): correct fmt of operator funcs applied with labeled args (#2251)
1 parent 3de64ba commit 30f5cc3

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

compiler/src/formatting/fmt.re

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,16 @@ let is_keyword_function = expr => {
231231
};
232232
};
233233

234+
let has_labeled_arg = args =>
235+
List.exists(
236+
a =>
237+
switch (a.paa_label) {
238+
| Unlabeled => false
239+
| _ => true
240+
},
241+
args,
242+
);
243+
234244
let needs_grouping = (~parent, ~side: infix_side, expr) => {
235245
switch (expr.pexp_desc, side) {
236246
| (PExpIf(_), _) => ParenGrouping
@@ -1608,7 +1618,7 @@ let print_expression = (fmt, ~infix_wrap=d => group(indent(d)), expr) => {
16081618
~f=(~final, vb) => fmt.print_value_binding(fmt, vb),
16091619
vbs,
16101620
)
1611-
| PExpApp(fn, [arg]) when is_prefix_op(fn) =>
1621+
| PExpApp(fn, [arg]) when is_prefix_op(fn) && !has_labeled_arg([arg]) =>
16121622
fmt.print_infix_prefix_op(fmt, fn)
16131623
++ fmt.print_comment_range(fmt, fn.pexp_loc, arg.paa_loc)
16141624
++ (
@@ -1623,7 +1633,8 @@ let print_expression = (fmt, ~infix_wrap=d => group(indent(d)), expr) => {
16231633
| None => fmt.print_application_argument(fmt, arg)
16241634
}
16251635
)
1626-
| PExpApp(fn, [lhs, rhs]) when is_infix_op(fn) =>
1636+
| PExpApp(fn, [lhs, rhs])
1637+
when is_infix_op(fn) && !has_labeled_arg([lhs, rhs]) =>
16271638
// To ensure adequate grouping/breaking of subexpressions, chains of
16281639
// binops are included in a single Doc.group, with new groups inserted
16291640
// where necessary. By default, this group indents when breaking. This

compiler/test/grainfmt/application.expected.gr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,12 @@ let preExistingObjectsWithRefCountMismatch2 = Map.make():
8484
)
8585
>
8686

87+
use Int32.{ (-), (+) }
88+
89+
1l - 2l
90+
(+)(x=1l, y=2l)
91+
(!)(rhs=true)
92+
8793
Int32.(-)(1l, 2l)
94+
Int32.(+)(x=1l, y=2l)
95+
Pervasives.(!)(rhs=true)

compiler/test/grainfmt/application.input.gr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,12 @@ let preExistingObjectsch2 = Map.make(): (Map.Map<Number, (Number,Number)>)
5151

5252
let preExistingObjectsWithRefCountMismatch2 = Map.make(): (Map.Map<Number, (Number,Number,Number,Number,Number,Number,Number,Number,Number,Number,Number)>)
5353

54+
use Int32.{ (-), (+) }
55+
56+
(-)(1l, 2l)
57+
(+)(x=1l,y=2l)
58+
(!)(rhs=true)
59+
5460
Int32.(-)(1l, 2l)
61+
Int32.(+)(x=1l,y=2l)
62+
Pervasives.(!)(rhs=true)

0 commit comments

Comments
 (0)