Search in sources :

Example 91 with UsageInfo

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

the class BaseRefactoringProcessor method createPresentation.

@NotNull
private static UsageViewPresentation createPresentation(@NotNull UsageViewDescriptor descriptor, @NotNull Usage[] usages) {
    UsageViewPresentation presentation = new UsageViewPresentation();
    presentation.setTabText(RefactoringBundle.message("usageView.tabText"));
    presentation.setTargetsNodeText(descriptor.getProcessedElementsHeader());
    presentation.setShowReadOnlyStatusAsRed(true);
    presentation.setShowCancelButton(true);
    presentation.setUsagesString(RefactoringBundle.message("usageView.usagesText"));
    int codeUsageCount = 0;
    int nonCodeUsageCount = 0;
    int dynamicUsagesCount = 0;
    Set<PsiFile> codeFiles = new HashSet<>();
    Set<PsiFile> nonCodeFiles = new HashSet<>();
    Set<PsiFile> dynamicUsagesCodeFiles = new HashSet<>();
    for (Usage usage : usages) {
        if (usage instanceof PsiElementUsage) {
            final PsiElementUsage elementUsage = (PsiElementUsage) usage;
            final PsiElement element = elementUsage.getElement();
            if (element == null)
                continue;
            final PsiFile containingFile = element.getContainingFile();
            if (elementUsage.isNonCodeUsage()) {
                nonCodeUsageCount++;
                nonCodeFiles.add(containingFile);
            } else {
                codeUsageCount++;
                codeFiles.add(containingFile);
            }
            if (usage instanceof UsageInfo2UsageAdapter) {
                final UsageInfo usageInfo = ((UsageInfo2UsageAdapter) usage).getUsageInfo();
                if (usageInfo instanceof MoveRenameUsageInfo && usageInfo.isDynamicUsage()) {
                    dynamicUsagesCount++;
                    dynamicUsagesCodeFiles.add(containingFile);
                }
            }
        }
    }
    codeFiles.remove(null);
    nonCodeFiles.remove(null);
    dynamicUsagesCodeFiles.remove(null);
    String codeReferencesText = descriptor.getCodeReferencesText(codeUsageCount, codeFiles.size());
    presentation.setCodeUsagesString(codeReferencesText);
    final String commentReferencesText = descriptor.getCommentReferencesText(nonCodeUsageCount, nonCodeFiles.size());
    if (commentReferencesText != null) {
        presentation.setNonCodeUsagesString(commentReferencesText);
    }
    presentation.setDynamicUsagesString("Dynamic " + StringUtil.decapitalize(descriptor.getCodeReferencesText(dynamicUsagesCount, dynamicUsagesCodeFiles.size())));
    String generatedCodeString;
    if (codeReferencesText.contains("in code")) {
        generatedCodeString = StringUtil.replace(codeReferencesText, "in code", "in generated code");
    } else {
        generatedCodeString = codeReferencesText + " in generated code";
    }
    presentation.setUsagesInGeneratedCodeString(generatedCodeString);
    return presentation;
}
Also used : PsiElementUsage(com.intellij.usages.rules.PsiElementUsage) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) HashSet(com.intellij.util.containers.HashSet) THashSet(gnu.trove.THashSet) PsiElementUsage(com.intellij.usages.rules.PsiElementUsage) NotNull(org.jetbrains.annotations.NotNull)

Example 92 with UsageInfo

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

the class CommonMoveUtil method retargetUsages.

