fables-tales / rubyfmt

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

Properly multiline user-multilined call chains #382

Closed reese closed 1 year ago

reese commented 1 year ago

Related to #359

In general, rubyfmt respects (within limits) user choices about multilining expressions, but we don't currently do this for multiline expressions. This has a few poor side effects: (1) users often want to multiline to make e.g. adding comments for specific sections of a call chain easier, and (2) there end up being some (IMO) awkward formatting of chains like the following:

MyClass.call(
  [],
  ""
).do_some_other_stuff!

which is inconsistent with rubyfmt's normal call chain styling.

This PR implements multilining for user-multilined expressions. It does this by checking the line location of the DotTypeOrOp items in the call chain and multilining if they're on multiple lines. This might be a slightly imperfect if the user writes something like this

# both format as `foo.bar.baz`
foo
.bar.baz

foo.bar.
baz

but IMO this is a reasonable implementation tradeoff. We could alternatively check every expression's start/end, but this ends up requiring a lot more trickery and is more prone to inconsistency.