Search in sources :

Example 1 with UsageView

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

the class RemoveUsageAction method getUsages.

@NotNull
private static Usage[] getUsages(AnActionEvent context) {
    ApplicationManager.getApplication().assertIsDispatchThread();
    UsageView usageView = context.getData(UsageView.USAGE_VIEW_KEY);
    if (usageView == null)
        return Usage.EMPTY_ARRAY;
    Usage[] usages = context.getData(UsageView.USAGES_KEY);
    return usages == null ? Usage.EMPTY_ARRAY : usages;
}
Also used : UsageView(com.intellij.usages.UsageView) Usage(com.intellij.usages.Usage) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with UsageView

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

the class PsiElementListNavigator method navigateOrCreatePopup.

/**
   * listUpdaterTask should be started after alarm is initialized so one-item popup won't blink
   */
@Nullable
public static JBPopup navigateOrCreatePopup(@NotNull final NavigatablePsiElement[] targets, final String title, final String findUsagesTitle, final ListCellRenderer listRenderer, @Nullable final ListBackgroundUpdaterTask listUpdaterTask, @NotNull final Consumer<Object[]> consumer) {
    if (targets.length == 0)
        return null;
    if (targets.length == 1 && (listUpdaterTask == null || listUpdaterTask.isFinished())) {
        consumer.consume(targets);
        return null;
    }
    final CollectionListModel<NavigatablePsiElement> model = new CollectionListModel<>(targets);
    final JBList list = new JBList(model);
    HintUpdateSupply.installSimpleHintUpdateSupply(list);
    list.setTransferHandler(new TransferHandler() {

        @Nullable
        @Override
        protected Transferable createTransferable(JComponent c) {
            final Object[] selectedValues = list.getSelectedValues();
            final PsiElement[] copy = new PsiElement[selectedValues.length];
            for (int i = 0; i < selectedValues.length; i++) {
                copy[i] = (PsiElement) selectedValues[i];
            }
            return new PsiCopyPasteManager.MyTransferable(copy);
        }

        @Override
        public int getSourceActions(JComponent c) {
            return COPY;
        }
    });
    list.setCellRenderer(listRenderer);
    list.setFont(EditorUtil.getEditorFont());
    final PopupChooserBuilder builder = new PopupChooserBuilder(list);
    if (listRenderer instanceof PsiElementListCellRenderer) {
        ((PsiElementListCellRenderer) listRenderer).installSpeedSearch(builder);
    }
    PopupChooserBuilder popupChooserBuilder = builder.setTitle(title).setMovable(true).setResizable(true).setItemChoosenCallback(() -> {
        int[] ids = list.getSelectedIndices();
        if (ids == null || ids.length == 0)
            return;
        Object[] selectedElements = list.getSelectedValues();
        consumer.consume(selectedElements);
    }).setCancelCallback(() -> {
        HintUpdateSupply.hideHint(list);
        if (listUpdaterTask != null) {
            listUpdaterTask.cancelTask();
        }
        return true;
    });
    final Ref<UsageView> usageView = new Ref<>();
    if (findUsagesTitle != null) {
        popupChooserBuilder = popupChooserBuilder.setCouldPin(popup -> {
            final List<NavigatablePsiElement> items = model.getItems();
            usageView.set(FindUtil.showInUsageView(null, items.toArray(new PsiElement[items.size()]), findUsagesTitle, targets[0].getProject()));
            popup.cancel();
            return false;
        });
    }
    final JBPopup popup = popupChooserBuilder.createPopup();
    builder.getScrollPane().setBorder(null);
    builder.getScrollPane().setViewportBorder(null);
    if (listUpdaterTask != null) {
        listUpdaterTask.init((AbstractPopup) popup, list, usageView);
    }
    return popup;
}
Also used : EditorUtil(com.intellij.openapi.editor.ex.util.EditorUtil) Transferable(java.awt.datatransfer.Transferable) PsiCopyPasteManager(com.intellij.ide.PsiCopyPasteManager) PsiElement(com.intellij.psi.PsiElement) Logger(com.intellij.openapi.diagnostic.Logger) ProgressManager(com.intellij.openapi.progress.ProgressManager) JBList(com.intellij.ui.components.JBList) AbstractPopup(com.intellij.ui.popup.AbstractPopup) CollectionListModel(com.intellij.ui.CollectionListModel) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement) Editor(com.intellij.openapi.editor.Editor) JBPopup(com.intellij.openapi.ui.popup.JBPopup) MouseEvent(java.awt.event.MouseEvent) ListBackgroundUpdaterTask(com.intellij.codeInsight.navigation.ListBackgroundUpdaterTask) HintUpdateSupply(com.intellij.ui.popup.HintUpdateSupply) UsageView(com.intellij.usages.UsageView) Nullable(org.jetbrains.annotations.Nullable) PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder) List(java.util.List) FindUtil(com.intellij.find.FindUtil) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) RelativePoint(com.intellij.ui.awt.RelativePoint) PsiElementListCellRenderer(com.intellij.ide.util.PsiElementListCellRenderer) Consumer(com.intellij.util.Consumer) Alarm(com.intellij.util.Alarm) javax.swing(javax.swing) Transferable(java.awt.datatransfer.Transferable) PsiCopyPasteManager(com.intellij.ide.PsiCopyPasteManager) UsageView(com.intellij.usages.UsageView) Ref(com.intellij.openapi.util.Ref) JBList(com.intellij.ui.components.JBList) JBList(com.intellij.ui.components.JBList) List(java.util.List) CollectionListModel(com.intellij.ui.CollectionListModel) PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder) JBPopup(com.intellij.openapi.ui.popup.JBPopup) Nullable(org.jetbrains.annotations.Nullable) PsiElement(com.intellij.psi.PsiElement) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement) PsiElementListCellRenderer(com.intellij.ide.util.PsiElementListCellRenderer) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with UsageView

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

