Search in sources :

Example 6 with UsageView

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

the class UsageViewManagerImpl method addContent.

@NotNull
@Override
public Content addContent(String contentName, String tabName, String toolwindowTitle, boolean reusable, final JComponent component, boolean toOpenInNewTab, boolean isLockable) {
    Key<Boolean> contentKey = reusable ? REUSABLE_CONTENT_KEY : NOT_REUSABLE_CONTENT_KEY;
    Content contentToDelete = null;
    if (!toOpenInNewTab && reusable) {
        Content[] contents = myFindContentManager.getContents();
        for (Content content : contents) {
            if (!content.isPinned() && content.getUserData(contentKey) != null) {
                UsageView usageView = content.getUserData(NEW_USAGE_VIEW_KEY);
                if (usageView == null || !usageView.isSearchInProgress()) {
                    contentToDelete = content;
                }
            }
        }
    }
    Content content = ContentFactory.SERVICE.getInstance().createContent(component, contentName, isLockable);
    content.setTabName(tabName);
    content.setToolwindowTitle(toolwindowTitle);
    content.putUserData(contentKey, Boolean.TRUE);
    content.putUserData(ToolWindow.SHOW_CONTENT_ICON, Boolean.TRUE);
    myFindContentManager.addContent(content);
    if (contentToDelete != null) {
        myFindContentManager.removeContent(contentToDelete, true);
    }
    myFindContentManager.setSelectedContent(content);
    return content;
}
Also used : UsageView(com.intellij.usages.UsageView) NotNull(org.jetbrains.annotations.NotNull)

Example 7 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 8 with UsageView

use of com.intellij.usages.UsageView 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)

Example 9 with UsageView

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

the class PredefinedSearchScopeProviderImpl method getPredefinedScopes.

