Fix case of expr in binop that ends in closure, when it doesn't start on a new line

This commit is contained in:
Joshua Warner 2024-12-18 10:31:16 -08:00
parent 769baa9f63
commit bab5e65480
No known key found for this signature in database
GPG key ID: 89AD497003F93FDD
5 changed files with 74 additions and 1 deletions

View file

@ -1405,8 +1405,12 @@ fn fmt_binops<'a>(
expr_lift_spaces(Parens::InOperator, buf.text.bump(), &loc_left_side.value);
format_spaces(buf, lifted_left_side.before, Newlines::Yes, indent);
buf.indent(indent);
let line_indent = buf.cur_line_indent();
let need_parens = matches!(lifted_left_side.item, Expr::BinOps(..))
|| starts_with_unary_minus(lifted_left_side.item);
|| starts_with_unary_minus(lifted_left_side.item)
|| (ends_with_closure(&lifted_left_side.item) && line_indent < indent);
if need_parens {
fmt_parens(&lifted_left_side.item, buf, indent);
@ -1451,6 +1455,17 @@ fn fmt_binops<'a>(
format_spaces(buf, lifted_right_side.after, Newlines::Yes, indent);
}
fn ends_with_closure(item: &Expr<'_>) -> bool {
match item {
Expr::Closure(..) => true,
Expr::Apply(expr, args, _) => args
.last()
.map(|a| ends_with_closure(&a.value))
.unwrap_or_else(|| ends_with_closure(&expr.value)),
_ => false,
}
}
fn starts_with_unary_minus(item: Expr<'_>) -> bool {
match item {
Expr::UnaryOp(