Search in sources :

Example 1 with GrClosureSignature

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

the class JavaSafeDeleteDelegateForGroovy method createUsageInfoForParameter.

@Override
public void createUsageInfoForParameter(PsiReference reference, List<UsageInfo> usages, final PsiParameter parameter, final PsiMethod method) {
    int index = method.getParameterList().getParameterIndex(parameter);
    final PsiElement element = reference.getElement();
    GrCall call = null;
    if (element instanceof GrCall) {
        call = (GrCall) element;
    } else if (element.getParent() instanceof GrCall) {
        call = (GrCall) element.getParent();
    }
    if (call != null) {
        GrClosureSignature signature = GrClosureSignatureUtil.createSignature(call);
        //todo ???
        if (signature == null)
            return;
        GrClosureSignatureUtil.ArgInfo<PsiElement>[] argInfos = GrClosureSignatureUtil.mapParametersToArguments(signature, call);
        //todo???
        if (argInfos == null)
            return;
        for (PsiElement arg : argInfos[index].args) {
            usages.add(new SafeDeleteReferenceJavaDeleteUsageInfo(arg, parameter, true));
        }
    } else if (element instanceof GrDocMethodReference) {
        @NonNls final StringBuilder newText = new StringBuilder();
        newText.append("/** @see ");
        GrDocReferenceElement holder = ((GrDocMethodReference) element).getReferenceHolder();
        if (holder != null) {
            newText.append(holder.getText());
        }
        newText.append('#');
        newText.append(method.getName());
        newText.append('(');
        final List<PsiParameter> parameters = new ArrayList<>(Arrays.asList(method.getParameterList().getParameters()));
        parameters.remove(parameter);
        newText.append(StringUtil.join(parameters, psiParameter -> parameter.getType().getCanonicalText(), ","));
        newText.append(")*/");
        usages.add(new SafeDeleteReferenceJavaDeleteUsageInfo(element, parameter, true) {

            @Override
            public void deleteElement() throws IncorrectOperationException {
                ((GrDocMethodReference) element).bindToText(method.getProject(), newText.toString());
            }
        });
    }
}
Also used : SafeDeleteReferenceJavaDeleteUsageInfo(com.intellij.refactoring.safeDelete.usageInfo.SafeDeleteReferenceJavaDeleteUsageInfo) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrDocReferenceElement(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocReferenceElement) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) ArrayList(java.util.ArrayList) List(java.util.List) GrDocMethodReference(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocMethodReference)

Example 2 with GrClosureSignature

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

the class GrIntroduceClosureParameterProcessor method removeParametersFromCall.

private static void removeParametersFromCall(GrMethodCallExpression methodCall, GrIntroduceParameterSettings settings) {
    final GroovyResolveResult resolveResult = methodCall.advancedResolve();
    final PsiElement resolved = resolveResult.getElement();
    LOG.assertTrue(resolved instanceof PsiMethod);
    final GrClosureSignature signature = GrClosureSignatureUtil.createSignature((PsiMethod) resolved, resolveResult.getSubstitutor());
    final GrClosureSignatureUtil.ArgInfo<PsiElement>[] argInfos = GrClosureSignatureUtil.mapParametersToArguments(signature, methodCall);
    LOG.assertTrue(argInfos != null);
    settings.parametersToRemove().forEach(new TIntProcedure() {

        @Override
        public boolean execute(int value) {
            final List<PsiElement> args = argInfos[value].args;
            for (PsiElement arg : args) {
                arg.delete();
            }
            return true;
        }
    });
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) TIntProcedure(gnu.trove.TIntProcedure) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) List(java.util.List) ArrayList(java.util.ArrayList) GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList)

Example 3 with GrClosureSignature

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

the class GroovyIntroduceParameterUtil method removeParametersFromCall.

private static void removeParametersFromCall(GrMethodCallExpression methodCall, GrIntroduceParameterSettings settings) {
    final GroovyResolveResult resolveResult = methodCall.advancedResolve();
    final PsiElement resolved = resolveResult.getElement();
    LOG.assertTrue(resolved instanceof PsiMethod);
    final GrClosureSignature signature = GrClosureSignatureUtil.createSignature((PsiMethod) resolved, resolveResult.getSubstitutor());
    final GrClosureSignatureUtil.ArgInfo<PsiElement>[] argInfos = GrClosureSignatureUtil.mapParametersToArguments(signature, methodCall);
    LOG.assertTrue(argInfos != null);
    settings.parametersToRemove().forEach(new TIntProcedure() {

        @Override
        public boolean execute(int value) {
            final List<PsiElement> args = argInfos[value].args;
            for (PsiElement arg : args) {
                arg.delete();
            }
            return true;
        }
    });
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) TIntProcedure(gnu.trove.TIntProcedure) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) TIntArrayList(gnu.trove.TIntArrayList) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 4 with GrClosureSignature

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

