Search in sources :

Example 1 with UsageInfo

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

the class PyTestCase method findUsage.

/**
   * Finds all usages of element. Works much like method in {@link com.intellij.testFramework.fixtures.CodeInsightTestFixture#findUsages(com.intellij.psi.PsiElement)},
   * but supports {@link com.intellij.find.findUsages.CustomUsageSearcher} and {@link com.intellij.psi.search.searches.ReferencesSearch} as well
   *
   * @param element what to find
   * @return usages
   */
@NotNull
protected Collection<PsiElement> findUsage(@NotNull final PsiElement element) {
    final Collection<PsiElement> result = new ArrayList<>();
    final CollectProcessor<Usage> usageCollector = new CollectProcessor<>();
    for (final CustomUsageSearcher searcher : CustomUsageSearcher.EP_NAME.getExtensions()) {
        searcher.processElementUsages(element, usageCollector, new FindUsagesOptions(myFixture.getProject()));
    }
    for (final Usage usage : usageCollector.getResults()) {
        if (usage instanceof PsiElementUsage) {
            result.add(((PsiElementUsage) usage).getElement());
        }
    }
    for (final PsiReference reference : ReferencesSearch.search(element).findAll()) {
        result.add(reference.getElement());
    }
    for (final UsageInfo info : myFixture.findUsages(element)) {
        result.add(info.getElement());
    }
    return result;
}
Also used : PsiElementUsage(com.intellij.usages.rules.PsiElementUsage) Usage(com.intellij.usages.Usage) CustomUsageSearcher(com.intellij.find.findUsages.CustomUsageSearcher) FindUsagesOptions(com.intellij.find.findUsages.FindUsagesOptions) UsageInfo(com.intellij.usageView.UsageInfo) CollectProcessor(com.intellij.util.CommonProcessors.CollectProcessor) PsiElementUsage(com.intellij.usages.rules.PsiElementUsage) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with UsageInfo

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

the class GrChangeSignatureProcessor method preprocessUsages.

@Override
protected boolean preprocessUsages(@NotNull Ref<UsageInfo[]> refUsages) {
    MultiMap<PsiElement, String> conflictDescriptions = new MultiMap<>();
    collectConflictsFromExtensions(refUsages, conflictDescriptions, myChangeInfo);
    final UsageInfo[] usagesIn = refUsages.get();
    RenameUtil.addConflictDescriptions(usagesIn, conflictDescriptions);
    Set<UsageInfo> usagesSet = new HashSet<>(Arrays.asList(usagesIn));
    RenameUtil.removeConflictUsages(usagesSet);
    if (!conflictDescriptions.isEmpty()) {
        if (ApplicationManager.getApplication().isUnitTestMode()) {
            throw new ConflictsInTestsException(conflictDescriptions.values());
        }
        ConflictsDialog dialog = prepareConflictsDialog(conflictDescriptions, usagesIn);
        if (!dialog.showAndGet()) {
            if (dialog.isShowConflicts())
                prepareSuccessful();
            return false;
        }
    }
    refUsages.set(usagesSet.toArray(new UsageInfo[usagesSet.size()]));
    prepareSuccessful();
    return true;
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) PsiElement(com.intellij.psi.PsiElement) UsageInfo(com.intellij.usageView.UsageInfo) HashSet(com.intellij.util.containers.HashSet)

Example 3 with UsageInfo

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

the class GrChageSignatureUsageSearcher method findSimpleUsagesWithoutParameters.

