Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: minimise bracket printing in case of string operands of binary operator #4809

Merged
merged 5 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 4 additions & 3 deletions src/main/java/spoon/support/compiler/jdt/ParentExiter.java
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,11 @@ public <T> void visitCtBinaryOperator(CtBinaryOperator<T> operator) {
} else if (jdtTreeBuilder.getContextBuilder().stack.peek().node instanceof StringLiteralConcatenation) {
CtBinaryOperator<?> op = operator.getFactory().Core().createBinaryOperator();
op.setKind(BinaryOperatorKind.PLUS);
op.setLeftHandOperand(operator.getRightHandOperand());
op.setRightHandOperand((CtExpression<?>) child);
op.setLeftHandOperand(operator.getLeftHandOperand());
op.setRightHandOperand(operator.getRightHandOperand());
op.setType((CtTypeReference) operator.getFactory().Type().STRING.clone());
operator.setRightHandOperand(op);
operator.setLeftHandOperand(op);
operator.setRightHandOperand(((CtExpression<?>) child));
Comment on lines -448 to +452
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have done exactly what I have explained here. I ensured that the left operand takes the nested operator whenever possible.

int[] lineSeparatorPositions = jdtTreeBuilder.getContextBuilder().getCompilationUnitLineSeparatorPositions();
SourcePosition leftPosition = op.getLeftHandOperand().getPosition();
SourcePosition rightPosition = op.getRightHandOperand().getPosition();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public void testParenOptimizationCorrectlyPrintsParenthesesForExpressions(String
@ValueSource(strings = {
"int sum = 1 + 2 + 3",
"java.lang.String s = \"Sum: \" + (1 + 2)",
"java.lang.String s = \"Sum: \" + 1 + 2"
"java.lang.String s = \"Sum: \" + 1 + 2",
"java.lang.System.out.println(\"1\" + \"2\" + \"3\" + \"4\")"
})
public void testParenOptimizationCorrectlyPrintsParenthesesForStatements(String rawStatement) {
// contract: When input expressions as part of statements are minimally parenthesized,
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/spoon/test/comment/CommentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,9 @@ public void testInLineComment() {


CtLocalVariable ctLocalVariableString = m1.getBody().getStatement(12);
assertEquals(createFakeComment(f, "comment multi line string"), ((CtBinaryOperator) ((CtBinaryOperator) ctLocalVariableString.getDefaultExpression()).getRightHandOperand()).getLeftHandOperand().getComments().get(0));
assertEquals("\"\" + (\"\"// comment multi line string" + newLine
+ " + \"\")", ctLocalVariableString.getDefaultExpression().toString());
assertEquals(createFakeComment(f, "comment multi line string"), (((CtBinaryOperator) ctLocalVariableString.getDefaultExpression()).getLeftHandOperand()).getComments().get(0));
assertEquals("(\"\" + \"\")// comment multi line string" + newLine
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The minimizeRoundBrackets was false for this test case. Hence, I rearranged the brackets.

+ " + \"\"", ctLocalVariableString.getDefaultExpression().toString());

ctLocalVariable1 = m1.getBody().getStatement(13);
assertEquals("boolean c = (i == 1) ? // comment before then boolean CtConditional" + newLine
Expand Down