Skip to content

Commit

Permalink
Don't double-indent multiline expressions in binary operations (#462)
Browse files Browse the repository at this point in the history
* Don't double-indent breakables on the LHS of binaries

* Fixtures
  • Loading branch information
reese authored Feb 7, 2024
1 parent 48f336e commit 39c7660
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
14 changes: 14 additions & 0 deletions fixtures/small/binary_operators_actual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,17 @@ def bees!
# Maybe there should be the same amount of bees
equal_amount_of_bees?
end

MyClass.calculate_stuff / 2
MyClass
.calculate_stuff / 2
{1 => "", 2 => ""}.keys.sum / 5
{1 => "",
2 => ""}.keys.sum / 5

Foo.new(
a: Calculator.calculate_stuff
.map { _1.do_things! }
.sum +
1000.to_f
)
19 changes: 19 additions & 0 deletions fixtures/small/binary_operators_expected.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,22 @@ def bees!
# Maybe there should be the same amount of bees
equal_amount_of_bees?
end

MyClass.calculate_stuff / 2
MyClass
.calculate_stuff /
2
{1 => "", 2 => ""}.keys.sum / 5
{
1 => "",
2 => ""
}.keys.sum /
5

Foo.new(
a: Calculator
.calculate_stuff
.map { _1.do_things! }
.sum +
1000.to_f
)
7 changes: 5 additions & 2 deletions librubyfmt/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2506,11 +2506,14 @@ fn format_binary_inner(ps: &mut dyn ConcreteParserState, binary: Binary) {
false,
Box::new(|ps| {
let op = binary.2;
let left_hand_side = *binary.1;

if let Expression::Binary(bin) = *binary.1 {
if let Expression::Binary(bin) = left_hand_side {
format_binary_inner(ps, bin);
} else {
format_expression(ps, *binary.1);
ps.dedent(Box::new(|ps| {
format_expression(ps, left_hand_side);
}));
}

let comparison_operators = vec![">", ">=", "===", "==", "<", "<=", "<=>", "!="];
Expand Down

0 comments on commit 39c7660

Please sign in to comment.