Skip to content

Commit

Permalink
fix(grainfmt): correct fmt of operator funcs applied with labeled args (
Browse files Browse the repository at this point in the history
  • Loading branch information
spotandjake authored Mar 2, 2025
1 parent 3de64ba commit 30f5cc3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
15 changes: 13 additions & 2 deletions compiler/src/formatting/fmt.re
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,16 @@ let is_keyword_function = expr => {
};
};

let has_labeled_arg = args =>
List.exists(
a =>
switch (a.paa_label) {
| Unlabeled => false
| _ => true
},
args,
);

let needs_grouping = (~parent, ~side: infix_side, expr) => {
switch (expr.pexp_desc, side) {
| (PExpIf(_), _) => ParenGrouping
Expand Down Expand Up @@ -1608,7 +1618,7 @@ let print_expression = (fmt, ~infix_wrap=d => group(indent(d)), expr) => {
~f=(~final, vb) => fmt.print_value_binding(fmt, vb),
vbs,
)
| PExpApp(fn, [arg]) when is_prefix_op(fn) =>
| PExpApp(fn, [arg]) when is_prefix_op(fn) && !has_labeled_arg([arg]) =>
fmt.print_infix_prefix_op(fmt, fn)
++ fmt.print_comment_range(fmt, fn.pexp_loc, arg.paa_loc)
++ (
Expand All @@ -1623,7 +1633,8 @@ let print_expression = (fmt, ~infix_wrap=d => group(indent(d)), expr) => {
| None => fmt.print_application_argument(fmt, arg)
}
)
| PExpApp(fn, [lhs, rhs]) when is_infix_op(fn) =>
| PExpApp(fn, [lhs, rhs])
when is_infix_op(fn) && !has_labeled_arg([lhs, rhs]) =>
// To ensure adequate grouping/breaking of subexpressions, chains of
// binops are included in a single Doc.group, with new groups inserted
// where necessary. By default, this group indents when breaking. This
Expand Down
8 changes: 8 additions & 0 deletions compiler/test/grainfmt/application.expected.gr
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,12 @@ let preExistingObjectsWithRefCountMismatch2 = Map.make():
)
>

use Int32.{ (-), (+) }

1l - 2l
(+)(x=1l, y=2l)
(!)(rhs=true)

Int32.(-)(1l, 2l)
Int32.(+)(x=1l, y=2l)
Pervasives.(!)(rhs=true)
8 changes: 8 additions & 0 deletions compiler/test/grainfmt/application.input.gr
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,12 @@ let preExistingObjectsch2 = Map.make(): (Map.Map<Number, (Number,Number)>)

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

use Int32.{ (-), (+) }

(-)(1l, 2l)
(+)(x=1l,y=2l)
(!)(rhs=true)

Int32.(-)(1l, 2l)
Int32.(+)(x=1l,y=2l)
Pervasives.(!)(rhs=true)

0 comments on commit 30f5cc3

Please sign in to comment.