Search in sources :

Example 26 with GrListOrMap

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

the class ArgumentListGenerator method generateMultiArg.

private boolean generateMultiArg(GrClosureSignatureUtil.ArgInfo<PsiElement> arg, GrClosureParameter param, PsiSubstitutor substitutor, Project project, GroovyPsiElement context) {
    final PsiType type = param.getType();
    if (type instanceof PsiEllipsisType) {
        for (PsiElement element : arg.args) {
            LOG.assertTrue(element instanceof GrExpression);
            ((GrExpression) element).accept(myExpressionGenerator);
            myBuilder.append(", ");
        }
        if (!arg.args.isEmpty()) {
            myBuilder.delete(myBuilder.length() - 2, myBuilder.length());
            return true;
        } else {
            return false;
        }
    } else if (type instanceof PsiArrayType) {
        myBuilder.append("new ");
        if (arg.args.isEmpty()) {
            TypeWriter.writeType(myBuilder, ((PsiArrayType) type).getComponentType(), context);
            myBuilder.append("[0]");
        } else {
            TypeWriter.writeTypeForNew(myBuilder, type, context);
            myBuilder.append("{");
            for (PsiElement element : arg.args) {
                LOG.assertTrue(element instanceof GrExpression);
                ((GrExpression) element).accept(myExpressionGenerator);
                myBuilder.append(", ");
            }
            if (!arg.args.isEmpty())
                myBuilder.delete(myBuilder.length() - 2, myBuilder.length());
            //if (arg.args.size() > 0) myBuilder.removeFromTheEnd(2);
            myBuilder.append('}');
        }
    } else {
        final GrExpression listOrMap = GroovyRefactoringUtil.generateArgFromMultiArg(substitutor, arg.args, type, project);
        LOG.assertTrue(listOrMap instanceof GrListOrMap);
        listOrMap.accept(myExpressionGenerator);
    }
    return true;
}
Also used : 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)

Example 27 with GrListOrMap

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

the class GroovyStaticTypeCheckVisitor method processTupleAssignment.

@Override
protected void processTupleAssignment(@NotNull GrTupleExpression tupleExpression, @NotNull GrExpression initializer) {
    if (initializer instanceof GrListOrMap && !((GrListOrMap) initializer).isMap()) {
        final GrListOrMap initializerList = (GrListOrMap) initializer;
        final GrExpression[] vars = tupleExpression.getExpressions();
        final GrExpression[] expressions = initializerList.getInitializers();
        if (vars.length > expressions.length) {
            registerError(initializer, GroovyBundle.message("incorrect.number.of.values", vars.length, expressions.length), LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR);
        } else {
            for (int i = 0; i < vars.length; i++) {
                processAssignmentWithinMultipleAssignment(vars[i], expressions[i], tupleExpression);
            }
        }
    } else {
        registerError(initializer, GroovyBundle.message("multiple.assignments.without.list.expr"), LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR);
    }
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)

Example 28 with GrListOrMap

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

the class GrChangeSignatureUsageProcessor method processMethodUsage.

