Search in sources :

Example 11 with Usage

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

the class FindManagerTest method testReplaceAll.

public void testReplaceAll() throws FindManager.MalformedReplacementStringException {
    final FindModel findModel = new FindModel();
    String toFind = "xxx";
    @SuppressWarnings("SpellCheckingInspection") String toReplace = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    findModel.setStringToFind(toFind);
    findModel.setStringToReplace(toReplace);
    findModel.setWholeWordsOnly(true);
    findModel.setFromCursor(false);
    findModel.setGlobal(true);
    findModel.setMultipleFiles(false);
    findModel.setProjectScope(true);
    findModel.setRegularExpressions(false);
    findModel.setPromptOnReplace(false);
    myFindManager.setFindNextModel(null);
    myFindManager.getFindInFileModel().copyFrom(findModel);
    String text = StringUtil.repeat(toFind + "\n", 6);
    configureByText(FileTypes.PLAIN_TEXT, text);
    final List<Usage> usages = FindUtil.findAll(getProject(), myEditor, findModel);
    assertNotNull(usages);
    CommandProcessor.getInstance().executeCommand(getProject(), () -> {
        for (Usage usage : usages) {
            try {
                ReplaceInProjectManager.getInstance(getProject()).replaceUsage(usage, findModel, Collections.<Usage>emptySet(), false);
            } catch (FindManager.MalformedReplacementStringException e) {
                throw new RuntimeException(e);
            }
        }
    }, "", null);
    String newText = StringUtil.repeat(toReplace + "\n", 6);
    assertEquals(newText, getEditor().getDocument().getText());
}
Also used : Usage(com.intellij.usages.Usage)

Example 12 with Usage

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

the class ImportUsagesAction method isEnabled.

private boolean isEnabled(DataContext dc) {
    final Project project = CommonDataKeys.PROJECT.getData(dc);
    final Usage[] usages = UsageView.USAGES_KEY.getData(dc);
    return project != null && usages != null && usages.length > 0;
}
Also used : Project(com.intellij.openapi.project.Project) Usage(com.intellij.usages.Usage)

Example 13 with Usage

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

the class UsageFavoriteNodeProvider method getFavoriteNodes.

@Override
public Collection<AbstractTreeNode> getFavoriteNodes(DataContext context, ViewSettings viewSettings) {
    final Project project = CommonDataKeys.PROJECT.getData(context);
    if (project == null) {
        return null;
    }
    final Usage[] usages = UsageView.USAGES_KEY.getData(context);
    if (usages != null) {
        final List<AbstractTreeNode> result = new SmartList<>();
        final MultiMap<VirtualFile, Usage> map = new MultiMap<>();
        final List<Usage> nonMapped = new ArrayList<>();
        for (Usage usage : usages) {
            if (usage instanceof UsageInFile) {
                map.putValue(((UsageInFile) usage).getFile(), usage);
            } else if (usage instanceof UsageInFiles) {
                final VirtualFile[] files = ((UsageInFiles) usage).getFiles();
                for (VirtualFile file : files) {
                    map.putValue(file, usage);
                }
            } else {
                nonMapped.add(usage);
            }
        }
        final TreeSet<VirtualFile> keys = new TreeSet<>(VIRTUAL_FILE_COMPARATOR);
        keys.addAll(map.keySet());
        for (VirtualFile key : keys) {
            final FileGroupingProjectNode grouping = new FileGroupingProjectNode(project, new File(key.getPath()), viewSettings);
            result.add(grouping);
            final Collection<Usage> subUsages = map.get(key);
            for (Usage usage : subUsages) {
                if (usage instanceof UsageInfo2UsageAdapter) {
                    final UsageProjectTreeNode node = new UsageProjectTreeNode(project, ((UsageInfo2UsageAdapter) usage).getUsageInfo(), viewSettings);
                    grouping.addChild(node);
                } else if (NullUsage.INSTANCE.equals(usage)) {
                    continue;
                } else {
                    grouping.addChild(new NoteProjectNode(project, new NoteNode(usage.getPresentation().getPlainText(), true), viewSettings));
                }
            }
        }
        for (Usage usage : nonMapped) {
            if (usage instanceof UsageInfo2UsageAdapter) {
                final UsageProjectTreeNode node = new UsageProjectTreeNode(project, ((UsageInfo2UsageAdapter) usage).getUsageInfo(), viewSettings);
                result.add(node);
            } else if (NullUsage.INSTANCE.equals(usage)) {
                continue;
            } else {
                result.add(new NoteProjectNode(project, new NoteNode(usage.getPresentation().getPlainText(), true), viewSettings));
            }
        }
        return result;
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) NullUsage(com.intellij.usages.impl.NullUsage) Usage(com.intellij.usages.Usage) UsageInFiles(com.intellij.usages.rules.UsageInFiles) UsageInFile(com.intellij.usages.rules.UsageInFile) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) Project(com.intellij.openapi.project.Project) MultiMap(com.intellij.util.containers.MultiMap) UsageInfo2UsageAdapter(com.intellij.usages.UsageInfo2UsageAdapter) SmartList(com.intellij.util.SmartList) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) UsageInFile(com.intellij.usages.rules.UsageInFile) File(java.io.File)

