Search in sources :

Example 16 with GrExpression

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

the class RenameAliasImportedMethodProcessor method renameElement.

@Override
public void renameElement(PsiElement psiElement, String newName, UsageInfo[] usages, @Nullable RefactoringElementListener listener) throws IncorrectOperationException {
    boolean isGetter = GroovyPropertyUtils.isSimplePropertyGetter((PsiMethod) psiElement);
    boolean isSetter = GroovyPropertyUtils.isSimplePropertySetter((PsiMethod) psiElement);
    List<UsageInfo> methodAccess = new ArrayList<>(usages.length);
    List<UsageInfo> propertyAccess = new ArrayList<>(usages.length);
    for (UsageInfo usage : usages) {
        final PsiElement element = usage.getElement();
        if (element instanceof GrReferenceExpression && ((GrReferenceExpression) element).advancedResolve().isInvokedOnProperty()) {
            propertyAccess.add(usage);
        } else {
            methodAccess.add(usage);
        }
    }
    super.renameElement(psiElement, newName, methodAccess.toArray(new UsageInfo[methodAccess.size()]), listener);
    final String propertyName;
    if (isGetter) {
        propertyName = GroovyPropertyUtils.getPropertyNameByGetterName(newName, true);
    } else if (isSetter) {
        propertyName = GroovyPropertyUtils.getPropertyNameBySetterName(newName);
    } else {
        propertyName = null;
    }
    if (propertyName == null) {
        //it means accessor is renamed to not-accessor and we should replace all property-access-refs with method-access-refs
        final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(psiElement.getProject());
        for (UsageInfo info : propertyAccess) {
            final PsiElement element = info.getElement();
            if (element instanceof GrReferenceExpression) {
                final PsiElement qualifier = ((GrReferenceExpression) element).getQualifier();
                String qualifierPrefix = qualifier == null ? "" : qualifier.getText() + ".";
                if (isGetter) {
                    final GrExpression call = factory.createExpressionFromText(qualifierPrefix + newName + "()");
                    ((GrReferenceExpression) element).replaceWithExpression(call, true);
                } else {
                    final PsiElement parent = element.getParent();
                    assert parent instanceof GrAssignmentExpression;
                    final GrExpression rValue = ((GrAssignmentExpression) parent).getRValue();
                    final GrExpression call = factory.createExpressionFromText(qualifierPrefix + newName + "(" + (rValue == null ? "" : rValue.getText()) + ")");
                    ((GrAssignmentExpression) parent).replaceWithExpression(call, true);
                }
            }
        }
    } else {
        for (UsageInfo usage : propertyAccess) {
            final PsiReference ref = usage.getReference();
            if (ref != null) {
                ref.handleElementRename(propertyName);
            }
        }
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) UsageInfo(com.intellij.usageView.UsageInfo) UnresolvableCollisionUsageInfo(com.intellij.refactoring.rename.UnresolvableCollisionUsageInfo) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 17 with GrExpression

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

the class GroovyIntroduceParameterUtil method getOccurrences.

static PsiElement[] getOccurrences(GrIntroduceParameterSettings settings) {
    final GrParametersOwner scope = settings.getToReplaceIn();
    final GrExpression expression = settings.getExpression();
    if (expression != null) {
        final PsiElement expr = PsiUtil.skipParentheses(expression, false);
        if (expr == null)
            return PsiElement.EMPTY_ARRAY;
        final PsiElement[] occurrences = GroovyRefactoringUtil.getExpressionOccurrences(expr, scope);
        if (occurrences == null || occurrences.length == 0) {
            throw new GrRefactoringError(GroovyRefactoringBundle.message("no.occurrences.found"));
        }
        return occurrences;
    } else {
        final GrVariable var = settings.getVar();
        LOG.assertTrue(var != null);
        final List<PsiElement> list = Collections.synchronizedList(new ArrayList<PsiElement>());
        ReferencesSearch.search(var, new LocalSearchScope(scope)).forEach(psiReference -> {
            final PsiElement element = psiReference.getElement();
            if (element != null) {
                list.add(element);
            }
            return true;
        });
        return list.toArray(new PsiElement[list.size()]);
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GrParametersOwner(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrParametersOwner) GrRefactoringError(org.jetbrains.plugins.groovy.refactoring.GrRefactoringError) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 18 with GrExpression

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

the class GroovyIntroduceParameterUtil method processChangedMethodCall.

public static void processChangedMethodCall(PsiElement element, GrIntroduceParameterSettings settings, Project project) {
    if (!(element.getParent() instanceof GrMethodCallExpression)) {
        LOG.error(element.getParent());
        return;
    }
    GrMethodCallExpression methodCall = (GrMethodCallExpression) element.getParent();
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    final String name = settings.getName();
    LOG.assertTrue(name != null);
    GrExpression expression = factory.createExpressionFromText(name, null);
    final GrArgumentList argList = methodCall.getArgumentList();
    final PsiElement[] exprs = argList.getAllArguments();
    if (exprs.length > 0) {
        argList.addAfter(expression, exprs[exprs.length - 1]);
    } else {
        argList.add(expression);
    }
    removeParametersFromCall(methodCall, settings);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 19 with GrExpression

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

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

the class CompleteReferenceExpression method getBindings.

private void getBindings() {
    final PsiClass containingClass = PsiTreeUtil.getParentOfType(myRefExpr, PsiClass.class);
    if (containingClass != null)
        return;
    final PsiFile file = FileContextUtil.getContextFile(myRefExpr);
    if (file instanceof GroovyFile) {
        ((GroovyFile) file).accept(new GroovyRecursiveElementVisitor() {

            @Override
            public void visitAssignmentExpression(@NotNull GrAssignmentExpression expression) {
                super.visitAssignmentExpression(expression);
                final GrExpression value = expression.getLValue();
                if (value instanceof GrReferenceExpression && !((GrReferenceExpression) value).isQualified()) {
                    final PsiElement resolved = ((GrReferenceExpression) value).resolve();
                    if (resolved instanceof GrBindingVariable) {
                        myProcessor.execute(resolved, ResolveState.initial());
                    } else if (resolved == null) {
                        myProcessor.execute(new GrBindingVariable((GroovyFile) file, ((GrReferenceExpression) value).getReferenceName(), true), ResolveState.initial());
                    }
                }
            }

            @Override
            public void visitTypeDefinition(@NotNull GrTypeDefinition typeDefinition) {
            //don't go into classes
            }
        });
    }
}
Also used : GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrBindingVariable(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrBindingVariable) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Aggregations

GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)312 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)93 Nullable (org.jetbrains.annotations.Nullable)68 PsiElement (com.intellij.psi.PsiElement)62 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)43 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)43 NotNull (org.jetbrains.annotations.NotNull)37 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)35 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)33 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)29 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)27 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)26 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)26 PsiType (com.intellij.psi.PsiType)24 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)23 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)23 IElementType (com.intellij.psi.tree.IElementType)22 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)18 ArrayList (java.util.ArrayList)16 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)16