prefer better matching constants to methods

This commit is contained in:
peter
2012-05-24 19:29:49 +02:00
parent 382b5d03f8
commit efdf0ca923
3 changed files with 29 additions and 12 deletions
@@ -60,7 +60,7 @@ public class JavaCompletionSorting {
List<LookupElementWeigher> afterNegativeStats = new ArrayList<LookupElementWeigher>();
if (!smart) {
ContainerUtil.addIfNotNull(afterNegativeStats, preferStatics(position));
ContainerUtil.addIfNotNull(afterNegativeStats, preferStatics(position, expectedTypes));
}
afterNegativeStats.add(new PreferLocalVariablesLiteralsAndAnnoMethodsWeigher(type, position));
ContainerUtil.addIfNotNull(afterNegativeStats, recursion(parameters, expectedTypes));
@@ -126,7 +126,7 @@ public class JavaCompletionSorting {
}
@Nullable
private static LookupElementWeigher preferStatics(PsiElement position) {
private static LookupElementWeigher preferStatics(PsiElement position, final ExpectedTypeInfo[] infos) {
if (PsiTreeUtil.getParentOfType(position, PsiDocComment.class) != null) {
return null;
}
@@ -149,7 +149,7 @@ public class JavaCompletionSorting {
if (o instanceof PsiKeyword) return -3;
if (!(o instanceof PsiMember)) return 0;
if (((PsiMember)o).hasModifierProperty(PsiModifier.STATIC)) {
if (((PsiMember)o).hasModifierProperty(PsiModifier.STATIC) && !hasNonVoid(infos)) {
if (o instanceof PsiMethod) return -5;
if (o instanceof PsiField) return -4;
}
@@ -179,14 +179,7 @@ public class JavaCompletionSorting {
}
}
boolean hasNonVoid = false;
for (ExpectedTypeInfo info : expectedInfos) {
if (!PsiType.VOID.equals(info.getType())) {
hasNonVoid = true;
}
}
if (hasNonVoid) {
if (hasNonVoid(expectedInfos)) {
if (item.getObject() instanceof PsiKeyword) {
String keyword = ((PsiKeyword)item.getObject()).getText();
if (PsiKeyword.NEW.equals(keyword) || PsiKeyword.NULL.equals(keyword)) {
@@ -201,6 +194,16 @@ public class JavaCompletionSorting {
return ExpectedTypeMatching.normal;
}
private static boolean hasNonVoid(ExpectedTypeInfo[] expectedInfos) {
boolean hasNonVoid = false;
for (ExpectedTypeInfo info : expectedInfos) {
if (!PsiType.VOID.equals(info.getType())) {
hasNonVoid = true;
}
}
return hasNonVoid;
}
@Nullable
private static String getLookupObjectName(Object o) {
if (o instanceof PsiVariable) {
@@ -0,0 +1,10 @@
class Foo {
String s = Util.se<caret>
}
class Util {
public static final String serial = "serialVersionUID";
public static Runnable superExpressionInIllegalContext() {}
}
@@ -87,7 +87,7 @@ public class NormalCompletionOrderingTest extends CompletionSortingTestCase {
final String path = getTestName(false) + ".java";
myFixture.configureByFile(path);
myFixture.complete(CompletionType.BASIC, 2);
assertPreferredItems(0, "booleanMethod", "voidMethod", "registerNatives", "BOOLEAN", "AN_OBJECT");
assertPreferredItems(0, "BOOLEAN", "booleanMethod", "AN_OBJECT", "voidMethod", "registerNatives");
}
public void testDispreferDeclared() throws Throwable {
@@ -314,4 +314,8 @@ public class NormalCompletionOrderingTest extends CompletionSortingTestCase {
checkPreferredItems 0, 'noo', 'new', 'null', 'noo2', 'notify', 'notifyAll'
}
public void testPreferBetterMatchingConstantToMethods() {
checkPreferredItems 0, 'serial', 'superExpressionInIllegalContext'
}
}