Merge pull request #2089 from rtfeldman/joshuawarner32/fix-backpassing-formatting

Fix formatting of applies in backpassing
This commit is contained in:
Folkert de Vries 2021-11-27 15:46:10 +01:00 committed by GitHub
commit d0da22edfa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 1 deletions

View file

@ -861,6 +861,14 @@ fn fmt_backpassing<'a>(
indent
};
let pattern_needs_parens = loc_patterns
.iter()
.any(|p| pattern_needs_parens_when_backpassing(&p.value));
if pattern_needs_parens {
buf.push('(');
}
let mut it = loc_patterns.iter().peekable();
while let Some(loc_pattern) = it.next() {
@ -876,6 +884,10 @@ fn fmt_backpassing<'a>(
}
}
if pattern_needs_parens {
buf.push(')');
}
if arguments_are_multiline {
newline(buf, indent);
} else {
@ -896,7 +908,7 @@ fn fmt_backpassing<'a>(
// the body of the Backpass can be on the same line, or
// on a new line. If it's on the same line, insert a space.
match &loc_ret.value {
match &loc_body.value {
SpaceBefore(_, _) => {
// the body starts with (first comment and then) a newline
// do nothing
@ -911,6 +923,16 @@ fn fmt_backpassing<'a>(
loc_ret.format_with_options(buf, Parens::NotNeeded, Newlines::Yes, indent);
}
fn pattern_needs_parens_when_backpassing(pat: &Pattern) -> bool {
match pat {
Pattern::Apply(_, _) => true,
Pattern::SpaceBefore(a, _) | Pattern::SpaceAfter(a, _) => {
pattern_needs_parens_when_backpassing(a)
}
_ => false,
}
}
fn fmt_record<'a>(
buf: &mut String<'a>,
update: Option<&'a Located<Expr<'a>>>,