-
Notifications
You must be signed in to change notification settings - Fork 864
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 lambda formatting #163
base: master
Are you sure you want to change the base?
Conversation
.flatMap( | ||
m -> | ||
m.getFieldValues() | ||
.flatMap(m -> m.getFieldValues() | ||
.entrySet() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These indents are now wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with Jake. IMO, entrySet()
and all the lines afterwards should be indented +4, rather than, say, +12, as is the case currently.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, I think. Thanks.
.flatMap( | ||
m -> | ||
m.getFieldValues() | ||
.flatMap(m -> m.getFieldValues() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, lines like this would look better with a break after the arrow ->
, but no break before the arrow parameter list. So something like this:
...
.flatMap(m ->
m.getFieldValues()
.stream()
.filter(...)
...);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My colleagues (who use a lot of lambdas) vastly prefer having fewer line-breaks. I'm presently trying to sell them on this tool, and I think they will have a hard time buying with the extra newline. (Of course I could maintain a fork, but for a formatting tool, it's better to stay as close to upstream as possible, since it's a matter of industry-wide standardization).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fair enough.
The reason I suggested this change is because, when I last used Ruby about 3 years ago, I seem to remember that the preferred formatting style of lambda expressions, when used with Ruby's functional equivalent of for-loops, followed this style. And I just happened to remember that style recently, and thought to myself that it introduces a sort of visual break that makes things a bit easier to scan IMO, compared to your current proposal of having more on the same line but consequently more tightly-packed and IMO more difficult to read.
I hope that this clarifies my viewpoint, and that perhaps it convinces you and your colleagues to consider this option. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@novalis ^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll run it by them again.
google#19 The style guide says: "A line is never broken adjacent to the arrow in a lambda, except that a break may come immediately after the arrow if the body of the lambda consists of a single unbraced expression." There are two changes here: 1. Don't put a newline after the arrow. 2. When the only argument to a function is a lambda, don't put a newline after the open-paren of the function. I think this newline was going in because a lambda is a single expression that is longer than (the remainder of) a line. But generally, it's prettier to break inside the lambda.
054e3ca
to
4594013
Compare
This change causes the formatter to never break in these places, even if breaks are necessary to prevent >100 character lines, e.g.: class T {
{
ffffffffffffffffffffffffffffffffffffffffffffffffff(
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx -> {});
}
} |
Well, I can definitely see why this issue has been open for a while :). Will try again. |
OK, so I hacked together something that works for @cushon's f...(x -> ...) case. But I'm deeply unhappy with it, because (a) it doesn't yet work if you put another pair of parens around the lambda, although I think I can fix that with yet more code, and (b) more importantly, because it doesn't look like any of the other code in the formatter. My idea was: if you've got a lambda that's an argument to a function, figure out how much space you have on the right to decide whether to break. To do this, I look back in the positionTokenMap until I find the open-paren for the function, and check its column in the positionToColumn map. Then I look at the formatted length of the argument to the lambda, and compare all that to 100; if it's greater, then we add a breakToFill(). Else, we don't. This does preserve my result for all of the test cases that I modified, as well as a few that I added for @cushon's case and a few related cases (except, as noted above, the case where the lambda is for One interesting example is
There is enough horizontal space to put the first arg to the lamba on the line with the enclosing function call, but that would be much uglier wrapping before the args. (Notes that this problem comes up regardless of whether we take @jbduncan's suggestion about wrapping after arrows). Anyway, my hack is probably not how this code is intended to work. What I imagine is something higher-level, like attaching a "priority" to each break, where the higher-priority breaks are taken before the lower-priority ones. So in So I wonder if it is better or just do the hack? |
Any progress on this? |
Not on my end -- it seems really hard to do right, and I haven't had time to look at it lately. Sorry. |
This is roughly what That approach has merit, and it was considered when we settled on the break-at-the-highest-syntactic-level approach used by |
#19
The style guide says:
"A line is never broken adjacent to the arrow in a lambda, except that
a break may come immediately after the arrow if the body of the lambda
consists of a single unbraced expression."
There are two changes here:
a newline after the open-paren of the function. I think
this newline was getting added because a lambda is a single
expression that is longer than (the remainder of) a line. But
generally, it's prettier to break inside the lambda.