fmt: Use format_with_options Parens for as pattern arguments

This commit is contained in:
Agus Zubiaga 2024-09-02 12:02:01 -03:00
parent 2486ce0e48
commit 4e19753189
No known key found for this signature in database
3 changed files with 12 additions and 44 deletions

View file

@ -38,6 +38,7 @@ pub enum Parens {
InFunctionType,
InApply,
InOperator,
InAsPattern,
}
/// In an AST node, do we show newlines around it

View file

@ -1240,50 +1240,7 @@ fn fmt_closure<'a>(
let mut it = loc_patterns.iter().peekable();
while let Some(loc_pattern) = it.next() {
let requires_parens = {
let mut curr = loc_pattern.value;
loop {
match curr {
Pattern::SpaceAfter(pattern, _) | Pattern::SpaceBefore(pattern, _) => {
curr = *pattern;
}
Pattern::As(_, _) => {
break true;
}
Pattern::Identifier { .. }
| Pattern::QualifiedIdentifier { .. }
| Pattern::Tag(_)
| Pattern::OpaqueRef(_)
| Pattern::Apply(_, _)
| Pattern::RecordDestructure(_)
| Pattern::Tuple(_)
| Pattern::Underscore(_)
| Pattern::RequiredField(_, _)
| Pattern::OptionalField(_, _)
| Pattern::NumLiteral(_)
| Pattern::NonBase10Literal { .. }
| Pattern::FloatLiteral(_)
| Pattern::StrLiteral(_)
| Pattern::SingleQuote(_)
| Pattern::List(_)
| Pattern::ListRest(_)
| Pattern::Malformed(_)
| Pattern::MalformedIdent(_, _) => {
break false;
}
}
}
};
if requires_parens {
buf.push('(');
}
loc_pattern.format(buf, indent);
if requires_parens {
buf.push(')');
}
loc_pattern.format_with_options(buf, Parens::InAsPattern, Newlines::No, indent);
if it.peek().is_some() {
buf.indent(indent);

View file

@ -244,9 +244,19 @@ impl<'a> Formattable for Pattern<'a> {
}
As(pattern, pattern_as) => {
let needs_parens = parens == Parens::InAsPattern;
if needs_parens {
buf.push('(');
}
fmt_pattern(buf, &pattern.value, indent, parens);
pattern_as.format(buf, indent + INDENT);
if needs_parens {
buf.push(')');
}
}
// Space