mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
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:
+7
-5
@@ -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)) {
|
||||
|
||||
+1
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
+10
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
+8
@@ -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) {}
|
||||
}
|
||||
}
|
||||
+12
-8
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user