public static NonCodeUsageInfo[] retargetUsages(final UsageInfo[] usages, final Map<PsiElement, PsiElement> oldToNewElementsMapping) throws IncorrectOperationException {
    Arrays.sort(usages, (o1, o2) -> {
        final VirtualFile file1 = o1.getVirtualFile();
        final VirtualFile file2 = o2.getVirtualFile();
        if (Comparing.equal(file1, file2)) {
            final ProperTextRange rangeInElement1 = o1.getRangeInElement();
            final ProperTextRange rangeInElement2 = o2.getRangeInElement();
            if (rangeInElement1 != null && rangeInElement2 != null) {
                return rangeInElement2.getStartOffset() - rangeInElement1.getStartOffset();
            }
            return 0;
        }
        if (file1 == null)
            return -1;
        if (file2 == null)
            return 1;
        return Comparing.compare(file1.getPath(), file2.getPath());
    });
    List<NonCodeUsageInfo> nonCodeUsages = new ArrayList<>();
    for (UsageInfo usage : usages) {
        if (usage instanceof NonCodeUsageInfo) {
            nonCodeUsages.add((NonCodeUsageInfo) usage);
        } else if (usage instanceof MoveRenameUsageInfo) {
            final MoveRenameUsageInfo moveRenameUsage = (MoveRenameUsageInfo) usage;
            final PsiElement oldElement = moveRenameUsage.getReferencedElement();
            final PsiElement newElement = oldToNewElementsMapping.get(oldElement);
            LOG.assertTrue(newElement != null, oldElement);
            final PsiReference reference = moveRenameUsage.getReference();
            if (reference != null) {
                try {
                    reference.bindToElement(newElement);
                } catch (IncorrectOperationException e) {
                //
                }
            }
        }
    }
    return nonCodeUsages.toArray(new NonCodeUsageInfo[nonCodeUsages.size()]);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProperTextRange(com.intellij.openapi.util.ProperTextRange) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) PsiReference(com.intellij.psi.PsiReference) IncorrectOperationException(com.intellij.util.IncorrectOperationException) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) PsiElement(com.intellij.psi.PsiElement)

Example 93 with UsageInfo

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

the class JavaFindUsagesHelper method addMethodsUsages.

private static boolean addMethodsUsages(@NotNull final PsiClass aClass, @NotNull final PsiManager manager, @NotNull final JavaClassFindUsagesOptions options, @NotNull final Processor<UsageInfo> processor) {
    if (options.isIncludeInherited) {
        final PsiMethod[] methods = ReadAction.compute(aClass::getAllMethods);
        for (int i = 0; i < methods.length; i++) {
            final PsiMethod method = methods[i];
            // filter overridden methods
            final int finalI = i;
            final PsiClass methodClass = ReadAction.compute(() -> {
                MethodSignature methodSignature = method.getSignature(PsiSubstitutor.EMPTY);
                for (int j = 0; j < finalI; j++) {
                    if (methodSignature.equals(methods[j].getSignature(PsiSubstitutor.EMPTY)))
                        return null;
                }
                return method.getContainingClass();
            });
            if (methodClass == null)
                continue;
            boolean equivalent = ReadAction.compute(() -> manager.areElementsEquivalent(methodClass, aClass));
            if (equivalent) {
                if (!addElementUsages(method, options, processor))
                    return false;
            } else {
                MethodReferencesSearch.SearchParameters parameters = new MethodReferencesSearch.SearchParameters(method, options.searchScope, true, options.fastTrack);
                boolean success = MethodReferencesSearch.search(parameters).forEach(new PsiReferenceProcessorAdapter(reference -> {
                    addResultFromReference(reference, methodClass, manager, aClass, options, processor);
                    return true;
                }));
                if (!success)
                    return false;
            }
        }
    } else {
        PsiMethod[] methods = ReadAction.compute(aClass::getMethods);
        for (PsiMethod method : methods) {
            if (!addElementUsages(method, options, processor))
                return false;
        }
    }
    return true;
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) PsiMetaOwner(com.intellij.psi.meta.PsiMetaOwner) java.util(java.util) PsiElementProcessorAdapter(com.intellij.psi.search.PsiElementProcessorAdapter) PsiMetaData(com.intellij.psi.meta.PsiMetaData) UsageInfo(com.intellij.usageView.UsageInfo) NullableComputable(com.intellij.openapi.util.NullableComputable) SearchScope(com.intellij.psi.search.SearchScope) ContainerUtil(com.intellij.util.containers.ContainerUtil) FindBundle(com.intellij.find.FindBundle) ReadAction(com.intellij.openapi.application.ReadAction) ReadActionProcessor(com.intellij.openapi.application.ReadActionProcessor) Comparing(com.intellij.openapi.util.Comparing) PomService(com.intellij.pom.references.PomService) PsiUtil(com.intellij.psi.util.PsiUtil) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Logger(com.intellij.openapi.diagnostic.Logger) PomTarget(com.intellij.pom.PomTarget) Extensions(com.intellij.openapi.extensions.Extensions) ProgressManager(com.intellij.openapi.progress.ProgressManager) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) TextRange(com.intellij.openapi.util.TextRange) AliasingPsiTargetMapper(com.intellij.psi.targets.AliasingPsiTargetMapper) com.intellij.psi.search.searches(com.intellij.psi.search.searches) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) AliasingPsiTarget(com.intellij.psi.targets.AliasingPsiTarget) MethodSignature(com.intellij.psi.util.MethodSignature) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) ThrowSearchUtil(com.intellij.psi.impl.search.ThrowSearchUtil) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) PsiReferenceProcessorAdapter(com.intellij.psi.search.PsiReferenceProcessorAdapter) MethodSignature(com.intellij.psi.util.MethodSignature) PsiReferenceProcessorAdapter(com.intellij.psi.search.PsiReferenceProcessorAdapter)

