iterate over: correct offset to detect template applicability

This commit is contained in:
Anna Kozlova
2012-02-02 21:12:17 +04:00
parent e497b81929
commit 24ec63e74e
6 changed files with 79 additions and 0 deletions

View File

@@ -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();

View File

@@ -0,0 +1,11 @@
import java.lang.annotation.Annotation;
// "Iterate" "true"
class Test {
void foo() {
for (Annotation annotation : getClass().getAnnotations()) {
}
}
}

View File

@@ -0,0 +1,11 @@
import java.lang.annotation.Annotation;
// "Iterate" "true"
class Test {
void foo() {
for (Annotation annotation : getClass().getAnnotations()) {
}
}
}

View File

@@ -0,0 +1,6 @@
// "Iterate" "true"
class Test {
void foo() {
getClass().getAnnotatio<caret>ns()
}
}

View File

@@ -0,0 +1,6 @@
// "Iterate" "true"
class Test {
void foo() {
getClass().getAnnotations()<caret>
}
}

View File

@@ -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";
}
}