Search in sources :

Example 11 with UsageInfo

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

Example 12 with UsageInfo

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

the class ExtractClosureFromMethodProcessor method preprocessUsages.

@Override
protected boolean preprocessUsages(@NotNull Ref<UsageInfo[]> refUsages) {
    UsageInfo[] usagesIn = refUsages.get();
    MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    final GrStatement[] statements = myHelper.getStatements();
    for (GrStatement statement : statements) {
        GroovyIntroduceParameterUtil.detectAccessibilityConflicts(statement, usagesIn, conflicts, false, myProject);
    }
    for (UsageInfo info : usagesIn) {
        if (info instanceof OtherLanguageUsageInfo) {
            final String lang = CommonRefactoringUtil.htmlEmphasize(info.getElement().getLanguage().getDisplayName());
            conflicts.putValue(info.getElement(), GroovyRefactoringBundle.message("cannot.process.usage.in.language.{0}", lang));
        }
    }
    if (!myMethod.hasModifierProperty(PsiModifier.PRIVATE)) {
        final AnySupers anySupers = new AnySupers();
        for (GrStatement statement : statements) {
            statement.accept(anySupers);
        }
        if (anySupers.containsSupers()) {
            for (UsageInfo usageInfo : usagesIn) {
                if (!(usageInfo.getElement() instanceof PsiMethod) && !(usageInfo instanceof InternalUsageInfo)) {
                    if (!PsiTreeUtil.isAncestor(myMethod.getContainingClass(), usageInfo.getElement(), false)) {
                        conflicts.putValue(statements[0], RefactoringBundle.message("parameter.initializer.contains.0.but.not.all.calls.to.method.are.in.its.class", CommonRefactoringUtil.htmlEmphasize(PsiKeyword.SUPER)));
                        break;
                    }
                }
            }
        }
    }
    if (!conflicts.isEmpty() && ApplicationManager.getApplication().isUnitTestMode()) {
        throw new ConflictsInTestsException(conflicts.values());
    }
    if (!conflicts.isEmpty()) {
        final ConflictsDialog conflictsDialog = prepareConflictsDialog(conflicts, usagesIn);
        if (!conflictsDialog.showAndGet()) {
            if (conflictsDialog.isShowConflicts())
                prepareSuccessful();
            return false;
        }
    }
    prepareSuccessful();
    return true;
}
Also used : GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) MultiMap(com.intellij.util.containers.MultiMap) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) UsageInfo(com.intellij.usageView.UsageInfo) NoConstructorClassUsageInfo(com.intellij.refactoring.util.usageInfo.NoConstructorClassUsageInfo) DefaultConstructorImplicitUsageInfo(com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo) AnySupers(org.jetbrains.plugins.groovy.refactoring.util.AnySupers)

Example 13 with UsageInfo

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

the class GroovyInlineLocalProcessor method preprocessUsages.

@Override
protected boolean preprocessUsages(@NotNull Ref<UsageInfo[]> refUsages) {
    final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    final UsageInfo[] usages = refUsages.get();
    for (UsageInfo usage : usages) {
        collectConflicts(usage.getReference(), conflicts);
    }
    return showConflicts(conflicts, usages);
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) PsiElement(com.intellij.psi.PsiElement) UsageInfo(com.intellij.usageView.UsageInfo)

Example 14 with UsageInfo

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

the class GrAliasImportIntention method findUsages.

private static List<UsageInfo> findUsages(PsiMember member, GroovyFileBase file) {
    LocalSearchScope scope = new LocalSearchScope(file);
    final ArrayList<UsageInfo> infos = new ArrayList<>();
    final HashSet<Object> usedRefs = ContainerUtil.newHashSet();
    final Processor<PsiReference> consumer = reference -> {
        if (usedRefs.add(reference)) {
            infos.add(new UsageInfo(reference));
        }
        return true;
    };
    if (member instanceof PsiMethod) {
        MethodReferencesSearch.search((PsiMethod) member, scope, false).forEach(consumer);
    } else {
        ReferencesSearch.search(member, scope).forEach(consumer);
        if (member instanceof PsiField) {
            final PsiMethod getter = GroovyPropertyUtils.findGetterForField((PsiField) member);
            if (getter != null) {
                MethodReferencesSearch.search(getter, scope, false).forEach(consumer);
            }
            final PsiMethod setter = GroovyPropertyUtils.findSetterForField((PsiField) member);
            if (setter != null) {
                MethodReferencesSearch.search(setter, scope, false).forEach(consumer);
            }
        }
    }
    return infos;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) IntentionUtils(org.jetbrains.plugins.groovy.intentions.base.IntentionUtils) Document(com.intellij.openapi.editor.Document) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) Computable(com.intellij.openapi.util.Computable) UsageInfo(com.intellij.usageView.UsageInfo) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) ContainerUtil(com.intellij.util.containers.ContainerUtil) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PreferrableNameSuggestionProvider(com.intellij.refactoring.rename.PreferrableNameSuggestionProvider) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) MethodReferencesSearch(com.intellij.psi.search.searches.MethodReferencesSearch) TemplateManager(com.intellij.codeInsight.template.TemplateManager) Intention(org.jetbrains.plugins.groovy.intentions.base.Intention) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) Project(com.intellij.openapi.project.Project) MyLookupExpression(com.intellij.refactoring.rename.inplace.MyLookupExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) LinkedHashSet(java.util.LinkedHashSet) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) RangeMarker(com.intellij.openapi.editor.RangeMarker) Extensions(com.intellij.openapi.extensions.Extensions) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Template(com.intellij.codeInsight.template.Template) TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyPropertyUtils(org.jetbrains.plugins.groovy.lang.psi.util.GroovyPropertyUtils) UsageViewUtil(com.intellij.usageView.UsageViewUtil) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Processor(com.intellij.util.Processor) NameSuggestionProvider(com.intellij.refactoring.rename.NameSuggestionProvider) ApplicationManager(com.intellij.openapi.application.ApplicationManager) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) TemplateEditingAdapter(com.intellij.codeInsight.template.TemplateEditingAdapter) PostprocessReformattingAspect(com.intellij.psi.impl.source.PostprocessReformattingAspect) ArrayList(java.util.ArrayList) UsageInfo(com.intellij.usageView.UsageInfo)

Example 15 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)

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