Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 (code style settings can be downloaded here) with the next settings: enter image description here

Could anybody explain how I can reach expected result?

like image 953
Geniy Avatar asked Jul 15 '15 07:07

Geniy


People also ask

How do I fix code format in IntelliJ?

Sometimes code formatting can get out of sync, but there's an easy fix in IntelliJ IDEA. You can use ⌘⌥L (macOS), or Ctrl+Alt+L (Windows/Linux) to reformat a selection of code according to your reformat settings.

How do I format my Java code?

Open the required file. Go to Source | Format Document or press Ctrl+Shift+F.

How do you call a method in IntelliJ?

Open a file in the editor and place the caret at the declaration or usage of the desired method or a field. Alternatively, select the desired method or the field in the Project tool window. From the main menu, select Navigate | Call Hierarchy or press Ctrl+Alt+H .


2 Answers

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.

like image 87
Geniy Avatar answered Sep 16 '22 16:09

Geniy


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

like image 28
FloatOverflow Avatar answered Sep 20 '22 16:09

FloatOverflow