Commit 99585ebc authored by peter's avatar peter
Browse files

unify various places where switch-case is checked in java completion

parent ee20aa08
Branches unavailable Tags unavailable
No related merge requests found
Showing with 18 additions and 22 deletions
+18 -22
......@@ -65,9 +65,8 @@ public class BasicExpressionCompletionContributor {
addKeyword(result, position, PsiKeyword.TRUE);
addKeyword(result, position, PsiKeyword.FALSE);
final PsiElement parent = position.getParent();
if (parent != null && !(parent.getParent() instanceof PsiSwitchLabelStatement)) {
for (final PsiExpression expression : ThisGetter.getThisExpressionVariants(position)) {
if (!JavaCompletionContributor.IN_SWITCH_LABEL.accepts(position)) {
for (PsiExpression expression : ThisGetter.getThisExpressionVariants(position)) {
result.consume(new ExpressionLookupItem(expression));
}
}
......
......@@ -78,7 +78,10 @@ public class JavaCompletionContributor extends CompletionContributor {
private static final ElementPattern<PsiElement> ANNOTATION_ATTRIBUTE_NAME =
or(psiElement(PsiIdentifier.class).withParent(NAME_VALUE_PAIR),
psiElement().afterLeaf("(").withParent(psiReferenceExpression().withParent(NAME_VALUE_PAIR)));
private static final ElementPattern SWITCH_LABEL =
public static final ElementPattern<PsiElement> IN_SWITCH_LABEL =
psiElement().withSuperParent(2, psiElement(PsiSwitchLabelStatement.class).withSuperParent(2, PsiSwitchStatement.class));
private static final ElementPattern IN_ENUM_SWITCH_LABEL =
psiElement().withSuperParent(2, psiElement(PsiSwitchLabelStatement.class).withSuperParent(2,
psiElement(PsiSwitchStatement.class).with(new PatternCondition<PsiSwitchStatement>("enumExpressionType") {
@Override
......@@ -89,6 +92,7 @@ public class JavaCompletionContributor extends CompletionContributor {
return aClass != null && aClass.isEnum();
}
})));
private static final ElementPattern<PsiElement> AFTER_NUMBER_LITERAL =
psiElement().afterLeaf(psiElement().withElementType(
elementType().oneOf(JavaTokenType.DOUBLE_LITERAL, JavaTokenType.LONG_LITERAL, JavaTokenType.FLOAT_LITERAL, JavaTokenType.INTEGER_LITERAL)));
......@@ -153,7 +157,7 @@ public class JavaCompletionContributor extends CompletionContributor {
return new ExcludeFilter(var);
}
if (SWITCH_LABEL.accepts(position)) {
if (IN_ENUM_SWITCH_LABEL.accepts(position)) {
return new ClassFilter(PsiField.class) {
@Override
public boolean isAcceptable(Object element, PsiElement context) {
......@@ -420,7 +424,7 @@ public class JavaCompletionContributor extends CompletionContributor {
MultiMap<CompletionResultSet, LookupElement> items = MultiMap.create();
final PsiElement position = parameters.getPosition();
final boolean first = parameters.getInvocationCount() <= 1;
final boolean isSwitchLabel = SWITCH_LABEL.accepts(position);
final boolean isSwitchLabel = IN_ENUM_SWITCH_LABEL.accepts(position);
final boolean isAfterNew = JavaClassNameCompletionContributor.AFTER_NEW.accepts(position);
final boolean pkgContext = JavaCompletionUtil.inSomePackage(position);
final PsiType[] expectedTypes = ExpectedTypesGetter.getExpectedTypes(parameters.getPosition(), true);
......@@ -531,8 +535,7 @@ public class JavaCompletionContributor extends CompletionContributor {
return false;
}
PsiElement grand = parent.getParent();
if (grand instanceof PsiSwitchLabelStatement) {
if (IN_SWITCH_LABEL.accepts(position)) {
return false;
}
......@@ -540,6 +543,7 @@ public class JavaCompletionContributor extends CompletionContributor {
return isSecondCompletion;
}
PsiElement grand = parent.getParent();
if (grand instanceof PsiAnonymousClass) {
grand = grand.getParent();
}
......
......@@ -51,7 +51,7 @@ public class JavaCompletionStatistician extends CompletionStatistician{
}
PsiElement position = location.getCompletionParameters().getPosition();
if (SUPER_CALL.accepts(position) || ReferenceExpressionCompletionContributor.IN_SWITCH_LABEL.accepts(position)) {
if (SUPER_CALL.accepts(position) || JavaCompletionContributor.IN_SWITCH_LABEL.accepts(position)) {
return StatisticsInfo.EMPTY;
}
......
......@@ -98,13 +98,13 @@ public class JavaKeywordCompletion {
psiElement().withParent(psiElement(PsiReferenceExpression.class).withParent(
not(
or(
psiElement(PsiSwitchLabelStatement.class),
psiElement(PsiExpressionStatement.class),
psiElement(PsiPrefixExpression.class)
)
)
)),
not(psiElement().afterLeaf("."))
not(psiElement().afterLeaf(".")),
not(JavaCompletionContributor.IN_SWITCH_LABEL)
);
private final CompletionParameters myParameters;
......
......@@ -18,7 +18,6 @@ package com.intellij.codeInsight.completion;
import com.intellij.codeInsight.lookup.ExpressionLookupItem;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.patterns.ElementPattern;
import com.intellij.patterns.StandardPatterns;
import com.intellij.psi.*;
import com.intellij.psi.filters.*;
......@@ -44,21 +43,15 @@ import static com.intellij.patterns.PsiJavaPatterns.psiElement;
*/
public class ReferenceExpressionCompletionContributor {
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.completion.ReferenceExpressionCompletionContributor");
public static final ElementPattern<PsiElement> IN_SWITCH_LABEL =
psiElement().withSuperParent(2, psiElement(PsiSwitchLabelStatement.class).withSuperParent(2, PsiSwitchStatement.class));
@NotNull
@NotNull
static ElementFilter getReferenceFilter(PsiElement element, boolean allowRecursion) {
//throw foo
if (psiElement().withParent(psiElement(PsiReferenceExpression.class).withParent(PsiThrowStatement.class)).accepts(element)) {
return TrueFilter.INSTANCE;
}
if (psiElement().inside(
StandardPatterns.or(
psiElement(PsiAnnotationParameterList.class),
psiElement(PsiSwitchLabelStatement.class))
).accepts(element)) {
if (psiElement().inside(StandardPatterns.or(psiElement(PsiAnnotationParameterList.class), JavaCompletionContributor.IN_SWITCH_LABEL)).accepts(element)) {
return new ElementExtractorFilter(new AndFilter(
new ClassFilter(PsiField.class),
new ModifierFilter(PsiKeyword.STATIC, PsiKeyword.FINAL)
......@@ -176,7 +169,7 @@ public class ReferenceExpressionCompletionContributor {
@NotNull
public static Set<PsiField> findConstantsUsedInSwitch(@Nullable PsiElement position) {
return IN_SWITCH_LABEL.accepts(position)
return JavaCompletionContributor.IN_SWITCH_LABEL.accepts(position)
? findConstantsUsedInSwitch(ObjectUtils.assertNotNull(PsiTreeUtil.getParentOfType(position, PsiSwitchStatement.class)))
: Collections.emptySet();
}
......
......@@ -56,7 +56,7 @@ public class JavaMembersGetter extends MembersGetter {
addConstantsFromReferencedClassesInSwitch(results);
}
if (myPlace.getParent().getParent() instanceof PsiSwitchLabelStatement) {
if (JavaCompletionContributor.IN_SWITCH_LABEL.accepts(myPlace)) {
return; //non-enum values are processed above, enum values will be suggested by reference completion
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment