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