fables-tales / rubyfmt

Ruby Autoformatter!
MIT License
1.07k stars 50 forks source link

Improve line length handling of end-of-line breakables in call chains #463

Open reese opened 6 months ago

reese commented 6 months ago

When calculating the line length for call chains, we do some hackery to make common expressions render nicely. Namely, we generally chop off the last do/end block in order to have e.g. MyClass.do_stuff.each do ... not split to multiple lines all the time. However, we were also chopping off the block parameters when we did this, which meant that instead of breaking the call chain when the params went over the line length, we'd break the block parameters, which led to a lot of situations of this shape:

MyClass.do_stuff.pretend_this_is_longer.each do |
    my_arg
  |
  # body
end

That's pretty unfortunate -- instead, we should leave the block args intact whenever possible and consider them part of the call chain. We can still chop off the final block's body like we did before, which keeps most of the existing behavior, but this renders more nicely for most common cases as well.