Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/prettier-plugin-java/src/printers/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ export default {
if (!path.node.children.QuestionMark) {
return isInParentheses ? binaryExpression : group(binaryExpression);
}
const isInReturn = grandparentNodeName === "returnStatement";
const prefix = group(
isInReturn ? indent(binaryExpression) : binaryExpression
);
const [consequent, alternate] = map(path, print, "expression");
const suffix = [
line,
Expand All @@ -139,9 +143,9 @@ export default {
? suffix
: align(Math.max(0, options.tabWidth - 2), suffix);
if (isNestedTernary) {
return [group(binaryExpression), alignedSuffix];
return [prefix, alignedSuffix];
}
const parts = [group(binaryExpression), indent(alignedSuffix)];
const parts = [prefix, indent(alignedSuffix)];
return isInParentheses ? parts : group(parts);
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ int ternaryOperationThatShouldBreak2() {
return thisIsAVeryLongInteger ? thisIsAnotherVeryLongOne : thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne;
}

void ternaryOperationThatShouldBreak3() {
aaaaaaaaaa && bbbbbbbbbb && cccccccccc && dddddddddd && eeeeeeeeee && ffffffffff ? gggggggggg : hhhhhhhhhh;
var v = aaaaaaaaaa && bbbbbbbbbb && cccccccccc && dddddddddd && eeeeeeeeee && ffffffffff ? gggggggggg : hhhhhhhhhh;
v = aaaaaaaaaa && bbbbbbbbbb && cccccccccc && dddddddddd && eeeeeeeeee && ffffffffff ? gggggggggg : hhhhhhhhhh;
f(aaaaaaaaaa && bbbbbbbbbb && cccccccccc && dddddddddd && eeeeeeeeee && ffffffffff ? gggggggggg : hhhhhhhhhh);
return aaaaaaaaaa && bbbbbbbbbb && cccccccccc && dddddddddd && eeeeeeeeee && ffffffffff ? gggggggggg : hhhhhhhhhh;
}

int ternaryOperationThatShouldNotBreak() {
int a = b ? b : c;
return b ? b : c;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,53 @@ int ternaryOperationThatShouldBreak2() {
: thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne;
}

void ternaryOperationThatShouldBreak3() {
aaaaaaaaaa &&
bbbbbbbbbb &&
cccccccccc &&
dddddddddd &&
eeeeeeeeee &&
ffffffffff
? gggggggggg
: hhhhhhhhhh;
var v =
aaaaaaaaaa &&
bbbbbbbbbb &&
cccccccccc &&
dddddddddd &&
eeeeeeeeee &&
ffffffffff
? gggggggggg
: hhhhhhhhhh;
v =
aaaaaaaaaa &&
bbbbbbbbbb &&
cccccccccc &&
dddddddddd &&
eeeeeeeeee &&
ffffffffff
? gggggggggg
: hhhhhhhhhh;
f(
aaaaaaaaaa &&
bbbbbbbbbb &&
cccccccccc &&
dddddddddd &&
eeeeeeeeee &&
ffffffffff
? gggggggggg
: hhhhhhhhhh
);
return aaaaaaaaaa &&
bbbbbbbbbb &&
cccccccccc &&
dddddddddd &&
eeeeeeeeee &&
ffffffffff
? gggggggggg
: hhhhhhhhhh;
}

int ternaryOperationThatShouldNotBreak() {
int a = b ? b : c;
return b ? b : c;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ int ternaryOperationThatShouldBreak2() {
return thisIsAVeryLongInteger ? thisIsAnotherVeryLongOne : thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne;
}

void ternaryOperationThatShouldBreak3() {
aaaaaaaaaa && bbbbbbbbbb && cccccccccc && dddddddddd && eeeeeeeeee && ffffffffff ? gggggggggg : hhhhhhhhhh;
var v = aaaaaaaaaa && bbbbbbbbbb && cccccccccc && dddddddddd && eeeeeeeeee && ffffffffff ? gggggggggg : hhhhhhhhhh;
v = aaaaaaaaaa && bbbbbbbbbb && cccccccccc && dddddddddd && eeeeeeeeee && ffffffffff ? gggggggggg : hhhhhhhhhh;
f(aaaaaaaaaa && bbbbbbbbbb && cccccccccc && dddddddddd && eeeeeeeeee && ffffffffff ? gggggggggg : hhhhhhhhhh);
return aaaaaaaaaa && bbbbbbbbbb && cccccccccc && dddddddddd && eeeeeeeeee && ffffffffff ? gggggggggg : hhhhhhhhhh;
}

int ternaryOperationThatShouldNotBreak() {
int a = b ? b : c;
return b ? b : c;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,53 @@ int ternaryOperationThatShouldBreak2() {
: thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne;
}

void ternaryOperationThatShouldBreak3() {
aaaaaaaaaa &&
bbbbbbbbbb &&
cccccccccc &&
dddddddddd &&
eeeeeeeeee &&
ffffffffff
? gggggggggg
: hhhhhhhhhh;
var v =
aaaaaaaaaa &&
bbbbbbbbbb &&
cccccccccc &&
dddddddddd &&
eeeeeeeeee &&
ffffffffff
? gggggggggg
: hhhhhhhhhh;
v =
aaaaaaaaaa &&
bbbbbbbbbb &&
cccccccccc &&
dddddddddd &&
eeeeeeeeee &&
ffffffffff
? gggggggggg
: hhhhhhhhhh;
f(
aaaaaaaaaa &&
bbbbbbbbbb &&
cccccccccc &&
dddddddddd &&
eeeeeeeeee &&
ffffffffff
? gggggggggg
: hhhhhhhhhh
);
return aaaaaaaaaa &&
bbbbbbbbbb &&
cccccccccc &&
dddddddddd &&
eeeeeeeeee &&
ffffffffff
? gggggggggg
: hhhhhhhhhh;
}

int ternaryOperationThatShouldNotBreak() {
int a = b ? b : c;
return b ? b : c;
Expand Down