Search in sources :

Example 1 with FindUsagesManager

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);
}
Also used : FindManagerImpl(com.intellij.find.impl.FindManagerImpl) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) FindUsagesHandler(com.intellij.find.findUsages.FindUsagesHandler) ReadWriteAccessDetector(com.intellij.codeInsight.highlighting.ReadWriteAccessDetector) TextRange(com.intellij.openapi.util.TextRange) FindUsagesManager(com.intellij.find.findUsages.FindUsagesManager) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with FindUsagesManager

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()]);
}
Also used : Project(com.intellij.openapi.project.Project) ConfigurableUsageTarget(com.intellij.usages.ConfigurableUsageTarget) ArrayList(java.util.ArrayList) FindUsagesManager(com.intellij.find.findUsages.FindUsagesManager) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with FindUsagesManager

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());
}
Also used : FindUsagesHandler(com.intellij.find.findUsages.FindUsagesHandler) PsiNamedElement(com.intellij.psi.PsiNamedElement) PsiElement2UsageTargetAdapter(com.intellij.find.findUsages.PsiElement2UsageTargetAdapter) Document(com.intellij.openapi.editor.Document) FindManagerImpl(com.intellij.find.impl.FindManagerImpl) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) FindUsagesManager(com.intellij.find.findUsages.FindUsagesManager) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 4 with FindUsagesManager

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();
}
Also used : FindManagerImpl(com.intellij.find.impl.FindManagerImpl) FindUsagesHandler(com.intellij.find.findUsages.FindUsagesHandler) FindUsagesOptions(com.intellij.find.findUsages.FindUsagesOptions) CommonProcessors(com.intellij.util.CommonProcessors) FindUsagesManager(com.intellij.find.findUsages.FindUsagesManager) UsageInfo(com.intellij.usageView.UsageInfo) PsiElement(com.intellij.psi.PsiElement)

Example 5 with FindUsagesManager

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);
}
Also used : MouseEvent(java.awt.event.MouseEvent) ArrayList(java.util.ArrayList) ItemPresentation(com.intellij.navigation.ItemPresentation) RelativePoint(com.intellij.ui.awt.RelativePoint) RelativePoint(com.intellij.ui.awt.RelativePoint) UsageView(com.intellij.usages.UsageView) Project(com.intellij.openapi.project.Project) ConfigurableUsageTarget(com.intellij.usages.ConfigurableUsageTarget) BaseListPopupStep(com.intellij.openapi.ui.popup.util.BaseListPopupStep) FindUsagesManager(com.intellij.find.findUsages.FindUsagesManager)

Aggregations

FindUsagesManager (com.intellij.find.findUsages.FindUsagesManager)9 FindUsagesHandler (com.intellij.find.findUsages.FindUsagesHandler)7 FindManagerImpl (com.intellij.find.impl.FindManagerImpl)7 NotNull (org.jetbrains.annotations.NotNull)4 PsiElement (com.intellij.psi.PsiElement)3 UsageInfo (com.intellij.usageView.UsageInfo)3 FindUsagesOptions (com.intellij.find.findUsages.FindUsagesOptions)2 Project (com.intellij.openapi.project.Project)2 PsiFile (com.intellij.psi.PsiFile)2 ConfigurableUsageTarget (com.intellij.usages.ConfigurableUsageTarget)2 CommonProcessors (com.intellij.util.CommonProcessors)2 DartServerFindUsagesHandler (com.jetbrains.lang.dart.ide.findUsages.DartServerFindUsagesHandler)2 ArrayList (java.util.ArrayList)2 ReadWriteAccessDetector (com.intellij.codeInsight.highlighting.ReadWriteAccessDetector)1 JavaMethodFindUsagesOptions (com.intellij.find.findUsages.JavaMethodFindUsagesOptions)1 PsiElement2UsageTargetAdapter (com.intellij.find.findUsages.PsiElement2UsageTargetAdapter)1 ItemPresentation (com.intellij.navigation.ItemPresentation)1 CommandProcessor (com.intellij.openapi.command.CommandProcessor)1 Document (com.intellij.openapi.editor.Document)1 BaseListPopupStep (com.intellij.openapi.ui.popup.util.BaseListPopupStep)1