Search in sources :

Example 6 with UsageInfo

use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.

the class GrAliasImportIntention method updateRefs.

private static void updateRefs(List<UsageInfo> usages, final String memberName, final GrImportStatement updatedImport) {
    if (updatedImport == null)
        return;
    final String name = ApplicationManager.getApplication().runReadAction(new Computable<String>() {

        @Nullable
        @Override
        public String compute() {
            return updatedImport.getImportedName();
        }
    });
    for (final UsageInfo usage : usages) {
        ApplicationManager.getApplication().runWriteAction(() -> {
            final PsiElement usageElement = usage.getElement();
            if (usageElement == null)
                return;
            if (usageElement.getParent() instanceof GrImportStatement)
                return;
            if (usageElement instanceof GrReferenceElement) {
                final GrReferenceElement ref = (GrReferenceElement) usageElement;
                final PsiElement qualifier = ref.getQualifier();
                if (qualifier == null) {
                    final String refName = ref.getReferenceName();
                    if (refName == null)
                        return;
                    if (memberName.equals(refName)) {
                        ref.handleElementRename(name);
                    } else if (refName.equals(GroovyPropertyUtils.getPropertyNameByAccessorName(memberName))) {
                        final String newPropName = GroovyPropertyUtils.getPropertyNameByAccessorName(name);
                        if (newPropName != null) {
                            ref.handleElementRename(newPropName);
                        } else {
                            ref.handleElementRename(name);
                        }
                    } else if (refName.equals(GroovyPropertyUtils.getGetterNameBoolean(memberName))) {
                        final String getterName = GroovyPropertyUtils.getGetterNameBoolean(name);
                        ref.handleElementRename(getterName);
                    } else if (refName.equals(GroovyPropertyUtils.getGetterNameNonBoolean(memberName))) {
                        final String getterName = GroovyPropertyUtils.getGetterNameNonBoolean(name);
                        ref.handleElementRename(getterName);
                    } else if (refName.equals(GroovyPropertyUtils.getSetterName(memberName))) {
                        final String getterName = GroovyPropertyUtils.getSetterName(name);
                        ref.handleElementRename(getterName);
                    }
                }
            }
        });
    }
}
Also used : GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) Nullable(org.jetbrains.annotations.Nullable) UsageInfo(com.intellij.usageView.UsageInfo) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)

Example 7 with UsageInfo

use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.

the class MoveGroovyClassHandler method preprocessUsages.

@Override
public void preprocessUsages(Collection<UsageInfo> results) {
    //remove all alias-imported usages from collection
    for (Iterator<UsageInfo> iterator = results.iterator(); iterator.hasNext(); ) {
        UsageInfo info = iterator.next();
        if (info == null) {
            LOG.debug("info==null");
            continue;
        }
        final PsiReference ref = info.getReference();
        if (ref == null)
            continue;
        final PsiElement element = ref.getElement();
        if (!(element instanceof GrReferenceElement))
            continue;
        final GroovyResolveResult resolveResult = ((GrReferenceElement) element).advancedResolve();
        final PsiElement context = resolveResult.getCurrentFileResolveContext();
        if (!(context instanceof GrImportStatement))
            continue;
        if (!((GrImportStatement) context).isAliasedImport())
            continue;
        iterator.remove();
    }
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) UsageInfo(com.intellij.usageView.UsageInfo) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)

Example 8 with UsageInfo

use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.

the class GrIntroduceClosureParameterProcessor method processInternalUsages.

