mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-143839 No smart completion in method return type
This commit is contained in:
@@ -61,6 +61,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
import java.util.*;
|
||||
|
||||
import static com.intellij.patterns.PsiJavaPatterns.*;
|
||||
import static com.intellij.util.ObjectUtils.assertNotNull;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
@@ -233,6 +234,20 @@ public class JavaCompletionContributor extends CompletionContributor {
|
||||
new JavaInheritorsGetter(ConstructorInsertHandler.BASIC_INSTANCE).generateVariants(parameters, matcher, inheritors);
|
||||
}
|
||||
|
||||
if (MethodReturnTypeProvider.IN_METHOD_RETURN_TYPE.accepts(position)) {
|
||||
MethodReturnTypeProvider.addProbableReturnTypes(parameters, new Consumer<LookupElement>() {
|
||||
@Override
|
||||
public void consume(LookupElement element) {
|
||||
PsiType type = assertNotNull(element.as(PsiTypeLookupItem.CLASS_CONDITION_KEY)).getPsiType();
|
||||
PsiClass aClass = type instanceof PsiClassType && ((PsiClassType)type).getParameterCount() == 0 ? ((PsiClassType)type).resolve() : null;
|
||||
if (aClass != null) {
|
||||
inheritors.registerClass(aClass);
|
||||
}
|
||||
result.addElement(element);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
PsiElement parent = position.getParent();
|
||||
if (parent instanceof PsiReferenceExpression) {
|
||||
final List<ExpectedTypeInfo> expected = Arrays.asList(ExpectedTypesProvider.getExpectedTypes((PsiExpression)parent, true));
|
||||
|
||||
+4
-4
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.intellij.codeInsight.completion;
|
||||
|
||||
import com.intellij.codeInsight.ExceptionUtil;
|
||||
import com.intellij.codeInsight.*;
|
||||
import com.intellij.codeInsight.completion.scope.JavaCompletionProcessor;
|
||||
import com.intellij.codeInsight.lookup.*;
|
||||
@@ -38,10 +39,7 @@ import com.intellij.psi.infos.CandidateInfo;
|
||||
import com.intellij.psi.javadoc.PsiDocTag;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.Consumer;
|
||||
import com.intellij.util.ProcessingContext;
|
||||
import com.intellij.util.ReflectionUtil;
|
||||
import com.intellij.util.SmartList;
|
||||
import com.intellij.util.*;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import gnu.trove.THashSet;
|
||||
import gnu.trove.TObjectHashingStrategy;
|
||||
@@ -134,6 +132,8 @@ public class JavaSmartCompletionContributor extends CompletionContributor {
|
||||
extend(CompletionType.SMART, SmartCastProvider.INSIDE_TYPECAST_TYPE, new SmartCastProvider());
|
||||
|
||||
extend(CompletionType.SMART, SameSignatureCallParametersProvider.IN_CALL_ARGUMENT, new SameSignatureCallParametersProvider());
|
||||
|
||||
extend(CompletionType.SMART, MethodReturnTypeProvider.IN_METHOD_RETURN_TYPE, new MethodReturnTypeProvider());
|
||||
|
||||
extend(CompletionType.SMART, psiElement().afterLeaf(PsiKeyword.INSTANCEOF), new CompletionProvider<CompletionParameters>() {
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright 2000-2015 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.codeInsight.completion;
|
||||
|
||||
import com.intellij.codeInsight.ExpectedTypesProvider;
|
||||
import com.intellij.codeInsight.lookup.LookupElement;
|
||||
import com.intellij.codeInsight.lookup.PsiTypeLookupItem;
|
||||
import com.intellij.patterns.ElementPattern;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.Consumer;
|
||||
import com.intellij.util.ProcessingContext;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import static com.intellij.patterns.PsiJavaPatterns.psiElement;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
class MethodReturnTypeProvider extends CompletionProvider<CompletionParameters> {
|
||||
protected static final ElementPattern<PsiElement> IN_METHOD_RETURN_TYPE =
|
||||
psiElement().withParents(PsiJavaCodeReferenceElement.class, PsiTypeElement.class, PsiMethod.class)
|
||||
.andNot(JavaCompletionData.AFTER_DOT);
|
||||
|
||||
@Override
|
||||
protected void addCompletions(@NotNull CompletionParameters parameters,
|
||||
ProcessingContext context,
|
||||
@NotNull final CompletionResultSet result) {
|
||||
addProbableReturnTypes(parameters, result);
|
||||
|
||||
}
|
||||
|
||||
static void addProbableReturnTypes(@NotNull CompletionParameters parameters, final Consumer<LookupElement> consumer) {
|
||||
final PsiElement position = parameters.getPosition();
|
||||
PsiMethod method = PsiTreeUtil.getParentOfType(position, PsiMethod.class);
|
||||
assert method != null;
|
||||
|
||||
final PsiTypeVisitor<PsiType> eachProcessor = new PsiTypeVisitor<PsiType>() {
|
||||
private Set<PsiType> myProcessed = ContainerUtil.newHashSet();
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public PsiType visitType(PsiType type) {
|
||||
if (myProcessed.add(type)) {
|
||||
int priority = type.equalsToText(CommonClassNames.JAVA_LANG_OBJECT) ? 1 : 1000 - myProcessed.size();
|
||||
consumer.consume(PrioritizedLookupElement.withPriority(PsiTypeLookupItem.createLookupItem(type, position), priority));
|
||||
}
|
||||
return type;
|
||||
}
|
||||
};
|
||||
for (PsiType type : getReturnTypeCandidates(method)) {
|
||||
eachProcessor.visitType(type);
|
||||
ExpectedTypesProvider.processAllSuperTypes(type, eachProcessor, position.getProject(), ContainerUtil.<PsiType>newHashSet());
|
||||
}
|
||||
}
|
||||
|
||||
private static PsiType[] getReturnTypeCandidates(@NotNull PsiMethod method) {
|
||||
PsiType lub = null;
|
||||
boolean hasVoid = false;
|
||||
for (PsiReturnStatement statement : PsiUtil.findReturnStatements(method)) {
|
||||
PsiExpression value = statement.getReturnValue();
|
||||
if (value == null) {
|
||||
hasVoid = true;
|
||||
}
|
||||
else {
|
||||
PsiType type = value.getType();
|
||||
if (lub == null) {
|
||||
lub = type;
|
||||
}
|
||||
else if (type != null) {
|
||||
lub = GenericsUtil.getLeastUpperBound(lub, type, method.getManager());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (hasVoid && lub == null) {
|
||||
lub = PsiType.VOID;
|
||||
}
|
||||
if (lub instanceof PsiIntersectionType) {
|
||||
return ((PsiIntersectionType)lub).getConjuncts();
|
||||
}
|
||||
return lub == null ? PsiType.EMPTY_ARRAY : new PsiType[]{lub};
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
class Foo {
|
||||
public S<caret> get() {
|
||||
return new MyStringBuffer();
|
||||
}
|
||||
}
|
||||
|
||||
class MyStringBuffer extends StringBuffer implements SomeInterface<String> {}
|
||||
interface SomeInterface<T> {}
|
||||
class SomeOtherClass {}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
import java.lang.StringBuilder;
|
||||
|
||||
public class Main {
|
||||
|
||||
public <caret> main() {
|
||||
if (equals(2)) {
|
||||
return "a";
|
||||
} else {
|
||||
return new StringBuilder();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+5
@@ -691,4 +691,9 @@ interface TxANotAnno {}
|
||||
assert LookupElementPresentation.renderElement(items.find { it.lookupString == 'BAR' }).itemTextForeground == JBColor.RED
|
||||
}
|
||||
|
||||
public void testPreferValueTypesReturnedFromMethod() {
|
||||
checkPreferredItems 0, 'StringBuffer', 'String', 'Serializable', 'SomeInterface', 'SomeOtherClass'
|
||||
assert 'SomeInterface<String>' == LookupElementPresentation.renderElement(myFixture.lookupElements[3]).itemText
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+5
@@ -1148,6 +1148,11 @@ public class SmartTypeCompletionTest extends LightFixtureCompletionTestCase {
|
||||
doFirstItemTest('\t');
|
||||
}
|
||||
|
||||
public void testSuggestMethodReturnType() {
|
||||
configureByTestName();
|
||||
myFixture.assertPreferredCompletionItems(0, "Serializable", "CharSequence", "Object");
|
||||
}
|
||||
|
||||
public void testNonStaticField() throws Exception { doAntiTest(); }
|
||||
|
||||
private void doActionTest() throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user