Search in sources :

Example 11 with GrParameter

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

the class ConvertParameterToMapEntryIntention method performRefactoring.

private static void performRefactoring(final PsiElement element, final GrParametersOwner owner, final Collection<PsiElement> occurrences, final boolean createNewFirstParam, @Nullable final String mapParamName, final boolean specifyMapType) {
    final GrParameter param = getAppropriateParameter(element);
    assert param != null;
    final String paramName = param.getName();
    final String mapName = createNewFirstParam ? mapParamName : getFirstParameter(owner).getName();
    final Project project = element.getProject();
    final Runnable runnable = () -> {
        final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
        final GrParameterList list = owner.getParameterList();
        assert list != null;
        final int index = list.getParameterNumber(param);
        if (!createNewFirstParam && index <= 0) {
            // bad undo
            return;
        }
        //final List<GrCall> calls = getCallOccurrences(occurrences);
        try {
            for (PsiElement occurrence : occurrences) {
                GrReferenceExpression refExpr = null;
                GroovyResolveResult resolveResult = null;
                boolean isExplicitGetterCall = false;
                if (occurrence instanceof GrReferenceExpression) {
                    final PsiElement parent = occurrence.getParent();
                    if (parent instanceof GrCall) {
                        refExpr = (GrReferenceExpression) occurrence;
                        resolveResult = refExpr.advancedResolve();
                        final PsiElement resolved = resolveResult.getElement();
                        if (resolved instanceof PsiMethod && GroovyPropertyUtils.isSimplePropertyGetter(((PsiMethod) resolved)) && //check for explicit getter call
                        ((PsiMethod) resolved).getName().equals(refExpr.getReferenceName())) {
                            isExplicitGetterCall = true;
                        }
                    } else if (parent instanceof GrReferenceExpression) {
                        resolveResult = ((GrReferenceExpression) parent).advancedResolve();
                        final PsiElement resolved = resolveResult.getElement();
                        if (resolved instanceof PsiMethod && "call".equals(((PsiMethod) resolved).getName())) {
                            refExpr = (GrReferenceExpression) parent;
                        }
                    }
                }
                if (refExpr == null)
                    continue;
                final GrClosureSignature signature = generateSignature(owner, refExpr);
                if (signature == null)
                    continue;
                GrCall call;
                if (isExplicitGetterCall) {
                    PsiElement parent = refExpr.getParent();
                    LOG.assertTrue(parent instanceof GrCall);
                    parent = parent.getParent();
                    if (parent instanceof GrReferenceExpression && "call".equals(((GrReferenceExpression) parent).getReferenceName())) {
                        parent = parent.getParent();
                    }
                    if (parent instanceof GrCall) {
                        call = (GrCall) parent;
                    } else {
                        continue;
                    }
                } else {
                    call = (GrCall) refExpr.getParent();
                }
                if (resolveResult.isInvokedOnProperty()) {
                    final PsiElement parent = call.getParent();
                    if (parent instanceof GrCall) {
                        call = (GrCall) parent;
                    } else if (parent instanceof GrReferenceExpression && parent.getParent() instanceof GrCall) {
                        final PsiElement resolved = ((GrReferenceExpression) parent).resolve();
                        if (resolved instanceof PsiMethod && "call".equals(((PsiMethod) resolved).getName())) {
                            call = (GrCall) parent.getParent();
                        } else {
                            continue;
                        }
                    }
                }
                final GrClosureSignatureUtil.ArgInfo<PsiElement>[] argInfos = GrClosureSignatureUtil.mapParametersToArguments(signature, call);
                if (argInfos == null)
                    continue;
                final GrClosureSignatureUtil.ArgInfo<PsiElement> argInfo = argInfos[index];
                final GrNamedArgument namedArg;
                if (argInfo.isMultiArg) {
                    if (argInfo.args.isEmpty())
                        continue;
                    String arg = "[" + StringUtil.join(ContainerUtil.map(argInfo.args, element1 -> element1.getText()), ", ") + "]";
                    for (PsiElement psiElement : argInfo.args) {
                        psiElement.delete();
                    }
                    namedArg = factory.createNamedArgument(paramName, factory.createExpressionFromText(arg));
                } else {
                    if (argInfo.args.isEmpty())
                        continue;
                    final PsiElement argument = argInfo.args.iterator().next();
                    assert argument instanceof GrExpression;
                    namedArg = factory.createNamedArgument(paramName, (GrExpression) argument);
                    argument.delete();
                }
                call.addNamedArgument(namedArg);
            }
        } catch (IncorrectOperationException e) {
            LOG.error(e);
        }
        //Replace of occurrences of old parameter in closure/method
        final Collection<PsiReference> references = ReferencesSearch.search(param).findAll();
        for (PsiReference ref : references) {
            final PsiElement elt = ref.getElement();
            if (elt instanceof GrReferenceExpression) {
                GrReferenceExpression expr = (GrReferenceExpression) elt;
                final GrExpression newExpr = factory.createExpressionFromText(mapName + "." + paramName);
                expr.replaceWithExpression(newExpr, true);
            }
        }
        //Add new map parameter to closure/method if it's necessary
        if (createNewFirstParam) {
            try {
                final GrParameter newParam = factory.createParameter(mapName, specifyMapType ? MAP_TYPE_TEXT : "", null);
                list.addAfter(newParam, null);
            } catch (IncorrectOperationException e) {
                LOG.error(e);
            }
        }
        //Eliminate obsolete parameter from parameter list
        param.delete();
    };
    CommandProcessor.getInstance().executeCommand(project, () -> ApplicationManager.getApplication().runWriteAction(runnable), REFACTORING_NAME, null);
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrClosureSignatureUtil(org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil) Project(com.intellij.openapi.project.Project) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) Collection(java.util.Collection) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 12 with GrParameter

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

