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