Search in sources :

Example 11 with GrListOrMap

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

the class GroovyTypeCheckVisitorHelper method getExpressionArgumentsOfCall.

@Nullable
public static List<GrExpression> getExpressionArgumentsOfCall(@NotNull GrArgumentList argumentList) {
    final ArrayList<GrExpression> args = ContainerUtil.newArrayList();
    for (GroovyPsiElement arg : argumentList.getAllArguments()) {
        if (arg instanceof GrSpreadArgument) {
            GrExpression spreaded = ((GrSpreadArgument) arg).getArgument();
            if (spreaded instanceof GrListOrMap && !((GrListOrMap) spreaded).isMap()) {
                Collections.addAll(args, ((GrListOrMap) spreaded).getInitializers());
            } else {
                return null;
            }
        } else if (arg instanceof GrExpression) {
            args.add((GrExpression) arg);
        } else if (arg instanceof GrNamedArgument) {
            args.add(((GrNamedArgument) arg).getExpression());
        }
    }
    final PsiElement parent = argumentList.getParent();
    if (parent instanceof GrIndexProperty && PsiUtil.isLValue((GroovyPsiElement) parent)) {
        args.add(TypeInferenceHelper.getInitializerFor((GrExpression) parent));
    } else if (parent instanceof GrMethodCallExpression) {
        ContainerUtil.addAll(args, ((GrMethodCallExpression) parent).getClosureArguments());
    }
    return args;
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrIndexProperty(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrSpreadArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrSpreadArgument) Nullable(org.jetbrains.annotations.Nullable)

Example 12 with GrListOrMap

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

the class GppClosureParameterTypeProvider method getClosureParameterType.

@Override
protected PsiType getClosureParameterType(GrClosableBlock closure, int index) {
    final PsiElement parent = closure.getParent();
    if (parent instanceof GrNamedArgument) {
        final Pair<PsiMethod, PsiSubstitutor> pair = getOverriddenMethod((GrNamedArgument) parent);
        if (pair != null) {
            final PsiParameter[] parameters = pair.first.getParameterList().getParameters();
            if (parameters.length > index) {
                return pair.second.substitute(parameters[index].getType());
            }
            return null;
        }
    }
    if (parent instanceof GrListOrMap) {
        final GrListOrMap list = (GrListOrMap) parent;
        if (!list.isMap()) {
            final PsiType listType = list.getType();
            final int argIndex = Arrays.asList(list.getInitializers()).indexOf(closure);
            assert argIndex >= 0;
            if (listType instanceof GrTupleType) {
                for (PsiType type : GroovyExpectedTypesProvider.getDefaultExpectedTypes(list)) {
                    if (!(type instanceof PsiClassType))
                        continue;
                    final GroovyResolveResult[] candidates = PsiUtil.getConstructorCandidates((PsiClassType) type, ((GrTupleType) listType).getComponentTypes(), closure);
                    for (GroovyResolveResult resolveResult : candidates) {
                        final PsiElement method = resolveResult.getElement();
                        if (!(method instanceof PsiMethod) || !((PsiMethod) method).isConstructor())
                            continue;
                        final PsiParameter[] parameters = ((PsiMethod) method).getParameterList().getParameters();
                        if (parameters.length <= argIndex)
                            continue;
                        final PsiType toCastTo = resolveResult.getSubstitutor().substitute(parameters[argIndex].getType());
                        final PsiType suggestion = getSingleMethodParameterType(toCastTo, index, closure);
                        if (suggestion != null)
                            return suggestion;
                    }
                }
            }
            return null;
        }
    }
    for (PsiType constraint : GroovyExpectedTypesProvider.getDefaultExpectedTypes(closure)) {
        final PsiType suggestion = getSingleMethodParameterType(constraint, index, closure);
        if (suggestion != null) {
            return suggestion;
        }
    }
    return null;
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrTupleType(org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)

Example 13 with GrListOrMap

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

the class GppExpectedTypesContributor method addExpectedConstructorParameters.

private static List<TypeConstraint> addExpectedConstructorParameters(GrListOrMap list, GrExpression[] args, GrExpression arg) {
    PsiType[] argTypes = ContainerUtil.map2Array(args, PsiType.class, (NullableFunction<GrExpression, PsiType>) grExpression -> grExpression.getType());
    final ArrayList<TypeConstraint> result = new ArrayList<>();
    for (PsiType type : GroovyExpectedTypesProvider.getDefaultExpectedTypes(list)) {
        if (type instanceof PsiClassType) {
            for (GroovyResolveResult resolveResult : PsiUtil.getConstructorCandidates((PsiClassType) type, argTypes, list)) {
                final PsiElement method = resolveResult.getElement();
                if (method instanceof PsiMethod && ((PsiMethod) method).isConstructor()) {
                    final Map<GrExpression, Pair<PsiParameter, PsiType>> map = GrClosureSignatureUtil.mapArgumentsToParameters(resolveResult, list, false, true, GrNamedArgument.EMPTY_ARRAY, args, GrClosableBlock.EMPTY_ARRAY);
                    if (map != null) {
                        final Pair<PsiParameter, PsiType> pair = map.get(arg);
                        if (pair != null) {
                            result.add(SubtypeConstraint.create(pair.second));
                        }
                    }
                }
            }
        }
    }
    return result;
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint) GroovyExpectedTypesContributor(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.GroovyExpectedTypesContributor) GrClosureSignatureUtil(org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) NullableFunction(com.intellij.util.NullableFunction) ContainerUtil(com.intellij.util.containers.ContainerUtil) GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) ArrayList(java.util.ArrayList) List(java.util.List) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrTupleType(org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType) Map(java.util.Map) Pair(com.intellij.openapi.util.Pair) SubtypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.SubtypeConstraint) com.intellij.psi(com.intellij.psi) GroovyExpectedTypesProvider(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.GroovyExpectedTypesProvider) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) PsiUtil(org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil) ArrayList(java.util.ArrayList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint) Pair(com.intellij.openapi.util.Pair)

Example 14 with GrListOrMap

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

the class TypeInferenceHelper method getInitializerFor.

@Nullable
public static GrExpression getInitializerFor(GrExpression lValue) {
    final PsiElement parent = lValue.getParent();
    if (parent instanceof GrAssignmentExpression)
        return ((GrAssignmentExpression) parent).getRValue();
    if (parent instanceof GrTupleExpression) {
        final int i = ((GrTupleExpression) parent).indexOf(lValue);
        final PsiElement pparent = parent.getParent();
        org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.LOG.assertTrue(pparent instanceof GrAssignmentExpression);
        final GrExpression rValue = ((GrAssignmentExpression) pparent).getRValue();
        if (rValue instanceof GrListOrMap && !((GrListOrMap) rValue).isMap()) {
            final GrExpression[] initializers = ((GrListOrMap) rValue).getInitializers();
            if (initializers.length < i)
                return initializers[i];
        }
    }
    return null;
}
Also used : GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 15 with GrListOrMap

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

the class GroovyLanguageInjectionSupport method getTopLevelInjectionTarget.

@NotNull
public static PsiElement getTopLevelInjectionTarget(@NotNull final PsiElement host) {
    PsiElement target = host;
    PsiElement parent = target.getParent();
    for (; parent != null; target = parent, parent = target.getParent()) {
        if (parent instanceof GrBinaryExpression)
            continue;
        if (parent instanceof GrString)
            continue;
        if (parent instanceof GrParenthesizedExpression)
            continue;
        if (parent instanceof GrConditionalExpression && ((GrConditionalExpression) parent).getCondition() != target)
            continue;
        if (parent instanceof GrAnnotationArrayInitializer)
            continue;
        if (parent instanceof GrListOrMap) {
            parent = parent.getParent();
            continue;
        }
        break;
    }
    return target;
}
Also used : GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) GrAnnotationArrayInitializer(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArrayInitializer) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)39 PsiElement (com.intellij.psi.PsiElement)13 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)12 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)12 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)12 Nullable (org.jetbrains.annotations.Nullable)9 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)9 NotNull (org.jetbrains.annotations.NotNull)8 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)5 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)4 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)4 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)4 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)4 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)3 GrArgumentLabel (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel)3 ASTNode (com.intellij.lang.ASTNode)2 Pair (com.intellij.openapi.util.Pair)2 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)2 IElementType (com.intellij.psi.tree.IElementType)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2