mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-20 21:41:24 +07:00
IDEA-153437 Do not suggest to loop over the loop variable (and don't duplicate "new")
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 2000-2016 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.completion;
|
||||
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiVariable;
|
||||
import com.intellij.psi.filters.ElementFilter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
class ExcludeFilter implements ElementFilter {
|
||||
private final PsiElement myExcluded;
|
||||
|
||||
public ExcludeFilter(@NotNull PsiVariable excluded) {
|
||||
myExcluded = excluded;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAcceptable(Object element, @Nullable PsiElement context) {
|
||||
return element != myExcluded;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isClassAcceptable(Class hintClass) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -43,7 +43,6 @@ import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.filters.*;
|
||||
import com.intellij.psi.filters.classes.AnnotationTypeFilter;
|
||||
import com.intellij.psi.filters.classes.AssignableFromContextFilter;
|
||||
import com.intellij.psi.filters.element.ExcludeDeclaredFilter;
|
||||
import com.intellij.psi.filters.element.ModifierFilter;
|
||||
import com.intellij.psi.filters.getters.ExpectedTypesGetter;
|
||||
import com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl;
|
||||
@@ -158,7 +157,7 @@ public class JavaCompletionContributor extends CompletionContributor {
|
||||
|
||||
PsiVariable var = PsiTreeUtil.getParentOfType(position, PsiVariable.class, false, PsiClass.class);
|
||||
if (var != null && PsiTreeUtil.isAncestor(var.getInitializer(), position, false)) {
|
||||
return new ExcludeDeclaredFilter(new ClassFilter(PsiVariable.class));
|
||||
return new ExcludeFilter(var);
|
||||
}
|
||||
|
||||
if (SWITCH_LABEL.accepts(position)) {
|
||||
@@ -170,6 +169,11 @@ public class JavaCompletionContributor extends CompletionContributor {
|
||||
};
|
||||
}
|
||||
|
||||
PsiForeachStatement loop = PsiTreeUtil.getParentOfType(position, PsiForeachStatement.class);
|
||||
if (loop != null && PsiTreeUtil.isAncestor(loop.getIteratedValue(), position, false)) {
|
||||
return new ExcludeFilter(loop.getIterationParameter());
|
||||
}
|
||||
|
||||
return TrueFilter.INSTANCE;
|
||||
}
|
||||
|
||||
|
||||
@@ -251,7 +251,8 @@ public class JavaKeywordCompletion {
|
||||
PsiElement prevLeaf = PsiTreeUtil.prevVisibleLeaf(position);
|
||||
addFinal(result, position, prevLeaf);
|
||||
|
||||
if (isStatementPosition(position)) {
|
||||
boolean statementPosition = isStatementPosition(position);
|
||||
if (statementPosition) {
|
||||
addCaseDefault(result, position);
|
||||
if (START_SWITCH.accepts(position)) {
|
||||
return;
|
||||
@@ -263,7 +264,7 @@ public class JavaKeywordCompletion {
|
||||
|
||||
addThisSuper(result, position);
|
||||
|
||||
addExpressionKeywords(parameters, result, position, prevLeaf);
|
||||
addExpressionKeywords(parameters, result, position, prevLeaf, statementPosition);
|
||||
|
||||
addFileHeaderKeywords(result, position, prevLeaf);
|
||||
|
||||
@@ -358,7 +359,7 @@ public class JavaKeywordCompletion {
|
||||
}
|
||||
}
|
||||
|
||||
private static void addExpressionKeywords(CompletionParameters parameters, Consumer<LookupElement> result, PsiElement position, @Nullable PsiElement prevLeaf) {
|
||||
private static void addExpressionKeywords(CompletionParameters parameters, Consumer<LookupElement> result, PsiElement position, @Nullable PsiElement prevLeaf, boolean statementPosition) {
|
||||
if (psiElement(JavaTokenType.DOUBLE_COLON).accepts(prevLeaf)) {
|
||||
PsiMethodReferenceExpression parent = PsiTreeUtil.getParentOfType(parameters.getPosition(), PsiMethodReferenceExpression.class);
|
||||
TailType tail = parent != null && !LambdaHighlightingUtil.insertSemicolon(parent.getParent()) ? TailType.SEMICOLON : TailType.NONE;
|
||||
@@ -368,7 +369,9 @@ public class JavaKeywordCompletion {
|
||||
|
||||
if (isExpressionPosition(position)) {
|
||||
if (PsiTreeUtil.getParentOfType(position, PsiAnnotation.class) == null) {
|
||||
result.consume(TailTypeDecorator.withTail(createKeyword(position, PsiKeyword.NEW), TailType.INSERT_SPACE));
|
||||
if (!statementPosition) {
|
||||
result.consume(TailTypeDecorator.withTail(createKeyword(position, PsiKeyword.NEW), TailType.INSERT_SPACE));
|
||||
}
|
||||
result.consume(createKeyword(position, PsiKeyword.NULL));
|
||||
}
|
||||
if (mayExpectBoolean(parameters)) {
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Test {
|
||||
{
|
||||
|
||||
ArrayList<Node> nodes = new ArrayList<>();
|
||||
for (Node n : n<caret>)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Node {}
|
||||
@@ -167,6 +167,11 @@ public class VariablesCompletionTest extends LightFixtureCompletionTestCase {
|
||||
assertStringItems("stringBuffer", "buffer");
|
||||
}
|
||||
|
||||
public void testDontIterateOverLoopVariable() throws Throwable {
|
||||
configure()
|
||||
myFixture.assertPreferredCompletionItems 0, 'nodes', 'new', 'null'
|
||||
}
|
||||
|
||||
public void testDuplicateSuggestionsFromUsage() {
|
||||
configure();
|
||||
assertStringItems("preferencePolicy", "policy", "aPreferencePolicy");
|
||||
|
||||
Reference in New Issue
Block a user