mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-20 05:21:29 +07:00
IDEA-155572 Basic completion could prefer false when boolean is expected
This commit is contained in:
@@ -73,7 +73,7 @@ public class JavaCompletionSorting {
|
||||
}
|
||||
|
||||
List<LookupElementWeigher> afterPrefix = ContainerUtil.newArrayList();
|
||||
afterPrefix.add(new PreferByKindWeigher(type, position));
|
||||
afterPrefix.add(new PreferByKindWeigher(type, position, expectedTypes));
|
||||
if (!smart) {
|
||||
ContainerUtil.addIfNotNull(afterPrefix, preferStatics(position, expectedTypes));
|
||||
if (!afterNew) {
|
||||
|
||||
@@ -17,6 +17,7 @@ package com.intellij.codeInsight.completion;
|
||||
|
||||
import com.intellij.codeInsight.AnnotationTargetUtil;
|
||||
import com.intellij.codeInsight.ExceptionUtil;
|
||||
import com.intellij.codeInsight.ExpectedTypeInfo;
|
||||
import com.intellij.codeInsight.lookup.LookupElement;
|
||||
import com.intellij.codeInsight.lookup.LookupElementWeigher;
|
||||
import com.intellij.openapi.util.Condition;
|
||||
@@ -34,6 +35,7 @@ import com.intellij.psi.util.proximity.KnownElementWeigher;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -70,13 +72,15 @@ public class PreferByKindWeigher extends LookupElementWeigher {
|
||||
private final PsiElement myPosition;
|
||||
private final Set<PsiField> myNonInitializedFields;
|
||||
private final Condition<PsiClass> myRequiredSuper;
|
||||
private final ExpectedTypeInfo[] myExpectedTypes;
|
||||
|
||||
public PreferByKindWeigher(CompletionType completionType, final PsiElement position) {
|
||||
public PreferByKindWeigher(CompletionType completionType, final PsiElement position, ExpectedTypeInfo[] expectedTypes) {
|
||||
super("kind");
|
||||
myCompletionType = completionType;
|
||||
myPosition = position;
|
||||
myNonInitializedFields = CheckInitialized.getNonInitializedFields(position);
|
||||
myRequiredSuper = createSuitabilityCondition(position);
|
||||
myExpectedTypes = expectedTypes;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -175,9 +179,14 @@ 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)) && myCompletionType == CompletionType.SMART) {
|
||||
boolean inReturn = psiElement().withParents(PsiReferenceExpression.class, PsiReturnStatement.class).accepts(myPosition);
|
||||
return inReturn ? MyResult.probableKeyword : MyResult.normal;
|
||||
if (PsiKeyword.TRUE.equals(keyword) || PsiKeyword.FALSE.equals(keyword)) {
|
||||
if (myCompletionType == CompletionType.SMART) {
|
||||
boolean inReturn = psiElement().withParents(PsiReferenceExpression.class, PsiReturnStatement.class).accepts(myPosition);
|
||||
return inReturn ? MyResult.probableKeyword : MyResult.normal;
|
||||
} else if (Arrays.stream(myExpectedTypes).anyMatch(info -> PsiType.BOOLEAN.isConvertibleFrom(info.getDefaultType())) &&
|
||||
PsiTreeUtil.getParentOfType(myPosition, PsiIfStatement.class, true, PsiStatement.class, PsiMember.class) == null) {
|
||||
return MyResult.probableKeyword;
|
||||
}
|
||||
}
|
||||
if (PsiKeyword.INTERFACE.equals(keyword) && psiElement().afterLeaf("@").accepts(myPosition)) {
|
||||
return MyResult.improbableKeyword;
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
class Test {
|
||||
{
|
||||
String factory = null;
|
||||
bar(fa<caret>);
|
||||
}
|
||||
void bar(boolean b) {}
|
||||
}
|
||||
@@ -7,6 +7,6 @@ class A{
|
||||
public void foo(boolean bol){}
|
||||
|
||||
{
|
||||
foo(<caret>equals);
|
||||
foo(x<caret>equals);
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,6 @@ class A{
|
||||
public void foo(boolean bol){}
|
||||
|
||||
{
|
||||
foo(<caret>true);
|
||||
foo(x<caret>true);
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,6 @@ class A{
|
||||
public void foo(boolean bol){}
|
||||
|
||||
{
|
||||
foo(<caret>equals(null));
|
||||
foo(x<caret>equals(null));
|
||||
}
|
||||
}
|
||||
@@ -754,4 +754,8 @@ interface TxANotAnno {}
|
||||
checkPreferredItems 0, 'class'
|
||||
}
|
||||
|
||||
public void testPreferBooleanKeywordsWhenExpectedBoolean() {
|
||||
checkPreferredItems 0, 'false', 'factory'
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -814,7 +814,7 @@ public class ListUtils {
|
||||
|
||||
public void testDoubleFalse() throws Throwable {
|
||||
configureByFile(getTestName(false) + ".java");
|
||||
assertFirstStringItems("fefefef", "false", "float", "finalize");
|
||||
assertFirstStringItems("false", "fefefef", "float", "finalize");
|
||||
}
|
||||
|
||||
public void testSameNamedVariableInNestedClasses() throws Throwable {
|
||||
|
||||
@@ -257,7 +257,7 @@ public class SmartTypeCompletionOrderingTest extends CompletionSortingTestCase {
|
||||
invokeCompletion(getTestName(false) + ".java");
|
||||
assertPreferredItems(0, "foo", "false");
|
||||
myFixture.type(',');
|
||||
complete();
|
||||
myFixture.complete(CompletionType.SMART)
|
||||
assertPreferredItems(0, "bar", "foo", "equals", "false", "true");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user