the class ShowImplementationsAction method showImplementations.

protected void showImplementations(@NotNull PsiElement[] impls, @NotNull final Project project, final String text, final Editor editor, final PsiFile file, final PsiElement element, boolean invokedFromEditor, boolean invokedByShortcut) {
    if (impls.length == 0)
        return;
    FeatureUsageTracker.getInstance().triggerFeatureUsed(CODEASSISTS_QUICKDEFINITION_FEATURE);
    if (LookupManager.getInstance(project).getActiveLookup() != null) {
        FeatureUsageTracker.getInstance().triggerFeatureUsed(CODEASSISTS_QUICKDEFINITION_LOOKUP_FEATURE);
    }
    int index = 0;
    if (invokedFromEditor && file != null && impls.length > 1) {
        final VirtualFile virtualFile = file.getVirtualFile();
        final PsiFile containingFile = impls[0].getContainingFile();
        if (virtualFile != null && containingFile != null && virtualFile.equals(containingFile.getVirtualFile())) {
            final PsiFile secondContainingFile = impls[1].getContainingFile();
            if (secondContainingFile != containingFile) {
                index = 1;
            }
        }
    }
    final Ref<UsageView> usageView = new Ref<>();
    final String title = CodeInsightBundle.message("implementation.view.title", text);
    JBPopup popup = SoftReference.dereference(myPopupRef);
    if (popup != null && popup.isVisible() && popup instanceof AbstractPopup) {
        final ImplementationViewComponent component = (ImplementationViewComponent) ((AbstractPopup) popup).getComponent();
        ((AbstractPopup) popup).setCaption(title);
        component.update(impls, index);
        updateInBackground(editor, element, component, title, (AbstractPopup) popup, usageView);
        if (invokedByShortcut) {
            ((AbstractPopup) popup).focusPreferredComponent();
        }
        return;
    }
    final ImplementationViewComponent component = new ImplementationViewComponent(impls, index);
    if (component.hasElementsToShow()) {
        final PopupUpdateProcessor updateProcessor = new PopupUpdateProcessor(project) {

            @Override
            public void updatePopup(Object lookupItemObject) {
                final PsiElement element = lookupItemObject instanceof PsiElement ? (PsiElement) lookupItemObject : DocumentationManager.getInstance(project).getElementFromLookup(editor, file);
                updateElementImplementations(element, editor, project, file);
            }
        };
        popup = JBPopupFactory.getInstance().createComponentPopupBuilder(component, component.getPreferredFocusableComponent()).setProject(project).addListener(updateProcessor).addUserData(updateProcessor).setDimensionServiceKey(project, DocumentationManager.JAVADOC_LOCATION_AND_SIZE, false).setResizable(true).setMovable(true).setRequestFocus(invokedFromEditor && LookupManager.getActiveLookup(editor) == null).setTitle(title).setCouldPin(popup1 -> {
            usageView.set(component.showInUsageView());
            popup1.cancel();
            myTaskRef = null;
            return false;
        }).setCancelCallback(() -> {
            ImplementationsUpdaterTask task = SoftReference.dereference(myTaskRef);
            if (task != null) {
                task.cancelTask();
            }
            return Boolean.TRUE;
        }).createPopup();
        updateInBackground(editor, element, component, title, (AbstractPopup) popup, usageView);
        PopupPositionManager.positionPopupInBestPosition(popup, editor, DataManager.getInstance().getDataContext());
        component.setHint(popup, title);
        myPopupRef = new WeakReference<>(popup);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DocumentationManager(com.intellij.codeInsight.documentation.DocumentationManager) java.util(java.util) VirtualFile(com.intellij.openapi.vfs.VirtualFile) NonNls(org.jetbrains.annotations.NonNls) Computable(com.intellij.openapi.util.Computable) ReadAction(com.intellij.openapi.application.ReadAction) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) CodeInsightBundle(com.intellij.codeInsight.CodeInsightBundle) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) PopupUpdateProcessor(com.intellij.ui.popup.PopupUpdateProcessor) ImplementationSearcher(com.intellij.codeInsight.navigation.ImplementationSearcher) FeatureUsageTracker(com.intellij.featureStatistics.FeatureUsageTracker) Project(com.intellij.openapi.project.Project) PopupPositionManager(com.intellij.ui.popup.PopupPositionManager) Logger(com.intellij.openapi.diagnostic.Logger) WeakReference(java.lang.ref.WeakReference) DataManager(com.intellij.ide.DataManager) BackgroundableProcessIndicator(com.intellij.openapi.progress.impl.BackgroundableProcessIndicator) TextEditor(com.intellij.openapi.fileEditor.TextEditor) ProgressManager(com.intellij.openapi.progress.ProgressManager) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement) SoftReference(com.intellij.reference.SoftReference) LookupManager(com.intellij.codeInsight.lookup.LookupManager) AbstractPopup(com.intellij.ui.popup.AbstractPopup) TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) FileEditor(com.intellij.openapi.fileEditor.FileEditor) JBPopup(com.intellij.openapi.ui.popup.JBPopup) BackgroundUpdaterTask(com.intellij.codeInsight.navigation.BackgroundUpdaterTask) TestOnly(org.jetbrains.annotations.TestOnly) Reference(java.lang.ref.Reference) com.intellij.openapi.actionSystem(com.intellij.openapi.actionSystem) UsageView(com.intellij.usages.UsageView) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) SymbolPresentationUtil(com.intellij.psi.presentation.java.SymbolPresentationUtil) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) JBPopupFactory(com.intellij.openapi.ui.popup.JBPopupFactory) TargetElementUtil(com.intellij.codeInsight.TargetElementUtil) ApplicationManager(com.intellij.openapi.application.ApplicationManager) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) ImplementationViewComponent(com.intellij.codeInsight.hint.ImplementationViewComponent) Ref(com.intellij.openapi.util.Ref) AbstractPopup(com.intellij.ui.popup.AbstractPopup) ImplementationViewComponent(com.intellij.codeInsight.hint.ImplementationViewComponent) UsageView(com.intellij.usages.UsageView) Ref(com.intellij.openapi.util.Ref) PopupUpdateProcessor(com.intellij.ui.popup.PopupUpdateProcessor) JBPopup(com.intellij.openapi.ui.popup.JBPopup) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement)

