Java code formatting in intellij Idea (chained method calls)
Asked Answered
J

2

7

I have a small problem with java code formatting in Intellij Idea 14.1.4. I have a piece of code formatted manually by me, that looks good for me:

public class Test {
    private static final ImmutableMap<String, String> map = new ImmutableMap.Builder<String, String>()
            .put("key", "value")
            .put("key", "value")
            .put("key", "value")
            .put("key", "value")
            .put("key", "value")
            .put("key", "value")
            .build()
}

but when I reformatted this code(Ctrl + Alt + L), I got:

public class Test {
    private static final ImmutableMap<String, String> map =
            new ImmutableMap.Builder<String, String>().put("key", "value")
                    .put("key", "value")
                    .put("key", "value")
                    .put("key", "value")
                    .put("key", "value")
                    .put("key", "value")
                    .build()
}

Expected result: Intellij won't reformat anything because the code is already well-formatted.

I have a scheme with the next settings:

enter image description here

Could anybody explain how I can reach expected result?

Jozef answered 15/7, 2015 at 7:40 Comment(4)
I tried you code in 14.1.3 But it is not recreated.Stingy
Have you tried to format your code with "Align with multiline" checked?Me
version 14.1.4. what groups I need to chose "Align with multiline" for?Jozef
try "chained method calls"Me
J
8

The problem was resolved when I ticked property

"keep when reformatting"/"Line breaks"

it helps to format code on my own, with custom line breaks.

Jozef answered 17/7, 2015 at 9:30 Comment(0)
W
3

There is another one option called "Wrap first call" in IntelliJ IDEA 2017.3 (I'm not sure when they exactly added it):

Wrap first call option

Wingspan answered 17/10, 2017 at 0:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.