Skip to content

Commit 4f6719f

Browse files
authored
Fix add block to handle if expressions over multiple lines (eclipse-jdt#2065)
- add logic to ControlStatementsFix to look at the last line of the expression instead of the if statement start - add new test to CleanUpTest - fixes eclipse-jdt#2060
1 parent bf34c50 commit 4f6719f

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/ControlStatementsFix.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ public void rewriteASTInternal(CompilationUnitRewrite cuRewrite, LinkedProposalM
224224

225225
TextEditGroup group= createTextEditGroup(label, cuRewrite);
226226
List<Comment> commentsToPreserve= new ArrayList<>();
227-
int controlStatementLine= cuRoot.getLineNumber(fControlStatement.getStartPosition());
227+
int startPosition= expression == null ? defaultStartPosition : (expression.getStartPosition() + cuRoot.getExtendedLength(expression));
228+
int controlStatementLine= cuRoot.getLineNumber(startPosition);
228229
int bodyLine= cuRoot.getLineNumber(fBody.getStartPosition());
229230
IBuffer cuBuffer= cuRewrite.getCu().getBuffer();
230231
// Get extended body text and convert multiple indent tabs to be just one tab as they will be relative to control statement
@@ -235,7 +236,6 @@ public void rewriteASTInternal(CompilationUnitRewrite cuRewrite, LinkedProposalM
235236
// If single body statement is on next line, we need to preserve any comments pertaining to the
236237
// control statement (e.g. NLS comment for if expression)
237238
if (controlStatementLine != bodyLine) {
238-
int startPosition= expression == null ? defaultStartPosition : (expression.getStartPosition() + cuRoot.getExtendedLength(expression));
239239
List<Comment> comments= cuRoot.getCommentList();
240240
for (Comment comment : comments) {
241241
int commentLine= cuRoot.getLineNumber(comment.getStartPosition());

org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20651,6 +20651,41 @@ public void foo(String x) {
2065120651
assertRefactoringResultAsExpected(new ICompilationUnit[] {cu1}, new String[] {expected1}, null);
2065220652
}
2065320653

20654+
@Test
20655+
public void testAddParenthesesIssue2060() throws Exception {
20656+
20657+
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
20658+
String sample= """
20659+
package test1;
20660+
public class E {
20661+
public void foo(String x) {
20662+
if (x.equals("abc") || //$NON-NLS-1$
20663+
x.equals("def")) //$NON-NLS-1$
20664+
System.out.println("def"); //$NON-NLS-1$
20665+
}
20666+
}
20667+
""";
20668+
ICompilationUnit cu1= pack1.createCompilationUnit("E.java", sample, false, null);
20669+
20670+
enable(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS);
20671+
enable(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS_ALWAYS);
20672+
20673+
sample= """
20674+
package test1;
20675+
public class E {
20676+
public void foo(String x) {
20677+
if (x.equals("abc") || //$NON-NLS-1$
20678+
x.equals("def")) { //$NON-NLS-1$
20679+
\tSystem.out.println("def"); //$NON-NLS-1$
20680+
}
20681+
}
20682+
}
20683+
""";
20684+
String expected1= sample;
20685+
20686+
assertRefactoringResultAsExpected(new ICompilationUnit[] {cu1}, new String[] {expected1}, null);
20687+
}
20688+
2065420689
@Test
2065520690
public void testRemoveParentheses01() throws Exception {
2065620691

0 commit comments

Comments
 (0)