Search in sources :

Example 1 with TreeItem

use of com.intellij.util.TreeItem in project intellij-community by JetBrains.

the class FavoritesManager method writeRoots.

private static void writeRoots(Element element, Collection<TreeItem<Pair<AbstractUrl, String>>> roots) {
    for (TreeItem<Pair<AbstractUrl, String>> root : roots) {
        final AbstractUrl url = root.getData().getFirst();
        if (url == null)
            continue;
        final Element list = new Element(FAVORITES_ROOT);
        url.write(list);
        list.setAttribute(CLASS_NAME, root.getData().getSecond());
        element.addContent(list);
        final List<TreeItem<Pair<AbstractUrl, String>>> children = root.getChildren();
        if (children != null && !children.isEmpty()) {
            writeRoots(list, children);
        }
    }
}
Also used : TreeItem(com.intellij.util.TreeItem) NamedLibraryElement(com.intellij.ide.projectView.impl.nodes.NamedLibraryElement) LibraryGroupElement(com.intellij.ide.projectView.impl.nodes.LibraryGroupElement) Element(org.jdom.Element)

Example 2 with TreeItem

use of com.intellij.util.TreeItem 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 3 with TreeItem

use of com.intellij.util.TreeItem in project intellij-community by JetBrains.

the class FavoritesManager method appendChildNodes.

private void appendChildNodes(AbstractTreeNode node, TreeItem<Pair<AbstractUrl, String>> treeItem) {
    final Collection<? extends AbstractTreeNode> children = node.getChildren();
    for (AbstractTreeNode child : children) {
        final TreeItem<Pair<AbstractUrl, String>> childTreeItem = new TreeItem<>(createPairForNode(child));
        treeItem.addChild(childTreeItem);
        appendChildNodes(child, childTreeItem);
    }
}
Also used : TreeItem(com.intellij.util.TreeItem) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode)

Example 4 with TreeItem

use of com.intellij.util.TreeItem in project intellij-community by JetBrains.

the class FavoritesManager method canAddRoots.

public boolean canAddRoots(@NotNull String name, @NotNull Collection<AbstractTreeNode> nodes) {
    final Collection<TreeItem<Pair<AbstractUrl, String>>> list = getFavoritesListRootUrls(name);
    final HashSet<AbstractUrl> set = new HashSet<>(ContainerUtil.map(list, item -> item.getData().getFirst()));
    for (AbstractTreeNode node : nodes) {
        final Pair<AbstractUrl, String> pair = createPairForNode(node);
        if (pair != null && !set.contains(pair.getFirst()))
            return true;
    }
    return false;
}
Also used : com.intellij.openapi.util(com.intellij.openapi.util) java.util(java.util) ArrayUtil(com.intellij.util.ArrayUtil) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiUtilBase(com.intellij.psi.util.PsiUtilBase) NonNls(org.jetbrains.annotations.NonNls) ContainerUtil(com.intellij.util.containers.ContainerUtil) com.intellij.openapi.roots(com.intellij.openapi.roots) ModuleUtil(com.intellij.openapi.module.ModuleUtil) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) Project(com.intellij.openapi.project.Project) com.intellij.ide.projectView.impl(com.intellij.ide.projectView.impl) Messages(com.intellij.openapi.ui.Messages) ProjectComponent(com.intellij.openapi.components.ProjectComponent) Module(com.intellij.openapi.module.Module) Extensions(com.intellij.openapi.extensions.Extensions) NamedLibraryElement(com.intellij.ide.projectView.impl.nodes.NamedLibraryElement) AddToFavoritesAction(com.intellij.ide.favoritesTreeView.actions.AddToFavoritesAction) Convertor(com.intellij.util.containers.Convertor) TreeItem(com.intellij.util.TreeItem) EP_NAME(com.intellij.ide.favoritesTreeView.FavoritesListProvider.EP_NAME) Disposable(com.intellij.openapi.Disposable) IdeBundle(com.intellij.ide.IdeBundle) Nullable(org.jetbrains.annotations.Nullable) InputValidator(com.intellij.openapi.ui.InputValidator) ApplicationManager(com.intellij.openapi.application.ApplicationManager) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) LibraryGroupElement(com.intellij.ide.projectView.impl.nodes.LibraryGroupElement) Element(org.jdom.Element) Consumer(com.intellij.util.Consumer) TreeItem(com.intellij.util.TreeItem) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode)

