Search in sources :

Example 11 with GrClosureSignature

use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature in project intellij-community by JetBrains.

the class GrClosureType method create.

public static GrClosureType create(GroovyResolveResult[] results, GroovyPsiElement context) {
    List<GrClosureSignature> signatures = new ArrayList<>();
    for (GroovyResolveResult result : results) {
        if (result.getElement() instanceof PsiMethod) {
            signatures.add(GrClosureSignatureUtil.createSignature((PsiMethod) result.getElement(), result.getSubstitutor()));
        }
    }
    final GlobalSearchScope resolveScope = context.getResolveScope();
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(context.getProject());
    if (signatures.size() == 1) {
        return create(signatures.get(0), resolveScope, facade, LanguageLevel.JDK_1_5, true);
    } else {
        return create(GrClosureSignatureUtil.createMultiSignature(signatures.toArray(new GrClosureSignature[signatures.size()])), resolveScope, facade, LanguageLevel.JDK_1_5, true);
    }
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ArrayList(java.util.ArrayList) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)

Example 12 with GrClosureSignature

use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature in project intellij-community by JetBrains.

the class GrClosureType method create.

public static GrClosureType create(@NotNull GrClosableBlock closure, boolean shouldInferTypeParameters) {
    final GrClosureSignature signature = GrClosureSignatureUtil.createSignature(closure);
    final GlobalSearchScope resolveScope = closure.getResolveScope();
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(closure.getProject());
    return create(signature, resolveScope, facade, LanguageLevel.JDK_1_5, shouldInferTypeParameters);
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)

Example 13 with GrClosureSignature

use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature in project intellij-community by JetBrains.

the class ArgumentInstruction method inferMixinType.

@Override
@Nullable
public PsiType inferMixinType() {
    PsiElement element = getElement();
    assert element != null;
    GrCall call = findCall(element);
    GrExpression[] arguments = call.getExpressionArguments();
    boolean hasNamed = PsiImplUtil.hasNamedArguments(call.getArgumentList());
    int index = ArrayUtil.indexOf(arguments, element) + (hasNamed ? 1 : 0);
    GroovyResolveResult[] variants = call.getCallVariants((GrReferenceExpression) element);
    PsiType result = null;
    for (GroovyResolveResult variant : variants) {
        GrClosureSignature signature = GrClosureSignatureUtil.createSignature(variant);
        if (signature == null)
            continue;
        if (GrClosureSignatureUtil.mapParametersToArguments(signature, call) != null && !haveNullParameters(call)) {
            return null;
        }
        GrClosureParameter[] parameters = signature.getParameters();
        if (index >= parameters.length)
            continue;
        result = TypesUtil.getLeastUpperBoundNullable(result, parameters[index].getType(), element.getManager());
    }
    return result;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GrClosureParameter(org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType) Nullable(org.jetbrains.annotations.Nullable)

Example 14 with GrClosureSignature

use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature in project intellij-community by JetBrains.

the class GrClosureSignatureUtil method generateSimpleSignatures.

public static List<GrClosureSignature> generateSimpleSignatures(@NotNull GrSignature signature) {
    final List<GrClosureSignature> result = new ArrayList<>();
    signature.accept(new GrRecursiveSignatureVisitor() {

        @Override
        public void visitClosureSignature(GrClosureSignature signature) {
            final GrClosureParameter[] original = signature.getParameters();
            final ArrayList<GrClosureParameter> parameters = new ArrayList<>(original.length);
            for (GrClosureParameter parameter : original) {
                parameters.add(new GrDelegatingClosureParameter(parameter) {

                    @Override
                    public boolean isOptional() {
                        return false;
                    }

                    @Nullable
                    @Override
                    public GrExpression getDefaultInitializer() {
                        return null;
                    }
                });
            }
            final int pcount = signature.isVarargs() ? signature.getParameterCount() - 2 : signature.getParameterCount() - 1;
            for (int i = pcount; i >= 0; i--) {
                if (original[i].isOptional()) {
                    result.add(new GrImmediateClosureSignatureImpl(parameters.toArray(new GrClosureParameter[parameters.size()]), signature.getReturnType(), signature.isVarargs(), false));
                    parameters.remove(i);
                }
            }
            result.add(new GrImmediateClosureSignatureImpl(parameters.toArray(new GrClosureParameter[parameters.size()]), signature.getReturnType(), signature.isVarargs(), false));
        }
    });
    return result;
}
Also used : GrRecursiveSignatureVisitor(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrRecursiveSignatureVisitor) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GrClosureParameter(org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter)

Example 15 with GrClosureSignature

use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature in project intellij-community by JetBrains.

the class GrClosureSignatureUtil method createSignature.

@Nullable
public static GrClosureSignature createSignature(GrCall call) {
    if (call instanceof GrMethodCall) {
        final GrExpression invokedExpression = ((GrMethodCall) call).getInvokedExpression();
        final PsiType type = invokedExpression.getType();
        if (type instanceof GrClosureType) {
            final GrSignature signature = ((GrClosureType) type).getSignature();
            final Trinity<GrClosureSignature, ArgInfo<PsiType>[], ApplicabilityResult> trinity = getApplicableSignature(signature, PsiUtil.getArgumentTypes(invokedExpression, true), call);
            if (trinity != null) {
                return trinity.first;
            }
            return null;
        }
    }
    final GroovyResolveResult resolveResult = call.advancedResolve();
    final PsiElement element = resolveResult.getElement();
    if (element instanceof PsiMethod) {
        return createSignature((PsiMethod) element, resolveResult.getSubstitutor());
    }
    return null;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrClosureType(org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GrClosureSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)33 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)13 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)9 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)8 ArrayList (java.util.ArrayList)6 Nullable (org.jetbrains.annotations.Nullable)6 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)6 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)6 GrClosureParameter (org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter)6 GrClosureSignatureUtil (org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil)6 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)5 MethodSignature (com.intellij.psi.util.MethodSignature)4 List (java.util.List)4 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)4 GrRecursiveSignatureVisitor (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrRecursiveSignatureVisitor)4 GrSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature)4 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)4 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)4 Pair (com.intellij.openapi.util.Pair)3 TIntProcedure (gnu.trove.TIntProcedure)3