Correctly handle left/right breaking of binary expression#4985
Correctly handle left/right breaking of binary expression#4985MichaReiser merged 1 commit intomainfrom
Conversation
|
Current dependencies on/for this PR:
This comment was auto-generated by Graphite. |
PR Check ResultsEcosystem✅ ecosystem check detected no changes. BenchmarkLinuxWindows |
| BinaryLayout::Default => { | ||
| let comments = f.context().comments().clone(); | ||
| let operator_comments = comments.dangling_comments(item.as_any_node_ref()); | ||
| let needs_space = !is_simple_power_expression(item); | ||
|
|
||
| let before_operator_space = if needs_space { | ||
| soft_line_break_or_space() | ||
| } else { | ||
| soft_line_break() | ||
| }; | ||
|
|
||
| write!( | ||
| f, | ||
| [ | ||
| left.format(), | ||
| before_operator_space, | ||
| op.format(), | ||
| trailing_comments(operator_comments), | ||
| ] | ||
| )?; | ||
|
|
||
| // Format the operator on its own line if the right side has any leading comments. | ||
| if comments.has_leading_comments(right.as_ref()) { | ||
| write!(f, [hard_line_break()])?; | ||
| } else if needs_space { | ||
| write!(f, [space()])?; | ||
| } | ||
|
|
||
| write!(f, [group(&right.format())]) | ||
| } |
| write!( | ||
| f, | ||
| [ | ||
| // Wrap the left in a group and gives it an id. The printer first breaks the | ||
| // right side if `right` contains any line break because the printer breaks | ||
| // sequences of groups from right to left. | ||
| // Indents the left side if the group breaks. | ||
| group(&format_args![ | ||
| if_group_breaks(&text("(")), | ||
| indent_if_group_breaks( | ||
| &format_args![ | ||
| soft_line_break(), | ||
| left.format(), | ||
| soft_line_break_or_space(), | ||
| op.format(), | ||
| space() | ||
| ], | ||
| left_group | ||
| ) | ||
| ]) | ||
| .with_group_id(Some(left_group)), | ||
| // 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)), | ||
| // 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)) | ||
| ] | ||
| ) | ||
| } |
a315ff0 to
5243b37
Compare
439c585 to
a39dded
Compare
a39dded to
d785d44
Compare
f672744 to
89c5ea6
Compare
d785d44 to
5c30e73
Compare
d6272b0 to
d90460d
Compare
5c30e73 to
516784c
Compare
| text("("), | ||
| block_indent(&format_args
Summary
Black supports for layouts when it comes to breaking binary expressions:
Our current implementation only handles
ExpandRightandDefaultcorrectly. This PR adds support forExpandRightThenLeftandExpandLeft.Test Plan
I added tests that play through all 4 binary expression layouts.