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

View file

@ -2733,6 +2733,46 @@ mod test_fmt {
)); ));
} }
#[test]
fn backpassing_simple() {
expr_formats_same(indoc!(
r#"
getChar = \ctx ->
x <- Task.await (getCharScope scope)
42
42
"#
));
}
#[test]
fn backpassing_apply_tag() {
expr_formats_same(indoc!(
r#"
getChar = \ctx ->
(T val newScope) <- Task.await (getCharScope scope)
42
42
"#
));
}
#[test]
fn backpassing_body_on_newline() {
expr_formats_same(indoc!(
r#"
getChar = \ctx ->
x <-
Task.await (getCharScope scope)
42
42
"#
));
}
// this is a parse error atm // this is a parse error atm
// #[test] // #[test]
// fn multiline_apply() { // fn multiline_apply() {