简体   繁体   中英

How configure intellij java formatter to format .stream() like other methods

I'm trying to set up the IntelliJ formatter to use the same new line format for streams as for other method calls.

If I got a line of code which exceeds the limit of 100 characters, IntelliJ formats it like this:

StringBuilder stringBuilder = new StringBuilder();
stringBuilder
    .append("foo")
    .append("foo")
    .append("foo")
    .append("foo")
    .append("foo")
    .append("foo")
    .append("foo")
    .append("foo");

If I got a line of code containing a stream which exceeds the limit of 100 characters too, intellij starts a new line after the stream statement:

List<Integer> list = new ArrayList<>();
list.subList(0, 2).stream()
    .filter(foo -> foo.equals(foo))
    .filter(foo -> foo.equals(foo))
    .filter(foo -> foo.equals(foo));

How can I tell the formatter to have the same formatting for streams like other methods?

List<Integer> list = new ArrayList<>();
list
    .subList(0, 2)
    .stream()
    .filter(foo -> foo.equals(foo))
    .filter(foo -> foo.equals(foo))
    .filter(foo -> foo.equals(foo));

This formatting should only apply if the code line exceeds the limit of 100 characters, otherwise it should stay in one line.

Edit: I updated my intellij version to 2019.2 and reimported my CodeStyle xml. Now i can not reproduce my problem anymore.

I cannot reproduce the problem with IntelliJ IDEA Ultimate 2019.2. I created the following class:

public class Foo {

    void bar() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("foo").append("foo").append("foo").append("foo").append("foo").append("foo").append("foo").append("foo");

        List<Integer> list = new ArrayList<>();
        list.subList(0, 2).stream().filter(foo -> foo.equals(foo)).filter(foo -> foo.equals(foo)).filter(foo -> foo.equals(foo));
    }

}

After reformatting (CTRL+ALT+L), the code looked like this:

public class Foo {

    void bar() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder
                .append("foo")
                .append("foo")
                .append("foo")
                .append("foo")
                .append("foo")
                .append("foo")
                .append("foo")
                .append("foo");

        List<Integer> list = new ArrayList<>();
        list
                .subList(0, 2)
                .stream()
                .filter(foo -> foo.equals(foo))
                .filter(foo -> foo.equals(foo))
                .filter(foo -> foo.equals(foo));
    }

}

Here are my code style settings as an XML:

<code_scheme name="Project" version="173">
  <codeStyleSettings language="JAVA">
    <option name="ALIGN_MULTILINE_CHAINED_METHODS" value="true" />
    <option name="ALIGN_MULTILINE_PARAMETERS_IN_CALLS" value="true" />
    <option name="METHOD_PARAMETERS_WRAP" value="2" />
    <option name="THROWS_LIST_WRAP" value="1" />
    <option name="METHOD_CALL_CHAIN_WRAP" value="2" />
    <option name="WRAP_FIRST_METHOD_IN_CALL_CHAIN" value="true" />
    <option name="BINARY_OPERATION_WRAP" value="1" />
    <option name="ARRAY_INITIALIZER_WRAP" value="1" />
  </codeStyleSettings>
</code_scheme>

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