Lambda formatting
Is there any possibility to define the formatting of lambda expressions only? I have the following Lambda Expression:
Collection<String> ids = config.getIdAttributes(dcxObject.getType())
.stream()
.map(SimpleAttribute::getName)
.collect(Collectors.toList());
After calling reformat code (STRG + ALT + L) it formats the expression to
Collection<String> ids = config.getIdAttributes(dcxObject.getType()).stream().map(SimpleAttribute::getName).collect(
Collectors.toList());
which is (imho) less readable. The only way I found to disable the formatting is to use the formatter markers in comments:
//@formatter:off
Collection<String> ids = config.getIdAttributes(dcxObject.getType())
.stream()
.map(SimpleAttribute::getName)
.collect(Collectors.toList());
//@formatter:on
As I have to put this always before and after the lambda expressions and it is an IDE specific setting, the workaround is not really acceptable.
It would be nice, if we can adjust the formatting specifically for lambda expressions. Especially, it should be adjustable in which case the lambda is wrapped. Although, it makes sense to wrap the lambda in the example above, it does not make sense to wrap the following expression:
String id = ids.stream().collect(Collectors.joining());
Cheers,
Fabian
Please sign in to leave a comment.
Did you try to enable the option to keep the line breaks?
Hi Serge,
I've retried with keep line breaks and yes the expressions wrapping is kept. But of course, the wrapping of the following statement is also kept (I know a very theoretically case):
It feels a bit like choosing between the devil and the deep blue sea :(
Cheers,
Fabian
Hi, Fabian, what is your "Wrapping and Braces | Method call arguments | Take priority over call chain wrapping" setting?
Hi Yaroslav,
it is set to false on my side.
Hi Fabian
I just had the same problem and none of the above answers delivered a satisfying result.
This does the trick for me:
settings > code style > java > wrapping and braces > chained method calls > "wrap always"
I just want to add also that wrap always is a little annoying when its something short like
which will be formatted as
I've had some success with Chop if long and un-checking "take priority" from method arguments.
Again, it would be nice for streams/lambdas to have their own thing, but maybe just some increased flexibility to chained method calls may work too.
Justin, please submit a request at https://youtrack.jetbrains.com/issues/IDEA with the sample code and the expected result.
A similar feature has already been requested (quite a while ago):
https://youtrack.jetbrains.com/issue/IDEA-122600
Please upvote!