the class ConvertParameterToMapEntryIntention method getAppropriateParameter.

@Nullable
private static GrParameter getAppropriateParameter(final PsiElement element) {
    if (element instanceof GrParameter) {
        return (GrParameter) element;
    }
    if (element instanceof GrReferenceExpression) {
        final GrReferenceExpression expr = (GrReferenceExpression) element;
        final PsiElement resolved = expr.resolve();
        LOG.assertTrue(resolved instanceof GrParameter);
        return ((GrParameter) resolved);
    }
    LOG.error("Selected expression is not resolved to method/closure parameter");
    return null;
}
Also used : GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 13 with GrParameter

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

the class ConvertClosureArgToItIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrClosableBlock closure = (GrClosableBlock) element;
    final GrParameterList parameterList = closure.getParameterList();
    final GrParameter parameter = parameterList.getParameters()[0];
    final Set<GrReferenceExpression> referencesToChange = new HashSet<>();
    final GroovyRecursiveElementVisitor visitor = new GroovyRecursiveElementVisitor() {

        @Override
        public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
            super.visitReferenceExpression(referenceExpression);
            if (!referenceExpression.getText().equals(parameter.getName())) {
                return;
            }
            final PsiElement referent = referenceExpression.resolve();
            if (parameter.equals(referent)) {
                referencesToChange.add(referenceExpression);
            }
        }
    };
    closure.accept(visitor);
    parameter.delete();
    for (GrReferenceExpression referenceExpression : referencesToChange) {
        PsiImplUtil.replaceExpression("it", referenceExpression);
    }
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) HashSet(java.util.HashSet) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 14 with GrParameter

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

the class MyPredicate method getParameterByArgument.

@Nullable
private static GrParameter getParameterByArgument(GrExpression arg) {
    PsiElement parent = PsiUtil.skipParentheses(arg.getParent(), true);
    if (!(parent instanceof GrArgumentList))
        return null;
    final GrArgumentList argList = (GrArgumentList) parent;
    parent = parent.getParent();
    if (!(parent instanceof GrMethodCall))
        return null;
    final GrMethodCall methodCall = (GrMethodCall) parent;
    final GrExpression expression = methodCall.getInvokedExpression();
    if (!(expression instanceof GrReferenceExpression))
        return null;
    final GroovyResolveResult resolveResult = ((GrReferenceExpression) expression).advancedResolve();
    if (resolveResult == null)
        return null;
    GrClosableBlock[] closures = methodCall.getClosureArguments();
    final Map<GrExpression, Pair<PsiParameter, PsiType>> mapToParams = GrClosureSignatureUtil.mapArgumentsToParameters(resolveResult, arg, false, false, argList.getNamedArguments(), argList.getExpressionArguments(), closures);
    if (mapToParams == null)
        return null;
    final Pair<PsiParameter, PsiType> parameterPair = mapToParams.get(arg);
    final PsiParameter parameter = parameterPair == null ? null : parameterPair.getFirst();
    return parameter instanceof GrParameter ? ((GrParameter) parameter) : null;
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) Pair(com.intellij.openapi.util.Pair) Nullable(org.jetbrains.annotations.Nullable)

Example 15 with GrParameter

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

the class GroovyInlineMethodUtil method replaceParametersWithArguments.

/**
   * Inline method call's arguments as its parameters
   *
   * @param call   method call
   * @param method given method
   */
public static void replaceParametersWithArguments(GrCallExpression call, GrMethod method) throws IncorrectOperationException {
    GrParameter[] parameters = method.getParameters();
    if (parameters.length == 0)
        return;
    GrArgumentList argumentList = call.getArgumentList();
    if (argumentList == null) {
        setDefaultValuesToParameters(method, null, call);
        return;
    }
    Project project = call.getProject();
    final GroovyResolveResult resolveResult = call.advancedResolve();
    GrClosureSignature signature = GrClosureSignatureUtil.createSignature(method, resolveResult.getSubstitutor());
    if (signature == null) {
        return;
    }
    GrClosureSignatureUtil.ArgInfo<PsiElement>[] infos = GrClosureSignatureUtil.mapParametersToArguments(signature, call.getNamedArguments(), call.getExpressionArguments(), call.getClosureArguments(), call, true, false);
    if (infos == null)
        return;
    for (int i = 0; i < infos.length; i++) {
        GrClosureSignatureUtil.ArgInfo<PsiElement> argInfo = infos[i];
        GrParameter parameter = parameters[i];
        final GrExpression arg = inferArg(signature, parameters, parameter, argInfo, project);
        if (arg != null) {
            replaceAllOccurrencesWithExpression(method, call, arg, parameter);
        }
    }
}
Also used : GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrClosureSignatureUtil(org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil) Project(com.intellij.openapi.project.Project) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)

Aggregations

GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)99 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)22 NotNull (org.jetbrains.annotations.NotNull)20 PsiElement (com.intellij.psi.PsiElement)19 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)19 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)18 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)16 GrParameterList (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList)14 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)13 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)12 ArrayList (java.util.ArrayList)11 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)10 TextRange (com.intellij.openapi.util.TextRange)9 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)9 Nullable (org.jetbrains.annotations.Nullable)8 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)7 Project (com.intellij.openapi.project.Project)6 IncorrectOperationException (com.intellij.util.IncorrectOperationException)6 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)6 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)6