Search code examples
javaintellij-ideacode-formatting

Java code formatting in intellij Idea (chained method calls)


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?


Solution

  • 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.