odd formatting for lambdas #19
Copy link
Copy link
Closed
Description
are the extra newlines intentional?
before:
Stream<ItemKey> itemIdsStream = stream(members)
.flatMap(m -> m.getFieldValues()
.entrySet()
.stream()
.filter(fv -> itemLinkFieldIds.contains(fv.getKey()))
.flatMap(fv -> FieldDTO.deserializeStringToListOfStrings(fv.getValue())
.stream()
.map(id -> new ItemKey(fieldsById.get(fv.getKey()).getItemTypeId(), id))));or even
Stream<ItemKey> itemIdsStream =
stream(members)
.flatMap(
m -> m.getFieldValues()
.entrySet()
.stream()
.filter(fv -> itemLinkFieldIds.contains(fv.getKey()))
.flatMap(
fv -> FieldDTO.deserializeStringToListOfStrings(fv.getValue())
.stream()
.map(
id -> new ItemKey(
fieldsById.get(fv.getKey()).getItemTypeId(), id))));after:
// items
Stream<ItemKey> itemIdsStream =
stream(members)
.flatMap(
m ->
m.getFieldValues().entrySet().stream()
.filter(fv -> itemLinkFieldIds.contains(fv.getKey()))
.flatMap(
fv ->
FieldDTO.deserializeStringToListOfStrings(fv.getValue()).stream()
.map(
id ->
new ItemKey(
fieldsById.get(fv.getKey()).getItemTypeId(), id))));Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
kevinb9n commentedon Dec 17, 2015
I don't think the problem is lambda-specific, but this lambda-using code is doing a really good job of tripping up on the general issue. If we can solve that issue, ideally this code ought to be able to show up as
cpovirk commentedon Dec 17, 2015
In addition to the general problem, do we also have something lambda-specific? Your example has the line break after
->, but the formatter apparently puts it before. Should that change?A result of the current behavior is that any chained calls or arguments to the next expression are indented 1 space from the expression, rather than 4. This is probably to spec, and IIRC it also comes up with the ternary operator, but it looks even weirder here because it's 1 space, rather than the 3 we see with the ternary operator. I'm talking about lines like this one...
...(which kind of works out because
mhappens to be one letter, perhaps a common case for lambda variables) and this one (which is hard to love)...kevinb9n commentedon Dec 17, 2015
Yes, the issue of whether to break before or after -> is on our list of style guide issues to resolve before we move to Java 8.
cushon commentedon Dec 17, 2015
The formatter started always breaking after
->in b8e6744. I thought that decision had been made already, but I think I misread @kevinb9n's comment in #2.Anyway, the current behaviour is:
Instead of:
I like the consistency between expression and statement lambdas, and it seems like the only way to support the suggested formatting of this example:
kevinb9n commentedon Dec 17, 2015
The decision might not have been officially made, but it has been now. Keep
-> {together, but otherwise break after->.GuiSim commentedon Mar 11, 2016
Has the code been modified to reflect this decision?
cushon commentedon Mar 11, 2016
It has been modified to break after
->and-> {. It hasn't been modified to keep e.g.m ->on the previous line (we haven't decided not to do that, it just hasn't happened).There should be another release soon, or you can build it at head to see the current behaviour.
jbduncan commentedon Aug 17, 2016
Hmm, I have some code which, before I tried applying google-java-format to it, I half-expected to end up looking like this:
but when I actually did format it (using the version built from 00530c0), it looked like this:
Is the current behaviour intentional?
basil commentedon Dec 23, 2016
Has a decision been made about this issue? It's really annoying that the lambda parameter and the arrow token always begin a new line. For example, consider how
google-java-formatindents this example:This would look much nicer as follows:
This problem is exacerbated in AOSP mode. Here's how
google-java-formatindents the same example when in AOSP mode:Lambdas in AOSP mode are indented 12 spaces! This creates a huge amount of unnecessary whitespace and inhibits readability, especially compared to using a traditional
forloop. I want to encourage people to use lambdas in our codebase, but it's difficult to do that when using them triples the indentation level and makes the code less readable. In contrast, it looks much nicer if the lambda parameter and arrow token are kept on the preceding line:Notice how in the above, the body of the lambda is indented only four spaces from the
forEach, the same amount as it would have been in a traditionalforloop.jbduncan commentedon Feb 10, 2017
Hi @basil, I think you can workaround this nasty lambda indentation behaviour for a good number of cases in the meantime, by using method references[1] whenever possible.
If I were to use the example you gave above as a template, then here's what I'd turn it into:
[1] Or refactoring the lambda expression into a private method, and calling it from a method reference or a new lambda...
alexkleiman commentedon Feb 10, 2017
@jbduncan that may work in some individual cases, but unfortunately, it is not feasible to apply this workaround when reformatting a codebase which already makes heavy use of lambdas. That being said, it is certainly a useful workaround to apply on a case-by-case basis until this bug is resolved.
Improve lambda formatting
Improve lambda formatting
Improve lambda formatting
ilya40umov commentedon Jul 10, 2017
+1 Formatting of lambdas in the current version (1.3) is quite annoying (gets unreadable quite easily).
e.g.
23 remaining items
MrIvanPlays commentedon Sep 7, 2019
Any progress on this?
linusjf commentedon Sep 7, 2019
What do you mean? No, I don't have access to clang-9 on termux, as yet.
The only workaround for now is to manually format the code as expected and wrap it in // clang-format off and // clang-format on markers. That works if you're using clang-format. It will not prevent Google-Java-format from reformatting your code if it deems it necessary.
linusjf commentedon Sep 8, 2019
My usecase is to use Google-Java-format and clang-format in that order. If I don't like what clang-format does, I comment-block that section. Google-Java-format does a slightly better job, overall, but then I haven't access to clang-format 9.
mpern commentedon Jul 7, 2020
Since almost a year passed after the last comment and there was a release of GJF recently (so at least the project isn't dead):
Is there any progress on improving the formatting of lambdas by GJF?
allensuner commentedon Apr 20, 2021
I would like to echo the above comment I really don't feel like adding a line break or new line solely for the purpose of using a language feature is a good workaround.
mpern commentedon Apr 21, 2021
If you want to have a solution now:
Get rid of GJF and use spotless to run the Eclipse JDT formatter.
kevinb9n commentedon Aug 23, 2021
Wow. I apologize for not having given any update on this issue in so long (and while so much discussion happened). As I suppose is obvious, we haven't been paying attention in an "organized/committed" way but have just watched for low-hanging fruit.
This one seems to be... high-hanging fruit:
koppor commentedon Oct 5, 2024
Note that this behavior was the motivation of the fork "Palantir Java Format". However, they seem not to have any capacity to suport Java 21 (see palantir/palantir-java-format#952).
Merge pull request google#19 from MrDolch/renovate/org.apache.maven.p…
Hug lambda arguments in `google-java-format`.
cushon commentedon Oct 16, 2026
212d5fe changes lambda formatting to keep the start of the lambda (up to and including the
->) on the previous line if it fits.Using the example from #19 (comment), this changes the output from:
to:
It took a while to figure out how to support this in a way that was consistent with the formatter's 'rectangle rule' approach. This was enabled by some other changes to the formatter which make it easier to express exceptions to the rectangle rule.
If you have a chance to try out the changes in this issue before the next release, please file issues if you run into any bugs or edge cases, or if there were issues with lambda formatting in the examples in this bug that aren't addressed by the changes.
Stephan202 commentedon Oct 16, 2026
Very nice! 🚀
I ran a quick test with the Error Prone Support code base. I've gotten so used to the old format that my brain will need some adjusting, but it looks good!
There was only one change that I would have expected to be different. In the diff below, the text inside the text block should also be deindented:
cushon commentedon Oct 19, 2026
Thanks @Stephan202! I have a fix for that text block issue in #1486
Indent text blocks in the main formatting pass