private PsiMethod[] findSimpleUsagesWithoutParameters(final PsiMethod method, final ArrayList<UsageInfo> result, boolean isToModifyArgs, boolean isToThrowExceptions, boolean isOriginal) {
    GlobalSearchScope projectScope = GlobalSearchScope.projectScope(method.getProject());
    PsiMethod[] overridingMethods = OverridingMethodsSearch.search(method).toArray(PsiMethod.EMPTY_ARRAY);
    for (PsiMethod overridingMethod : overridingMethods) {
        if (GroovyLanguage.INSTANCE.equals(overridingMethod.getLanguage())) {
            result.add(new OverriderUsageInfo(overridingMethod, method, isOriginal, isToModifyArgs, isToThrowExceptions));
        }
    }
    boolean needToChangeCalls = !myChangeInfo.isGenerateDelegate() && (myChangeInfo.isNameChanged() || myChangeInfo.isParameterSetOrOrderChanged() || myChangeInfo.isExceptionSetOrOrderChanged() || myChangeInfo.isVisibilityChanged());
    if (needToChangeCalls) {
        PsiReference[] refs = MethodReferencesSearch.search(method, projectScope, true).toArray(PsiReference.EMPTY_ARRAY);
        for (PsiReference ref : refs) {
            PsiElement element = ref.getElement();
            if (!GroovyLanguage.INSTANCE.equals(element.getLanguage()))
                continue;
            boolean isToCatchExceptions = isToThrowExceptions && needToCatchExceptions(RefactoringUtil.getEnclosingMethod(element));
            if (PsiUtil.isMethodUsage(element)) {
                result.add(new GrMethodCallUsageInfo(element, isToModifyArgs, isToCatchExceptions, method));
            } else if (element instanceof GrDocTagValueToken) {
                result.add(new UsageInfo(ref.getElement()));
            } else if (element instanceof GrMethod && ((GrMethod) element).isConstructor()) {
                DefaultConstructorImplicitUsageInfo implicitUsageInfo = new DefaultConstructorImplicitUsageInfo((GrMethod) element, ((GrMethod) element).getContainingClass(), method);
                result.add(implicitUsageInfo);
            } else if (element instanceof PsiClass) {
                LOG.assertTrue(method.isConstructor());
                final PsiClass psiClass = (PsiClass) element;
                if (psiClass instanceof GrAnonymousClassDefinition) {
                    result.add(new GrMethodCallUsageInfo(element, isToModifyArgs, isToCatchExceptions, method));
                    continue;
                }
                /*if (!(myChangeInfo instanceof JavaChangeInfoImpl)) continue; todo propagate methods
          if (shouldPropagateToNonPhysicalMethod(method, result, psiClass,
                                                 ((JavaChangeInfoImpl)myChangeInfo).propagateParametersMethods)) {
            continue;
          }
          if (shouldPropagateToNonPhysicalMethod(method, result, psiClass,
                                                 ((JavaChangeInfoImpl)myChangeInfo).propagateExceptionsMethods)) {
            continue;
          }*/
                result.add(new NoConstructorClassUsageInfo(psiClass));
            } else if (ref instanceof PsiCallReference) {
                result.add(new CallReferenceUsageInfo((PsiCallReference) ref));
            } else {
                result.add(new MoveRenameUsageInfo(element, ref, method));
            }
        }
    } else if (myChangeInfo.isParameterTypesChanged()) {
        PsiReference[] refs = MethodReferencesSearch.search(method, projectScope, true).toArray(PsiReference.EMPTY_ARRAY);
        for (PsiReference reference : refs) {
            final PsiElement element = reference.getElement();
            if (element instanceof GrDocTagValueToken) {
                result.add(new UsageInfo(reference));
            }
        }
    }
    // Conflicts
    if (method instanceof GrMethod) {
        detectLocalsCollisionsInMethod((GrMethod) method, result, isOriginal);
    }
    for (final PsiMethod overridingMethod : overridingMethods) {
        if (overridingMethod instanceof GrMethod) {
            detectLocalsCollisionsInMethod((GrMethod) overridingMethod, result, isOriginal);
        }
    }
    return overridingMethods;
}
Also used : GrDocTagValueToken(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocTagValueToken) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) NoConstructorClassUsageInfo(com.intellij.refactoring.util.usageInfo.NoConstructorClassUsageInfo) DefaultConstructorImplicitUsageInfo(com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) UnresolvableCollisionUsageInfo(com.intellij.refactoring.rename.UnresolvableCollisionUsageInfo) NoConstructorClassUsageInfo(com.intellij.refactoring.util.usageInfo.NoConstructorClassUsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) DefaultConstructorImplicitUsageInfo(com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo)

Example 4 with UsageInfo

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

the class GrChageSignatureUsageSearcher method addParameterUsages.

