Search in sources :

Example 76 with UsageInfo

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

the class AutomaticRenamer method findUsagesForElement.

private boolean findUsagesForElement(PsiNamedElement element, List<UsageInfo> result, final boolean searchInStringsAndComments, final boolean searchInNonJavaFiles, List<UnresolvableCollisionUsageInfo> unresolvedUsages, Map<PsiElement, String> allRenames) {
    final String newName = getNewName(element);
    if (newName != null) {
        final LinkedHashMap<PsiNamedElement, String> renames = new LinkedHashMap<>();
        renames.putAll(myRenames);
        if (allRenames != null) {
            for (PsiElement psiElement : allRenames.keySet()) {
                if (psiElement instanceof PsiNamedElement) {
                    renames.put((PsiNamedElement) psiElement, allRenames.get(psiElement));
                }
            }
        }
        final UsageInfo[] usages = RenameUtil.findUsages(element, newName, searchInStringsAndComments, searchInNonJavaFiles, renames);
        for (final UsageInfo usage : usages) {
            if (usage instanceof UnresolvableCollisionUsageInfo) {
                if (unresolvedUsages != null) {
                    unresolvedUsages.add((UnresolvableCollisionUsageInfo) usage);
                }
                return false;
            }
        }
        ContainerUtil.addAll(result, usages);
    }
    return true;
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) UnresolvableCollisionUsageInfo(com.intellij.refactoring.rename.UnresolvableCollisionUsageInfo) PsiElement(com.intellij.psi.PsiElement) UnresolvableCollisionUsageInfo(com.intellij.refactoring.rename.UnresolvableCollisionUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo)

Example 77 with UsageInfo

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

the class SafeDeleteProcessor method preprocessUsages.

@Override
protected boolean preprocessUsages(@NotNull Ref<UsageInfo[]> refUsages) {
    UsageInfo[] usages = refUsages.get();
    ArrayList<String> conflicts = new ArrayList<>();
    for (PsiElement element : myElements) {
        for (SafeDeleteProcessorDelegate delegate : Extensions.getExtensions(SafeDeleteProcessorDelegate.EP_NAME)) {
            if (delegate.handlesElement(element)) {
                Collection<String> foundConflicts = delegate instanceof SafeDeleteProcessorDelegateBase ? ((SafeDeleteProcessorDelegateBase) delegate).findConflicts(element, myElements, usages) : delegate.findConflicts(element, myElements);
                if (foundConflicts != null) {
                    conflicts.addAll(foundConflicts);
                }
                break;
            }
        }
    }
    final HashMap<PsiElement, UsageHolder> elementsToUsageHolders = sortUsages(usages);
    final Collection<UsageHolder> usageHolders = elementsToUsageHolders.values();
    for (UsageHolder usageHolder : usageHolders) {
        if (usageHolder.hasUnsafeUsagesInCode()) {
            conflicts.add(usageHolder.getDescription());
        }
    }
    if (!conflicts.isEmpty()) {
        final RefactoringEventData conflictData = new RefactoringEventData();
        conflictData.putUserData(RefactoringEventData.CONFLICTS_KEY, conflicts);
        myProject.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).conflictsDetected("refactoring.safeDelete", conflictData);
        if (ApplicationManager.getApplication().isUnitTestMode()) {
            if (!ConflictsInTestsException.isTestIgnore())
                throw new ConflictsInTestsException(conflicts);
        } else {
            UnsafeUsagesDialog dialog = new UnsafeUsagesDialog(ArrayUtil.toStringArray(conflicts), myProject);
            if (!dialog.showAndGet()) {
                final int exitCode = dialog.getExitCode();
                // dialog is always dismissed;
                prepareSuccessful();
                if (exitCode == UnsafeUsagesDialog.VIEW_USAGES_EXIT_CODE) {
                    showUsages(Arrays.stream(usages).filter(usage -> usage instanceof SafeDeleteReferenceUsageInfo && !((SafeDeleteReferenceUsageInfo) usage).isSafeDelete()).toArray(UsageInfo[]::new), usages);
                }
                return false;
            } else {
                myPreviewNonCodeUsages = false;
            }
        }
    }
    UsageInfo[] preprocessedUsages = usages;
    for (SafeDeleteProcessorDelegate delegate : Extensions.getExtensions(SafeDeleteProcessorDelegate.EP_NAME)) {
        preprocessedUsages = delegate.preprocessUsages(myProject, preprocessedUsages);
        if (preprocessedUsages == null)
            return false;
    }
    final UsageInfo[] filteredUsages = UsageViewUtil.removeDuplicatedUsages(preprocessedUsages);
    // dialog is always dismissed
    prepareSuccessful();
    if (filteredUsages == null) {
        return false;
    }
    refUsages.set(filteredUsages);
    return true;
}
Also used : RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) SafeDeleteReferenceUsageInfo(com.intellij.refactoring.safeDelete.usageInfo.SafeDeleteReferenceUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) SafeDeleteReferenceUsageInfo(com.intellij.refactoring.safeDelete.usageInfo.SafeDeleteReferenceUsageInfo) SafeDeleteCustomUsageInfo(com.intellij.refactoring.safeDelete.usageInfo.SafeDeleteCustomUsageInfo) SafeDeleteReferenceSimpleDeleteUsageInfo(com.intellij.refactoring.safeDelete.usageInfo.SafeDeleteReferenceSimpleDeleteUsageInfo) SafeDeleteUsageInfo(com.intellij.refactoring.safeDelete.usageInfo.SafeDeleteUsageInfo)