Example 4 with UsageView

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

the class GotoTargetHandler method show.

private void show(@NotNull final Project project, @NotNull Editor editor, @NotNull PsiFile file, @NotNull final GotoData gotoData) {
    final PsiElement[] targets = gotoData.targets;
    final List<AdditionalAction> additionalActions = gotoData.additionalActions;
    if (targets.length == 0 && additionalActions.isEmpty()) {
        HintManager.getInstance().showErrorHint(editor, getNotFoundMessage(project, editor, file));
        return;
    }
    boolean finished = gotoData.listUpdaterTask == null || gotoData.listUpdaterTask.isFinished();
    if (targets.length == 1 && additionalActions.isEmpty() && finished) {
        navigateToElement(targets[0]);
        return;
    }
    for (PsiElement eachTarget : targets) {
        gotoData.renderers.put(eachTarget, createRenderer(gotoData, eachTarget));
    }
    final String name = ((PsiNamedElement) gotoData.source).getName();
    final String title = getChooserTitle(gotoData.source, name, targets.length, finished);
    if (shouldSortTargets()) {
        Arrays.sort(targets, createComparator(gotoData.renderers, gotoData));
    }
    List<Object> allElements = new ArrayList<>(targets.length + additionalActions.size());
    Collections.addAll(allElements, targets);
    allElements.addAll(additionalActions);
    final JBList list = new JBList(new CollectionListModel<>(allElements));
    HintUpdateSupply.installSimpleHintUpdateSupply(list);
    list.setFont(EditorUtil.getEditorFont());
    list.setCellRenderer(new DefaultListCellRenderer() {

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            if (value == null)
                return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value instanceof AdditionalAction) {
                return myActionElementRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            }
            PsiElementListCellRenderer renderer = getRenderer(value, gotoData.renderers, gotoData);
            return renderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        }
    });
    final Runnable runnable = () -> {
        int[] ids = list.getSelectedIndices();
        if (ids == null || ids.length == 0)
            return;
        Object[] selectedElements = list.getSelectedValues();
        for (Object element : selectedElements) {
            if (element instanceof AdditionalAction) {
                ((AdditionalAction) element).execute();
            } else {
                Navigatable nav = element instanceof Navigatable ? (Navigatable) element : EditSourceUtil.getDescriptor((PsiElement) element);
                try {
                    if (nav != null && nav.canNavigate()) {
                        navigateToElement(nav);
                    }
                } catch (IndexNotReadyException e) {
                    DumbService.getInstance(project).showDumbModeNotification("Navigation is not available while indexing");
                }
            }
        }
    };
    final PopupChooserBuilder builder = new PopupChooserBuilder(list);
    builder.setFilteringEnabled(o -> {
        if (o instanceof AdditionalAction) {
            return ((AdditionalAction) o).getText();
        }
        return getRenderer(o, gotoData.renderers, gotoData).getElementText((PsiElement) o);
    });
    final Ref<UsageView> usageView = new Ref<>();
    final JBPopup popup = builder.setTitle(title).setItemChoosenCallback(runnable).setMovable(true).setCancelCallback(() -> {
        HintUpdateSupply.hideHint(list);
        final ListBackgroundUpdaterTask task = gotoData.listUpdaterTask;
        if (task != null) {
            task.cancelTask();
        }
        return true;
    }).setCouldPin(popup1 -> {
        usageView.set(FindUtil.showInUsageView(gotoData.source, gotoData.targets, getFindUsagesTitle(gotoData.source, name, gotoData.targets.length), gotoData.source.getProject()));
        popup1.cancel();
        return false;
    }).setAdText(getAdText(gotoData.source, targets.length)).createPopup();
    builder.getScrollPane().setBorder(null);
    builder.getScrollPane().setViewportBorder(null);
    if (gotoData.listUpdaterTask != null) {
        Alarm alarm = new Alarm(popup);
        alarm.addRequest(() -> popup.showInBestPositionFor(editor), 300);
        gotoData.listUpdaterTask.init((AbstractPopup) popup, list, usageView);
        ProgressManager.getInstance().run(gotoData.listUpdaterTask);
    } else {
        popup.showInBestPositionFor(editor);
    }
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) Navigatable(com.intellij.pom.Navigatable) UsageView(com.intellij.usages.UsageView) JBPopup(com.intellij.openapi.ui.popup.JBPopup) PsiElement(com.intellij.psi.PsiElement) Ref(com.intellij.openapi.util.Ref) Alarm(com.intellij.util.Alarm) IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException) JBList(com.intellij.ui.components.JBList) PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder) PsiElementListCellRenderer(com.intellij.ide.util.PsiElementListCellRenderer)

