mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-13 21:55:01 +07:00
IDEA-71518 let parameters completion pass method parameters
This commit is contained in:
@@ -1214,13 +1214,13 @@ public class ExpectedTypesProvider {
|
||||
return TailTypes.CALL_RPARENTH_SEMICOLON;
|
||||
}
|
||||
|
||||
final boolean chainable = !PsiType.VOID.equals(returnType) && returnType != null;
|
||||
final boolean chainable = !PsiType.VOID.equals(returnType) && returnType != null || method.isConstructor() && call instanceof PsiNewExpression;
|
||||
|
||||
final PsiElement parent = call.getParent();
|
||||
final boolean statementContext = parent instanceof PsiExpressionStatement || parent instanceof PsiVariable ||
|
||||
parent instanceof PsiCodeBlock || parent instanceof PsiThrowStatement;
|
||||
parent instanceof PsiCodeBlock;
|
||||
|
||||
if (statementContext && !chainable) {
|
||||
if (parent instanceof PsiThrowStatement || statementContext && !chainable) {
|
||||
return TailTypes.CALL_RPARENTH_SEMICOLON;
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -114,7 +114,7 @@ public class JavaSmartCompletionContributor extends CompletionContributor {
|
||||
extend(CompletionType.SMART,
|
||||
psiElement().beforeLeaf(psiElement(JavaTokenType.RPARENTH)).afterLeaf("(").withParent(
|
||||
psiElement(PsiReferenceExpression.class).withParent(
|
||||
psiElement(PsiExpressionList.class).withParent(PsiMethodCallExpression.class))), new SameSignatureCallParametersProvider());
|
||||
psiElement(PsiExpressionList.class).withParent(PsiCall.class))), new SameSignatureCallParametersProvider());
|
||||
|
||||
extend(CompletionType.SMART, psiElement().afterLeaf(PsiKeyword.INSTANCEOF), new CompletionProvider<CompletionParameters>() {
|
||||
protected void addCompletions(@NotNull final CompletionParameters parameters, final ProcessingContext context, @NotNull final CompletionResultSet result) {
|
||||
|
||||
+53
-26
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.intellij.codeInsight.completion;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.intellij.codeInsight.ExpectedTypesProvider;
|
||||
import com.intellij.codeInsight.lookup.LookupElement;
|
||||
import com.intellij.codeInsight.lookup.LookupElementBuilder;
|
||||
@@ -30,9 +31,11 @@ import com.intellij.util.Function;
|
||||
import com.intellij.util.PlatformIcons;
|
||||
import com.intellij.util.ProcessingContext;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
@@ -42,18 +45,19 @@ class SameSignatureCallParametersProvider extends CompletionProvider<CompletionP
|
||||
protected void addCompletions(@NotNull CompletionParameters parameters,
|
||||
ProcessingContext context,
|
||||
@NotNull CompletionResultSet result) {
|
||||
final PsiMethodCallExpression methodCall = PsiTreeUtil.getParentOfType(parameters.getPosition(), PsiMethodCallExpression.class);
|
||||
final PsiCall methodCall = PsiTreeUtil.getParentOfType(parameters.getPosition(), PsiCall.class);
|
||||
assert methodCall != null;
|
||||
final PsiReferenceExpression expression = methodCall.getMethodExpression();
|
||||
|
||||
List<Pair<PsiMethod, PsiSubstitutor>> candidates = getSuperMethodCandidates(expression);
|
||||
List<Pair<PsiMethod, PsiSubstitutor>> candidates = getCallCandidates(methodCall);
|
||||
|
||||
PsiMethod container = PsiTreeUtil.getParentOfType(methodCall, PsiMethod.class);
|
||||
while (container != null) {
|
||||
for (final Pair<PsiMethod, PsiSubstitutor> candidate : candidates) {
|
||||
if (container.getParameterList().getParametersCount() > 1 && isSuperMethod(container, candidate.first, candidate.second)) {
|
||||
result.addElement(createParametersLookupElement(container, methodCall));
|
||||
return;
|
||||
if (container.getParameterList().getParametersCount() > 1 && candidate.first.getParameterList().getParametersCount() > 1) {
|
||||
PsiMethod from = getMethodToTakeParametersFrom(container, candidate.first, candidate.second);
|
||||
if (from != null) {
|
||||
result.addElement(createParametersLookupElement(from, methodCall, candidate.first));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,8 +66,8 @@ class SameSignatureCallParametersProvider extends CompletionProvider<CompletionP
|
||||
}
|
||||
}
|
||||
|
||||
private static LookupElement createParametersLookupElement(PsiMethod method, PsiElement call) {
|
||||
final String lookupString = StringUtil.join(method.getParameterList().getParameters(), new Function<PsiParameter, String>() {
|
||||
private static LookupElement createParametersLookupElement(PsiMethod takeParametersFrom, PsiElement call, PsiMethod invoked) {
|
||||
final String lookupString = StringUtil.join(takeParametersFrom.getParameterList().getParameters(), new Function<PsiParameter, String>() {
|
||||
public String fun(PsiParameter psiParameter) {
|
||||
return psiParameter.getName();
|
||||
}
|
||||
@@ -77,12 +81,19 @@ class SameSignatureCallParametersProvider extends CompletionProvider<CompletionP
|
||||
final LookupElement element = LookupElementBuilder.create(lookupString).setIcon(icon);
|
||||
element.putUserData(JavaCompletionUtil.SUPER_METHOD_PARAMETERS, Boolean.TRUE);
|
||||
|
||||
return TailTypeDecorator.withTail(element, ExpectedTypesProvider.getFinalCallParameterTailType(call, method.getReturnType(), method));
|
||||
return TailTypeDecorator.withTail(element, ExpectedTypesProvider.getFinalCallParameterTailType(call, invoked.getReturnType(), invoked));
|
||||
}
|
||||
|
||||
private static List<Pair<PsiMethod, PsiSubstitutor>> getSuperMethodCandidates(PsiReferenceExpression expression) {
|
||||
private static List<Pair<PsiMethod, PsiSubstitutor>> getCallCandidates(PsiCall expression) {
|
||||
List<Pair<PsiMethod, PsiSubstitutor>> candidates = new ArrayList<Pair<PsiMethod, PsiSubstitutor>>();
|
||||
for (final JavaResolveResult candidate : expression.multiResolve(true)) {
|
||||
JavaResolveResult[] results;
|
||||
if (expression instanceof PsiMethodCallExpression) {
|
||||
results = ((PsiMethodCallExpression)expression).getMethodExpression().multiResolve(false);
|
||||
} else {
|
||||
results = new JavaResolveResult[]{expression.resolveMethodGenerics()};
|
||||
}
|
||||
|
||||
for (final JavaResolveResult candidate : results) {
|
||||
final PsiElement element = candidate.getElement();
|
||||
if (element instanceof PsiMethod) {
|
||||
final PsiClass psiClass = ((PsiMethod)element).getContainingClass();
|
||||
@@ -100,26 +111,42 @@ class SameSignatureCallParametersProvider extends CompletionProvider<CompletionP
|
||||
}
|
||||
|
||||
|
||||
private static boolean isSuperMethod(PsiMethod container, PsiMethod callee, PsiSubstitutor substitutor) {
|
||||
if (PsiSuperMethodUtil.isSuperMethod(container, callee)) {
|
||||
return true;
|
||||
@Nullable
|
||||
private static PsiMethod getMethodToTakeParametersFrom(PsiMethod place, PsiMethod invoked, PsiSubstitutor substitutor) {
|
||||
if (PsiSuperMethodUtil.isSuperMethod(place, invoked)) {
|
||||
return place;
|
||||
}
|
||||
|
||||
final PsiParameter[] parameters = container.getParameterList().getParameters();
|
||||
final PsiParameter[] superParams = callee.getParameterList().getParameters();
|
||||
if (superParams.length != parameters.length) {
|
||||
return false;
|
||||
Map<String, PsiType> requiredNames = Maps.newHashMap();
|
||||
final PsiParameter[] parameters = place.getParameterList().getParameters();
|
||||
final PsiParameter[] callParams = invoked.getParameterList().getParameters();
|
||||
if (callParams.length > parameters.length) {
|
||||
return null;
|
||||
}
|
||||
final boolean checkNames = callee.isConstructor();
|
||||
for (int i = 0; i < parameters.length; i++) {
|
||||
|
||||
final boolean checkNames = invoked.isConstructor();
|
||||
boolean sameTypes = true;
|
||||
for (int i = 0; i < callParams.length; i++) {
|
||||
PsiParameter callParam = callParams[i];
|
||||
PsiParameter parameter = parameters[i];
|
||||
final PsiParameter superParam = superParams[i];
|
||||
if (checkNames && !Comparing.equal(parameter.getName(), superParam.getName()) ||
|
||||
!Comparing.equal(parameter.getType(), substitutor.substitute(superParam.getType()))) {
|
||||
return false;
|
||||
requiredNames.put(callParam.getName(), substitutor.substitute(callParam.getType()));
|
||||
if (checkNames && !Comparing.equal(parameter.getName(), callParam.getName()) ||
|
||||
!Comparing.equal(parameter.getType(), substitutor.substitute(callParam.getType()))) {
|
||||
sameTypes = false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
if (sameTypes && callParams.length == parameters.length) {
|
||||
return place;
|
||||
}
|
||||
|
||||
for (PsiParameter parameter : parameters) {
|
||||
PsiType type = requiredNames.remove(parameter.getName());
|
||||
if (type != null && !parameter.getType().equals(type)) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return requiredNames.isEmpty() ? invoked : null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
public class SomeClass {
|
||||
Border createBorder(Color color, int top, boolean isOpaque, int bottom, int right, int left) {
|
||||
new Insets(top, left, bottom, right)<caret>
|
||||
}
|
||||
}
|
||||
|
||||
class Insets {
|
||||
Insets(int top, int left, int bottom, int right) {}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
public class SomeClass {
|
||||
Border createBorder(Color color, int top, boolean isOpaque, int bottom, int right, int left) {
|
||||
new Insets(<caret>)
|
||||
}
|
||||
}
|
||||
|
||||
class Insets {
|
||||
Insets(int top, int left, int bottom, int right) {}
|
||||
}
|
||||
+1
-1
@@ -10,6 +10,6 @@ class Foo {
|
||||
public class Bar {
|
||||
|
||||
{
|
||||
new Foo(Foo.FOO_BAR);<caret>
|
||||
new Foo(Foo.FOO_BAR)<caret>
|
||||
}
|
||||
}
|
||||
|
||||
+7
@@ -757,6 +757,13 @@ public class SmartTypeCompletionTest extends LightFixtureCompletionTestCase {
|
||||
checkResultByTestName();
|
||||
}
|
||||
|
||||
public void testSameNamedArguments() throws Throwable {
|
||||
configureByTestName();
|
||||
getLookup().setCurrentItem(getLookup().getItems().get(4));
|
||||
select();
|
||||
checkResultByTestName();
|
||||
}
|
||||
|
||||
public void testWrongAnonymous() throws Throwable {
|
||||
configureByTestName();
|
||||
select();
|
||||
|
||||
Reference in New Issue
Block a user