preselect X instead of XImpl, again

This commit is contained in:
peter
2012-05-02 13:44:50 +02:00
parent 3f049592aa
commit af378a02e4
5 changed files with 49 additions and 14 deletions
@@ -17,10 +17,13 @@ package com.intellij.codeInsight.completion;
import com.intellij.codeInsight.ExpectedTypeInfo;
import com.intellij.codeInsight.ExpectedTypeInfoImpl;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementWeigher;
import com.intellij.codeInsight.lookup.PsiTypeLookupItem;
import com.intellij.codeInsight.completion.impl.CompletionSorterImpl;
import com.intellij.codeInsight.completion.impl.LiftShorterItemsClassifier;
import com.intellij.codeInsight.lookup.*;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.patterns.PsiJavaPatterns;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
@@ -31,6 +34,7 @@ import com.intellij.psi.util.PropertyUtil;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.util.ProcessingContext;
import com.intellij.util.containers.ContainerUtil;
import gnu.trove.THashSet;
import org.jetbrains.annotations.NotNull;
@@ -75,6 +79,32 @@ public class JavaCompletionSorting {
CompletionSorter sorter = CompletionSorter.defaultSorter(parameters, result.getPrefixMatcher());
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, ProcessingContext context) {
if (super.shouldLift(shorterElement, longerElement, context)) {
return true;
}
Object object = shorterElement.getObject();
if (object instanceof PsiClass) {
PsiFile file = ((PsiClass)object).getContainingFile();
if (file != null) {
VirtualFile vFile = file.getOriginalFile().getVirtualFile();
if (vFile != null && fileIndex.isInSource(vFile)) {
return true;
}
}
}
return false;
}
});
}
});
}
if (smart) {
sorter = sorter.weighBefore("negativeStats", new PreferDefaultTypeWeigher(expectedTypes, parameters));
@@ -101,7 +101,7 @@ public class NormalCompletionOrderingTest extends CompletionSortingTestCase {
public void testDispreferImpls() throws Throwable {
myFixture.addClass("package foo; public class Xxx {}");
configureSecondCompletion();
assertPreferredItems(1, "Xxx", "XxxEx", "XxxImpl", "Xxy");
assertPreferredItems(0, "Xxx", "XxxEx", "XxxImpl", "Xxy");
}
public void testPreferOwnInnerClasses() throws Throwable {
@@ -128,7 +128,7 @@ public class NormalCompletionOrderingTest extends CompletionSortingTestCase {
myFixture.addClass("package foo; public interface XaYa {}");
myFixture.addClass("package foo; public interface XyYa {}");
configureSecondCompletion();
assertPreferredItems(1, "XaYa", "XaYaEx", "XaYaImpl", "XyYa", "XyYaXa");
assertPreferredItems(0, "XaYa", "XaYaEx", "XaYaImpl", "XyYa", "XyYaXa");
}
public void testPreferLessParameters() throws Throwable {
@@ -219,7 +219,7 @@ public class NormalCompletionOrderingTest extends CompletionSortingTestCase {
}
public void testPreferClassStaticMembers() {
checkPreferredItems(1, "Zoo", "Zoo.A", "Zoo.B", "Zoo.C", "Zoo.D", "Zoo.E", "Zoo.F", "Zoo.G", "Zoo.H");
checkPreferredItems(0, "Zoo", "Zoo.A", "Zoo.B", "Zoo.C", "Zoo.D", "Zoo.E", "Zoo.F", "Zoo.G", "Zoo.H");
}
public void testPreferFinallyToFinal() {
@@ -21,7 +21,7 @@ public class SmartTypeCompletionOrderingTest extends CompletionSortingTestCase {
}
public void testJComponentAdd() throws Throwable {
checkPreferredItems(0, "name", "b", "fooBean239", "this", "getName");
checkPreferredItems(0, "name", "b", "foo", "fooBean239", "this", "getName");
}
public void testJComponentAddNew() throws Throwable {
@@ -292,7 +292,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);
return new LiftShorterItemsClassifier(next, new LiftShorterItemsClassifier.LiftingCondition());
}
});
}
@@ -30,14 +30,16 @@ import java.util.*;
/**
* @author peter
*/
class LiftShorterItemsClassifier extends Classifier<LookupElement> {
public class LiftShorterItemsClassifier extends Classifier<LookupElement> {
private final TreeSet<String> mySortedStrings;
private final MultiMap<String, LookupElement> myElements;
private final MultiMap<String, String> myPrefixes;
private final Classifier<LookupElement> myNext;
private final LiftingCondition myCondition;
public LiftShorterItemsClassifier(Classifier<LookupElement> next) {
public LiftShorterItemsClassifier(Classifier<LookupElement> next, LiftingCondition condition) {
myNext = next;
myCondition = condition;
mySortedStrings = new TreeSet<String>();
myElements = new MultiMap<String, LookupElement>();
myPrefixes = new MultiMap<String, String>();
@@ -76,9 +78,6 @@ class LiftShorterItemsClassifier extends Classifier<LookupElement> {
@Override
public Iterable<LookupElement> classify(Iterable<LookupElement> source, ProcessingContext context) {
if (context.get(CompletionLookupArranger.PURE_RELEVANCE) == Boolean.TRUE) {
return myNext.classify(source, context);
}
return liftShorterElements(source, new THashSet<LookupElement>(TObjectHashingStrategy.IDENTITY), context);
}
@@ -99,7 +98,7 @@ class LiftShorterItemsClassifier extends Classifier<LookupElement> {
for (String prefix : prefixes) {
List<LookupElement> shorter = new SmartList<LookupElement>();
for (LookupElement shorterElement : myElements.get(prefix)) {
if (srcSet.contains(shorterElement) && processed.add(shorterElement)) {
if (srcSet.contains(shorterElement) && myCondition.shouldLift(shorterElement, element, context) && processed.add(shorterElement)) {
shorter.add(shorterElement);
}
}
@@ -134,4 +133,10 @@ class LiftShorterItemsClassifier extends Classifier<LookupElement> {
}
myNext.describeItems(map, context);
}
public static class LiftingCondition {
public boolean shouldLift(LookupElement shorterElement, LookupElement longerElement, ProcessingContext context) {
return context.get(CompletionLookupArranger.PURE_RELEVANCE) != Boolean.TRUE;
}
}
}