Example 78 with UsageInfo

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

the class RenameUtil method removeConflictUsages.

@Nullable
public static List<UnresolvableCollisionUsageInfo> removeConflictUsages(Set<UsageInfo> usages) {
    final List<UnresolvableCollisionUsageInfo> result = new ArrayList<>();
    for (Iterator<UsageInfo> iterator = usages.iterator(); iterator.hasNext(); ) {
        UsageInfo usageInfo = iterator.next();
        if (usageInfo instanceof UnresolvableCollisionUsageInfo) {
            result.add((UnresolvableCollisionUsageInfo) usageInfo);
            iterator.remove();
        }
    }
    return result.isEmpty() ? null : result;
}
Also used : UsageInfo(com.intellij.usageView.UsageInfo) Nullable(org.jetbrains.annotations.Nullable)

Example 79 with UsageInfo

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

the class RenameUtil method findUsages.

@NotNull
public static UsageInfo[] findUsages(@NotNull final PsiElement element, final String newName, boolean searchInStringsAndComments, boolean searchForTextOccurrences, Map<? extends PsiElement, String> allRenames) {
    final List<UsageInfo> result = Collections.synchronizedList(new ArrayList<UsageInfo>());
    PsiManager manager = element.getManager();
    GlobalSearchScope projectScope = GlobalSearchScope.projectScope(manager.getProject());
    RenamePsiElementProcessor processor = RenamePsiElementProcessor.forElement(element);
    Collection<PsiReference> refs = processor.findReferences(element, searchInStringsAndComments);
    for (final PsiReference ref : refs) {
        if (ref == null) {
            LOG.error("null reference from processor " + processor);
            continue;
        }
        PsiElement referenceElement = ref.getElement();
        result.add(new MoveRenameUsageInfo(referenceElement, ref, ref.getRangeInElement().getStartOffset(), ref.getRangeInElement().getEndOffset(), element, ref.resolve() == null && !(ref instanceof PsiPolyVariantReference && ((PsiPolyVariantReference) ref).multiResolve(true).length > 0)));
    }
    processor.findCollisions(element, newName, allRenames, result);
    final PsiElement searchForInComments = processor.getElementToSearchInStringsAndComments(element);
    if (searchInStringsAndComments && searchForInComments != null) {
        String stringToSearch = ElementDescriptionUtil.getElementDescription(searchForInComments, NonCodeSearchDescriptionLocation.STRINGS_AND_COMMENTS);
        if (stringToSearch.length() > 0) {
            final String stringToReplace = getStringToReplace(element, newName, false, processor);
            UsageInfoFactory factory = new NonCodeUsageInfoFactory(searchForInComments, stringToReplace);
            TextOccurrencesUtil.addUsagesInStringsAndComments(searchForInComments, stringToSearch, result, factory);
        }
    }
    if (searchForTextOccurrences && searchForInComments != null) {
        String stringToSearch = ElementDescriptionUtil.getElementDescription(searchForInComments, NonCodeSearchDescriptionLocation.NON_JAVA);
        if (stringToSearch.length() > 0) {
            final String stringToReplace = getStringToReplace(element, newName, true, processor);
            addTextOccurrence(searchForInComments, result, projectScope, stringToSearch, stringToReplace);
        }
        final Pair<String, String> additionalStringToSearch = processor.getTextOccurrenceSearchStrings(searchForInComments, newName);
        if (additionalStringToSearch != null && additionalStringToSearch.first.length() > 0) {
            addTextOccurrence(searchForInComments, result, projectScope, additionalStringToSearch.first, additionalStringToSearch.second);
        }
    }
    return result.toArray(new UsageInfo[result.size()]);
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) UsageInfoFactory(com.intellij.usageView.UsageInfoFactory) UsageInfo(com.intellij.usageView.UsageInfo) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 80 with UsageInfo

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

the class SlicePanel method getSelectedUsageInfos.

@Nullable
private List<UsageInfo> getSelectedUsageInfos() {
    TreePath[] paths = myTree.getSelectionPaths();
    if (paths == null)
        return null;
    final ArrayList<UsageInfo> result = new ArrayList<>();
    for (TreePath path : paths) {
        SliceNode sliceNode = fromPath(path);
        if (sliceNode != null) {
            result.add(sliceNode.getValue().getUsageInfo());
        }
    }
    if (result.isEmpty())
        return null;
    return result;
}
Also used : TreePath(javax.swing.tree.TreePath) ArrayList(java.util.ArrayList) UsageInfo(com.intellij.usageView.UsageInfo) Nullable(org.jetbrains.annotations.Nullable)

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