Search in sources :

Example 1 with Usage

use of com.intellij.usages.Usage in project intellij-community by JetBrains.

the class PyTestCase method findUsage.

/**
   * Finds all usages of element. Works much like method in {@link com.intellij.testFramework.fixtures.CodeInsightTestFixture#findUsages(com.intellij.psi.PsiElement)},
   * but supports {@link com.intellij.find.findUsages.CustomUsageSearcher} and {@link com.intellij.psi.search.searches.ReferencesSearch} as well
   *
   * @param element what to find
   * @return usages
   */
@NotNull
protected Collection<PsiElement> findUsage(@NotNull final PsiElement element) {
    final Collection<PsiElement> result = new ArrayList<>();
    final CollectProcessor<Usage> usageCollector = new CollectProcessor<>();
    for (final CustomUsageSearcher searcher : CustomUsageSearcher.EP_NAME.getExtensions()) {
        searcher.processElementUsages(element, usageCollector, new FindUsagesOptions(myFixture.getProject()));
    }
    for (final Usage usage : usageCollector.getResults()) {
        if (usage instanceof PsiElementUsage) {
            result.add(((PsiElementUsage) usage).getElement());
        }
    }
    for (final PsiReference reference : ReferencesSearch.search(element).findAll()) {
        result.add(reference.getElement());
    }
    for (final UsageInfo info : myFixture.findUsages(element)) {
        result.add(info.getElement());
    }
    return result;
}
Also used : PsiElementUsage(com.intellij.usages.rules.PsiElementUsage) Usage(com.intellij.usages.Usage) CustomUsageSearcher(com.intellij.find.findUsages.CustomUsageSearcher) FindUsagesOptions(com.intellij.find.findUsages.FindUsagesOptions) UsageInfo(com.intellij.usageView.UsageInfo) CollectProcessor(com.intellij.util.CommonProcessors.CollectProcessor) PsiElementUsage(com.intellij.usages.rules.PsiElementUsage) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with Usage

use of com.intellij.usages.Usage in project intellij-community by JetBrains.

the class UsageListCellRenderer method customizeCellRenderer.

