mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-27 05:49:08 +00:00
Fix formatting of applies in backpassing
This commit is contained in:
parent
0a58d6e60e
commit
02b51bcd37
2 changed files with 63 additions and 1 deletions
|
@ -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>>>,
|
||||||
|
|
|
@ -2714,6 +2714,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() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue