简体   繁体   中英

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: 在此输入图像描述

Could anybody explain how I can reach expected result?

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.

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

换行首次通话选项

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM