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("(")),
indent_if_group_breaks(
&format_args![
soft_line_break(),
left.format(),
soft_line_break_or_space(),
op.format(), op.format(),
space(), space()
group(&right).should_expand(true),
], ],
// Break after the operator, try to keep the right flat, otherwise expand it left_group
format_args![ )
text("("), ])
block_indent(&format_args![ .with_group_id(Some(left_group)),
left, // Wrap the right in a group and indents its content but only if the left side breaks
hard_line_break(), group(&indent_if_group_breaks(&right.format(), left_group)),
op.format(), // If the left side breaks, insert a hard line break to finish the indent and close the open paren.
space(), if_group_breaks(&format_args![hard_line_break(), text(")")])
group(&right), .with_group_id(Some(left_group))
]), ]
text(")")
],
]]
) )
} else { } else {
let comments = f.context().comments().clone(); let comments = f.context().comments().clone();