Open
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))));
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
m
happens 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-format
indents this example:This would look much nicer as follows:
This problem is exacerbated in AOSP mode. Here's how
google-java-format
indents 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
for
loop. 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 traditionalfor
loop.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
44 remaining items