use of com.intellij.find.findUsages.PsiElement2UsageTargetAdapter in project intellij-community by JetBrains.
the class MavenDomTestCase method search.
protected List<PsiElement> search(VirtualFile file) throws IOException {
final MapDataContext context = createDataContext(file);
UsageTarget[] targets = UsageTargetUtil.findUsageTargets(new DataProvider() {
@Override
public Object getData(@NonNls String dataId) {
return context.getData(dataId);
}
});
PsiElement target = ((PsiElement2UsageTargetAdapter) targets[0]).getElement();
List<PsiReference> result = new ArrayList<>(ReferencesSearch.search(target).findAll());
return ContainerUtil.map(result, psiReference -> psiReference.getElement());
}
use of com.intellij.find.findUsages.PsiElement2UsageTargetAdapter in project intellij-community by JetBrains.
the class JavaUsageTypeProviderTest method assertUsageType.
private void assertUsageType(UsageType expected, PsiClass target) {
UsageTarget[] targets = { new PsiElement2UsageTargetAdapter(target) };
PsiElement element = myFixture.getReferenceAtCaretPositionWithAssertion().getElement();
UsageType usageType = new JavaUsageTypeProvider().getUsageType(element, targets);
assertEquals(expected, usageType);
}
use of com.intellij.find.findUsages.PsiElement2UsageTargetAdapter in project intellij-community by JetBrains.
the class UsageViewTest method testUsageViewCanRerunAfterTargetWasInvalidatedAndRestored.
public void testUsageViewCanRerunAfterTargetWasInvalidatedAndRestored() throws Exception {
PsiFile psiFile = myFixture.addFileToProject("X.java", "public class X{" + " void foo() {\n" + " bar();\n" + " bar();\n" + " }" + " void bar() {}\n" + "}");
Usage usage = createUsage(psiFile, psiFile.getText().indexOf("bar();"));
PsiElement[] members = psiFile.getChildren()[psiFile.getChildren().length - 1].getChildren();
PsiNamedElement bar = (PsiNamedElement) members[members.length - 3];
assertEquals("bar", bar.getName());
UsageTarget target = new PsiElement2UsageTargetAdapter(bar);
FindUsagesManager usagesManager = ((FindManagerImpl) FindManager.getInstance(getProject())).getFindUsagesManager();
FindUsagesHandler handler = usagesManager.getNewFindUsagesHandler(bar, false);
UsageViewImpl usageView = (UsageViewImpl) usagesManager.doFindUsages(new PsiElement[] { bar }, PsiElement.EMPTY_ARRAY, handler, handler.getFindUsagesOptions(), false);
Disposer.register(myFixture.getTestRootDisposable(), usageView);
assertTrue(usageView.canPerformReRun());
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
Document document = documentManager.getDocument(psiFile);
String barDef = "void bar() {}\n";
String commentedBarDef = "//" + barDef;
WriteCommandAction.runWriteCommandAction(getProject(), () -> {
String text = document.getText();
document.replaceString(text.indexOf(barDef), text.indexOf(barDef) + barDef.length(), commentedBarDef);
});
documentManager.commitAllDocuments();
// target invalidated
assertFalse(usageView.canPerformReRun());
WriteCommandAction.runWriteCommandAction(getProject(), () -> {
String text = document.getText();
document.replaceString(text.indexOf(commentedBarDef), text.indexOf(commentedBarDef) + commentedBarDef.length(), barDef);
});
documentManager.commitAllDocuments();
assertTrue(usageView.canPerformReRun());
usageView.doReRun();
Set<Usage> usages = usageView.getUsages();
assertEquals(2, usages.size());
}
use of com.intellij.find.findUsages.PsiElement2UsageTargetAdapter in project intellij-plugins by JetBrains.
the class DartRenameDialog method fillTargetsAndUsageToEditIdMap.
private void fillTargetsAndUsageToEditIdMap(@NotNull final List<UsageTarget> usageTargets, @NotNull final Map<Usage, String> usageToEditIdMap) {
final SourceChange change = myRefactoring.getChange();
assert change != null;
final DartAnalysisServerService service = DartAnalysisServerService.getInstance(myProject);
final PsiManager psiManager = PsiManager.getInstance(myProject);
for (SourceFileEdit fileEdit : change.getEdits()) {
final VirtualFile file = AssistUtils.findVirtualFile(fileEdit);
final PsiFile psiFile = file == null ? null : psiManager.findFile(file);
if (psiFile == null)
continue;
for (SourceEdit sourceEdit : fileEdit.getEdits()) {
final int offset = service.getConvertedOffset(file, sourceEdit.getOffset());
final int length = service.getConvertedOffset(file, sourceEdit.getOffset() + sourceEdit.getLength()) - offset;
final TextRange range = TextRange.create(offset, offset + length);
final boolean potentialUsage = myRefactoring.getPotentialEdits().contains(sourceEdit.getId());
final PsiElement usageElement = DartServerFindUsagesHandler.getUsagePsiElement(psiFile, range);
if (usageElement != null) {
if (DartComponentType.typeOf(usageElement) != null) {
usageTargets.add(new PsiElement2UsageTargetAdapter(usageElement));
} else {
final UsageInfo usageInfo = DartServerFindUsagesHandler.getUsageInfo(usageElement, range, potentialUsage);
if (usageInfo != null) {
usageToEditIdMap.put(new UsageInfo2UsageAdapter(usageInfo), sourceEdit.getId());
}
}
}
}
}
}
use of com.intellij.find.findUsages.PsiElement2UsageTargetAdapter 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);
}
Aggregations