IDEA-145768 MagicConstant completion in incomplete code

This commit is contained in:
peter
2015-10-02 14:42:41 +02:00
parent feaf208a2c
commit 0eb643a725
2 changed files with 30 additions and 12 deletions
@@ -45,7 +45,7 @@ public class MagicCompletionContributor extends CompletionContributor {
private static final ElementPattern<PsiElement> IN_METHOD_CALL_ARGUMENT =
psiElement().withParent(psiElement(PsiReferenceExpression.class).inside(psiElement(PsiExpressionList.class).withParent(PsiCall.class)));
private static final ElementPattern<PsiElement> IN_BINARY_COMPARISON =
psiElement().withParent(psiElement(PsiReferenceExpression.class).inside(psiElement(PsiBinaryExpression.class)));
psiElement().withParent(psiElement(PsiReferenceExpression.class).inside(psiElement(PsiPolyadicExpression.class)));
private static final ElementPattern<PsiElement> IN_ASSIGNMENT =
psiElement().withParent(psiElement(PsiReferenceExpression.class).inside(psiElement(PsiAssignmentExpression.class)));
private static final ElementPattern<PsiElement> IN_RETURN =
@@ -129,17 +129,13 @@ public class MagicCompletionContributor extends CompletionContributor {
}
}
else if (IN_BINARY_COMPARISON.accepts(pos)) {
PsiBinaryExpression exp = PsiTreeUtil.getParentOfType(pos, PsiBinaryExpression.class);
PsiPolyadicExpression exp = PsiTreeUtil.getParentOfType(pos, PsiPolyadicExpression.class);
if (exp != null && (exp.getOperationTokenType() == JavaTokenType.EQEQ || exp.getOperationTokenType() == JavaTokenType.NE)) {
PsiExpression l = exp.getLOperand();
PsiModifierListOwner resolved = resolveExpression(l);
if (resolved != null) {
result.add(Pair.create(resolved, l.getType()));
}
PsiExpression r = exp.getROperand();
resolved = resolveExpression(r);
if (r != null && resolved != null) {
result.add(Pair.create(resolved, r.getType()));
for (PsiExpression operand : exp.getOperands()) {
PsiModifierListOwner resolved = resolveExpression(operand);
if (resolved != null) {
result.add(Pair.create(resolved, operand.getType()));
}
}
}
}
@@ -54,7 +54,7 @@ class Foo {
myFixture.configureByText "a.java", """
class Bar {
static void foo(ModifierList ml) {
static void foo() {
if (getConstant() == <caret>) {}
}
@@ -75,6 +75,28 @@ interface Foo {
myFixture.assertPreferredCompletionItems 0, 'BAR', 'FOO'
}
public void "test magic constant in equality before another equality"() {
addMagicConstant()
myFixture.configureByText "a.java", """
class Bar {
static void foo() {
if (getConstant() == <caret>getConstant() == 2) {}
}
@org.intellij.lang.annotations.MagicConstant(flagsFromClass = Foo.class)
public native int getConstant();
}
interface Foo {
int FOO = 1;
int BAR = 2;
}
"""
myFixture.complete(CompletionType.SMART)
myFixture.assertPreferredCompletionItems 0, 'BAR', 'FOO'
}
private PsiClass addModifierList() {
addMagicConstant()