Java formatting of nested method calls
Answered
Hi everyone,
I'm trying to get nested method calls formatted in a certain way. Here's a sample piece of code it is formatted by the formatter:
private final ZonedDateTime weekStart = ZonedDateTime.of(LocalDateTime.of(2017,
Month.NOVEMBER,
12,
9,
0,
0
), ZoneId.of("America/New_York"));
And here are my code style settings for method calls:
What I'd like is for it to be formatted like this, with the outer call chopped first and the nested calls in one line (as long as they fit):
private final ZonedDateTime weekStart = ZonedDateTime.of(
LocalDateTime.of(2017, Month.NOVEMBER, 12, 9, 0, 0),
ZoneId.of("America/New_York")
);
I've tried various combinations of settings but can't get it to do this.
Any suggestions?
Please sign in to leave a comment.
Also note that the "New life after '('" setting doesn't seem to be honoured... the first parameter is always put on the same line as the open-parentheses if there's space!
Assuming that you start with a single line (no wrapping) and set the following options under Settings|Editor|Code Style|Java|Wrapping and Braces:
- Method call arguments: Chop down if long,
- New line after '(': on
- Place ')' on new line
will give exactly the formatting you expect. I also have "Keep line breaks" = "on".
Yup this turns out to be a bug when "Keep line breaks = off" see https://youtrack.jetbrains.com/issue/IDEA-158487 I have turned that on as a workaround until I see a fix.
Actually - even with "Keep line breaks" on, I still get the outer call wrapped preferentially:
This is uglier than my desired formatting above, but at least it's consistent.