private void showUsages(final UsageInfo[] usages) { UsageViewPresentation presentation = new UsageViewPresentation(); presentation.setTabText("Safe Delete Conflicts"); presentation.setTargetsNodeText( RefactoringBundle.message("attempting.to.delete.targets.node.text")); presentation.setShowReadOnlyStatusAsRed(true); presentation.setShowCancelButton(true); presentation.setCodeUsagesString(RefactoringBundle.message("references.found.in.code")); presentation.setUsagesInGeneratedCodeString( RefactoringBundle.message("references.found.in.generated.code")); presentation.setNonCodeUsagesString( RefactoringBundle.message("occurrences.found.in.comments.strings.and.non.java.files")); presentation.setUsagesString(RefactoringBundle.message("usageView.usagesText")); UsageViewManager manager = UsageViewManager.getInstance(myProject); final UsageView usageView = showUsages(usages, presentation, manager); usageView.addPerformOperationAction( new RerunSafeDelete(myProject, myElements, usageView), RefactoringBundle.message("retry.command"), null, RefactoringBundle.message("rerun.safe.delete")); usageView.addPerformOperationAction( () -> { UsageInfo[] preprocessedUsages = usages; for (SafeDeleteProcessorDelegate delegate : Extensions.getExtensions(SafeDeleteProcessorDelegate.EP_NAME)) { preprocessedUsages = delegate.preprocessUsages(myProject, preprocessedUsages); if (preprocessedUsages == null) return; } final UsageInfo[] filteredUsages = UsageViewUtil.removeDuplicatedUsages(preprocessedUsages); execute(filteredUsages); }, "Delete Anyway", RefactoringBundle.message("usageView.need.reRun"), RefactoringBundle.message("usageView.doAction")); }
@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; }