use of com.intellij.find.findUsages.PsiElement2UsageTargetAdapter in project intellij-community by JetBrains.
the class BaseRefactoringProcessor method previewRefactoring.
protected void previewRefactoring(@NotNull UsageInfo[] usages) {
if (ApplicationManager.getApplication().isUnitTestMode()) {
if (!PREVIEW_IN_TESTS)
throw new RuntimeException("Unexpected preview in tests: " + StringUtil.join(usages, info -> info.toString(), ", "));
ensureElementsWritable(usages, createUsageViewDescriptor(usages));
execute(usages);
return;
}
final UsageViewDescriptor viewDescriptor = createUsageViewDescriptor(usages);
final PsiElement[] elements = viewDescriptor.getElements();
final PsiElement2UsageTargetAdapter[] targets = PsiElement2UsageTargetAdapter.convert(elements);
Factory<UsageSearcher> factory = new Factory<UsageSearcher>() {
@Override
public UsageSearcher create() {
return new UsageInfoSearcherAdapter() {
@Override
public void generate(@NotNull final Processor<Usage> processor) {
ApplicationManager.getApplication().runReadAction(new Runnable() {
@Override
public void run() {
for (int i = 0; i < elements.length; i++) {
elements[i] = targets[i].getElement();
}
refreshElements(elements);
}
});
processUsages(processor, myProject);
}
@NotNull
@Override
protected UsageInfo[] findUsages() {
return BaseRefactoringProcessor.this.findUsages();
}
};
}
};
showUsageView(viewDescriptor, factory, usages);
}
use of com.intellij.find.findUsages.PsiElement2UsageTargetAdapter in project intellij-community by JetBrains.
the class FindUtil method showInUsageView.
@Nullable
public static UsageView showInUsageView(@Nullable PsiElement sourceElement, @NotNull PsiElement[] targets, @NotNull String title, @NotNull final Project project) {
if (targets.length == 0)
return null;
final UsageViewPresentation presentation = new UsageViewPresentation();
presentation.setCodeUsagesString(title);
presentation.setTabName(title);
presentation.setTabText(title);
UsageTarget[] usageTargets = sourceElement == null ? UsageTarget.EMPTY_ARRAY : new UsageTarget[] { new PsiElement2UsageTargetAdapter(sourceElement) };
PsiElement[] primary = sourceElement == null ? PsiElement.EMPTY_ARRAY : new PsiElement[] { sourceElement };
UsageView view = UsageViewManager.getInstance(project).showUsages(usageTargets, Usage.EMPTY_ARRAY, presentation);
SmartPointerManager smartPointerManager = SmartPointerManager.getInstance(project);
List<SmartPsiElementPointer> pointers = ContainerUtil.map(targets, smartPointerManager::createSmartPsiElementPointer);
// usage view will load document/AST so still referencing all these PSI elements might lead to out of memory
//noinspection UnusedAssignment
targets = PsiElement.EMPTY_ARRAY;
ProgressManager.getInstance().run(new Task.Backgroundable(project, "Updating Usage View ...") {
@Override
public void run(@NotNull ProgressIndicator indicator) {
for (final SmartPsiElementPointer pointer : pointers) {
if (((UsageViewImpl) view).isDisposed())
break;
ApplicationManager.getApplication().runReadAction(() -> {
final PsiElement target = pointer.getElement();
if (target != null) {
view.appendUsage(UsageInfoToUsageConverter.convert(primary, new UsageInfo(target)));
}
});
}
UIUtil.invokeLaterIfNeeded(((UsageViewImpl) view)::expandAll);
}
});
return view;
}
use of com.intellij.find.findUsages.PsiElement2UsageTargetAdapter in project intellij-community by JetBrains.
the class InheritanceToDelegationProcessor method showObjectUpcastedUsageView.
private void showObjectUpcastedUsageView(final ObjectUpcastedUsageInfo[] usages) {
UsageViewPresentation presentation = new UsageViewPresentation();
presentation.setTargetsNodeText(RefactoringBundle.message("replacing.inheritance.with.delegation"));
presentation.setCodeUsagesString(RefactoringBundle.message("instances.casted.to.java.lang.object"));
final String upcastedString = RefactoringBundle.message("instances.upcasted.to.object");
presentation.setUsagesString(upcastedString);
presentation.setTabText(upcastedString);
UsageViewManager manager = UsageViewManager.getInstance(myProject);
manager.showUsages(new UsageTarget[] { new PsiElement2UsageTargetAdapter(myClass) }, UsageInfoToUsageConverter.convert(new PsiElement[] { myClass }, usages), presentation);
WindowManager.getInstance().getStatusBar(myProject).setInfo(RefactoringBundle.message("instances.upcasted.to.java.lang.object.found"));
}
use of com.intellij.find.findUsages.PsiElement2UsageTargetAdapter in project intellij-community by JetBrains.
the class ResourceBundleStructureViewComponent method getData.
@Override
public Object getData(final String dataId) {
if (CommonDataKeys.VIRTUAL_FILE.is(dataId)) {
return new ResourceBundleAsVirtualFile(myResourceBundle);
} else if (PlatformDataKeys.FILE_EDITOR.is(dataId)) {
return getFileEditor();
} else if (ResourceBundle.ARRAY_DATA_KEY.is(dataId)) {
return new ResourceBundle[] { myResourceBundle };
} else if (IProperty.ARRAY_KEY.is(dataId)) {
final Collection<ResourceBundleEditorViewElement> selectedElements = ((ResourceBundleEditor) getFileEditor()).getSelectedElements();
if (selectedElements.isEmpty()) {
return null;
} else if (selectedElements.size() == 1) {
return ContainerUtil.getFirstItem(selectedElements).getProperties();
} else {
return ContainerUtil.toArray(ContainerUtil.flatten(ContainerUtil.mapNotNull(selectedElements, (NullableFunction<ResourceBundleEditorViewElement, List<IProperty>>) element -> {
final IProperty[] properties = element.getProperties();
return properties == null ? null : ContainerUtil.newArrayList(properties);
})), IProperty[]::new);
}
} else if (LangDataKeys.PSI_ELEMENT_ARRAY.is(dataId)) {
final List<PsiElement> elements = new ArrayList<>();
Collections.addAll(elements, getSelectedPsiFiles());
final IProperty[] properties = (IProperty[]) getData(IProperty.ARRAY_KEY.getName());
if (properties != null) {
for (IProperty property : properties) {
final PsiElement element = property.getPsiElement();
if (element.isValid()) {
elements.add(element);
}
}
}
return elements.toArray(new PsiElement[elements.size()]);
} else if (PlatformDataKeys.DELETE_ELEMENT_PROVIDER.is(dataId)) {
if (getSelectedPsiFiles().length != 0) {
return new ResourceBundleDeleteProvider();
}
final IProperty[] properties = IProperty.ARRAY_KEY.getData(this);
if (properties != null && properties.length != 0) {
return new PropertiesDeleteProvider(((ResourceBundleEditor) getFileEditor()).getPropertiesInsertDeleteManager(), properties);
}
} else if (UsageView.USAGE_TARGETS_KEY.is(dataId)) {
final PsiElement[] chosenElements = (PsiElement[]) getData(LangDataKeys.PSI_ELEMENT_ARRAY.getName());
if (chosenElements != null) {
final UsageTarget[] usageTargets = new UsageTarget[chosenElements.length];
for (int i = 0; i < chosenElements.length; i++) {
usageTargets[i] = new PsiElement2UsageTargetAdapter(chosenElements[i]);
}
return usageTargets;
}
} else if (PlatformDataKeys.COPY_PROVIDER.is(dataId)) {
return new CopyProvider() {
@Override
public void performCopy(@NotNull final DataContext dataContext) {
final PsiElement[] selectedPsiElements = (PsiElement[]) getData(LangDataKeys.PSI_ELEMENT_ARRAY.getName());
if (selectedPsiElements != null) {
final List<String> names = new ArrayList<>(selectedPsiElements.length);
for (final PsiElement element : selectedPsiElements) {
if (element instanceof PsiNamedElement) {
names.add(((PsiNamedElement) element).getName());
}
}
CopyPasteManager.getInstance().setContents(new StringSelection(StringUtil.join(names, "\n")));
}
}
@Override
public boolean isCopyEnabled(@NotNull final DataContext dataContext) {
return true;
}
@Override
public boolean isCopyVisible(@NotNull final DataContext dataContext) {
return true;
}
};
}
return super.getData(dataId);
}
Aggregations