the class GroovyIntroduceParameterMethodUsagesProcessor method processChangeMethodUsage.

@Override
public boolean processChangeMethodUsage(IntroduceParameterData data, UsageInfo usage, UsageInfo[] usages) throws IncorrectOperationException {
    GrCall callExpression = GroovyRefactoringUtil.getCallExpressionByMethodReference(usage.getElement());
    if (callExpression == null)
        return true;
    GrArgumentList argList = callExpression.getArgumentList();
    GrExpression[] oldArgs = argList.getExpressionArguments();
    final GrExpression anchor;
    if (!data.getMethodToSearchFor().isVarArgs()) {
        anchor = getLast(oldArgs);
    } else {
        final PsiParameter[] parameters = data.getMethodToSearchFor().getParameterList().getParameters();
        if (parameters.length > oldArgs.length) {
            anchor = getLast(oldArgs);
        } else {
            final int lastNonVararg = parameters.length - 2;
            anchor = lastNonVararg >= 0 ? oldArgs[lastNonVararg] : null;
        }
    }
    PsiMethod method = PsiTreeUtil.getParentOfType(argList, PsiMethod.class);
    GrClosureSignature signature = GrClosureSignatureUtil.createSignature(callExpression);
    if (signature == null)
        signature = GrClosureSignatureUtil.createSignature(data.getMethodToSearchFor(), PsiSubstitutor.EMPTY);
    final GrClosureSignatureUtil.ArgInfo<PsiElement>[] actualArgs = GrClosureSignatureUtil.mapParametersToArguments(signature, callExpression.getNamedArguments(), callExpression.getExpressionArguments(), callExpression.getClosureArguments(), callExpression, false, true);
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(data.getProject());
    if (method != null && IntroduceParameterUtil.isMethodInUsages(data, method, usages)) {
        argList.addAfter(factory.createExpressionFromText(data.getParameterName()), anchor);
    } else {
        final PsiElement _expr = data.getParameterInitializer().getExpression();
        PsiElement initializer = ExpressionConverter.getExpression(_expr, GroovyLanguage.INSTANCE, data.getProject());
        LOG.assertTrue(initializer instanceof GrExpression);
        GrExpression newArg = GroovyIntroduceParameterUtil.addClosureToCall(initializer, argList);
        if (newArg == null) {
            final PsiElement dummy = argList.addAfter(factory.createExpressionFromText("1"), anchor);
            newArg = ((GrExpression) dummy).replaceWithExpression((GrExpression) initializer, true);
        }
        final PsiMethod methodToReplaceIn = data.getMethodToReplaceIn();
        new OldReferencesResolver(callExpression, newArg, methodToReplaceIn, data.getReplaceFieldsWithGetters(), initializer, signature, actualArgs, methodToReplaceIn.getParameterList().getParameters()).resolve();
        ChangeContextUtil.clearContextInfo(initializer);
        //newArg can be replaced by OldReferenceResolver
        if (newArg.isValid()) {
            JavaCodeStyleManager.getInstance(newArg.getProject()).shortenClassReferences(newArg);
            CodeStyleManager.getInstance(data.getProject()).reformat(newArg);
        }
    }
    if (actualArgs == null) {
        removeParamsFromUnresolvedCall(callExpression, data);
    } else {
        removeParametersFromCall(actualArgs, data.getParametersToRemove());
    }
    if (argList.getAllArguments().length == 0 && PsiImplUtil.hasClosureArguments(callExpression)) {
        final GrArgumentList emptyArgList = ((GrMethodCallExpression) factory.createExpressionFromText("foo{}")).getArgumentList();
        argList.replace(emptyArgList);
    }
    return false;
}
Also used : GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)

Example 5 with GrClosureSignature

use of org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature 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

GrClosureSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)33 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)13 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)9 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)8 ArrayList (java.util.ArrayList)6 Nullable (org.jetbrains.annotations.Nullable)6 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)6 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)6 GrClosureParameter (org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter)6 GrClosureSignatureUtil (org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil)6 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)5 MethodSignature (com.intellij.psi.util.MethodSignature)4 List (java.util.List)4 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)4 GrRecursiveSignatureVisitor (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrRecursiveSignatureVisitor)4 GrSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature)4 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)4 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)4 Pair (com.intellij.openapi.util.Pair)3 TIntProcedure (gnu.trove.TIntProcedure)3