private static void processInternalUsages(UsageInfo[] usages, GrIntroduceParameterSettings settings) {
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(settings.getProject());
    // Replacing expression occurrences
    for (UsageInfo usage : usages) {
        if (usage instanceof ChangedMethodCallInfo) {
            PsiElement element = usage.getElement();
            processChangedMethodCall(element, settings);
        } else if (usage instanceof InternalUsageInfo) {
            PsiElement element = usage.getElement();
            if (element == null)
                continue;
            GrExpression newExpr = factory.createExpressionFromText(settings.getName());
            if (element instanceof GrExpression) {
                ((GrExpression) element).replaceWithExpression(newExpr, true);
            } else {
                element.replace(newExpr);
            }
        }
    }
    final StringPartInfo info = settings.getStringPartInfo();
    if (info != null) {
        final GrExpression expr = info.replaceLiteralWithConcatenation(settings.getName());
        final Editor editor = PsiUtilBase.findEditor(expr);
        if (editor != null) {
            editor.getSelectionModel().removeSelection();
            editor.getCaretModel().moveToOffset(expr.getTextRange().getEndOffset());
        }
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) ChangedMethodCallInfo(com.intellij.refactoring.introduceParameter.ChangedMethodCallInfo) Editor(com.intellij.openapi.editor.Editor) StringPartInfo(org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo) ExternalUsageInfo(com.intellij.refactoring.introduceParameter.ExternalUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) InternalUsageInfo(com.intellij.refactoring.introduceParameter.InternalUsageInfo) InternalUsageInfo(com.intellij.refactoring.introduceParameter.InternalUsageInfo)

Example 9 with UsageInfo

use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.

the class GrIntroduceClosureParameterProcessor method preprocessUsages.

@Override
protected boolean preprocessUsages(@NotNull Ref<UsageInfo[]> refUsages) {
    UsageInfo[] usagesIn = refUsages.get();
    MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    if (!mySettings.generateDelegate()) {
        detectAccessibilityConflicts(usagesIn, conflicts);
    }
    final GrExpression expression = mySettings.getExpression();
    if (expression != null && toSearchFor instanceof PsiMember) {
        final AnySupers anySupers = new AnySupers();
        expression.accept(anySupers);
        if (anySupers.containsSupers()) {
            final PsiElement containingClass = PsiUtil.getFileOrClassContext(toReplaceIn);
            for (UsageInfo usageInfo : usagesIn) {
                if (!(usageInfo.getElement() instanceof PsiMethod) && !(usageInfo instanceof InternalUsageInfo)) {
                    if (!PsiTreeUtil.isAncestor(containingClass, usageInfo.getElement(), false)) {
                        conflicts.putValue(expression, RefactoringBundle.message("parameter.initializer.contains.0.but.not.all.calls.to.method.are.in.its.class", CommonRefactoringUtil.htmlEmphasize(PsiKeyword.SUPER)));
                        break;
                    }
                }
            }
        }
    }
    return showConflicts(conflicts, usagesIn);
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) ExternalUsageInfo(com.intellij.refactoring.introduceParameter.ExternalUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) InternalUsageInfo(com.intellij.refactoring.introduceParameter.InternalUsageInfo) AnySupers(org.jetbrains.plugins.groovy.refactoring.util.AnySupers) InternalUsageInfo(com.intellij.refactoring.introduceParameter.InternalUsageInfo)

Example 10 with UsageInfo

use of com.intellij.usageView.UsageInfo 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)

Aggregations

UsageInfo (com.intellij.usageView.UsageInfo)283 PsiElement (com.intellij.psi.PsiElement)70 NotNull (org.jetbrains.annotations.NotNull)57 ArrayList (java.util.ArrayList)41 MoveRenameUsageInfo (com.intellij.refactoring.util.MoveRenameUsageInfo)38 IncorrectOperationException (com.intellij.util.IncorrectOperationException)34 PsiFile (com.intellij.psi.PsiFile)33 MultiMap (com.intellij.util.containers.MultiMap)30 VirtualFile (com.intellij.openapi.vfs.VirtualFile)29 Nullable (org.jetbrains.annotations.Nullable)20 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)19 DefaultConstructorImplicitUsageInfo (com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo)17 NoConstructorClassUsageInfo (com.intellij.refactoring.util.usageInfo.NoConstructorClassUsageInfo)17 Project (com.intellij.openapi.project.Project)16 HashSet (com.intellij.util.containers.HashSet)15 TextRange (com.intellij.openapi.util.TextRange)12 PsiReference (com.intellij.psi.PsiReference)12 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)11 PsiClass (com.intellij.psi.PsiClass)11 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)11