Example 5 with TreeItem

use of com.intellij.util.TreeItem in project intellij-community by JetBrains.

the class FavoritesManager method addRoots.

public boolean addRoots(final String name, final Collection<AbstractTreeNode> nodes) {
    final Collection<TreeItem<Pair<AbstractUrl, String>>> list = getFavoritesListRootUrls(name);
    final HashSet<AbstractUrl> set = new HashSet<>(ContainerUtil.map(list, item -> item.getData().getFirst()));
    for (AbstractTreeNode node : nodes) {
        final Pair<AbstractUrl, String> pair = createPairForNode(node);
        if (pair != null) {
            if (set.contains(pair.getFirst()))
                continue;
            final TreeItem<Pair<AbstractUrl, String>> treeItem = new TreeItem<>(pair);
            list.add(treeItem);
            set.add(pair.getFirst());
            appendChildNodes(node, treeItem);
        }
    }
    rootsChanged();
    return true;
}
Also used : com.intellij.openapi.util(com.intellij.openapi.util) java.util(java.util) ArrayUtil(com.intellij.util.ArrayUtil) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiUtilBase(com.intellij.psi.util.PsiUtilBase) NonNls(org.jetbrains.annotations.NonNls) ContainerUtil(com.intellij.util.containers.ContainerUtil) com.intellij.openapi.roots(com.intellij.openapi.roots) ModuleUtil(com.intellij.openapi.module.ModuleUtil) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) Project(com.intellij.openapi.project.Project) com.intellij.ide.projectView.impl(com.intellij.ide.projectView.impl) Messages(com.intellij.openapi.ui.Messages) ProjectComponent(com.intellij.openapi.components.ProjectComponent) Module(com.intellij.openapi.module.Module) Extensions(com.intellij.openapi.extensions.Extensions) NamedLibraryElement(com.intellij.ide.projectView.impl.nodes.NamedLibraryElement) AddToFavoritesAction(com.intellij.ide.favoritesTreeView.actions.AddToFavoritesAction) Convertor(com.intellij.util.containers.Convertor) TreeItem(com.intellij.util.TreeItem) EP_NAME(com.intellij.ide.favoritesTreeView.FavoritesListProvider.EP_NAME) Disposable(com.intellij.openapi.Disposable) IdeBundle(com.intellij.ide.IdeBundle) Nullable(org.jetbrains.annotations.Nullable) InputValidator(com.intellij.openapi.ui.InputValidator) ApplicationManager(com.intellij.openapi.application.ApplicationManager) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) LibraryGroupElement(com.intellij.ide.projectView.impl.nodes.LibraryGroupElement) Element(org.jdom.Element) Consumer(com.intellij.util.Consumer) TreeItem(com.intellij.util.TreeItem) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode)

Aggregations

TreeItem (com.intellij.util.TreeItem)9 LibraryGroupElement (com.intellij.ide.projectView.impl.nodes.LibraryGroupElement)5 NamedLibraryElement (com.intellij.ide.projectView.impl.nodes.NamedLibraryElement)5 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 Element (org.jdom.Element)4 Module (com.intellij.openapi.module.Module)3 NotNull (org.jetbrains.annotations.NotNull)3 IdeBundle (com.intellij.ide.IdeBundle)2 EP_NAME (com.intellij.ide.favoritesTreeView.FavoritesListProvider.EP_NAME)2 AddToFavoritesAction (com.intellij.ide.favoritesTreeView.actions.AddToFavoritesAction)2 com.intellij.ide.projectView.impl (com.intellij.ide.projectView.impl)2 AbstractUrl (com.intellij.ide.projectView.impl.AbstractUrl)2 Disposable (com.intellij.openapi.Disposable)2 ApplicationManager (com.intellij.openapi.application.ApplicationManager)2 ProjectComponent (com.intellij.openapi.components.ProjectComponent)2 Extensions (com.intellij.openapi.extensions.Extensions)2 ModuleUtil (com.intellij.openapi.module.ModuleUtil)2 Project (com.intellij.openapi.project.Project)2 com.intellij.openapi.roots (com.intellij.openapi.roots)2