Fix multiline_binop_conditional_with_comments

This commit is contained in:
Richard Feldman 2022-07-06 16:07:46 -04:00
parent da8a1fb81f
commit f01d6a55f7
No known key found for this signature in database
GPG key ID: 7E4127D1E4241798
2 changed files with 46 additions and 7 deletions

View file

@ -828,10 +828,22 @@ fn fmt_if<'a, 'buf>(
match &expr_below {
Expr::SpaceAfter(expr_above, spaces_after_expr) => {
expr_above.format(buf, return_indent);
// If any of the spaces is a newline, add a newline at the top.
// Otherwise leave it as just a comment.
let newline_at = if spaces_after_expr
.iter()
.any(|spaces| matches!(spaces, CommentOrNewline::Newline))
{
NewlineAt::Top
} else {
NewlineAt::None
};
fmt_comments_only(
buf,
spaces_after_expr.iter(),
NewlineAt::None,
newline_at,
return_indent,
);
buf.newline();
@ -876,12 +888,18 @@ fn fmt_if<'a, 'buf>(
Expr::SpaceAfter(expr_above, spaces_above) => {
expr_above.format(buf, return_indent);
fmt_comments_only(
buf,
spaces_above.iter(),
NewlineAt::Top,
return_indent,
);
// If any of the spaces is a newline, add a newline at the top.
// Otherwise leave it as just a comment.
let newline_at = if spaces_above
.iter()
.any(|spaces| matches!(spaces, CommentOrNewline::Newline))
{
NewlineAt::Top
} else {
NewlineAt::None
};
fmt_comments_only(buf, spaces_above.iter(), newline_at, return_indent);
buf.newline();
}

View file

@ -4037,6 +4037,27 @@ mod test_fmt {
));
}
#[test]
fn multiline_binop_conditional_with_comments() {
expr_formats_same(indoc!(
r#"
if
x
+ 1 # comment 1
> 0 # comment 2
then
y
* 2 # comment 3
< 1 # comment 4
else
42
"#
));
}
"#
));
}
#[test]
fn precedence_conflict_greater_than() {
expr_formats_same(indoc!(