From 88e1c189551bc18f899ad4d23fdf95cbff7427bd Mon Sep 17 00:00:00 2001 From: Reese Williams Date: Wed, 21 Feb 2024 18:03:16 +0000 Subject: [PATCH] Also apply to params at the end of the line --- .../small/block_param_line_length_actual.rb | 5 +- .../small/block_param_line_length_expected.rb | 10 +++ .../breakables_over_line_length_expected.rb | 6 +- .../small/heredoc_method_call_expected.rb | 61 ++++++++++--------- librubyfmt/src/render_targets.rs | 14 +---- 5 files changed, 50 insertions(+), 46 deletions(-) diff --git a/fixtures/small/block_param_line_length_actual.rb b/fixtures/small/block_param_line_length_actual.rb index eea45f93..4f87e0fa 100644 --- a/fixtures/small/block_param_line_length_actual.rb +++ b/fixtures/small/block_param_line_length_actual.rb @@ -4,4 +4,7 @@ ThisIsAReallyLongClassName::ButSlightShorterWithMoreCalls.foo.bar.baz.quux.what_comes_after_quux_idk_aaaahhhh.map { |model| body_of_the_call -} \ No newline at end of file +} + +ThisIsAReallyLongClassName::ButSlightShorterWithMoreCalls.foo.bar.baz.quux.what_comes_after_quux_idk_aaaahhhhhhh.map(&:foo) +ThisIsAReallyLongClassName::ButSlightShorterWithMoreCalls::ThisIsAReallyLongClassName::ButSlightShorter.new(foo: bar_baz_quuz) \ No newline at end of file diff --git a/fixtures/small/block_param_line_length_expected.rb b/fixtures/small/block_param_line_length_expected.rb index 52b1a148..535a5a6e 100644 --- a/fixtures/small/block_param_line_length_expected.rb +++ b/fixtures/small/block_param_line_length_expected.rb @@ -12,3 +12,13 @@ .map { |model| body_of_the_call } + +ThisIsAReallyLongClassName::ButSlightShorterWithMoreCalls + .foo + .bar + .baz + .quux + .what_comes_after_quux_idk_aaaahhhhhhh + .map(&:foo) +ThisIsAReallyLongClassName::ButSlightShorterWithMoreCalls::ThisIsAReallyLongClassName::ButSlightShorter + .new(foo: bar_baz_quuz) diff --git a/fixtures/small/breakables_over_line_length_expected.rb b/fixtures/small/breakables_over_line_length_expected.rb index 6d4da475..79b4d17a 100644 --- a/fixtures/small/breakables_over_line_length_expected.rb +++ b/fixtures/small/breakables_over_line_length_expected.rb @@ -18,9 +18,7 @@ ReallyLongThing ] -if Opus::Foo::Bar::Baz::ReallyLongName::Example::ExampleExampleExampleExampleExampleExampleExampleExample.calls_a_thing( - a, - b - ) +if Opus::Foo::Bar::Baz::ReallyLongName::Example::ExampleExampleExampleExampleExampleExampleExampleExample + .calls_a_thing(a, b) puts("") end diff --git a/fixtures/small/heredoc_method_call_expected.rb b/fixtures/small/heredoc_method_call_expected.rb index 903e4ff9..324347ea 100644 --- a/fixtures/small/heredoc_method_call_expected.rb +++ b/fixtures/small/heredoc_method_call_expected.rb @@ -1,34 +1,35 @@ class William::Carlos::Williams - landscape_with_the_fall_of_icarus = T.let( - new( - <<~LANDSCAPE - According to Brueghel - when Icarus fell - it was spring - - a farmer was ploughing - his field - the whole pageantry - - of the year was - awake tingling - with itself - - sweating in the sun - that melted - the wings' wax - - unsignificantly - off the coast - there was - - a splash quite unnoticed - this was - Icarus drowning - LANDSCAPE - ), - Williams - ) + landscape_with_the_fall_of_icarus = T + .let( + new( + <<~LANDSCAPE + According to Brueghel + when Icarus fell + it was spring + + a farmer was ploughing + his field + the whole pageantry + + of the year was + awake tingling + with itself + + sweating in the sun + that melted + the wings' wax + + unsignificantly + off the coast + there was + + a splash quite unnoticed + this was + Icarus drowning + LANDSCAPE + ), + Williams + ) end optp diff --git a/librubyfmt/src/render_targets.rs b/librubyfmt/src/render_targets.rs index c110103e..992643a5 100644 --- a/librubyfmt/src/render_targets.rs +++ b/librubyfmt/src/render_targets.rs @@ -298,18 +298,10 @@ impl AbstractTokenTarget for BreakableCallChainEntry { { tokens.pop(); } - // If the last breakable extends beyond the line length but the call chain doesn't, - // the breakable will break itself, e.g. - // ```ruby - // # ↓ if the break is here, we'll break the parens instead of the call chain - // AssumeThisIs.one_hundred_twenty_characters(breaks_here) - // ``` + // If the last breakable is multiline (and not a block), ignore it. The user likely + // intentionally chose a line break strategy, so try our best to respect it if let Some(AbstractLineToken::BreakableEntry(be)) = tokens.last() { - // For block params, always pop it if it's multiline, otherwise we'd *always* multiline the whole block regardless of the contents. - // Never pop brace blocks, since we've already cleared their contents above, so now we're only looking at the params, which are still relevant. - if (be.delims != BreakableDelims::for_block_params() || be.is_multiline()) - && be.delims != BreakableDelims::for_brace_block() - { + if be.is_multiline() && be.delims != BreakableDelims::for_brace_block() { tokens.pop(); } }