mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-03 19:58:18 +00:00
fmt: Use format_with_options Parens for as pattern arguments
This commit is contained in:
parent
2486ce0e48
commit
4e19753189
3 changed files with 12 additions and 44 deletions
|
@ -38,6 +38,7 @@ pub enum Parens {
|
|||
InFunctionType,
|
||||
InApply,
|
||||
InOperator,
|
||||
InAsPattern,
|
||||
}
|
||||
|
||||
/// In an AST node, do we show newlines around it
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue