Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(grainfmt): Ensure constraints and keyword functions group properly #2070

Merged
merged 1 commit into from Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 15 additions & 3 deletions compiler/src/formatting/fmt.re
Expand Up @@ -157,6 +157,7 @@ let op_precedence = startsWith =>

let precedence = expr => {
switch (expr.pexp_desc) {
| PExpConstraint(_) => 140
| PExpId({txt: Identifier.IdentName({txt: op})}) =>
if (String.length(op) > 1) {
switch (String.sub(op, 0, 2)) {
Expand Down Expand Up @@ -250,6 +251,8 @@ let needs_grouping = (~parent, ~side: infix_side, expr) => {
} else {
FormatterGrouping;
};
} else if (is_keyword_function(fn1)) {
ParenGrouping;
} else {
FormatterGrouping;
}
Expand Down Expand Up @@ -2477,8 +2480,17 @@ let print_expression = (fmt, ~infix_wrap=d => group(indent(d)), expr) => {
)
++ hardline,
)
| PExpConstraint(expr, typ) =>
fmt.print_expression(fmt, expr)
| PExpConstraint(inner_expr, typ) =>
(
switch (needs_grouping(~parent=expr, ~side=Left, inner_expr)) {
| ParenGrouping =>
parens(
indent(break ++ fmt.print_expression(fmt, inner_expr)) ++ break,
)
| FormatterGrouping => group(fmt.print_expression(fmt, inner_expr))
| None => fmt.print_expression(fmt, inner_expr)
}
)
++ string(":")
++ group(
indent(
Expand All @@ -2487,7 +2499,7 @@ let print_expression = (fmt, ~infix_wrap=d => group(indent(d)), expr) => {
~none=breakable_space,
~lead=space,
~trail=breakable_space,
expr.pexp_loc,
inner_expr.pexp_loc,
typ.ptyp_loc,
)
++ fmt.print_type(fmt, typ),
Expand Down
2 changes: 2 additions & 0 deletions compiler/test/grainfmt/binops.expected.gr
Expand Up @@ -45,3 +45,5 @@ let (+++) = (+)
let (---) = (-)

let zz = 1 +++ 2 --- 3

(5 + 5): Number
2 changes: 2 additions & 0 deletions compiler/test/grainfmt/binops.input.gr
Expand Up @@ -44,3 +44,5 @@ let (+++) = (+)
let (---) = (-)

let zz = 1 +++ 2 --- 3

(5 + 5): Number
5 changes: 5 additions & 0 deletions compiler/test/grainfmt/keyword_expression.expected.gr
@@ -0,0 +1,5 @@
module KeywordExpression

(fail "unimplemented") + 5

(fail "unimplemented"): Number
5 changes: 5 additions & 0 deletions compiler/test/grainfmt/keyword_expression.input.gr
@@ -0,0 +1,5 @@
module KeywordExpression

(fail "unimplemented") + 5

(fail "unimplemented"): Number
1 change: 1 addition & 0 deletions compiler/test/suites/formatter.re
Expand Up @@ -15,6 +15,7 @@ describe("formatter", ({test, testSkip}) => {
assertFormatOutput("application2", "application2");
assertFormatOutput("application_indenting", "application_indenting");
assertFormatOutput("function_params", "function_params");
assertFormatOutput("keyword_expression", "keyword_expression");
assertFormatOutput("variants", "variants");
assertFormatOutput("matches", "matches");
assertFormatOutput("includes", "includes");
Expand Down