Fix formatting of negative numbers in pnc apply patterns

This commit is contained in:
Joshua Warner 2025-01-10 22:03:06 -08:00
parent 2e4e4bb957
commit 586dc3486f
No known key found for this signature in database
GPG key ID: 89AD497003F93FDD
7 changed files with 70 additions and 3 deletions

View file

@ -56,6 +56,7 @@ pub enum Parens {
InAsPattern, InAsPattern,
InApplyLastArg, InApplyLastArg,
InClosurePattern, InClosurePattern,
InPncApplyFunc,
} }
/// In an AST node, do we show newlines around it /// In an AST node, do we show newlines around it

View file

@ -205,6 +205,7 @@ impl From<Parens> for Prec {
Parens::InFunctionType => Prec::FunctionType, Parens::InFunctionType => Prec::FunctionType,
Parens::InOperator => Prec::FunctionType, Parens::InOperator => Prec::FunctionType,
Parens::InAsPattern => Prec::AsType, Parens::InAsPattern => Prec::AsType,
Parens::InPncApplyFunc => Prec::Term,
} }
} }
} }

View file

@ -277,7 +277,8 @@ fn fmt_pattern_only(
Pattern::NumLiteral(string) => { Pattern::NumLiteral(string) => {
buf.indent(indent); buf.indent(indent);
let needs_parens = parens == Parens::InClosurePattern; let needs_parens = parens == Parens::InClosurePattern
|| (parens == Parens::InPncApplyFunc && string.starts_with('-'));
if needs_parens { if needs_parens {
buf.push('('); buf.push('(');
} }
@ -419,6 +420,7 @@ fn fmt_pattern_only(
Pattern::As(pattern, pattern_as) => { Pattern::As(pattern, pattern_as) => {
let needs_parens = parens == Parens::InAsPattern let needs_parens = parens == Parens::InAsPattern
|| parens == Parens::InApply || parens == Parens::InApply
|| parens == Parens::InPncApplyFunc
|| parens == Parens::InClosurePattern; || parens == Parens::InClosurePattern;
if needs_parens { if needs_parens {
@ -472,7 +474,9 @@ pub fn pattern_fmt_apply(
buf.indent(indent); buf.indent(indent);
// Sometimes, an Apply pattern needs parens around it. // Sometimes, an Apply pattern needs parens around it.
// In particular when an Apply's argument is itself an Apply (> 0) arguments // In particular when an Apply's argument is itself an Apply (> 0) arguments
let parens = !args.is_empty() && parens == Parens::InApply && !use_commas_and_parens; let parens = !args.is_empty()
&& (parens == Parens::InApply || parens == Parens::InPncApplyFunc)
&& !use_commas_and_parens;
let indent_more = if is_multiline { let indent_more = if is_multiline {
indent + INDENT indent + INDENT
@ -494,7 +498,17 @@ pub fn pattern_fmt_apply(
} }
} }
fmt_pattern_only(&func.item, buf, Parens::InApply, indent, is_multiline); fmt_pattern_only(
&func.item,
buf,
if is_pnc {
Parens::InPncApplyFunc
} else {
Parens::InApply
},
indent,
is_multiline,
);
if use_commas_and_parens { if use_commas_and_parens {
buf.push('('); buf.push('(');

View file

@ -0,0 +1,46 @@
@0-10 SpaceAfter(
Defs(
Defs {
tags: [
EitherIndex(2147483648),
],
regions: [
@0-8,
],
space_before: [
Slice<roc_parse::ast::CommentOrNewline> { start: 0, length: 0 },
],
space_after: [
Slice<roc_parse::ast::CommentOrNewline> { start: 0, length: 0 },
],
spaces: [],
type_defs: [],
value_defs: [
Annotation(
@1-6 PncApply(
@1-3 NumLiteral(
"-8",
),
[],
),
@7-8 Apply(
"",
"C",
[],
),
),
],
},
@9-10 SpaceBefore(
Num(
"8",
),
[
Newline,
],
),
),
[
Newline,
],
)

View file

@ -0,0 +1,2 @@
(-8)():C
8

View file

@ -639,6 +639,7 @@ mod test_snapshots {
pass/plus_if.expr, pass/plus_if.expr,
pass/plus_when.expr, pass/plus_when.expr,
pass/pnc_apply_comment_after_newline.expr, pass/pnc_apply_comment_after_newline.expr,
pass/pnc_apply_neg_pattern.expr,
pass/pnc_parens_apply_etc.expr, pass/pnc_parens_apply_etc.expr,
pass/pos_inf_float.expr, pass/pos_inf_float.expr,
pass/positive_float.expr, pass/positive_float.expr,