Example 94 with UsageInfo

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

the class JavaSafeDeleteProcessor method showUsages.

@Override
public UsageView showUsages(UsageInfo[] usages, UsageViewPresentation presentation, UsageViewManager manager, PsiElement[] elements) {
    final List<PsiElement> overridingMethods = new ArrayList<>();
    final List<UsageInfo> others = new ArrayList<>();
    for (UsageInfo usage : usages) {
        if (usage instanceof SafeDeleteOverridingMethodUsageInfo) {
            overridingMethods.add(((SafeDeleteOverridingMethodUsageInfo) usage).getOverridingMethod());
        } else {
            others.add(usage);
        }
    }
    UsageTarget[] targets = new UsageTarget[elements.length + overridingMethods.size()];
    for (int i = 0; i < targets.length; i++) {
        if (i < elements.length) {
            targets[i] = new PsiElement2UsageTargetAdapter(elements[i]);
        } else {
            targets[i] = new PsiElement2UsageTargetAdapter(overridingMethods.get(i - elements.length));
        }
    }
    return manager.showUsages(targets, UsageInfoToUsageConverter.convert(elements, others.toArray(new UsageInfo[others.size()])), presentation);
}
Also used : PsiElement2UsageTargetAdapter(com.intellij.find.findUsages.PsiElement2UsageTargetAdapter) UsageInfo(com.intellij.usageView.UsageInfo)

Example 95 with UsageInfo

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

the class JavaSafeDeleteProcessor method findConflicts.

@Override
public Collection<String> findConflicts(PsiElement element, PsiElement[] elements, UsageInfo[] usages) {
    String methodRefFound = null;
    if (element instanceof PsiMethod || element instanceof PsiParameter) {
        PsiMethod method;
        if (element instanceof PsiMethod) {
            method = (PsiMethod) element;
        } else {
            PsiElement declarationScope = ((PsiParameter) element).getDeclarationScope();
            method = declarationScope instanceof PsiMethod ? (PsiMethod) declarationScope : null;
        }
        if (method != null) {
            for (UsageInfo usage : usages) {
                final PsiElement refElement = usage.getElement();
                if (refElement instanceof PsiMethodReferenceExpression && method.equals(((PsiMethodReferenceExpression) refElement).resolve())) {
                    methodRefFound = RefactoringBundle.message("expand.method.reference.warning");
                    break;
                }
            }
        }
    }
    if (methodRefFound != null) {
        Collection<String> result = new ArrayList<>();
        result.add(methodRefFound);
        final Collection<String> conflicts = super.findConflicts(element, elements, usages);
        if (conflicts != null) {
            result.addAll(conflicts);
        }
        return result;
    }
    return super.findConflicts(element, elements, usages);
}
Also used : UsageInfo(com.intellij.usageView.UsageInfo)

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