private static void processMethodUsage(PsiElement element, JavaChangeInfo changeInfo, boolean toChangeArguments, boolean toCatchExceptions, GrClosureSignatureUtil.ArgInfo<PsiElement>[] map, PsiSubstitutor substitutor) {
    if (map == null)
        return;
    if (changeInfo.isNameChanged()) {
        if (element instanceof GrReferenceElement) {
            element = ((GrReferenceElement) element).handleElementRename(changeInfo.getNewName());
        }
    }
    if (toChangeArguments) {
        JavaParameterInfo[] parameters = changeInfo.getNewParameters();
        GrArgumentList argumentList = PsiUtil.getArgumentsList(element);
        GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(element.getProject());
        if (argumentList == null) {
            if (element instanceof GrEnumConstant) {
                argumentList = factory.createArgumentList();
                argumentList = (GrArgumentList) element.add(argumentList);
            } else {
                return;
            }
        }
        Set<PsiElement> argsToDelete = new HashSet<>(map.length * 2);
        for (GrClosureSignatureUtil.ArgInfo<PsiElement> argInfo : map) {
            argsToDelete.addAll(argInfo.args);
        }
        GrExpression[] values = new GrExpression[parameters.length];
        for (int i = 0; i < parameters.length; i++) {
            JavaParameterInfo parameter = parameters[i];
            int index = parameter.getOldIndex();
            if (index >= 0) {
                argsToDelete.removeAll(map[index].args);
            } else {
                values[i] = createDefaultValue(factory, changeInfo, parameter, argumentList, substitutor);
            }
        }
        for (PsiElement arg : argsToDelete) {
            arg.delete();
        }
        boolean skipOptionals = false;
        //PsiTreeUtil.getChildOfAnyType(argumentList, GrExpression.class, GrNamedArgument.class);
        PsiElement anchor = null;
        for (int i = 0; i < parameters.length; i++) {
            JavaParameterInfo parameter = parameters[i];
            int index = parameter.getOldIndex();
            if (index >= 0) {
                GrClosureSignatureUtil.ArgInfo<PsiElement> argInfo = map[index];
                List<PsiElement> arguments = argInfo.args;
                if (argInfo.isMultiArg) {
                    //arguments for Map and varArg
                    if ((i != 0 || !(!arguments.isEmpty() && arguments.iterator().next() instanceof GrNamedArgument)) && (i != parameters.length - 1 || !parameter.isVarargType())) {
                        final PsiType type = parameter.createType(changeInfo.getMethod().getParameterList(), argumentList.getManager());
                        final GrExpression arg = GroovyRefactoringUtil.generateArgFromMultiArg(substitutor, arguments, type, element.getProject());
                        for (PsiElement argument : arguments) {
                            argument.delete();
                        }
                        anchor = argumentList.addAfter(arg, anchor);
                        JavaCodeStyleManager.getInstance(anchor.getProject()).shortenClassReferences(anchor);
                    }
                } else {
                    //arguments for simple parameters
                    if (arguments.size() == 1) {
                        //arg exists
                        PsiElement arg = arguments.iterator().next();
                        if (i == parameters.length - 1 && parameter.isVarargType()) {
                            if (arg instanceof GrSafeCastExpression) {
                                PsiElement expr = ((GrSafeCastExpression) arg).getOperand();
                                if (expr instanceof GrListOrMap && !((GrListOrMap) expr).isMap()) {
                                    final PsiElement copy = expr.copy();
                                    PsiElement[] newVarargs = ((GrListOrMap) copy).getInitializers();
                                    for (PsiElement vararg : newVarargs) {
                                        anchor = argumentList.addAfter(vararg, anchor);
                                    }
                                    arg.delete();
                                    continue;
                                }
                            }
                        }
                        PsiElement curArg = getNextOfType(argumentList, anchor, GrExpression.class);
                        if (curArg == arg) {
                            anchor = arg;
                        } else {
                            final PsiElement copy = arg.copy();
                            anchor = argumentList.addAfter(copy, anchor);
                            arg.delete();
                        }
                    } else {
                        //arg is skipped. Parameter is optional
                        skipOptionals = true;
                    }
                }
            } else {
                if (skipOptionals && isParameterOptional(parameter))
                    continue;
                if (forceOptional(parameter)) {
                    skipOptionals = true;
                    continue;
                }
                try {
                    final GrExpression value = values[i];
                    if (i > 0 && (value == null || anchor == null)) {
                        PsiElement comma = Factory.createSingleLeafElement(GroovyTokenTypes.mCOMMA, ",", 0, 1, SharedImplUtil.findCharTableByTree(argumentList.getNode()), argumentList.getManager()).getPsi();
                        if (anchor == null)
                            anchor = argumentList.getLeftParen();
                        anchor = argumentList.addAfter(comma, anchor);
                    }
                    if (value != null) {
                        anchor = argumentList.addAfter(value, anchor);
                    }
                } catch (IncorrectOperationException e) {
                    LOG.error(e.getMessage());
                }
            }
        }
        for (PsiElement arg : argsToDelete) {
            arg.delete();
        }
        GrCall call = GroovyRefactoringUtil.getCallExpressionByMethodReference(element);
        if (argumentList.getText().trim().isEmpty() && (call == null || !PsiImplUtil.hasClosureArguments(call))) {
            argumentList = argumentList.replaceWithArgumentList(factory.createArgumentList());
        }
        CodeStyleManager.getInstance(argumentList.getProject()).reformat(argumentList);
    }
    if (toCatchExceptions) {
        final ThrownExceptionInfo[] exceptionInfos = changeInfo.getNewExceptions();
        PsiClassType[] exceptions = getExceptions(exceptionInfos, element, element.getManager());
        fixExceptions(element, exceptions);
    }
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrEnumConstant(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant) GrSafeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrSafeCastExpression) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) HashSet(com.intellij.util.containers.HashSet) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrClosureSignatureUtil(org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 29 with GrListOrMap

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

the class GppClosureParameterTypeProvider method getOverriddenMethodVariants.

@NotNull
public static List<Pair<PsiMethod, PsiSubstitutor>> getOverriddenMethodVariants(GrNamedArgument namedArgument) {
    final GrArgumentLabel label = namedArgument.getLabel();
    if (label == null) {
        return Collections.emptyList();
    }
    final String methodName = label.getName();
    if (methodName == null) {
        return Collections.emptyList();
    }
    final PsiElement map = namedArgument.getParent();
    if (map instanceof GrListOrMap && ((GrListOrMap) map).isMap()) {
        for (PsiType expected : GroovyExpectedTypesProvider.getDefaultExpectedTypes((GrExpression) map)) {
            if (expected instanceof PsiClassType) {
                final List<Pair<PsiMethod, PsiSubstitutor>> pairs = getMethodsToOverrideImplementInInheritor((PsiClassType) expected, false);
                return ContainerUtil.findAll(pairs, pair -> methodName.equals(pair.first.getName()));
            }
        }
    }
    return Collections.emptyList();
}
Also used : GrArgumentLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 30 with GrListOrMap

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

the class GppTypeConverter method isConvertible.

@Override
public Boolean isConvertible(@NotNull PsiType lType, @NotNull PsiType rType, @NotNull GroovyPsiElement context) {
    if (context instanceof GrListOrMap && context.getReference() instanceof LiteralConstructorReference && ((LiteralConstructorReference) context.getReference()).getConstructedClassType() != null)
        return null;
    if (rType instanceof GrTupleType) {
        final GrTupleType tupleType = (GrTupleType) rType;
        final PsiType expectedComponent = PsiUtil.extractIterableTypeParameter(lType, false);
        if (expectedComponent != null && isMethodCallConversion(context)) {
            PsiType[] parameters = tupleType.getParameters();
            if (parameters.length == 1) {
                PsiType tupleComponent = parameters[0];
                if (tupleComponent != null && TypesUtil.isAssignable(expectedComponent, tupleComponent, context) && hasDefaultConstructor(lType)) {
                    return true;
                }
            }
        }
    }
    return null;
}
Also used : LiteralConstructorReference(org.jetbrains.plugins.groovy.findUsages.LiteralConstructorReference) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrTupleType(org.jetbrains.plugins.groovy.lang.psi.impl.GrTupleType) PsiType(com.intellij.psi.PsiType)

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