Skip to content

Commit 675eb4d

Browse files
committed
fix: indent binary expressions in annotation element value or argument lists
1 parent 6d3621b commit 675eb4d

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

packages/prettier-plugin-java/src/printers/expressions.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,12 @@ export default {
167167
(operators.length > 0 && !children.AssignmentOperator) ||
168168
(children.expression !== undefined &&
169169
isBinaryExpression(children.expression[0]));
170+
const isInList =
171+
(path.getNode(4) as JavaNonTerminal | null)?.name === "elementValue" ||
172+
(path.getNode(6) as JavaNonTerminal | null)?.name === "argumentList";
170173
return binary(operands, operators, {
171174
hasNonAssignmentOperators,
175+
isInList,
172176
isRoot: true,
173177
operatorPosition: options.experimentalOperatorPosition
174178
});
@@ -627,10 +631,12 @@ function binary(
627631
operators: { image: string; doc: Doc }[],
628632
{
629633
hasNonAssignmentOperators = false,
634+
isInList = false,
630635
isRoot = false,
631636
operatorPosition
632637
}: {
633638
hasNonAssignmentOperators?: boolean;
639+
isInList?: boolean;
634640
isRoot?: boolean;
635641
operatorPosition: "end" | "start";
636642
}
@@ -686,7 +692,9 @@ function binary(
686692
level.push(operands.shift()!);
687693
if (
688694
!levelOperator ||
689-
(!isAssignmentOperator(levelOperator) && levelOperator !== "instanceof")
695+
(!isInList &&
696+
!isAssignmentOperator(levelOperator) &&
697+
levelOperator !== "instanceof")
690698
) {
691699
return group(level);
692700
}

packages/prettier-plugin-java/test/unit-test/binary_expressions/operator-position-end/_output.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ public void binaryOperation() {
77

88
@Annotation(
99
"This operation with two very long string should break" +
10-
"in a very nice way"
10+
"in a very nice way"
1111
)
1212
public String binaryOperationThatShouldBreak() {
1313
System.out.println(
1414
"This operation with two very long string should break" +
15-
"in a very nice way"
15+
"in a very nice way"
1616
);
1717
return (
1818
"This operation with two very long string should break" +

packages/prettier-plugin-java/test/unit-test/binary_expressions/operator-position-start/_output.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ public void binaryOperation() {
77

88
@Annotation(
99
"This operation with two very long string should break"
10-
+ "in a very nice way"
10+
+ "in a very nice way"
1111
)
1212
public String binaryOperationThatShouldBreak() {
1313
System.out.println(
1414
"This operation with two very long string should break"
15-
+ "in a very nice way"
15+
+ "in a very nice way"
1616
);
1717
return (
1818
"This operation with two very long string should break"

0 commit comments

Comments
 (0)