Java intention: Quick fix for error "foreach not applicable to type java.util.Iterator" - handle comments and take care of empty block when copying the loop body (IDEA-124751)

This commit is contained in:
Pavel Dolgov
2016-07-04 14:37:31 +03:00
parent c78e91e07d
commit 65a5b0e437
6 changed files with 39 additions and 13 deletions
@@ -42,14 +42,14 @@ public class ReplaceIteratorForEachLoopWithIteratorForLoopFix implements Intenti
@NotNull
@Override
public String getText() {
return "Replace 'for each' loop with iterator 'for' loop";
return getFamilyName();
}
@Nls
@NotNull
@Override
public String getFamilyName() {
return getText();
return "Replace 'for each' loop with iterator 'for' loop";
}
@Override
@@ -112,9 +112,11 @@ public class ReplaceIteratorForEachLoopWithIteratorForLoopFix implements Intenti
newForLoop = (PsiForStatement)styleManager.reformat(newForLoop);
if (forEachBody instanceof PsiBlockStatement) {
final PsiStatement[] statements = ((PsiBlockStatement)forEachBody).getCodeBlock().getStatements();
for (int i = statements.length - 1; i >= 0; i--) {
newBodyBlock.addAfter(statements[i], newFirstStatement);
final PsiCodeBlock bodyCodeBlock = ((PsiBlockStatement)forEachBody).getCodeBlock();
final PsiElement firstBodyElement = bodyCodeBlock.getFirstBodyElement();
final PsiElement lastBodyElement = bodyCodeBlock.getLastBodyElement();
if (firstBodyElement != null && lastBodyElement != null) {
newBodyBlock.addRangeAfter(firstBodyElement, lastBodyElement, newFirstStatement);
}
}
else if (forEachBody != null && !(forEachBody instanceof PsiEmptyStatement)) {
@@ -6,6 +6,7 @@ public class CodeBlockBody {
for (Iterator<Integer> it2 = it1; it2.hasNext(); ) {
Integer integer = it2.next();
System.out.println(integer + " a");
// a comment
System.out.println(integer + " b");
}
}
@@ -0,0 +1,10 @@
// "Replace 'for each' loop with iterator 'for' loop" "true"
import java.util.Iterator;
public class EmptyBlockBody {
void foo(Iterator<Integer> it) {
for (Iterator<Integer> it1 = it; it1.hasNext(); ) {
Integer integer = it1.next();
}
}
}
@@ -5,6 +5,7 @@ public class CodeBlockBody {
void foo(Iterator<Integer> it,Iterator<Integer> it1) {
for (Integer integer : <caret>it1) {
System.out.println(integer + " a");
// a comment
System.out.println(integer + " b");
}
}
@@ -0,0 +1,8 @@
// "Replace 'for each' loop with iterator 'for' loop" "true"
import java.util.Iterator;
public class EmptyBlockBody {
void foo(Iterator<Integer> it) {
for (Integer integer : <caret>it) {}
}
}
@@ -28,9 +28,9 @@ public class ReplaceIteratorForEachLoopWithIteratorForLoopFixTest extends LightQ
public void test() throws Exception { doAllTests(); }
@Override
protected void beforeActionStarted(String testName, String contents) {
super.beforeActionStarted(testName, contents);
if (testName.startsWith("Final")) {
protected void setUp() throws Exception {
super.setUp();
if (getTestName(false).startsWith("Final")) {
final CodeStyleSettings codeStyleSettings = CodeStyleSettingsManager.getSettings(getProject());
myFinalLocals = codeStyleSettings.GENERATE_FINAL_LOCALS;
codeStyleSettings.GENERATE_FINAL_LOCALS = true;
@@ -38,12 +38,16 @@ public class ReplaceIteratorForEachLoopWithIteratorForLoopFixTest extends LightQ
}
@Override
protected void afterActionCompleted(String testName, String contents) {
if (testName.startsWith("Final")) {
final CodeStyleSettings codeStyleSettings = CodeStyleSettingsManager.getSettings(getProject());
codeStyleSettings.GENERATE_FINAL_LOCALS = myFinalLocals;
protected void tearDown() throws Exception {
try {
if (getTestName(false).startsWith("Final")) {
final CodeStyleSettings codeStyleSettings = CodeStyleSettingsManager.getSettings(getProject());
codeStyleSettings.GENERATE_FINAL_LOCALS = myFinalLocals;
}
}
finally {
super.tearDown();
}
super.afterActionCompleted(testName, contents);
}
@Override