mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
IDEA-97738 Completion should respect enum parameters
This commit is contained in:
@@ -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<LookupElement>("liftShorterClasses") {
|
||||
@Override
|
||||
public Classifier<LookupElement> createClassifier(Classifier<LookupElement> 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<LookupElementWeigher> 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<LookupElement> {
|
||||
final ProjectFileIndex fileIndex;
|
||||
private final PsiElement myPosition;
|
||||
|
||||
public LiftShorterClasses(PsiElement position) {
|
||||
super("liftShorterClasses");
|
||||
myPosition = position;
|
||||
fileIndex = ProjectRootManager.getInstance(myPosition.getProject()).getFileIndex();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Classifier<LookupElement> createClassifier(Classifier<LookupElement> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
public class Foo {
|
||||
void method(MyEnum e) { }
|
||||
|
||||
{
|
||||
method(<caret>);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
enum MyEnum { foo, bar }
|
||||
+8
-4
@@ -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')
|
||||
|
||||
+1
-1
@@ -261,7 +261,7 @@ public class CompletionServiceImpl extends CompletionService{
|
||||
return sorter.withClassifier("priority", true, new ClassifierFactory<LookupElement>("liftShorter") {
|
||||
@Override
|
||||
public Classifier<LookupElement> createClassifier(final Classifier<LookupElement> next) {
|
||||
return new LiftShorterItemsClassifier(next, new LiftShorterItemsClassifier.LiftingCondition(), false);
|
||||
return new LiftShorterItemsClassifier("liftShorter", next, new LiftShorterItemsClassifier.LiftingCondition(), false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
+4
-2
@@ -48,12 +48,14 @@ public class LiftShorterItemsClassifier extends Classifier<LookupElement> {
|
||||
};
|
||||
private final Map<LookupElement, FList<LookupElement>> myToLift = newIdentityHashMap();
|
||||
private final IdentityHashMap<FList<LookupElement>, IdentityHashMap<LookupElement, FList<LookupElement>>> myPrepends = newIdentityHashMap();
|
||||
private final String myName;
|
||||
private final Classifier<LookupElement> myNext;
|
||||
private final LiftingCondition myCondition;
|
||||
private final boolean myLiftBefore;
|
||||
private int myCount = 0;
|
||||
|
||||
public LiftShorterItemsClassifier(Classifier<LookupElement> next, LiftingCondition condition, boolean liftBefore) {
|
||||
public LiftShorterItemsClassifier(String name, Classifier<LookupElement> next, LiftingCondition condition, boolean liftBefore) {
|
||||
myName = name;
|
||||
myNext = next;
|
||||
myCondition = condition;
|
||||
myLiftBefore = liftBefore;
|
||||
@@ -150,7 +152,7 @@ public class LiftShorterItemsClassifier extends Classifier<LookupElement> {
|
||||
builder.append(", ");
|
||||
}
|
||||
|
||||
builder.append("liftShorter=").append(lifted.contains(element));
|
||||
builder.append(myName).append("=").append(lifted.contains(element));
|
||||
}
|
||||
}
|
||||
myNext.describeItems(map, context);
|
||||
|
||||
Reference in New Issue
Block a user