Search in sources :

Example 1 with GrClosureParameter

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

the class ArgumentListGenerator method generate.

public void generate(@Nullable GrClosureSignature signature, @NotNull GrExpression[] exprs, @NotNull GrNamedArgument[] namedArgs, @NotNull GrClosableBlock[] clArgs, @NotNull GroovyPsiElement context) {
    GrClosureSignatureUtil.ArgInfo<PsiElement>[] argInfos = signature == null ? null : GrClosureSignatureUtil.mapParametersToArguments(signature, namedArgs, exprs, clArgs, context, false, false);
    if (argInfos == null && signature != null) {
        argInfos = GrClosureSignatureUtil.mapParametersToArguments(signature, namedArgs, exprs, clArgs, context, true, true);
    }
    final PsiSubstitutor substitutor = signature == null ? PsiSubstitutor.EMPTY : signature.getSubstitutor();
    if (argInfos == null || NullUtils.hasNull(argInfos)) {
        generateSimple(exprs, namedArgs, clArgs, context, substitutor);
        return;
    }
    final GrClosureParameter[] params = signature.getParameters();
    final Project project = context.getProject();
    myBuilder.append('(');
    boolean hasCommaAtEnd = false;
    for (int i = 0; i < argInfos.length; i++) {
        GrClosureSignatureUtil.ArgInfo<PsiElement> arg = argInfos[i];
        if (arg == null)
            continue;
        final GrClosureParameter param = params[i];
        boolean generated = arg.isMultiArg ? generateMultiArg(arg, param, substitutor, project, context) : generateSingeArg(arg, param);
        if (generated) {
            hasCommaAtEnd = true;
            myBuilder.append(", ");
        }
    }
    if (hasCommaAtEnd) {
        myBuilder.delete(myBuilder.length() - 2, myBuilder.length());
    //myBuilder.removeFromTheEnd(2);
    }
    myBuilder.append(')');
}
Also used : GrClosureSignatureUtil(org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil) Project(com.intellij.openapi.project.Project) GrClosureParameter(org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 2 with GrClosureParameter

use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter 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 3 with GrClosureParameter

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

the class GrClosureSignatureUtil method getOptionalParamCount.

public static int getOptionalParamCount(GrClosureParameter[] parameters, boolean hasNamedArgs) {
    int count = 0;
    int i = 0;
    if (hasNamedArgs)
        i++;
    for (; i < parameters.length; i++) {
        GrClosureParameter parameter = parameters[i];
        if (parameter.isOptional())
            count++;
    }
    return count;
}
Also used : GrClosureParameter(org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter)

Example 4 with GrClosureParameter

use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter 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 5 with GrClosureParameter

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

the class GrClosureSignatureUtil method mapParametersToArguments.

@Nullable
public static ArgInfo<PsiElement>[] mapParametersToArguments(@NotNull GrClosureSignature signature, @NotNull GrNamedArgument[] namedArgs, @NotNull GrExpression[] expressionArgs, @NotNull GrClosableBlock[] closureArguments, @NotNull PsiElement context, boolean partial, boolean eraseArgs) {
    List<InnerArg> innerArgs = new ArrayList<>();
    boolean hasNamedArgs = namedArgs.length > 0;
    GrClosureParameter[] params = signature.getParameters();
    if (hasNamedArgs) {
        if (params.length == 0)
            return null;
        PsiType type = params[0].getType();
        if (InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_UTIL_MAP) || type == null || type.equalsToText(CommonClassNames.JAVA_LANG_OBJECT)) {
            innerArgs.add(new InnerArg(GrMapType.create(context.getResolveScope()), namedArgs));
        } else {
            return null;
        }
    }
    for (GrExpression expression : expressionArgs) {
        PsiType type = expression.getType();
        if (partial && expression instanceof GrNewExpression && com.intellij.psi.util.PsiUtil.resolveClassInType(type) == null) {
            type = null;
        }
        if (eraseArgs) {
            type = TypeConversionUtil.erasure(type);
        }
        innerArgs.add(new InnerArg(type, expression));
    }
    for (GrClosableBlock closureArgument : closureArguments) {
        innerArgs.add(new InnerArg(TypeConversionUtil.erasure(closureArgument.getType()), closureArgument));
    }
    return mapParametersToArguments(signature, innerArgs, hasNamedArgs, partial, context);
}
Also used : GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrClosureParameter(org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GrClosureParameter (org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter)12 GrClosureSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)6 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)3 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)3 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)3 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 GrRecursiveSignatureVisitor (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrRecursiveSignatureVisitor)2 GrReflectedMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrReflectedMethod)2 GrClosureSignatureUtil (org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil)2 CodeInsightSettings (com.intellij.codeInsight.CodeInsightSettings)1 Project (com.intellij.openapi.project.Project)1 PsiElement (com.intellij.psi.PsiElement)1 PsiType (com.intellij.psi.PsiType)1 ElementClassHint (com.intellij.psi.scope.ElementClassHint)1 MethodSignature (com.intellij.psi.util.MethodSignature)1 GrSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature)1 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)1 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)1 GrNewExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression)1