private static void addParameterUsages(PsiParameter parameter, ArrayList<UsageInfo> results, ParameterInfo info) {
    for (PsiReference psiReference : ReferencesSearch.search(parameter)) {
        PsiElement parmRef = psiReference.getElement();
        UsageInfo usageInfo = new ChangeSignatureParameterUsageInfo(parmRef, parameter.getName(), info.getName());
        results.add(usageInfo);
    }
    if (info.getName() != parameter.getName()) {
    }
}
Also used : UsageInfo(com.intellij.usageView.UsageInfo) UnresolvableCollisionUsageInfo(com.intellij.refactoring.rename.UnresolvableCollisionUsageInfo) NoConstructorClassUsageInfo(com.intellij.refactoring.util.usageInfo.NoConstructorClassUsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) DefaultConstructorImplicitUsageInfo(com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo)

Example 5 with UsageInfo

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

the class PyMoveModuleMembersProcessor method performRefactoring.

@Override
protected void performRefactoring(@NotNull final UsageInfo[] usages) {
    final MultiMap<PsiElement, UsageInfo> usagesByElement = MultiMap.create();
    for (UsageInfo usage : usages) {
        usagesByElement.putValue(((MyUsageInfo) usage).myMovedElement, usage);
    }
    CommandProcessor.getInstance().executeCommand(myElements[0].getProject(), new Runnable() {

        public void run() {
            ApplicationManager.getApplication().runWriteAction(new Runnable() {

                public void run() {
                    final PyFile destination = PyUtil.getOrCreateFile(myDestination, myProject);
                    CommonRefactoringUtil.checkReadOnlyStatus(myProject, destination);
                    for (final PsiNamedElement e : myElements) {
                        // TODO: Check for resulting circular imports
                        CommonRefactoringUtil.checkReadOnlyStatus(myProject, e);
                        assert e instanceof PyClass || e instanceof PyFunction || e instanceof PyTargetExpression;
                        final String name = e.getName();
                        if (name == null) {
                            continue;
                        }
                        if (e instanceof PyClass && destination.findTopLevelClass(name) != null) {
                            throw new IncorrectOperationException(PyBundle.message("refactoring.move.error.destination.file.contains.class.$0", name));
                        }
                        if (e instanceof PyFunction && destination.findTopLevelFunction(name) != null) {
                            throw new IncorrectOperationException(PyBundle.message("refactoring.move.error.destination.file.contains.function.$0", name));
                        }
                        if (e instanceof PyTargetExpression && destination.findTopLevelAttribute(name) != null) {
                            throw new IncorrectOperationException(PyBundle.message("refactoring.move.error.destination.file.contains.global.variable.$0", name));
                        }
                        final Collection<UsageInfo> usageInfos = usagesByElement.get(e);
                        final boolean usedFromOutside = ContainerUtil.exists(usageInfos, new Condition<UsageInfo>() {

                            @Override
                            public boolean value(UsageInfo usageInfo) {
                                final PsiElement element = usageInfo.getElement();
                                return element != null && !PsiTreeUtil.isAncestor(e, element, false);
                            }
                        });
                        if (usedFromOutside) {
                            PyMoveRefactoringUtil.checkValidImportableFile(e, destination.getVirtualFile());
                        }
                        new PyMoveSymbolProcessor(e, destination, usageInfos, myElements).moveElement();
                    }
                }
            });
        }
    }, REFACTORING_NAME, null);
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PsiElement(com.intellij.psi.PsiElement) UsageInfo(com.intellij.usageView.UsageInfo)

Aggregations

UsageInfo (com.intellij.usageView.UsageInfo)285 PsiElement (com.intellij.psi.PsiElement)70 NotNull (org.jetbrains.annotations.NotNull)57 ArrayList (java.util.ArrayList)42 MoveRenameUsageInfo (com.intellij.refactoring.util.MoveRenameUsageInfo)38 PsiFile (com.intellij.psi.PsiFile)34 IncorrectOperationException (com.intellij.util.IncorrectOperationException)34 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 PsiReference (com.intellij.psi.PsiReference)13 TextRange (com.intellij.openapi.util.TextRange)12 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)11 PsiClass (com.intellij.psi.PsiClass)11 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)11