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

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

Open
wants to merge 6 commits into
base: trunk
Choose a base branch
from

Conversation

reese
Copy link
Collaborator

@reese reese commented Feb 21, 2024

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.

tokens.get(tokens.len() - 2)
{
// Pop off all tokens that make up the `do`/`end` block (but not `do`!),
let index = tokens.len() - 2;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wholly recognize that this method is getting to be quite convoluted (it was already a bit of a mess before, and this PR only adds to it), but call chains have a sufficient enough number of ~weird cases to warrant the complexity, and it's well tested enough that I feel like this is okay to leave for now. I'm not 100% sure the best way we'd clean it up without making some concessions on the final output anyways.

@reese reese marked this pull request as ready for review February 21, 2024 06:05
@reese reese changed the title Improve line length handling of end-of-line block params in call chains Improve line length handling of end-of-line breakables in call chains Feb 21, 2024
@reese reese force-pushed the reese-improve-block-param-breaks branch from c4e1bec to 88e1c18 Compare February 21, 2024 18:06
@reese reese force-pushed the reese-improve-block-param-breaks branch from 064d74d to 01987e3 Compare February 21, 2024 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant