Lambda formatting

Answered

 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

8 comments
Comment actions Permalink

Did you try to enable the option to keep the line breaks?

0
Comment actions Permalink

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):

String id =
ids.stream(

)
.collect(Collectors
.joining(

));

It feels a bit like choosing between the devil and the deep blue sea :(

Cheers,

Fabian

0
Comment actions Permalink

Hi, Fabian, what is your "Wrapping and Braces | Method call arguments | Take priority over call chain wrapping" setting?

0
Comment actions Permalink

Hi Yaroslav,

it is set to false on my side.

1
Comment actions Permalink

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"

0
Comment actions Permalink

I just want to add also that wrap always is a little annoying when its something short like

UUID.fromString(str).toString();

which will be formatted as


UUID.randomUUID()
    .toString();

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. 

 

 

 

0
Comment actions Permalink

Justin, please submit a request at https://youtrack.jetbrains.com/issues/IDEA with the sample code and the expected result.

0
Comment actions Permalink

A similar feature has already been requested (quite a while ago):

https://youtrack.jetbrains.com/issue/IDEA-122600

Please upvote!

0

Please sign in to leave a comment.