merge some java completion weighers into PreferByKindWeigher

This commit is contained in:
peter
2014-01-30 19:03:35 +01:00
parent c491893fa6
commit 6281d32c9c
3 changed files with 29 additions and 48 deletions
@@ -30,7 +30,6 @@ import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.codeStyle.NameUtil;
import com.intellij.psi.codeStyle.VariableKind;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.util.PropertyUtil;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.TypeConversionUtil;
@@ -60,9 +59,6 @@ public class JavaCompletionSorting {
List<LookupElementWeigher> afterProximity = new ArrayList<LookupElementWeigher>();
afterProximity.add(new PreferContainingSameWords(expectedTypes));
if (smart) {
afterProximity.add(new PreferFieldsAndGetters());
}
afterProximity.add(new PreferShorter(expectedTypes));
CompletionSorter sorter = CompletionSorter.defaultSorter(parameters, result.getPrefixMatcher());
@@ -84,8 +80,7 @@ public class JavaCompletionSorting {
afterPrefix.add(new PreferExpected(false, expectedTypes));
}
Collections.addAll(afterPrefix, new PreferByKindWeigher(type, position), new PreferSimilarlyEnding(expectedTypes),
new PreferNonGeneric(), new PreferAccessible(position), new PreferSimple(),
new PreferEnumConstants(parameters));
new PreferNonGeneric(), new PreferAccessible(position), new PreferSimple());
sorter = sorter.weighAfter("prefix", afterPrefix.toArray(new LookupElementWeigher[afterPrefix.size()]));
sorter = sorter.weighAfter("proximity", afterProximity.toArray(new LookupElementWeigher[afterProximity.size()]));
@@ -428,30 +423,6 @@ public class JavaCompletionSorting {
}
}
private static class PreferEnumConstants extends LookupElementWeigher {
private final CompletionParameters myParameters;
public PreferEnumConstants(CompletionParameters parameters) {
super("constants");
myParameters = parameters;
}
@NotNull
@Override
public Comparable weigh(@NotNull LookupElement element) {
if (element.getObject() instanceof PsiEnumConstant) return -2;
if (!(myParameters.getOriginalFile() instanceof PsiJavaFile)) return -1;
if (PsiKeyword.TRUE.equals(element.getLookupString()) || PsiKeyword.FALSE.equals(element.getLookupString())) {
boolean inReturn = PsiTreeUtil.getParentOfType(myParameters.getPosition(), PsiReturnStatement.class, false, PsiMember.class) != null;
return inReturn ? -2 : 0;
}
return -1;
}
}
private static class PreferExpected extends LookupElementWeigher {
private final boolean myAcceptClasses;
private final ExpectedTypeInfo[] myExpectedTypes;
@@ -517,21 +488,6 @@ public class JavaCompletionSorting {
}
}
private static class PreferFieldsAndGetters extends LookupElementWeigher {
public PreferFieldsAndGetters() {
super("fieldsAndGetters");
}
@NotNull
@Override
public Comparable weigh(@NotNull LookupElement element) {
final Object object = element.getObject();
if (object instanceof PsiField) return -2;
if (object instanceof PsiMethod && PropertyUtil.isSimplePropertyGetter((PsiMethod)object)) return -1;
return 0;
}
}
private static class PreferShorter extends LookupElementWeigher {
private final ExpectedTypeInfo[] myExpectedTypes;
@@ -28,6 +28,7 @@ import com.intellij.psi.util.InheritanceUtil;
import com.intellij.psi.util.PropertyUtil;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.proximity.KnownElementWeigher;
import org.jetbrains.annotations.NotNull;
import java.util.Set;
@@ -113,6 +114,8 @@ public class PreferByKindWeigher extends LookupElementWeigher {
qualifiedWithGetter,
superMethodParameters,
expectedTypeConstant,
field,
getter,
normal,
collectionFactory,
expectedTypeMethod,
@@ -135,6 +138,10 @@ public class PreferByKindWeigher extends LookupElementWeigher {
if (PsiKeyword.ELSE.equals(keyword) || PsiKeyword.FINALLY.equals(keyword)) {
return MyResult.probableKeyword;
}
if (PsiKeyword.TRUE.equals(keyword) || PsiKeyword.FALSE.equals(keyword)) {
boolean inReturn = PsiTreeUtil.getParentOfType(myPosition, PsiReturnStatement.class, false, PsiMember.class) != null;
return inReturn ? MyResult.probableKeyword : MyResult.normal;
}
}
if (myCompletionType == CompletionType.SMART) {
@@ -168,11 +175,14 @@ public class PreferByKindWeigher extends LookupElementWeigher {
if (qualifier instanceof PsiField) {
return MyResult.qualifiedWithField;
}
if (qualifier instanceof PsiMethod && PropertyUtil.isSimplePropertyGetter((PsiMethod)qualifier)) {
if (isGetter(qualifier)) {
return MyResult.qualifiedWithGetter;
}
}
if (object instanceof PsiField) return MyResult.field;
if (isGetter(object)) return MyResult.getter;
return MyResult.normal;
}
@@ -205,6 +215,15 @@ public class PreferByKindWeigher extends LookupElementWeigher {
return MyResult.normal;
}
private static boolean isGetter(Object object) {
if (!(object instanceof PsiMethod)) return false;
PsiMethod method = (PsiMethod)object;
if (!PropertyUtil.hasGetterName(method)) return false;
return !KnownElementWeigher.isGetClass(method);
}
private static boolean isLastStatement(PsiStatement statement) {
if (statement == null || !(statement.getParent() instanceof PsiCodeBlock)) {
return true;
@@ -49,12 +49,14 @@ public class KnownElementWeigher extends ProximityWeigher {
final PsiClass containingClass = method.getContainingClass();
if (containingClass != null) {
String methodName = method.getName();
if ("finalize".equals(methodName) || "registerNatives".equals(methodName) || "getClass".equals(methodName) ||
methodName.startsWith("wait") || methodName.startsWith("notify")) {
if ("finalize".equals(methodName) || "registerNatives".equals(methodName) || methodName.startsWith("wait") || methodName.startsWith("notify")) {
if (CommonClassNames.JAVA_LANG_OBJECT.equals(containingClass.getQualifiedName())) {
return -1;
}
}
if (isGetClass(method)) {
return -1;
}
if ("subSequence".equals(methodName)) {
if (CommonClassNames.JAVA_LANG_STRING.equals(containingClass.getQualifiedName())) {
return -1;
@@ -72,6 +74,10 @@ public class KnownElementWeigher extends ProximityWeigher {
return 0;
}
public static boolean isGetClass(PsiMethod method) {
return "getClass".equals(method.getName()) && method.getParameterList().getParametersCount() <= 0;
}
private static Comparable getJdkClassProximity(@Nullable PsiClass element) {
if (element == null || element.getContainingClass() != null) {
return 0;