Implement Binary expression without best_fitting (#4952)

This commit is contained in:
Micha Reiser 2023-06-08 12:45:03 +02:00 committed by GitHub
parent 23abad0bd5
commit 83cf6d6e2f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -39,35 +39,35 @@ impl FormatNodeRule<ExprBinOp> for FormatExprBinOp {
let should_break_right = self.parentheses == Some(Parentheses::Custom); let should_break_right = self.parentheses == Some(Parentheses::Custom);
if should_break_right { if should_break_right {
let left = left.format().memoized(); let left_group = f.group_id("BinaryLeft");
let right = right.format().memoized();
write!( write!(
f, f,
[best_fitting![ [
// The whole expression on a single line // Wrap the left in a group and gives it an id. The printer first breaks the
format_args![left, space(), op.format(), space(), right], // right side if `right` contains any line break because the printer breaks
// Break the right, but keep the left flat // sequences of groups from right to left.
format_args![ // Indents the left side if the group breaks.
left, group(&format_args![
space(), if_group_breaks(&text("(")),
op.format(), indent_if_group_breaks(
space(), &format_args![
group(&right).should_expand(true), soft_line_break(),
], left.format(),
// Break after the operator, try to keep the right flat, otherwise expand it soft_line_break_or_space(),
format_args![ op.format(),
text("("), space()
block_indent(&format_args![ ],
left, left_group
hard_line_break(), )
op.format(), ])
space(), .with_group_id(Some(left_group)),
group(&right), // Wrap the right in a group and indents its content but only if the left side breaks
]), group(&indent_if_group_breaks(&right.format(), left_group)),
text(")") // If the left side breaks, insert a hard line break to finish the indent and close the open paren.
], if_group_breaks(&format_args![hard_line_break(), text(")")])
]] .with_group_id(Some(left_group))
]
) )
} else { } else {
let comments = f.context().comments().clone(); let comments = f.context().comments().clone();