mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-05 01:50:56 +07:00
disprefer *Impl classes in completion
This commit is contained in:
@@ -16,35 +16,25 @@
|
||||
package com.intellij.codeInsight.completion;
|
||||
|
||||
import com.intellij.codeInsight.lookup.LookupElement;
|
||||
import com.intellij.patterns.PsiJavaPatterns;
|
||||
import com.intellij.psi.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
public class PreferAbstractWeigher extends CompletionWeigher {
|
||||
public class DispreferImplementationsWeigher extends CompletionWeigher {
|
||||
|
||||
public Comparable weigh(@NotNull final LookupElement item, @NotNull final CompletionLocation location) {
|
||||
if (location.getCompletionType() != CompletionType.SMART) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final PsiElement position = location.getCompletionParameters().getPosition();
|
||||
final PsiElement parent = position.getParent();
|
||||
if (!(parent instanceof PsiJavaCodeReferenceElement) ||
|
||||
!(parent.getParent() instanceof PsiTypeElement) ||
|
||||
!(parent.getParent().getParent() instanceof PsiInstanceOfExpression)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final Object object = item.getObject();
|
||||
if (object instanceof PsiClass) {
|
||||
final PsiClass aClass = (PsiClass)object;
|
||||
if (aClass.isInterface()) {
|
||||
return 2;
|
||||
if (PsiJavaPatterns.psiElement().afterLeaf(PsiKeyword.NEW).accepts(location.getCompletionParameters().getPosition())) {
|
||||
return 0;
|
||||
}
|
||||
if (aClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
|
||||
return 1;
|
||||
|
||||
final String qname = ((PsiClass)object).getQualifiedName();
|
||||
if (qname != null && qname.endsWith("Impl")) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,6 +41,13 @@ public class ExplicitlyImportedWeigher extends ProximityWeigher {
|
||||
if (position == null){
|
||||
return null;
|
||||
}
|
||||
|
||||
final PsiFile elementFile = element.getContainingFile();
|
||||
final PsiFile positionFile = position.getContainingFile();
|
||||
if (positionFile != null && elementFile != null && positionFile.getOriginalFile().equals(elementFile.getOriginalFile())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (element instanceof PsiClass) {
|
||||
final String qname = ((PsiClass) element).getQualifiedName();
|
||||
if (qname != null) {
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
public class Aaaaaaa {
|
||||
private final String aaa;
|
||||
|
||||
Aaaaaaa(Object aabbb) {
|
||||
Xx<caret>
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class XxxImpl {}
|
||||
class Xxy {}
|
||||
@@ -0,0 +1,11 @@
|
||||
public class Aaaaaaa {
|
||||
private final String aaa;
|
||||
|
||||
Aaaaaaa(Object aabbb) {
|
||||
new Xx<caret>
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class XxxImpl {}
|
||||
class Xxy {}
|
||||
@@ -1,14 +0,0 @@
|
||||
package zzzzz;
|
||||
|
||||
public class Holder {
|
||||
{
|
||||
Foo foo = null;
|
||||
if (foo instanceof <caret>)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
interface Foo {}
|
||||
interface IFoo extends Foo {}
|
||||
abstract class Goo implements IFoo {}
|
||||
class Bar extends Goo {}
|
||||
@@ -63,8 +63,8 @@ public class HeavyNormalCompletionTest extends CompletionTestCase{
|
||||
LookupImpl lookup = (LookupImpl)LookupManager.getActiveLookup(myEditor);
|
||||
myItems = lookup.getItems().toArray(LookupElement.EMPTY_ARRAY);
|
||||
assertEquals(2, myItems.length);
|
||||
assertEquals("AxBxCxDxEx", myItems[0].getLookupString());
|
||||
assertEquals("AyByCyDyEy", myItems[1].getLookupString());
|
||||
assertEquals("AxBxCxDxEx", myItems[1].getLookupString());
|
||||
assertEquals("AyByCyDyEy", myItems[0].getLookupString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,8 +4,9 @@
|
||||
|
||||
package com.intellij.codeInsight.completion;
|
||||
|
||||
import com.intellij.codeInsight.lookup.impl.LookupImpl;
|
||||
import com.intellij.codeInsight.CodeInsightSettings;
|
||||
import com.intellij.codeInsight.lookup.impl.LookupImpl;
|
||||
import com.intellij.openapi.vfs.VfsUtil;
|
||||
import com.intellij.psi.PsiMethod;
|
||||
|
||||
@SuppressWarnings({"ALL"})
|
||||
@@ -105,6 +106,16 @@ public class NormalCompletionOrderingTest extends CompletionSortingTestCase {
|
||||
checkPreferredItems(0, "aabbb", "aaa");
|
||||
}
|
||||
|
||||
public void testDispreferImpls() throws Throwable {
|
||||
VfsUtil.saveText(getSourceRoot().createChildDirectory(this, "foo").createChildData(this, "Xxx.java"), "package foo; public class Xxx {}");
|
||||
checkPreferredItems(0, "Xxy", "Xxx", "XxxImpl");
|
||||
}
|
||||
|
||||
public void testDontDispreferImplsAfterNew() throws Throwable {
|
||||
VfsUtil.saveText(getSourceRoot().createChildDirectory(this, "foo").createChildData(this, "Xxx.java"), "package foo; public class Xxx {}");
|
||||
checkPreferredItems(0, "XxxImpl", "Xxy", "Xxx");
|
||||
}
|
||||
|
||||
public void testPreferLessParameters() throws Throwable {
|
||||
checkPreferredItems(0, "foo", "foo", "foo", "fox");
|
||||
assertEquals(0, ((PsiMethod)myItems[0].getObject()).getParameterList().getParametersCount());
|
||||
|
||||
@@ -203,10 +203,6 @@ public class SmartTypeCompletionOrderingTest extends CompletionSortingTestCase {
|
||||
checkPreferredItems(0, "Goo", "InnerGoo", "Bar", "AGoo");
|
||||
}
|
||||
|
||||
public void testPreferInterfaces() throws Throwable {
|
||||
checkPreferredItems(0, "IFoo", "Goo", "Bar");
|
||||
}
|
||||
|
||||
public void testLocalVariablesOutweighStats() throws Throwable {
|
||||
final LookupImpl lookup = invokeCompletion(BASE_PATH + "/" + getTestName(false) + ".java");
|
||||
assertPreferredItems(0, "foo", "param", "this", "bar", "goo");
|
||||
|
||||
@@ -800,10 +800,10 @@
|
||||
<weigher key="completion" implementationClass="com.intellij.codeInsight.completion.NameEndMatchingDegreeWeigher" id="nameEnd"
|
||||
order="after expectedType, before stats"/>
|
||||
|
||||
<weigher key="completion" implementationClass="com.intellij.codeInsight.completion.PreferAbstractWeigher" id="abstract"
|
||||
<weigher key="completion" implementationClass="com.intellij.codeInsight.completion.DispreferImplementationsWeigher" id="implementations"
|
||||
order="after prefix"/>
|
||||
<weigher key="completion" implementationClass="com.intellij.codeInsight.completion.PreferNonGenericWeigher" id="nonGeneric"
|
||||
order="after abstract"/>
|
||||
order="after implementations"/>
|
||||
<weigher key="completion" implementationClass="com.intellij.codeInsight.completion.PreferAccessibleWeigher" id="accessible"
|
||||
order="after nonGeneric"/>
|
||||
<weigher key="completion" implementationClass="com.intellij.codeInsight.completion.PreferSimpleWeigher" id="simple"
|
||||
|
||||
Reference in New Issue
Block a user