Make sure patterns continue to parse as such

This commit is contained in:
Joshua Warner 2024-12-01 22:10:26 -08:00
parent ea1ecb9e68
commit 64164eb1f4
No known key found for this signature in database
GPG key ID: 89AD497003F93FDD
6 changed files with 91 additions and 4 deletions

View file

@ -454,7 +454,15 @@ impl<'a> Formattable for TypeDef<'a> {
let make_multiline = ann.is_multiline() || has_abilities_multiline;
fmt_general_def(header, buf, indent, ":=", &ann.value, newlines);
fmt_general_def(
header,
Parens::NotNeeded,
buf,
indent,
":=",
&ann.value,
newlines,
);
if let Some(has_abilities) = has_abilities {
buf.spaces(1);
@ -824,8 +832,14 @@ impl<'a> Formattable for ValueDef<'a> {
use roc_parse::ast::ValueDef::*;
match self {
Annotation(loc_pattern, loc_annotation) => {
let pat_parens = if ann_pattern_needs_parens(&loc_pattern.value) {
Parens::InApply
} else {
Parens::NotNeeded
};
fmt_general_def(
loc_pattern,
pat_parens,
buf,
indent,
":",
@ -845,7 +859,20 @@ impl<'a> Formattable for ValueDef<'a> {
body_pattern,
body_expr,
} => {
fmt_general_def(ann_pattern, buf, indent, ":", &ann_type.value, newlines);
let pat_parens = if ann_pattern_needs_parens(&ann_pattern.value) {
Parens::InApply
} else {
Parens::NotNeeded
};
fmt_general_def(
ann_pattern,
pat_parens,
buf,
indent,
":",
&ann_type.value,
newlines,
);
fmt_annotated_body_comment(buf, indent, lines_between);
@ -860,8 +887,19 @@ impl<'a> Formattable for ValueDef<'a> {
}
}
fn ann_pattern_needs_parens(value: &Pattern<'_>) -> bool {
match value.extract_spaces().item {
Pattern::Tag(_) => true,
Pattern::Apply(func, _args) if matches!(func.extract_spaces().item, Pattern::Tag(..)) => {
true
}
_ => false,
}
}
fn fmt_general_def<L: Formattable>(
lhs: L,
lhs_parens: Parens,
buf: &mut Buf,
indent: u16,
@ -869,7 +907,7 @@ fn fmt_general_def<L: Formattable>(
rhs: &TypeAnnotation,
newlines: Newlines,
) {
lhs.format(buf, indent);
lhs.format_with_options(buf, lhs_parens, Newlines::Yes, indent);
buf.indent(indent);
if rhs.is_multiline() {