Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't double-indent multiline expressions in binary operations #462

Merged
merged 2 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading