Search in sources :

Example 16 with GrNamedArgument

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

the class EquivalenceChecker method applicationStatementsAreEquivalent.

private static boolean applicationStatementsAreEquivalent(GrApplicationStatement statement1, GrApplicationStatement statement2) {
    final GrExpression funExpression1 = statement1.getInvokedExpression();
    final GrExpression funExpression2 = statement2.getInvokedExpression();
    if (!expressionsAreEquivalent(funExpression1, funExpression2)) {
        return false;
    }
    final GrArgumentList argumentList1 = statement1.getArgumentList();
    final GrArgumentList argumentList2 = statement2.getArgumentList();
    final GrExpression[] args1 = argumentList1.getExpressionArguments();
    final GrExpression[] args2 = argumentList2.getExpressionArguments();
    if (!expressionListsAreEquivalent(args1, args2)) {
        return false;
    }
    final GrNamedArgument[] namedArgs1 = argumentList1.getNamedArguments();
    final GrNamedArgument[] namedArgs2 = argumentList2.getNamedArguments();
    if (!namedArgumentListsAreEquivalent(namedArgs1, namedArgs2)) {
        return false;
    }
    return true;
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)

Example 17 with GrNamedArgument

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

the class EquivalenceChecker method namedArgumentListsAreEquivalent.

private static boolean namedArgumentListsAreEquivalent(GrNamedArgument[] namedArgs1, GrNamedArgument[] namedArgs2) {
    if (namedArgs1.length != namedArgs2.length) {
        return false;
    }
    for (GrNamedArgument arg1 : namedArgs1) {
        final GrArgumentLabel label1 = arg1.getLabel();
        if (label1 == null) {
            return false;
        }
        final String name1 = label1.getName();
        boolean found = false;
        final GrExpression expression1 = arg1.getExpression();
        for (GrNamedArgument arg2 : namedArgs2) {
            final GrArgumentLabel label2 = arg2.getLabel();
            if (label2 == null) {
                return false;
            }
            final String name2 = label2.getName();
            final GrExpression expression2 = arg2.getExpression();
            if (name1 == null) {
                if (name2 == null && expressionsAreEquivalent(((GrExpression) label1.getNameElement()), (GrExpression) label2.getNameElement()) && expressionsAreEquivalent(expression1, expression2)) {
                    found = true;
                    break;
                }
            } else if (name1.equals(name2) && expressionsAreEquivalent(expression1, expression2)) {
                found = true;
                break;
            }
        }
        if (!found) {
            return false;
        }
    }
    return true;
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrArgumentLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel)

Example 18 with GrNamedArgument

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument 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 19 with GrNamedArgument

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

the class GrClosureSignatureUtil method getTypeByArg.

@Nullable
public static PsiType getTypeByArg(ArgInfo<PsiElement> arg, PsiManager manager, GlobalSearchScope resolveScope) {
    if (arg.isMultiArg) {
        if (arg.args.isEmpty())
            return LazyFqnClassType.getLazyType(CommonClassNames.JAVA_LANG_OBJECT, LanguageLevel.JDK_1_5, resolveScope, JavaPsiFacade.getInstance(manager.getProject())).createArrayType();
        PsiType leastUpperBound = null;
        PsiElement first = arg.args.get(0);
        if (first instanceof GrNamedArgument) {
            GrNamedArgument[] args = new GrNamedArgument[arg.args.size()];
            for (int i = 0, size = arg.args.size(); i < size; i++) {
                args[i] = (GrNamedArgument) arg.args.get(i);
            }
            return GrMapType.createFromNamedArgs(first, args);
        } else {
            for (PsiElement elem : arg.args) {
                if (elem instanceof GrExpression) {
                    leastUpperBound = TypesUtil.getLeastUpperBoundNullable(leastUpperBound, ((GrExpression) elem).getType(), manager);
                }
            }
            if (leastUpperBound == null)
                return null;
            return leastUpperBound.createArrayType();
        }
    } else {
        if (arg.args.isEmpty())
            return null;
        PsiElement elem = arg.args.get(0);
        if (elem instanceof GrExpression) {
            return ((GrExpression) elem).getType();
        }
        return null;
    }
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 20 with GrNamedArgument

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

the class GrClosureSignatureUtil method mapArgumentsToParameters.

@Nullable
public static Map<GrExpression, Pair<PsiParameter, PsiType>> mapArgumentsToParameters(@NotNull GroovyResolveResult resolveResult, @NotNull PsiElement context, final boolean partial, final boolean eraseArgs, @NotNull final GrNamedArgument[] namedArgs, @NotNull final GrExpression[] expressionArgs, @NotNull GrClosableBlock[] closureArguments) {
    final GrClosureSignature signature;
    final PsiParameter[] parameters;
    final PsiElement element = resolveResult.getElement();
    final PsiSubstitutor substitutor = resolveResult.getSubstitutor();
    if (element instanceof PsiMethod) {
        signature = createSignature((PsiMethod) element, substitutor, eraseArgs);
        parameters = ((PsiMethod) element).getParameterList().getParameters();
    } else if (element instanceof GrClosableBlock) {
        signature = eraseArgs ? createSignatureWithErasedParameterTypes((GrClosableBlock) element) : createSignature(((GrClosableBlock) element));
        parameters = ((GrClosableBlock) element).getAllParameters();
    } else {
        return null;
    }
    final ArgInfo<PsiElement>[] argInfos = mapParametersToArguments(signature, namedArgs, expressionArgs, closureArguments, context, partial, eraseArgs);
    if (argInfos == null) {
        return null;
    }
    final HashMap<GrExpression, Pair<PsiParameter, PsiType>> result = new HashMap<>();
    for (int i = 0; i < argInfos.length; i++) {
        ArgInfo<PsiElement> info = argInfos[i];
        if (info == null)
            continue;
        for (PsiElement arg : info.args) {
            if (arg instanceof GrNamedArgument) {
                arg = ((GrNamedArgument) arg).getExpression();
            }
            final GrExpression expression = (GrExpression) arg;
            PsiType type = parameters[i].getType();
            if (info.isMultiArg && type instanceof PsiArrayType) {
                type = ((PsiArrayType) type).getComponentType();
            }
            result.put(expression, Pair.create(parameters[i], substitutor.substitute(type)));
        }
    }
    return result;
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Pair(com.intellij.openapi.util.Pair) 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