IDEA-142746 Completion should prefer local variable, not equals()

This commit is contained in:
peter
2015-07-17 15:01:43 +02:00
parent 0b51adb93c
commit 3241305b74
5 changed files with 40 additions and 26 deletions
@@ -76,10 +76,10 @@ public class JavaCompletionSorting {
if (!smart) {
ContainerUtil.addIfNotNull(afterPrefix, preferStatics(position, expectedTypes));
}
afterPrefix.add(new PreferByKindWeigher(type, position));
if (!smart && !afterNew) {
afterPrefix.add(new PreferExpected(false, expectedTypes));
}
afterPrefix.add(new PreferByKindWeigher(type, position));
ContainerUtil.addIfNotNull(afterPrefix, recursion(parameters, expectedTypes));
Collections.addAll(afterPrefix, new PreferSimilarlyEnding(expectedTypes),
new PreferNonGeneric(), new PreferAccessible(position), new PreferSimple());
@@ -38,6 +38,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Set;
import static com.intellij.patterns.PsiJavaPatterns.elementType;
import static com.intellij.patterns.PsiJavaPatterns.psiElement;
import static com.intellij.patterns.StandardPatterns.or;
@@ -173,6 +174,9 @@ public class PreferByKindWeigher extends LookupElementWeigher {
if (PsiKeyword.INTERFACE.equals(keyword) && psiElement().afterLeaf("@").accepts(myPosition)) {
return MyResult.improbableKeyword;
}
if (PsiKeyword.NULL.equals(keyword) && psiElement().afterLeaf(psiElement().withElementType(elementType().oneOf(JavaTokenType.EQEQ, JavaTokenType.NE))).accepts(myPosition)) {
return MyResult.probableKeyword;
}
}
if (item.as(CastingLookupElementDecorator.CLASS_CONDITION_KEY) != null) {
@@ -187,32 +191,32 @@ public class PreferByKindWeigher extends LookupElementWeigher {
return MyResult.superMethodParameters;
}
if (object instanceof PsiMethod) {
PsiClass containingClass = ((PsiMethod)object).getContainingClass();
if (containingClass != null && CommonClassNames.JAVA_UTIL_COLLECTIONS.equals(containingClass.getQualifiedName())) {
return MyResult.collectionFactory;
}
}
Boolean expectedTypeMember = item.getUserData(MembersGetter.EXPECTED_TYPE_MEMBER);
if (expectedTypeMember != null) {
return expectedTypeMember ? (object instanceof PsiField ? MyResult.expectedTypeConstant : MyResult.expectedTypeMethod) : MyResult.classNameOrGlobalStatic;
}
final JavaChainLookupElement chain = item.as(JavaChainLookupElement.CLASS_CONDITION_KEY);
if (chain != null) {
Object qualifier = chain.getQualifier().getObject();
if (qualifier instanceof PsiLocalVariable || qualifier instanceof PsiParameter) {
return MyResult.localOrParameter;
}
if (qualifier instanceof PsiField) {
return MyResult.qualifiedWithField;
}
if (isGetter(qualifier)) {
return MyResult.qualifiedWithGetter;
}
}
if (myCompletionType == CompletionType.SMART) {
if (object instanceof PsiMethod) {
PsiClass containingClass = ((PsiMethod)object).getContainingClass();
if (containingClass != null && CommonClassNames.JAVA_UTIL_COLLECTIONS.equals(containingClass.getQualifiedName())) {
return MyResult.collectionFactory;
}
}
Boolean expectedTypeMember = item.getUserData(MembersGetter.EXPECTED_TYPE_MEMBER);
if (expectedTypeMember != null) {
return expectedTypeMember ? (object instanceof PsiField ? MyResult.expectedTypeConstant : MyResult.expectedTypeMethod) : MyResult.classNameOrGlobalStatic;
}
final JavaChainLookupElement chain = item.as(JavaChainLookupElement.CLASS_CONDITION_KEY);
if (chain != null) {
Object qualifier = chain.getQualifier().getObject();
if (qualifier instanceof PsiLocalVariable || qualifier instanceof PsiParameter) {
return MyResult.localOrParameter;
}
if (qualifier instanceof PsiField) {
return MyResult.qualifiedWithField;
}
if (isGetter(qualifier)) {
return MyResult.qualifiedWithGetter;
}
}
if (object instanceof PsiField) return MyResult.field;
if (isGetter(object)) return MyResult.getter;
@@ -0,0 +1,5 @@
class Foo {
public Object get(Object event) {
if (e<caret>)
}
}
@@ -677,4 +677,8 @@ interface TxANotAnno {}
assert lookup.items[-1].lookupString == 'ritar'
}
public void testPreferLocalToExpectedTypedMethod() {
checkPreferredItems 0, 'event', 'equals'
}
}
@@ -1078,6 +1078,7 @@ public class ListUtils {
public void testKeywordSmartEnter() {
configure()
myFixture.assertPreferredCompletionItems 0, 'null', 'nullity'
myFixture.performEditorAction(IdeActions.ACTION_CHOOSE_LOOKUP_ITEM_COMPLETE_STATEMENT)
checkResult()
}