formatter: add newlines between parens delimiters for multiline expressions

This commit is contained in:
Sean Hagstrom 2022-05-24 10:18:03 +01:00
parent 0d3a614945
commit e2e77a2324
No known key found for this signature in database
GPG key ID: AA9814E95B301A5C
3 changed files with 71 additions and 3 deletions

View file

@ -132,9 +132,20 @@ impl<'a> Formattable for Expr<'a> {
if parens == Parens::NotNeeded && !sub_expr_requests_parens(sub_expr) {
sub_expr.format_with_options(buf, Parens::NotNeeded, Newlines::Yes, indent);
} else {
let should_add_newlines = match sub_expr {
Expr::Closure(..)
| Expr::SpaceBefore(..)
| Expr::SpaceAfter(Closure(..), ..) => false,
_ => sub_expr.is_multiline(),
};
buf.indent(indent);
buf.push('(');
let next_indent = if starts_with_newline(sub_expr) {
if should_add_newlines {
buf.newline();
}
let next_indent = if starts_with_newline(sub_expr) || should_add_newlines {
indent + INDENT
} else {
indent
@ -146,6 +157,10 @@ impl<'a> Formattable for Expr<'a> {
Newlines::Yes,
next_indent,
);
if !matches!(sub_expr, Expr::SpaceAfter(..)) && should_add_newlines {
buf.newline();
}
buf.indent(indent);
buf.push(')');
}