Skip to content

Commit 12281ad

Browse files
authored
fix(grainfmt): Ensure constraints and keyword functions group properly (#2070)
1 parent c22932d commit 12281ad

File tree

6 files changed

+30
-3
lines changed

6 files changed

+30
-3
lines changed

compiler/src/formatting/fmt.re

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ let op_precedence = startsWith =>
157157

158158
let precedence = expr => {
159159
switch (expr.pexp_desc) {
160+
| PExpConstraint(_) => 140
160161
| PExpId({txt: Identifier.IdentName({txt: op})}) =>
161162
if (String.length(op) > 1) {
162163
switch (String.sub(op, 0, 2)) {
@@ -250,6 +251,8 @@ let needs_grouping = (~parent, ~side: infix_side, expr) => {
250251
} else {
251252
FormatterGrouping;
252253
};
254+
} else if (is_keyword_function(fn1)) {
255+
ParenGrouping;
253256
} else {
254257
FormatterGrouping;
255258
}
@@ -2483,8 +2486,17 @@ let print_expression = (fmt, ~infix_wrap=d => group(indent(d)), expr) => {
24832486
)
24842487
++ hardline,
24852488
)
2486-
| PExpConstraint(expr, typ) =>
2487-
fmt.print_expression(fmt, expr)
2489+
| PExpConstraint(inner_expr, typ) =>
2490+
(
2491+
switch (needs_grouping(~parent=expr, ~side=Left, inner_expr)) {
2492+
| ParenGrouping =>
2493+
parens(
2494+
indent(break ++ fmt.print_expression(fmt, inner_expr)) ++ break,
2495+
)
2496+
| FormatterGrouping => group(fmt.print_expression(fmt, inner_expr))
2497+
| None => fmt.print_expression(fmt, inner_expr)
2498+
}
2499+
)
24882500
++ string(":")
24892501
++ group(
24902502
indent(
@@ -2493,7 +2505,7 @@ let print_expression = (fmt, ~infix_wrap=d => group(indent(d)), expr) => {
24932505
~none=breakable_space,
24942506
~lead=space,
24952507
~trail=breakable_space,
2496-
expr.pexp_loc,
2508+
inner_expr.pexp_loc,
24972509
typ.ptyp_loc,
24982510
)
24992511
++ fmt.print_type(fmt, typ),

compiler/test/grainfmt/binops.expected.gr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,5 @@ let (+++) = (+)
4545
let (---) = (-)
4646

4747
let zz = 1 +++ 2 --- 3
48+
49+
(5 + 5): Number

compiler/test/grainfmt/binops.input.gr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,5 @@ let (+++) = (+)
4444
let (---) = (-)
4545

4646
let zz = 1 +++ 2 --- 3
47+
48+
(5 + 5): Number
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module KeywordExpression
2+
3+
(fail "unimplemented") + 5
4+
5+
(fail "unimplemented"): Number
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module KeywordExpression
2+
3+
(fail "unimplemented") + 5
4+
5+
(fail "unimplemented"): Number

compiler/test/suites/formatter.re

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe("formatter", ({test, testSkip}) => {
1515
assertFormatOutput("application2", "application2");
1616
assertFormatOutput("application_indenting", "application_indenting");
1717
assertFormatOutput("function_params", "function_params");
18+
assertFormatOutput("keyword_expression", "keyword_expression");
1819
assertFormatOutput("variants", "variants");
1920
assertFormatOutput("matches", "matches");
2021
assertFormatOutput("includes", "includes");

0 commit comments

Comments
 (0)