Search in sources :

Example 1 with GrCall

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

the class GroovyArgListSelectioner method select.

@Override
public List<TextRange> select(PsiElement element, CharSequence editorText, int cursorOffset, Editor editor) {
    List<TextRange> result = super.select(element, editorText, cursorOffset, editor);
    if (element instanceof GrArgumentList) {
        GrArgumentList args = ((GrArgumentList) element);
        TextRange range = args.getTextRange();
        if (range.contains(cursorOffset)) {
            PsiElement leftParen = args.getLeftParen();
            PsiElement rightParen = args.getRightParen();
            if (leftParen != null) {
                int leftOffset = leftParen.getTextOffset();
                if (rightParen != null) {
                    if (leftOffset + 1 < rightParen.getTextOffset()) {
                        int rightOffset = rightParen.getTextRange().getEndOffset();
                        range = new TextRange(leftParen.getTextRange().getStartOffset() + 1, rightOffset - 1);
                        result.add(range);
                    }
                } else {
                    range = new TextRange(leftParen.getTextRange().getStartOffset() + 1, element.getTextRange().getEndOffset());
                    result.add(range);
                }
            }
        }
    }
    final PsiElement parent = element.getParent();
    if (parent instanceof GrReferenceExpression) {
        final GrArgumentList argumentList = ((GrCall) parent.getParent()).getArgumentList();
        final PsiElement refName = ((GrReferenceExpression) parent).getReferenceNameElement();
        if (argumentList != null && refName == element) {
            result.add(new TextRange(refName.getTextRange().getStartOffset(), argumentList.getTextRange().getEndOffset()));
        }
    }
    return result;
}
Also used : GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 2 with GrCall

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall 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 3 with GrCall

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

the class RenameGrFieldProcessor method findCollisions.

@Override
public void findCollisions(PsiElement element, String newName, Map<? extends PsiElement, String> allRenames, List<UsageInfo> result) {
    List<UsageInfo> collisions = new ArrayList<>();
    for (UsageInfo info : result) {
        if (!(info instanceof MoveRenameUsageInfo))
            continue;
        final PsiElement infoElement = info.getElement();
        final PsiElement referencedElement = ((MoveRenameUsageInfo) info).getReferencedElement();
        if (!(infoElement instanceof GrReferenceExpression))
            continue;
        final GrReferenceExpression refExpr = (GrReferenceExpression) infoElement;
        if (!(referencedElement instanceof GrField || refExpr.advancedResolve().isInvokedOnProperty()))
            continue;
        if (!(refExpr.getParent() instanceof GrCall))
            continue;
        final PsiType[] argTypes = PsiUtil.getArgumentTypes(refExpr, false);
        final PsiType[] typeArguments = refExpr.getTypeArguments();
        final MethodResolverProcessor processor = new MethodResolverProcessor(newName, refExpr, false, null, argTypes, typeArguments);
        final PsiMethod resolved = ResolveUtil.resolveExistingElement(refExpr, processor, PsiMethod.class);
        if (resolved == null)
            continue;
        collisions.add(new UnresolvableCollisionUsageInfo(resolved, refExpr) {

            @Override
            public String getDescription() {
                return GroovyRefactoringBundle.message("usage.will.be.overriden.by.method", refExpr.getParent().getText(), PsiFormatUtil.formatMethod(resolved, PsiSubstitutor.EMPTY, PsiFormatUtilBase.SHOW_NAME, PsiFormatUtilBase.SHOW_TYPE));
            }
        });
    }
    result.addAll(collisions);
    super.findCollisions(element, newName, allRenames, result);
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) ArrayList(java.util.ArrayList) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) MethodResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.MethodResolverProcessor) UnresolvableCollisionUsageInfo(com.intellij.refactoring.rename.UnresolvableCollisionUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) UnresolvableCollisionUsageInfo(com.intellij.refactoring.rename.UnresolvableCollisionUsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo)

Example 4 with GrCall

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall 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 GrCall

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

the class ExtractClosureFromClosureProcessor method findUsages.

@NotNull
@Override
protected UsageInfo[] findUsages() {
    final GrVariable var = (GrVariable) myHelper.getToSearchFor();
    if (var != null) {
        final List<UsageInfo> result = new ArrayList<>();
        for (PsiReference ref : ReferencesSearch.search(var)) {
            final PsiElement element = ref.getElement();
            if (element.getLanguage() != GroovyLanguage.INSTANCE) {
                result.add(new OtherLanguageUsageInfo(ref));
                continue;
            }
            final GrCall call = GroovyRefactoringUtil.getCallExpressionByMethodReference(element);
            if (call == null)
                continue;
            result.add(new ExternalUsageInfo(element));
        }
        return result.toArray(new UsageInfo[result.size()]);
    }
    return UsageInfo.EMPTY_ARRAY;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) ExternalUsageInfo(com.intellij.refactoring.introduceParameter.ExternalUsageInfo) ExternalUsageInfo(com.intellij.refactoring.introduceParameter.ExternalUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)23 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)11 PsiElement (com.intellij.psi.PsiElement)10 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)10 Nullable (org.jetbrains.annotations.Nullable)8 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)8 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)7 GrClosureSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)6 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)4 ArrayList (java.util.ArrayList)3 NotNull (org.jetbrains.annotations.NotNull)3 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)3 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)3 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)3 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)3 Project (com.intellij.openapi.project.Project)2 PsiMethod (com.intellij.psi.PsiMethod)2 PsiReference (com.intellij.psi.PsiReference)2 UsageInfo (com.intellij.usageView.UsageInfo)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2