Can I customize intellij's formatter to put conditional operators on the start of a new line?

4

2

Currently, IntelliJ (specifically Android Studio) chops down long conditionals like this:

if (valueWithALongNameA || valueWithALongNameB && valueWithALongNameC) {
}

// becomes

if (valueWithALongNameA ||
    valueWithALongNameB &&
    valueWithALongNameC) {
}

But our coding conventions has us format them like this:

if (valueWithALongNameA || valueWithALongNameB && valueWithALongNameC) {
}

// becomes

if (valueWithALongNameA
    || valueWithALongNameB
    && valueWithALongNameC) {
}

Note that the || and && are after the newline in our style. Is there any way to have IntelliJ do this with its autoformat?

The main problem is that I use autoformat to ensure my code is formatted to our standards, but this causes properly-formatted conditionals to be unnecessarily changed, mucking up diffs.

I couldn't find anything that was relevant in preferences:

Most of the formatting preferences in Android Studio

I tried to get all of the preferences on-screen at once, but I had to cut off some, so I excluded the "Keep when reformatting" options and "Ensure right margin not exceeded". They're all checked aside from "Line breaks", "Control statement in one line", and "Multiple expressions in one line". The cut-off section title is "Braces placement".

Ben Leggiero

Posted 2015-06-17T14:22:16.163

Reputation: 424

Answers

4

I want the opposite of what you want, but this is the setting (OSX, 14.1.3). It is admittedly not obvious.

screenshot

screenshot

aliteralmind

Posted 2015-06-17T14:22:16.163

Reputation: 178