Skip to content

Commit 28c6331

Browse files
authored
Try and make single-call breakables less ocassionally-gross
1 parent b72b757 commit 28c6331

7 files changed

+72
-50
lines changed

fixtures/small/block_param_line_length_expected.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@
2020
.quux
2121
.what_comes_after_quux_idk_aaaahhhhhhh
2222
.map(&:foo)
23-
ThisIsAReallyLongClassName::ButSlightShorterWithMoreCalls::ThisIsAReallyLongClassName::ButSlightShorter
24-
.new(foo: bar_baz_quuz)
23+
ThisIsAReallyLongClassName::ButSlightShorterWithMoreCalls::ThisIsAReallyLongClassName::ButSlightShorter.new(
24+
foo: bar_baz_quuz
25+
)

fixtures/small/breakables_over_line_length_expected.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
ReallyLongThing
1919
]
2020

21-
if Opus::Foo::Bar::Baz::ReallyLongName::Example::ExampleExampleExampleExampleExampleExampleExampleExample
22-
.calls_a_thing(a, b)
21+
if Opus::Foo::Bar::Baz::ReallyLongName::Example::ExampleExampleExampleExampleExampleExampleExampleExample.calls_a_thing(
22+
a,
23+
b
24+
)
2325
puts("")
2426
end

fixtures/small/heredoc_method_call_expected.rb

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
11
class William::Carlos::Williams
2-
landscape_with_the_fall_of_icarus = T
3-
.let(
4-
new(
5-
<<~LANDSCAPE
6-
According to Brueghel
7-
when Icarus fell
8-
it was spring
9-
10-
a farmer was ploughing
11-
his field
12-
the whole pageantry
13-
14-
of the year was
15-
awake tingling
16-
with itself
17-
18-
sweating in the sun
19-
that melted
20-
the wings' wax
21-
22-
unsignificantly
23-
off the coast
24-
there was
25-
26-
a splash quite unnoticed
27-
this was
28-
Icarus drowning
29-
LANDSCAPE
30-
),
31-
Williams
32-
)
2+
landscape_with_the_fall_of_icarus = T.let(
3+
new(
4+
<<~LANDSCAPE
5+
According to Brueghel
6+
when Icarus fell
7+
it was spring
8+
9+
a farmer was ploughing
10+
his field
11+
the whole pageantry
12+
13+
of the year was
14+
awake tingling
15+
with itself
16+
17+
sweating in the sun
18+
that melted
19+
the wings' wax
20+
21+
unsignificantly
22+
off the coast
23+
there was
24+
25+
a splash quite unnoticed
26+
this was
27+
Icarus drowning
28+
LANDSCAPE
29+
),
30+
Williams
31+
)
3332
end
3433

3534
optp
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
things.each do |
2-
omg:,
3-
really:,
4-
dang:,
5-
long:,
6-
blockvar:,
7-
that_is_so_long_if_you_write_this:,
8-
you_should_refactor:,
9-
like_really_this_is_so_long:
10-
|
11-
do_things!
12-
end
1+
things
2+
.each do |
3+
omg:,
4+
really:,
5+
dang:,
6+
long:,
7+
blockvar:,
8+
that_is_so_long_if_you_write_this:,
9+
you_should_refactor:,
10+
like_really_this_is_so_long:
11+
|
12+
do_things!
13+
end

fixtures/small/multiline_chain_in_block_actual.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@
66
def ajax_get(route)
77
super
88
end
9+
10+
class Foo
11+
sig {override.returns(T::Array[T.class_of(Some::Really::Long::Name::ThatshouldprobablybealisedbutisntbecauseThis::IsATestStub)])}
12+
def example = begin; end
13+
end

fixtures/small/multiline_chain_in_block_expected.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,13 @@
77
def ajax_get(route)
88
super
99
end
10+
11+
class Foo
12+
sig {
13+
override.returns(
14+
T::Array[T.class_of(Some::Really::Long::Name::ThatshouldprobablybealisedbutisntbecauseThis::IsATestStub)]
15+
)
16+
}
17+
def example = begin
18+
end
19+
end

librubyfmt/src/render_targets.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,14 @@ impl AbstractTokenTarget for BreakableCallChainEntry {
304304
{
305305
tokens.pop();
306306
}
307-
// If the last breakable is multiline (and not a block), ignore it. The user likely
308-
// intentionally chose a line break strategy, so try our best to respect it
307+
let call_count = tokens.iter().filter(|t| matches!(t, AbstractLineToken::ConcreteLineToken(ConcreteLineToken::Dot | ConcreteLineToken::LonelyOperator))).count();
308+
// If the last breakable is multiline (and not a block/block params), ignore it. The user likely
309+
// intentionally chose a line break strategy, so try our best to respect it.
310+
//
311+
// However, if there's only one item in the chain, try our best to leave that in place.
312+
// `foo\n.bar` is always a little awkward.
309313
if let Some(AbstractLineToken::BreakableEntry(be)) = tokens.last() {
310-
if be.is_multiline() && be.delims != BreakableDelims::for_brace_block() {
314+
if (call_count == 1 || be.is_multiline()) && be.delims != BreakableDelims::for_brace_block() && be.delims != BreakableDelims::for_block_params() {
311315
tokens.pop();
312316
}
313317
}

0 commit comments

Comments
 (0)