From ecfddb515ceed5cd7cb94a3e692c592b03f15153 Mon Sep 17 00:00:00 2001 From: peter Date: Fri, 21 Dec 2012 13:11:01 +0100 Subject: [PATCH] IDEA-97738 Completion should respect enum parameters --- .../completion/JavaCompletionSorting.java | 67 ++++++++++--------- .../normalSorting/PreferEnumConstants.java | 11 +++ .../NormalCompletionOrderingTest.groovy | 12 ++-- .../impl/CompletionServiceImpl.java | 2 +- .../impl/LiftShorterItemsClassifier.java | 6 +- 5 files changed, 61 insertions(+), 37 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/completion/normalSorting/PreferEnumConstants.java diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionSorting.java b/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionSorting.java index 437de0c7ea9b..61738b40f4d4 100644 --- a/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionSorting.java +++ b/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionSorting.java @@ -79,42 +79,17 @@ public class JavaCompletionSorting { if (!smart && afterNew) { sorter = sorter.weighBefore("liftShorter", new PreferExpected(true, expectedTypes)); } else { - final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(position.getProject()).getFileIndex(); - sorter = ((CompletionSorterImpl)sorter).withClassifier("liftShorter", true, new ClassifierFactory("liftShorterClasses") { - @Override - public Classifier createClassifier(Classifier next) { - return new LiftShorterItemsClassifier(next, new LiftShorterItemsClassifier.LiftingCondition() { - @Override - public boolean shouldLift(LookupElement shorterElement, LookupElement longerElement) { - Object object = shorterElement.getObject(); - if (object instanceof PsiClass) { - PsiClass psiClass = (PsiClass)object; - PsiFile file = psiClass.getContainingFile(); - if (file != null) { - VirtualFile vFile = file.getOriginalFile().getVirtualFile(); - if (vFile != null && fileIndex.isInSource(vFile)) { - return true; - } - } - Object longerObject = longerElement.getObject(); - if (longerObject instanceof PsiMember && - psiClass.getManager().areElementsEquivalent(psiClass, ((PsiMember)longerObject).getContainingClass())) { - return true; - } - } - return false; - } - }, true); - } - }); + sorter = ((CompletionSorterImpl)sorter).withClassifier("liftShorterClasses", true, new LiftShorterClasses(position)); } List afterPrefix = ContainerUtil.newArrayList(); - afterPriority.add(new PreferByKindWeigher(type, position, true)); - afterPrefix.add(new PreferByKindWeigher(type, position, false)); + if (smart) { + afterPriority.add(new PreferByKindWeigher(type, position, true)); + } if (!smart && !afterNew) { afterPrefix.add(new PreferExpected(false, expectedTypes)); } + afterPrefix.add(new PreferByKindWeigher(type, position, false)); Collections.addAll(afterPrefix, new PreferNonGeneric(), new PreferAccessible(position), new PreferSimple(), new PreferEnumConstants(parameters)); @@ -567,4 +542,36 @@ public class JavaCompletionSorting { return 0; } } + + private static class LiftShorterClasses extends ClassifierFactory { + final ProjectFileIndex fileIndex; + private final PsiElement myPosition; + + public LiftShorterClasses(PsiElement position) { + super("liftShorterClasses"); + myPosition = position; + fileIndex = ProjectRootManager.getInstance(myPosition.getProject()).getFileIndex(); + } + + @Override + public Classifier createClassifier(Classifier next) { + return new LiftShorterItemsClassifier("liftShorterClasses", next, new LiftShorterItemsClassifier.LiftingCondition() { + @Override + public boolean shouldLift(LookupElement shorterElement, LookupElement longerElement) { + Object object = shorterElement.getObject(); + if (object instanceof PsiClass && longerElement.getObject() instanceof PsiClass) { + PsiClass psiClass = (PsiClass)object; + PsiFile file = psiClass.getContainingFile(); + if (file != null) { + VirtualFile vFile = file.getOriginalFile().getVirtualFile(); + if (vFile != null && fileIndex.isInSource(vFile)) { + return true; + } + } + } + return false; + } + }, true); + } + } } diff --git a/java/java-tests/testData/codeInsight/completion/normalSorting/PreferEnumConstants.java b/java/java-tests/testData/codeInsight/completion/normalSorting/PreferEnumConstants.java new file mode 100644 index 000000000000..0c2ecd407609 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/normalSorting/PreferEnumConstants.java @@ -0,0 +1,11 @@ +public class Foo { + void method(MyEnum e) { } + + { + method(); + } + + +} + +enum MyEnum { foo, bar } \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/completion/NormalCompletionOrderingTest.groovy b/java/java-tests/testSrc/com/intellij/codeInsight/completion/NormalCompletionOrderingTest.groovy index c83bf7e9073e..5bec97eec3ea 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/NormalCompletionOrderingTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/NormalCompletionOrderingTest.groovy @@ -200,7 +200,7 @@ public class NormalCompletionOrderingTest extends CompletionSortingTestCase { } public void testLocalVarsOverMethods() { - checkPreferredItems(0, "value", "validate", "validateTree", "valueOf"); + checkPreferredItems(0, "value", "validate", "validateTree"); } public void testCurrentClassBest() { @@ -244,7 +244,7 @@ public class NormalCompletionOrderingTest extends CompletionSortingTestCase { } public void testPreferClassStaticMembers() { - checkPreferredItems(0, "Zoo", "Zoo.A", "Zoo.B", "Zoo.C", "Zoo.D", "Zoo.E", "Zoo.F", "Zoo.G", "Zoo.H"); + checkPreferredItems(0, "Zoo.A", "Zoo", "Zoo.B", "Zoo.C", "Zoo.D", "Zoo.E", "Zoo.F", "Zoo.G", "Zoo.H"); } public void testPreferFinallyToFinal() { @@ -259,6 +259,10 @@ public class NormalCompletionOrderingTest extends CompletionSortingTestCase { checkPreferredItems(0, "private", "protected", "public", "paaa", "paab"); } + public void testPreferEnumConstants() { + checkPreferredItems(0, "MyEnum.bar", "MyEnum", "MyEnum.foo"); + } + public void testPreferElse() { checkPreferredItems(0, "else", "element"); } @@ -389,7 +393,7 @@ import java.lang.annotation.Target; } public void testPreferClassToItsConstants() { - checkPreferredItems 0, 'Calendar', 'Calendar.FIELD_COUNT' + checkPreferredItems 0, 'Calendar.FIELD_COUNT', 'Calendar', 'Calendar.AM' } public void testPreferLocalsToStaticsInSecondCompletion() { @@ -453,7 +457,7 @@ import java.lang.annotation.Target; repeatCompletion 'b' myFixture.completeBasic(); - assertPreferredItems(0, 'return', '_boo2', '_foo2', '_boo1', '_foo1', '_goo1', '_goo2') + assertPreferredItems(0, '_boo2', '_foo2', 'return', '_boo1', '_foo1', '_goo1', '_goo2') myFixture.type('_'); assertPreferredItems(0, '_boo2', '_foo2', '_boo1', '_foo1', '_goo1', '_goo2') myFixture.type('g') diff --git a/platform/lang-impl/src/com/intellij/codeInsight/completion/impl/CompletionServiceImpl.java b/platform/lang-impl/src/com/intellij/codeInsight/completion/impl/CompletionServiceImpl.java index a2a72a56fbd9..4751b5ab36c5 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/completion/impl/CompletionServiceImpl.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/completion/impl/CompletionServiceImpl.java @@ -261,7 +261,7 @@ public class CompletionServiceImpl extends CompletionService{ return sorter.withClassifier("priority", true, new ClassifierFactory("liftShorter") { @Override public Classifier createClassifier(final Classifier next) { - return new LiftShorterItemsClassifier(next, new LiftShorterItemsClassifier.LiftingCondition(), false); + return new LiftShorterItemsClassifier("liftShorter", next, new LiftShorterItemsClassifier.LiftingCondition(), false); } }); } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/completion/impl/LiftShorterItemsClassifier.java b/platform/lang-impl/src/com/intellij/codeInsight/completion/impl/LiftShorterItemsClassifier.java index f73ca1d941df..1d4e3f00b804 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/completion/impl/LiftShorterItemsClassifier.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/completion/impl/LiftShorterItemsClassifier.java @@ -48,12 +48,14 @@ public class LiftShorterItemsClassifier extends Classifier { }; private final Map> myToLift = newIdentityHashMap(); private final IdentityHashMap, IdentityHashMap>> myPrepends = newIdentityHashMap(); + private final String myName; private final Classifier myNext; private final LiftingCondition myCondition; private final boolean myLiftBefore; private int myCount = 0; - public LiftShorterItemsClassifier(Classifier next, LiftingCondition condition, boolean liftBefore) { + public LiftShorterItemsClassifier(String name, Classifier next, LiftingCondition condition, boolean liftBefore) { + myName = name; myNext = next; myCondition = condition; myLiftBefore = liftBefore; @@ -150,7 +152,7 @@ public class LiftShorterItemsClassifier extends Classifier { builder.append(", "); } - builder.append("liftShorter=").append(lifted.contains(element)); + builder.append(myName).append("=").append(lifted.contains(element)); } } myNext.describeItems(map, context);