diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/IterateOverIterableIntention.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/IterateOverIterableIntention.java index 808f30402dbe..514d5f78efaf 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/IterateOverIterableIntention.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/IterateOverIterableIntention.java @@ -51,6 +51,14 @@ public class IterateOverIterableIntention implements IntentionAction { final int selEnd = editor.getSelectionModel().getSelectionEnd(); startOffset = (offset == selStart) ? selEnd : selStart; } + PsiElement element = file.findElementAt(startOffset); + while (element instanceof PsiWhiteSpace) { + element = element.getPrevSibling(); + } + PsiStatement psiStatement = PsiTreeUtil.getParentOfType(element, PsiStatement.class, false); + if (psiStatement != null) { + startOffset = psiStatement.getTextRange().getStartOffset(); + } if (!template.isDeactivated() && (TemplateManagerImpl.isApplicable(file, offset, template) || (TemplateManagerImpl.isApplicable(file, startOffset, template)))) { @@ -97,6 +105,12 @@ public class IterateOverIterableIntention implements IntentionAction { } PsiElement element = file.findElementAt(editor.getCaretModel().getOffset()); + while (element instanceof PsiWhiteSpace) { + element = element.getPrevSibling(); + } + if (element instanceof PsiExpressionStatement) { + element = ((PsiExpressionStatement)element).getExpression().getLastChild(); + } while ((element = PsiTreeUtil.getParentOfType(element, PsiExpression.class, true)) != null) { if (element.getParent() instanceof PsiMethodCallExpression) continue; final PsiType type = ((PsiExpression)element).getType(); diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/iterateOver/after1.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/iterateOver/after1.java new file mode 100644 index 000000000000..a5873220466d --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/iterateOver/after1.java @@ -0,0 +1,11 @@ +import java.lang.annotation.Annotation; + +// "Iterate" "true" +class Test { + void foo() { + for (Annotation annotation : getClass().getAnnotations()) { + + } + + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/iterateOver/after2.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/iterateOver/after2.java new file mode 100644 index 000000000000..a5873220466d --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/iterateOver/after2.java @@ -0,0 +1,11 @@ +import java.lang.annotation.Annotation; + +// "Iterate" "true" +class Test { + void foo() { + for (Annotation annotation : getClass().getAnnotations()) { + + } + + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/iterateOver/before1.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/iterateOver/before1.java new file mode 100644 index 000000000000..d309549f1aa9 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/iterateOver/before1.java @@ -0,0 +1,6 @@ +// "Iterate" "true" +class Test { + void foo() { + getClass().getAnnotations() + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/iterateOver/before2.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/iterateOver/before2.java new file mode 100644 index 000000000000..d0785fb012ad --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/iterateOver/before2.java @@ -0,0 +1,6 @@ +// "Iterate" "true" +class Test { + void foo() { + getClass().getAnnotations() + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/IterateOverTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/IterateOverTest.java new file mode 100644 index 000000000000..17cf0dd8566c --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/IterateOverTest.java @@ -0,0 +1,31 @@ + +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.daemon.quickFix; + + + +public class IterateOverTest extends LightQuickFix15TestCase { + + public void test() throws Exception { doAllTests(); } + + @Override + protected String getBasePath() { + return "/codeInsight/daemonCodeAnalyzer/quickFix/iterateOver"; + } + +} +