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;
}
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;
}
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;
}
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()]);
}
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;
}
Aggregations