Search in sources :

Example 46 with GrNamedArgument

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument in project intellij-community by JetBrains.

the class PsiUtil method getArgumentTypes.

@Nullable
public static PsiType[] getArgumentTypes(@Nullable PsiElement place, boolean nullAsBottom, @Nullable GrExpression stopAt) {
    PsiElement parent = place instanceof GrEnumConstant ? place : place != null ? place.getParent() : null;
    if (parent instanceof GrIndexProperty) {
        GrIndexProperty index = (GrIndexProperty) parent;
        PsiType[] argTypes = getArgumentTypes(index.getNamedArguments(), index.getExpressionArguments(), index.getClosureArguments(), nullAsBottom, stopAt);
        if (isLValue(index) && argTypes != null) {
            PsiType rawInitializer = TypeInferenceHelper.getInitializerTypeFor(index);
            PsiType initializer = notNullizeType(rawInitializer, nullAsBottom, index);
            return ArrayUtil.append(argTypes, initializer);
        } else {
            return argTypes;
        }
    }
    if (parent instanceof GrCall) {
        GrCall call = (GrCall) parent;
        GrNamedArgument[] namedArgs = call.getNamedArguments();
        GrExpression[] expressions = call.getExpressionArguments();
        GrClosableBlock[] closures = call.getClosureArguments();
        return getArgumentTypes(namedArgs, expressions, closures, nullAsBottom, stopAt);
    } else if (parent instanceof GrAnonymousClassDefinition) {
        final GrArgumentList argList = ((GrAnonymousClassDefinition) parent).getArgumentListGroovy();
        if (argList == null) {
            return getArgumentTypes(GrNamedArgument.EMPTY_ARRAY, GrExpression.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, nullAsBottom, stopAt);
        } else {
            return getArgumentTypes(argList.getNamedArguments(), argList.getExpressionArguments(), GrClosableBlock.EMPTY_ARRAY, nullAsBottom, stopAt);
        }
    } else if (parent instanceof GrBinaryExpression) {
        GrExpression right = ((GrBinaryExpression) parent).getRightOperand();
        PsiType type = right != null ? right.getType() : null;
        return new PsiType[] { notNullizeType(type, nullAsBottom, parent) };
    } else if (parent instanceof GrAssignmentExpression) {
        PsiType type = ((GrAssignmentExpression) parent).getType();
        return new PsiType[] { notNullizeType(type, nullAsBottom, parent) };
    }
    return null;
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrIndexProperty(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) Nullable(org.jetbrains.annotations.Nullable)

Example 47 with GrNamedArgument

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument in project intellij-community by JetBrains.

the class PsiUtil method getAllArguments.

/**
   * Returns all arguments passed to method. First argument is null if Named Arguments is present.
   */
public static GrExpression[] getAllArguments(@NotNull GrCall call) {
    GrArgumentList argumentList = call.getArgumentList();
    if (argumentList == null)
        return GrExpression.EMPTY_ARRAY;
    GrClosableBlock[] closureArguments = call.getClosureArguments();
    GrExpression[] expressionArguments = argumentList.getExpressionArguments();
    GrNamedArgument[] namedArguments = argumentList.getNamedArguments();
    int length = expressionArguments.length + closureArguments.length;
    int k = 0;
    if (namedArguments.length > 0) {
        length++;
        k = 1;
    }
    GrExpression[] res = new GrExpression[length];
    for (GrExpression expressionArgument : expressionArguments) {
        res[k++] = expressionArgument;
    }
    for (GrClosableBlock closureArgument : closureArguments) {
        res[k++] = closureArgument;
    }
    return res;
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)

Example 48 with GrNamedArgument

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument in project intellij-community by JetBrains.

the class PsiUtil method getArgumentTypes.

@Nullable
public static PsiType[] getArgumentTypes(@NotNull GrNamedArgument[] namedArgs, @NotNull GrExpression[] expressions, @NotNull GrClosableBlock[] closures, boolean nullAsBottom, @Nullable GrExpression stopAt) {
    List<PsiType> result = new ArrayList<>();
    if (namedArgs.length > 0) {
        GrNamedArgument context = namedArgs[0];
        result.add(GrMapType.createFromNamedArgs(context, namedArgs));
    }
    for (GrExpression expression : expressions) {
        PsiType type = expression.getType();
        if (expression instanceof GrSpreadArgument) {
            if (type instanceof GrTupleType) {
                result.addAll(Arrays.asList(((GrTupleType) type).getComponentTypes()));
            } else {
                return null;
            }
        } else {
            if (type == null) {
                result.add(nullAsBottom ? null : TypesUtil.getJavaLangObject(expression));
            } else {
                if (stopAt == expression) {
                    type = TypeConversionUtil.erasure(type);
                }
                result.add(type);
            }
        }
        if (stopAt == expression) {
            return result.toArray(PsiType.createArray(result.size()));
        }
    }
    for (GrClosableBlock closure : closures) {
        PsiType closureType = closure.getType();
        if (closureType != null) {
            if (stopAt == closure) {
                closureType = TypeConversionUtil.erasure(closureType);
            }
            result.add(notNullizeType(closureType, nullAsBottom, closure));
        }
        if (stopAt == closure) {
            break;
        }
    }
    return result.toArray(PsiType.createArray(result.size()));
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrSpreadArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrSpreadArgument) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)48 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)17 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)16 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)13 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)12 PsiElement (com.intellij.psi.PsiElement)11 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)11 Nullable (org.jetbrains.annotations.Nullable)10 GrArgumentLabel (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel)8 NotNull (org.jetbrains.annotations.NotNull)5 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)5 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)5 GrString (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString)5 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4 NamedArgumentDescriptor (org.jetbrains.plugins.groovy.extensions.NamedArgumentDescriptor)4 GrClosureSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)4 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)4 ASTNode (com.intellij.lang.ASTNode)3 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)3 ArrayList (java.util.ArrayList)3