Example 5 with UsageView

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

the class BackgroundUpdaterTask method updateComponent.

public boolean updateComponent(final PsiElement element, @Nullable final Comparator comparator) {
    final UsageView view = myUsageView.get();
    if (view != null && !((UsageViewImpl) view).isDisposed()) {
        ApplicationManager.getApplication().runReadAction(() -> view.appendUsage(new UsageInfo2UsageAdapter(new UsageInfo(element))));
        return true;
    }
    if (myCanceled)
        return false;
    final JComponent content = myPopup.getContent();
    if (content == null || myPopup.isDisposed())
        return false;
    synchronized (lock) {
        if (myData.contains(element))
            return true;
        myData.add(element);
        if (comparator != null) {
            Collections.sort(myData, comparator);
        }
    }
    myAlarm.addRequest(() -> {
        myAlarm.cancelAllRequests();
        refreshModelImmediately();
    }, 200, ModalityState.stateForComponent(content));
    return true;
}
Also used : UsageView(com.intellij.usages.UsageView) UsageInfo2UsageAdapter(com.intellij.usages.UsageInfo2UsageAdapter) UsageViewImpl(com.intellij.usages.impl.UsageViewImpl) UsageInfo(com.intellij.usageView.UsageInfo)

Aggregations

UsageView (com.intellij.usages.UsageView)10 NotNull (org.jetbrains.annotations.NotNull)5 Editor (com.intellij.openapi.editor.Editor)3 Project (com.intellij.openapi.project.Project)3 JBPopup (com.intellij.openapi.ui.popup.JBPopup)3 Ref (com.intellij.openapi.util.Ref)3 PsiElement (com.intellij.psi.PsiElement)3 PsiElementListCellRenderer (com.intellij.ide.util.PsiElementListCellRenderer)2 Logger (com.intellij.openapi.diagnostic.Logger)2 ProgressManager (com.intellij.openapi.progress.ProgressManager)2 PopupChooserBuilder (com.intellij.openapi.ui.popup.PopupChooserBuilder)2 TextRange (com.intellij.openapi.util.TextRange)2 JBList (com.intellij.ui.components.JBList)2 AbstractPopup (com.intellij.ui.popup.AbstractPopup)2 Alarm (com.intellij.util.Alarm)2 Nullable (org.jetbrains.annotations.Nullable)2 CodeInsightBundle (com.intellij.codeInsight.CodeInsightBundle)1 TargetElementUtil (com.intellij.codeInsight.TargetElementUtil)1 DocumentationManager (com.intellij.codeInsight.documentation.DocumentationManager)1 ImplementationViewComponent (com.intellij.codeInsight.hint.ImplementationViewComponent)1