Formatting for Java Ternary Operation
The following Java code appears to be ignoring my Java code format configuration selection around the Ternary Operator:
var errorMessage =
(blockUnsaved.getDriverClassification() == DriverClassification.DELIVERY_PARTNER)
&& (blockUnsaved.getPay() < MIN_PAY_DELIVERY_PARTNER_SURGE)
? "block pay [%d] for DELIVERY_PARTNER block is below minimum pay [%d] to advance to completed".formatted(
blockUnsaved.getPay(),
MIN_PAY_DELIVERY_PARTNER_SURGE
)
: blockThresholdDetectItemForWfmBlockId
.stream()
.anyMatch(blockThresholdDetectItem ->
blockThresholdDetectItem.driverBatchId == null)
? "there were no driver batches"
: blockThresholdDetectItemForWfmBlockId
.stream()
.noneMatch(blockThresholdDetectItem ->
blockThresholdDetectItem.fetchPackageId != null)
? "there were no non-empty driver batches"
: "";
Given my configuration, shouldn't the 5th and 6th lines be indenting by 4 spaces like this?
var errorMessage =
(blockUnsaved.getDriverClassification() == DriverClassification.DELIVERY_PARTNER)
&& (blockUnsaved.getPay() < MIN_PAY_DELIVERY_PARTNER_SURGE)
? "block pay [%d] for DELIVERY_PARTNER block is below minimum pay [%d] to advance to completed".formatted(
blockUnsaved.getPay(),
MIN_PAY_DELIVERY_PARTNER_SURGE
)
: blockThresholdDetectItemForWfmBlockId
.stream()
.anyMatch(blockThresholdDetectItem ->
blockThresholdDetectItem.driverBatchId == null)
? "there were no driver batches"
: blockThresholdDetectItemForWfmBlockId
.stream()
.noneMatch(blockThresholdDetectItem ->
blockThresholdDetectItem.fetchPackageId != null)
? "there were no non-empty driver batches"
: "";
My configuration is:
Editor/Code Style/Java/Tabs and Indents:
- “Use tab character” is unchecked
- "Tab size:" is 2
- “Indent:" is 2
- “Continuation Indent:” is 4
- All remaining items unchecked/0
Editor/Code Style/Java/Wrapping and Braces/Ternary operation:
- “Wrapped always” is selected
- “Align when multiline” is unchecked
- “'?' and ‘:’ signs on next line” is checked
I tried turning on “Align when multiline”, but it pulled the “?” and “:” children to be left aligned with the parent boolean expression…which I specifically don't want. My goal is to have the boolean expression act like a parent, with each of the “?” and “:” acting as children indented by 4 spaces (which is what I have when I leave “Align when multiline” unselected, but the other problem in the first code sample above occurs).
Coming from Scala, I use the Java Ternary operator extensively. The fact that the wrapping indentation is off impacts ease of readability, especially when trying to scan code being seen for the first time.
What am I doing wrong, or what can I change to achieve the desired effect in the second code example?
Please sign in to leave a comment.
Please file a bug at https://youtrack.jetbrains.com/newIssue?project=IDEA. Attach a sample file to reproduce and your code style settings exported from the IDE or project.
Per your request, this has now been filed here:
https://youtrack.jetbrains.com/issue/IDEA-337858/Formatting-Issue-for-Java-Ternary-Operation