Example 14 with Usage

use of com.intellij.usages.Usage 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 15 with Usage

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

the class SlicePanel method createTree.

@NotNull
private JTree createTree() {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode();
    final Tree tree = new Tree(new DefaultTreeModel(root));
    tree.setOpaque(false);
    tree.setToggleClickCount(-1);
    SliceUsageCellRendererBase renderer = myProvider.getRenderer();
    renderer.setOpaque(false);
    tree.setCellRenderer(renderer);
    UIUtil.setLineStyleAngled(tree);
    tree.setRootVisible(false);
    tree.setShowsRootHandles(true);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    tree.setSelectionPath(new TreePath(root.getPath()));
    //ActionGroup group = (ActionGroup)ActionManager.getInstance().getAction(IdeActions.GROUP_METHOD_HIERARCHY_POPUP);
    //PopupHandler.installPopupHandler(tree, group, ActionPlaces.METHOD_HIERARCHY_VIEW_POPUP, ActionManager.getInstance());
    EditSourceOnDoubleClickHandler.install(tree);
    new TreeSpeedSearch(tree);
    TreeUtil.installActions(tree);
    ToolTipManager.sharedInstance().registerComponent(tree);
    myAutoScrollToSourceHandler.install(tree);
    tree.getSelectionModel().addTreeSelectionListener(e -> treeSelectionChanged());
    tree.addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
            if (KeyEvent.VK_ENTER == e.getKeyCode()) {
                List<Navigatable> navigatables = getNavigatables();
                if (navigatables.isEmpty())
                    return;
                for (Navigatable navigatable : navigatables) {
                    if (navigatable instanceof AbstractTreeNode && ((AbstractTreeNode) navigatable).getValue() instanceof Usage) {
                        navigatable = (Usage) ((AbstractTreeNode) navigatable).getValue();
                    }
                    if (navigatable.canNavigateToSource()) {
                        navigatable.navigate(false);
                        if (navigatable instanceof Usage) {
                            ((Usage) navigatable).highlightInEditor();
                        }
                    }
                }
                e.consume();
            }
        }
    });
    tree.addTreeWillExpandListener(new TreeWillExpandListener() {

        @Override
        public void treeWillCollapse(TreeExpansionEvent event) {
        }

        @Override
        public void treeWillExpand(TreeExpansionEvent event) {
            TreePath path = event.getPath();
            SliceNode node = fromPath(path);
            node.calculateDupNode();
        }
    });
    return tree;
}
Also used : TreeWillExpandListener(javax.swing.event.TreeWillExpandListener) Usage(com.intellij.usages.Usage) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) KeyAdapter(java.awt.event.KeyAdapter) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) Navigatable(com.intellij.pom.Navigatable) KeyEvent(java.awt.event.KeyEvent) TreePath(javax.swing.tree.TreePath) Tree(com.intellij.ui.treeStructure.Tree) ArrayList(java.util.ArrayList) List(java.util.List) TreeExpansionEvent(javax.swing.event.TreeExpansionEvent) NotNull(org.jetbrains.annotations.NotNull)

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