use of com.intellij.find.findUsages.FindUsagesManager in project intellij-community by JetBrains.
the class IdentifierHighlighterPass method getUsages.
@NotNull
private static Couple<Collection<TextRange>> getUsages(@NotNull PsiElement target, PsiElement psiElement, boolean withDeclarations, boolean detectAccess) {
List<TextRange> readRanges = new ArrayList<>();
List<TextRange> writeRanges = new ArrayList<>();
final ReadWriteAccessDetector detector = detectAccess ? ReadWriteAccessDetector.findDetector(target) : null;
final FindUsagesManager findUsagesManager = ((FindManagerImpl) FindManager.getInstance(target.getProject())).getFindUsagesManager();
final FindUsagesHandler findUsagesHandler = findUsagesManager.getFindUsagesHandler(target, true);
final LocalSearchScope scope = new LocalSearchScope(psiElement);
Collection<PsiReference> refs = findUsagesHandler != null ? findUsagesHandler.findReferencesToHighlight(target, scope) : ReferencesSearch.search(target, scope).findAll();
for (PsiReference psiReference : refs) {
if (psiReference == null) {
LOG.error("Null reference returned, findUsagesHandler=" + findUsagesHandler + "; target=" + target + " of " + target.getClass());
continue;
}
List<TextRange> destination;
if (detector == null || detector.getReferenceAccess(target, psiReference) == ReadWriteAccessDetector.Access.Read) {
destination = readRanges;
} else {
destination = writeRanges;
}
HighlightUsagesHandler.collectRangesToHighlight(psiReference, destination);
}
if (withDeclarations) {
final TextRange declRange = HighlightUsagesHandler.getNameIdentifierRange(psiElement.getContainingFile(), target);
if (declRange != null) {
if (detector != null && detector.isDeclarationWriteAccess(target)) {
writeRanges.add(declRange);
} else {
readRanges.add(declRange);
}
}
}
return Couple.<Collection<TextRange>>of(readRanges, writeRanges);
}
use of com.intellij.find.findUsages.FindUsagesManager in project intellij-community by JetBrains.
the class ShowRecentFindUsagesGroup method getChildren.
@Override
@NotNull
public AnAction[] getChildren(@Nullable final AnActionEvent e) {
if (e == null)
return EMPTY_ARRAY;
Project project = e.getData(CommonDataKeys.PROJECT);
if (project == null || DumbService.isDumb(project))
return EMPTY_ARRAY;
final FindUsagesManager findUsagesManager = ((FindManagerImpl) FindManager.getInstance(project)).getFindUsagesManager();
List<ConfigurableUsageTarget> history = new ArrayList<>(findUsagesManager.getHistory().getAll());
Collections.reverse(history);
String description = ActionManager.getInstance().getAction(UsageViewImpl.SHOW_RECENT_FIND_USAGES_ACTION_ID).getTemplatePresentation().getDescription();
List<AnAction> children = new ArrayList<>(history.size());
for (final ConfigurableUsageTarget usageTarget : history) {
if (!usageTarget.isValid()) {
continue;
}
String text = usageTarget.getLongDescriptiveName();
AnAction action = new AnAction(text, description, null) {
@Override
public void actionPerformed(final AnActionEvent e) {
findUsagesManager.rerunAndRecallFromHistory(usageTarget);
}
};
children.add(action);
}
return children.toArray(new AnAction[children.size()]);
}
use of com.intellij.find.findUsages.FindUsagesManager 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.FindUsagesManager in project smali by JesusFreke.
the class FindUsagesTest method findUsages.
private Collection<UsageInfo> findUsages(@NotNull PsiElement element) {
FindUsagesManager findUsagesManager = ((FindManagerImpl) FindManager.getInstance(getProject())).getFindUsagesManager();
FindUsagesHandler findUsagesHandler = findUsagesManager.getFindUsagesHandler(element, false);
Assert.assertNotNull(findUsagesHandler);
final FindUsagesOptions options = findUsagesHandler.getFindUsagesOptions();
final CommonProcessors.CollectProcessor<UsageInfo> processor = new CommonProcessors.CollectProcessor<UsageInfo>();
for (PsiElement primaryElement : findUsagesHandler.getPrimaryElements()) {
findUsagesHandler.processElementUsages(primaryElement, processor, options);
}
for (PsiElement secondaryElement : findUsagesHandler.getSecondaryElements()) {
findUsagesHandler.processElementUsages(secondaryElement, processor, options);
}
return processor.getResults();
}
use of com.intellij.find.findUsages.FindUsagesManager in project intellij-community by JetBrains.
the class ShowRecentFindUsagesAction method actionPerformed.
@Override
public void actionPerformed(AnActionEvent e) {
UsageView usageView = e.getData(UsageView.USAGE_VIEW_KEY);
Project project = e.getData(CommonDataKeys.PROJECT);
final FindUsagesManager findUsagesManager = ((FindManagerImpl) FindManager.getInstance(project)).getFindUsagesManager();
List<ConfigurableUsageTarget> history = new ArrayList<>(findUsagesManager.getHistory().getAll());
if (!history.isEmpty()) {
// skip most recent find usage, it's under your nose
history.remove(history.size() - 1);
Collections.reverse(history);
}
if (history.isEmpty()) {
// to fill the popup
history.add(null);
}
BaseListPopupStep<ConfigurableUsageTarget> step = new BaseListPopupStep<ConfigurableUsageTarget>(FindBundle.message("recent.find.usages.action.title"), history) {
@Override
public Icon getIconFor(final ConfigurableUsageTarget data) {
ItemPresentation presentation = data == null ? null : data.getPresentation();
return presentation == null ? null : presentation.getIcon(false);
}
@Override
@NotNull
public String getTextFor(final ConfigurableUsageTarget data) {
if (data == null) {
return FindBundle.message("recent.find.usages.action.nothing");
}
return data.getLongDescriptiveName();
}
@Override
public PopupStep onChosen(final ConfigurableUsageTarget selectedValue, final boolean finalChoice) {
return doFinalStep(() -> {
if (selectedValue != null) {
TransactionGuard.getInstance().submitTransactionAndWait(() -> findUsagesManager.rerunAndRecallFromHistory(selectedValue));
}
});
}
};
RelativePoint point;
if (e.getInputEvent() instanceof MouseEvent) {
point = new RelativePoint((MouseEvent) e.getInputEvent());
} else {
point = new RelativePoint(usageView.getComponent(), new Point(4, 4));
}
JBPopupFactory.getInstance().createListPopup(step).show(point);
}
Aggregations