IDEA-239386 Completion for $Class$.class should be suggested first if Class parameter is expected

GitOrigin-RevId: ba80deade49dc5cabc96cd89eb833fe024f56281
This commit is contained in:
Peter Gromov
2020-04-29 20:17:20 +00:00
committed by intellij-monorepo-bot
parent 5e9729ab5b
commit 33ae0aa674
3 changed files with 21 additions and 5 deletions
@@ -28,7 +28,10 @@ import gnu.trove.TObjectIntHashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static com.intellij.patterns.PsiJavaPatterns.psiElement;
@@ -71,10 +74,10 @@ public class JavaCompletionSorting {
if (smart) {
afterStats.add(new PreferDefaultTypeWeigher(expectedTypes, parameters, true));
} else {
ContainerUtil.addIfNotNull(afterStats, preferStatics(position, expectedTypes));
if (!afterNew) {
afterStats.add(new PreferExpected(false, expectedTypes, position));
}
ContainerUtil.addIfNotNull(afterStats, preferStatics(position, expectedTypes));
}
ContainerUtil.addIfNotNull(afterStats, recursion(parameters, expectedTypes));
@@ -188,7 +191,10 @@ public class JavaCompletionSorting {
};
}
private static ExpectedTypeMatching getExpectedTypeMatching(LookupElement item, ExpectedTypeInfo[] expectedInfos, @Nullable String expectedMemberName) {
private static ExpectedTypeMatching getExpectedTypeMatching(LookupElement item,
ExpectedTypeInfo[] expectedInfos,
@Nullable String expectedMemberName,
@NotNull PsiElement position) {
PsiType itemType = JavaCompletionUtil.getLookupElementType(item);
if (itemType != null) {
@@ -216,7 +222,8 @@ public class JavaCompletionSorting {
if (hasNonVoid(expectedInfos)) {
if (item.getObject() instanceof PsiKeyword) {
String keyword = ((PsiKeyword)item.getObject()).getText();
if (PsiKeyword.NEW.equals(keyword) || PsiKeyword.NULL.equals(keyword)) {
if (PsiKeyword.NEW.equals(keyword) && !(position.getParent() instanceof PsiMethodReferenceExpression) ||
PsiKeyword.NULL.equals(keyword)) {
return ExpectedTypeMatching.maybeExpected;
}
}
@@ -527,6 +534,7 @@ public class JavaCompletionSorting {
private static class PreferExpected extends LookupElementWeigher {
private final boolean myConstructorPossible;
private final ExpectedTypeInfo[] myExpectedTypes;
private final PsiElement myPosition;
private final List<PsiType> myExpectedClasses = new SmartList<>();
private final String myExpectedMemberName;
@@ -534,6 +542,7 @@ public class JavaCompletionSorting {
super("expectedType");
myConstructorPossible = constructorPossible;
myExpectedTypes = expectedTypes;
myPosition = position;
for (ExpectedTypeInfo info : expectedTypes) {
ContainerUtil.addIfNotNull(myExpectedClasses, PsiUtil.substituteTypeParameter(info.getDefaultType(), CommonClassNames.JAVA_LANG_CLASS, 0, false));
}
@@ -570,7 +579,7 @@ public class JavaCompletionSorting {
}
}
return getExpectedTypeMatching(item, myExpectedTypes, myExpectedMemberName);
return getExpectedTypeMatching(item, myExpectedTypes, myExpectedMemberName, myPosition);
}
}
@@ -0,0 +1,3 @@
class C {
Class<?> c = String.<caret>
}
@@ -91,6 +91,10 @@ class NormalCompletionOrderingTest extends CompletionSortingTestCase {
checkPreferredItems(0, "booleanMethod", "voidMethod", "AN_OBJECT", "BOOLEAN", "class")
}
void testPreferClassLiteralWhenClassIsExpected() {
checkPreferredItems(0, "class")
}
void testJComponentInstanceMembers() throws Throwable {
checkPreferredItems(0, "getAccessibleContext", "getUI", "getUIClassID")
}