@Override
protected void customizeCellRenderer(@NotNull final JList list, final Object value, final int index, final boolean selected, final boolean hasFocus) {
    Usage usage = (Usage) value;
    UsagePresentation presentation = usage.getPresentation();
    setIcon(presentation.getIcon());
    VirtualFile virtualFile = getVirtualFile(usage);
    if (virtualFile != null) {
        append(virtualFile.getName() + ": ", SimpleTextAttributes.REGULAR_ATTRIBUTES);
        setIcon(virtualFile.getFileType().getIcon());
        PsiFile psiFile = PsiManager.getInstance(myProject).findFile(virtualFile);
        if (psiFile != null) {
            setIcon(psiFile.getIcon(0));
        }
    }
    TextChunk[] text = presentation.getText();
    for (TextChunk textChunk : text) {
        SimpleTextAttributes simples = textChunk.getSimpleAttributesIgnoreBackground();
        append(textChunk.getText(), simples);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Usage(com.intellij.usages.Usage) SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes) PsiFile(com.intellij.psi.PsiFile) TextChunk(com.intellij.usages.TextChunk) UsagePresentation(com.intellij.usages.UsagePresentation)

Example 3 with Usage

use of com.intellij.usages.Usage in project intellij-community by JetBrains.

the class FindPopupPanel method getSelectedUsages.

@Nullable
private Usage[] getSelectedUsages() {
    int[] rows = myResultsPreviewTable.getSelectedRows();
    List<Usage> usages = null;
    for (int row : rows) {
        Object valueAt = myResultsPreviewTable.getModel().getValueAt(row, 0);
        if (valueAt instanceof Usage) {
            if (usages == null)
                usages = new SmartList<>();
            Usage at = (Usage) valueAt;
            usages.add(at);
        }
    }
    return usages != null ? ContainerUtil.toArray(usages, Usage.EMPTY_ARRAY) : null;
}
Also used : Usage(com.intellij.usages.Usage) SmartList(com.intellij.util.SmartList) RelativePoint(com.intellij.ui.awt.RelativePoint) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with Usage

use of com.intellij.usages.Usage in project intellij-community by JetBrains.

the class FindPopupPanel method navigateToSelectedUsage.

private void navigateToSelectedUsage() {
    Usage[] usages = getSelectedUsages();
    if (usages != null) {
        applyTo(FindManager.getInstance(myProject).getFindInProjectModel(), false);
        myBalloon.cancel();
        usages[0].navigate(true);
        for (int i = 1; i < usages.length; ++i) usages[i].highlightInEditor();
    }
}
Also used : Usage(com.intellij.usages.Usage) RelativePoint(com.intellij.ui.awt.RelativePoint)

Example 5 with Usage

use of com.intellij.usages.Usage in project intellij-community by JetBrains.

the class FindPopupPanel method findSettingsChanged.

private void findSettingsChanged() {
    if (isShowing()) {
        ScrollingUtil.ensureSelectionExists(myResultsPreviewTable);
    }
    final ModalityState state = ModalityState.current();
    finishPreviousPreviewSearch();
    mySearchRescheduleOnCancellationsAlarm.cancelAllRequests();
    applyTo(myHelper.getModel(), false);
    myHelper.updateFindSettings();
    FindModel findInProjectModel = FindManager.getInstance(myProject).getFindInProjectModel();
    FindModel copy = new FindModel();
    copy.copyFrom(findInProjectModel);
    findInProjectModel.copyFrom(myHelper.getModel());
    //todo check if we really need to do it now
    ((FindManagerImpl) FindManager.getInstance(myProject)).changeGlobalSettings(myHelper.getModel());
    FindSettings findSettings = FindSettings.getInstance();
    myScopeUI.applyTo(findSettings, mySelectedScope);
    findSettings.setFileMask(myHelper.getModel().getFileFilter());
    ValidationInfo result = getValidationInfo(myHelper.getModel());
    final ProgressIndicatorBase progressIndicatorWhenSearchStarted = new ProgressIndicatorBase();
    myResultsPreviewSearchProgress = progressIndicatorWhenSearchStarted;
    final DefaultTableModel model = new DefaultTableModel() {

        @Override
        public boolean isCellEditable(int row, int column) {
            return false;
        }
    };
    model.addColumn("Usages");
    // Use previously shown usage files as hint for faster search and better usage preview performance if pattern length increased
    final LinkedHashSet<VirtualFile> filesToScanInitially = new LinkedHashSet<>();
    if (myHelper.myPreviousModel != null && myHelper.myPreviousModel.getStringToFind().length() < myHelper.getModel().getStringToFind().length()) {
        final DefaultTableModel previousModel = (DefaultTableModel) myResultsPreviewTable.getModel();
        for (int i = 0, len = previousModel.getRowCount(); i < len; ++i) {
            final UsageInfo2UsageAdapter usage = (UsageInfo2UsageAdapter) previousModel.getValueAt(i, 0);
            final VirtualFile file = usage.getFile();
            if (file != null)
                filesToScanInitially.add(file);
        }
    }
    myHelper.myPreviousModel = myHelper.getModel().clone();
    myCodePreviewComponent.setVisible(false);
    mySearchTextArea.setInfoText(null);
    myResultsPreviewTable.setModel(model);
    if (result != null) {
        myResultsPreviewTable.getEmptyText().setText(UIBundle.message("message.nothingToShow") + " (" + result.message + ")");
        return;
    }
    myResultsPreviewTable.getColumnModel().getColumn(0).setCellRenderer(new FindDialog.UsageTableCellRenderer(myCbFileFilter.isSelected(), false));
    myResultsPreviewTable.getEmptyText().setText("Searching...");
    final AtomicInteger resultsCount = new AtomicInteger();
    final AtomicInteger resultsFilesCount = new AtomicInteger();
    ProgressIndicatorUtils.scheduleWithWriteActionPriority(myResultsPreviewSearchProgress, new ReadTask() {

        @Override
        public void computeInReadAction(@NotNull ProgressIndicator indicator) {
            final UsageViewPresentation presentation = FindInProjectUtil.setupViewPresentation(findSettings.isShowResultsInSeparateView(), /*findModel*/
            myHelper.getModel().clone());
            final boolean showPanelIfOnlyOneUsage = !findSettings.isSkipResultsWithOneUsage();
            final FindUsagesProcessPresentation processPresentation = FindInProjectUtil.setupProcessPresentation(myProject, showPanelIfOnlyOneUsage, presentation);
            Ref<VirtualFile> lastUsageFileRef = new Ref<>();
            FindInProjectUtil.findUsages(myHelper.getModel().clone(), myProject, info -> {
                final Usage usage = UsageInfo2UsageAdapter.CONVERTER.fun(info);
                usage.getPresentation().getIcon();
                VirtualFile file = lastUsageFileRef.get();
                VirtualFile usageFile = info.getVirtualFile();
                if (file == null || !file.equals(usageFile)) {
                    resultsFilesCount.incrementAndGet();
                    lastUsageFileRef.set(usageFile);
                }
                ApplicationManager.getApplication().invokeLater(() -> {
                    model.addRow(new Object[] { usage });
                    myCodePreviewComponent.setVisible(true);
                    if (model.getRowCount() == 1 && myResultsPreviewTable.getModel() == model) {
                        myResultsPreviewTable.setRowSelectionInterval(0, 0);
                    }
                }, state);
                return resultsCount.incrementAndGet() < ShowUsagesAction.USAGES_PAGE_SIZE;
            }, processPresentation, filesToScanInitially);
            boolean succeeded = !progressIndicatorWhenSearchStarted.isCanceled();
            if (succeeded) {
                ApplicationManager.getApplication().invokeLater(() -> {
                    if (progressIndicatorWhenSearchStarted == myResultsPreviewSearchProgress && !myResultsPreviewSearchProgress.isCanceled()) {
                        int occurrences = resultsCount.get();
                        int filesWithOccurrences = resultsFilesCount.get();
                        if (occurrences == 0)
                            myResultsPreviewTable.getEmptyText().setText(UIBundle.message("message.nothingToShow"));
                        myCodePreviewComponent.setVisible(occurrences > 0);
                        StringBuilder info = new StringBuilder();
                        if (occurrences > 0) {
                            info.append(Math.min(ShowUsagesAction.USAGES_PAGE_SIZE, occurrences));
                            boolean foundAllUsages = occurrences < ShowUsagesAction.USAGES_PAGE_SIZE;
                            if (!foundAllUsages) {
                                info.append("+");
                            }
                            info.append(UIBundle.message("message.matches", occurrences));
                            info.append(" in ");
                            info.append(filesWithOccurrences);
                            if (!foundAllUsages) {
                                info.append("+");
                            }
                            info.append(UIBundle.message("message.files", filesWithOccurrences));
                        }
                        mySearchTextArea.setInfoText(info.toString());
                    }
                }, state);
            }
        }

        @Override
        public void onCanceled(@NotNull ProgressIndicator indicator) {
            if (isShowing() && progressIndicatorWhenSearchStarted == myResultsPreviewSearchProgress) {
                scheduleResultsUpdate();
            }
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) UsageViewPresentation(com.intellij.usages.UsageViewPresentation) com.intellij.openapi.util(com.intellij.openapi.util) JBInsets(com.intellij.util.ui.JBInsets) UIUtil(com.intellij.util.ui.UIUtil) AllIcons(com.intellij.icons.AllIcons) ReadTask(com.intellij.openapi.progress.util.ReadTask) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ModalityState(com.intellij.openapi.application.ModalityState) JBLabel(com.intellij.ui.components.JBLabel) SmartList(com.intellij.util.SmartList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JBUI(com.intellij.util.ui.JBUI) Messages(com.intellij.openapi.ui.Messages) Logger(com.intellij.openapi.diagnostic.Logger) ListSelectionEvent(javax.swing.event.ListSelectionEvent) JBFont(com.intellij.util.ui.JBFont) ValidationInfo(com.intellij.openapi.ui.ValidationInfo) CustomComponentAction(com.intellij.openapi.actionSystem.ex.CustomComponentAction) ComponentPopupBuilder(com.intellij.openapi.ui.popup.ComponentPopupBuilder) PatternSyntaxException(java.util.regex.PatternSyntaxException) DefaultTableModel(javax.swing.table.DefaultTableModel) ActionButton(com.intellij.openapi.actionSystem.impl.ActionButton) WindowManager(com.intellij.openapi.wm.WindowManager) UsageViewPresentation(com.intellij.usages.UsageViewPresentation) KeymapUtil(com.intellij.openapi.keymap.KeymapUtil) com.intellij.ui(com.intellij.ui) JBScrollPane(com.intellij.ui.components.JBScrollPane) HelpManager(com.intellij.openapi.help.HelpManager) ListPopup(com.intellij.openapi.ui.popup.ListPopup) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) List(java.util.List) IdeFocusManager(com.intellij.openapi.wm.IdeFocusManager) JBPanel(com.intellij.ui.components.JBPanel) java.awt.event(java.awt.event) ApplicationManager(com.intellij.openapi.application.ApplicationManager) Registry(com.intellij.openapi.util.registry.Registry) Pattern(java.util.regex.Pattern) NotNull(org.jetbrains.annotations.NotNull) RelativePoint(com.intellij.ui.awt.RelativePoint) ProgressIndicatorBase(com.intellij.openapi.progress.util.ProgressIndicatorBase) java.util(java.util) ArrayUtil(com.intellij.util.ArrayUtil) ActionToolbarImpl(com.intellij.openapi.actionSystem.impl.ActionToolbarImpl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UsageInfo(com.intellij.usageView.UsageInfo) ProgressIndicatorUtils(com.intellij.openapi.progress.util.ProgressIndicatorUtils) ContainerUtil(com.intellij.util.containers.ContainerUtil) UsageInfo2UsageAdapter(com.intellij.usages.UsageInfo2UsageAdapter) ActionButtonWithText(com.intellij.openapi.actionSystem.impl.ActionButtonWithText) CommonBundle(com.intellij.CommonBundle) IdeFrameImpl(com.intellij.openapi.wm.impl.IdeFrameImpl) FindUsagesProcessPresentation(com.intellij.usages.FindUsagesProcessPresentation) UsagePreviewPanel(com.intellij.usages.impl.UsagePreviewPanel) Project(com.intellij.openapi.project.Project) DocumentEvent(javax.swing.event.DocumentEvent) VfsUtilCore(com.intellij.openapi.vfs.VfsUtilCore) Usage(com.intellij.usages.Usage) StringUtil(com.intellij.openapi.util.text.StringUtil) MigLayout(net.miginfocom.swing.MigLayout) com.intellij.find(com.intellij.find) UISettings(com.intellij.ide.ui.UISettings) OnePixelDivider(com.intellij.openapi.ui.OnePixelDivider) Disposable(com.intellij.openapi.Disposable) JBPopup(com.intellij.openapi.ui.popup.JBPopup) java.awt(java.awt) com.intellij.openapi.actionSystem(com.intellij.openapi.actionSystem) JBTable(com.intellij.ui.table.JBTable) DumbAwareAction(com.intellij.openapi.project.DumbAwareAction) JBPopupFactory(com.intellij.openapi.ui.popup.JBPopupFactory) ShowUsagesAction(com.intellij.find.actions.ShowUsagesAction) MnemonicHelper(com.intellij.openapi.MnemonicHelper) ListSelectionListener(javax.swing.event.ListSelectionListener) Alarm(com.intellij.util.Alarm) javax.swing(javax.swing) DefaultTableModel(javax.swing.table.DefaultTableModel) ProgressIndicatorBase(com.intellij.openapi.progress.util.ProgressIndicatorBase) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) Usage(com.intellij.usages.Usage) FindUsagesProcessPresentation(com.intellij.usages.FindUsagesProcessPresentation) RelativePoint(com.intellij.ui.awt.RelativePoint) ValidationInfo(com.intellij.openapi.ui.ValidationInfo) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UsageInfo2UsageAdapter(com.intellij.usages.UsageInfo2UsageAdapter) ModalityState(com.intellij.openapi.application.ModalityState) ReadTask(com.intellij.openapi.progress.util.ReadTask)

Aggregations

Usage (com.intellij.usages.Usage)17 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 NotNull (org.jetbrains.annotations.NotNull)5 Project (com.intellij.openapi.project.Project)4 PsiFile (com.intellij.psi.PsiFile)4 UsageInfo2UsageAdapter (com.intellij.usages.UsageInfo2UsageAdapter)4 RelativePoint (com.intellij.ui.awt.RelativePoint)3 SmartList (com.intellij.util.SmartList)3 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)2 PsiElement (com.intellij.psi.PsiElement)2 UsageInfo (com.intellij.usageView.UsageInfo)2 Nullable (org.jetbrains.annotations.Nullable)2 CommonBundle (com.intellij.CommonBundle)1 com.intellij.find (com.intellij.find)1 ShowUsagesAction (com.intellij.find.actions.ShowUsagesAction)1 CustomUsageSearcher (com.intellij.find.findUsages.CustomUsageSearcher)1 FindUsagesOptions (com.intellij.find.findUsages.FindUsagesOptions)1 AllIcons (com.intellij.icons.AllIcons)1 FavoritesManager (com.intellij.ide.favoritesTreeView.FavoritesManager)1 AbstractUrl (com.intellij.ide.projectView.impl.AbstractUrl)1