@NotNull
@Override
public List<SearchScope> getPredefinedScopes(@NotNull final Project project, @Nullable final DataContext dataContext, boolean suggestSearchInLibs, boolean prevSearchFiles, boolean currentSelection, boolean usageView, boolean showEmptyScopes) {
    Collection<SearchScope> result = ContainerUtil.newLinkedHashSet();
    result.add(GlobalSearchScope.everythingScope(project));
    result.add(GlobalSearchScope.projectScope(project));
    if (suggestSearchInLibs) {
        result.add(GlobalSearchScope.allScope(project));
    }
    if (ModuleUtil.isSupportedRootType(project, JavaSourceRootType.TEST_SOURCE)) {
        result.add(GlobalSearchScopesCore.projectProductionScope(project));
        result.add(GlobalSearchScopesCore.projectTestScope(project));
    }
    result.add(ScratchFileServiceImpl.buildScratchesSearchScope());
    final GlobalSearchScope openFilesScope = GlobalSearchScopes.openFilesScope(project);
    if (openFilesScope != GlobalSearchScope.EMPTY_SCOPE) {
        result.add(openFilesScope);
    } else if (showEmptyScopes) {
        result.add(new LocalSearchScope(PsiElement.EMPTY_ARRAY, IdeBundle.message("scope.open.files")));
    }
    final Editor selectedTextEditor = ApplicationManager.getApplication().isDispatchThread() ? FileEditorManager.getInstance(project).getSelectedTextEditor() : null;
    PsiFile psiFile = selectedTextEditor == null ? null : PsiDocumentManager.getInstance(project).getPsiFile(selectedTextEditor.getDocument());
    PsiFile currentFile = psiFile;
    if (dataContext != null) {
        PsiElement dataContextElement = CommonDataKeys.PSI_FILE.getData(dataContext);
        if (dataContextElement == null) {
            dataContextElement = CommonDataKeys.PSI_ELEMENT.getData(dataContext);
        }
        if (dataContextElement == null && psiFile != null) {
            dataContextElement = psiFile;
        }
        if (dataContextElement != null) {
            if (!PlatformUtils.isCidr()) {
                // TODO: have an API to disable module scopes.
                Module module = ModuleUtilCore.findModuleForPsiElement(dataContextElement);
                if (module == null) {
                    module = LangDataKeys.MODULE.getData(dataContext);
                }
                if (module != null && !(ModuleType.get(module) instanceof InternalModuleType)) {
                    result.add(module.getModuleScope());
                }
            }
            if (currentFile == null) {
                currentFile = dataContextElement.getContainingFile();
            }
        }
    }
    if (currentFile != null || showEmptyScopes) {
        PsiElement[] scope = currentFile != null ? new PsiElement[] { currentFile } : PsiElement.EMPTY_ARRAY;
        result.add(new LocalSearchScope(scope, IdeBundle.message("scope.current.file")));
    }
    if (currentSelection && selectedTextEditor != null && psiFile != null) {
        SelectionModel selectionModel = selectedTextEditor.getSelectionModel();
        if (selectionModel.hasSelection()) {
            int start = selectionModel.getSelectionStart();
            final PsiElement startElement = psiFile.findElementAt(start);
            if (startElement != null) {
                int end = selectionModel.getSelectionEnd();
                final PsiElement endElement = psiFile.findElementAt(end);
                if (endElement != null) {
                    final PsiElement parent = PsiTreeUtil.findCommonParent(startElement, endElement);
                    if (parent != null) {
                        final List<PsiElement> elements = new ArrayList<>();
                        final PsiElement[] children = parent.getChildren();
                        TextRange selection = new TextRange(start, end);
                        for (PsiElement child : children) {
                            if (!(child instanceof PsiWhiteSpace) && child.getContainingFile() != null && selection.contains(child.getTextOffset())) {
                                elements.add(child);
                            }
                        }
                        if (!elements.isEmpty()) {
                            SearchScope local = new LocalSearchScope(PsiUtilCore.toPsiElementArray(elements), IdeBundle.message("scope.selection"));
                            result.add(local);
                        }
                    }
                }
            }
        }
    }
    if (usageView) {
        addHierarchyScope(project, result);
        UsageView selectedUsageView = UsageViewManager.getInstance(project).getSelectedUsageView();
        if (selectedUsageView != null && !selectedUsageView.isSearchInProgress()) {
            final Set<Usage> usages = ContainerUtil.newTroveSet(selectedUsageView.getUsages());
            usages.removeAll(selectedUsageView.getExcludedUsages());
            if (prevSearchFiles) {
                final Set<VirtualFile> files = collectFiles(usages, true);
                if (!files.isEmpty()) {
                    GlobalSearchScope prev = new GlobalSearchScope(project) {

                        private Set<VirtualFile> myFiles;

                        @NotNull
                        @Override
                        public String getDisplayName() {
                            return IdeBundle.message("scope.files.in.previous.search.result");
                        }

                        @Override
                        public synchronized boolean contains(@NotNull VirtualFile file) {
                            if (myFiles == null) {
                                myFiles = collectFiles(usages, false);
                            }
                            return myFiles.contains(file);
                        }

                        @Override
                        public int compare(@NotNull VirtualFile file1, @NotNull VirtualFile file2) {
                            return 0;
                        }

                        @Override
                        public boolean isSearchInModuleContent(@NotNull Module aModule) {
                            return true;
                        }

                        @Override
                        public boolean isSearchInLibraries() {
                            return true;
                        }
                    };
                    result.add(prev);
                }
            } else {
                final List<PsiElement> results = new ArrayList<>(usages.size());
                for (Usage usage : usages) {
                    if (usage instanceof PsiElementUsage) {
                        final PsiElement element = ((PsiElementUsage) usage).getElement();
                        if (element != null && element.isValid() && element.getContainingFile() != null) {
                            results.add(element);
                        }
                    }
                }
                if (!results.isEmpty()) {
                    result.add(new LocalSearchScope(PsiUtilCore.toPsiElementArray(results), IdeBundle.message("scope.previous.search.results")));
                }
            }
        }
    }
    final FavoritesManager favoritesManager = FavoritesManager.getInstance(project);
    if (favoritesManager != null) {
        for (final String favorite : favoritesManager.getAvailableFavoritesListNames()) {
            final Collection<TreeItem<Pair<AbstractUrl, String>>> rootUrls = favoritesManager.getFavoritesListRootUrls(favorite);
            // ignore unused root
            if (rootUrls.isEmpty())
                continue;
            result.add(new GlobalSearchScope(project) {

                @NotNull
                @Override
                public String getDisplayName() {
                    return "Favorite \'" + favorite + "\'";
                }

                @Override
                public boolean contains(@NotNull final VirtualFile file) {
                    return ReadAction.compute(() -> favoritesManager.contains(favorite, file));
                }

                @Override
                public int compare(@NotNull final VirtualFile file1, @NotNull final VirtualFile file2) {
                    return 0;
                }

                @Override
                public boolean isSearchInModuleContent(@NotNull final Module aModule) {
                    return true;
                }

                @Override
                public boolean isSearchInLibraries() {
                    return true;
                }
            });
        }
    }
    ContainerUtil.addIfNotNull(result, getSelectedFilesScope(project, dataContext));
    return ContainerUtil.newArrayList(result);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TreeItem(com.intellij.util.TreeItem) NotNull(org.jetbrains.annotations.NotNull) PsiElementUsage(com.intellij.usages.rules.PsiElementUsage) UsageView(com.intellij.usages.UsageView) SelectionModel(com.intellij.openapi.editor.SelectionModel) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) FavoritesManager(com.intellij.ide.favoritesTreeView.FavoritesManager) PsiElementUsage(com.intellij.usages.rules.PsiElementUsage) Usage(com.intellij.usages.Usage) AbstractUrl(com.intellij.ide.projectView.impl.AbstractUrl) TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with UsageView

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

the class RerunSearchAction method update.

@Override
public void update(@NotNull AnActionEvent e) {
    UsageView usageView = UsageView.USAGE_VIEW_KEY.getData(e.getDataContext());
    boolean enabled = usageView instanceof UsageViewImpl && ((UsageViewImpl) usageView).canPerformReRun();
    e.getPresentation().setEnabled(enabled);
}
Also used : UsageView(com.intellij.usages.UsageView) UsageViewImpl(com.intellij.usages.impl.UsageViewImpl)

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