Search in sources :

Example 41 with GrReferenceExpression

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

the class GrAliasImportIntention method getElementPredicate.

@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
    return new PsiElementPredicate() {

        @Override
        public boolean satisfiedBy(PsiElement element) {
            if (element instanceof GrReferenceExpression) {
                GroovyResolveResult result = ((GrReferenceExpression) element).advancedResolve();
                PsiElement context = result.getCurrentFileResolveContext();
                if (!(context instanceof GrImportStatement))
                    return false;
                GrImportStatement importStatement = (GrImportStatement) context;
                if (!importStatement.isStatic() || importStatement.isAliasedImport())
                    return false;
                return true;
            } else if (element instanceof GrImportStatement) {
                final GrImportStatement importStatement = (GrImportStatement) element;
                if (!importStatement.isStatic())
                    return false;
                if (importStatement.isOnDemand())
                    return false;
                if (importStatement.isAliasedImport())
                    return false;
                return true;
            }
            return false;
        }
    };
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 42 with GrReferenceExpression

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

the class GrPullUpHelper method postProcessMember.

@Override
public void postProcessMember(PsiMember member) {
    ((GrMember) member).accept(myExplicitSuperDeleter);
    ((GrMember) member).accept(myThisSuperAdjuster);
    GroovyChangeContextUtil.decodeContextInfo(member, null, null);
    ((GroovyPsiElement) member).accept(new GroovyRecursiveElementVisitor() {

        @Override
        public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
            if (processRef(referenceExpression))
                return;
            super.visitReferenceExpression(referenceExpression);
        }

        @Override
        public void visitCodeReferenceElement(@NotNull GrCodeReferenceElement refElement) {
            if (processRef(refElement))
                return;
            super.visitCodeReferenceElement(refElement);
        }

        private boolean processRef(@NotNull GrReferenceElement<? extends GroovyPsiElement> refElement) {
            final PsiElement qualifier = refElement.getQualifier();
            if (qualifier != null) {
                final Boolean preserveQualifier = qualifier.getCopyableUserData(PRESERVE_QUALIFIER);
                if (preserveQualifier != null && !preserveQualifier) {
                    refElement.setQualifier(null);
                    return true;
                }
            }
            return false;
        }
    });
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 43 with GrReferenceExpression

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

the class RenameGrFieldProcessor method qualify.

private static void qualify(PsiMember member, GrReferenceExpression refExpr) {
    String name = refExpr.getReferenceName();
    final PsiClass clazz = member.getContainingClass();
    if (clazz == null)
        return;
    if (refExpr.getQualifierExpression() != null)
        return;
    final PsiElement replaced;
    if (member.hasModifierProperty(PsiModifier.STATIC)) {
        final GrReferenceExpression newRefExpr = GroovyPsiElementFactory.getInstance(member.getProject()).createReferenceExpressionFromText(clazz.getQualifiedName() + "." + name);
        replaced = refExpr.replace(newRefExpr);
    } else {
        final PsiClass containingClass = PsiTreeUtil.getParentOfType(refExpr, PsiClass.class);
        if (member.getManager().areElementsEquivalent(containingClass, clazz)) {
            final GrReferenceExpression newRefExpr = GroovyPsiElementFactory.getInstance(member.getProject()).createReferenceExpressionFromText("this." + name);
            replaced = refExpr.replace(newRefExpr);
        } else {
            final GrReferenceExpression newRefExpr = GroovyPsiElementFactory.getInstance(member.getProject()).createReferenceExpressionFromText(clazz.getQualifiedName() + ".this." + name);
            replaced = refExpr.replace(newRefExpr);
        }
    }
    JavaCodeStyleManager.getInstance(replaced.getProject()).shortenClassReferences(replaced);
}
Also used : GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 44 with GrReferenceExpression

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

the class RenameGrFieldProcessor method renameElement.

@Override
public void renameElement(final PsiElement psiElement, String newName, final UsageInfo[] usages, @Nullable RefactoringElementListener listener) throws IncorrectOperationException {
    GrField field = (GrField) psiElement;
    Map<GrReferenceExpression, PsiElement> handled = ContainerUtil.newHashMap();
    for (UsageInfo usage : usages) {
        final PsiReference ref = usage.getReference();
        if (ref instanceof GrReferenceExpression) {
            PsiElement resolved = ref.resolve();
            ref.handleElementRename(getNewNameFromTransformations(resolved, newName));
            handled.put((GrReferenceExpression) ref, resolved);
        } else if (ref != null) {
            handleElementRename(newName, ref, field.getName());
        }
    }
    field.setName(newName);
    PsiManager manager = psiElement.getManager();
    for (GrReferenceExpression expression : handled.keySet()) {
        PsiElement oldResolved = handled.get(expression);
        if (oldResolved == null)
            continue;
        PsiElement resolved = expression.resolve();
        if (resolved == null)
            continue;
        if (manager.areElementsEquivalent(oldResolved, resolved))
            continue;
        if (oldResolved.equals(field) || isQualificationNeeded(manager, oldResolved, resolved)) {
            qualify(field, expression);
        }
    }
    if (listener != null) {
        listener.elementRenamed(psiElement);
    }
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) UsageInfo(com.intellij.usageView.UsageInfo) UnresolvableCollisionUsageInfo(com.intellij.refactoring.rename.UnresolvableCollisionUsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 45 with GrReferenceExpression

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

Aggregations

GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)177 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)92 PsiElement (com.intellij.psi.PsiElement)56 Nullable (org.jetbrains.annotations.Nullable)28 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)27 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)26 NotNull (org.jetbrains.annotations.NotNull)25 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)22 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)22 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)21 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)17 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)16 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)15 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)14 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)14 PsiType (com.intellij.psi.PsiType)13 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)12 Project (com.intellij.openapi.project.Project)11 ArrayList (java.util.ArrayList)11 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)11