Search in sources :

Example 46 with GlobalSearchScope

use of com.intellij.psi.search.GlobalSearchScope in project intellij-community by JetBrains.

the class SearchScope method iterateContent.

public void iterateContent(@NotNull final Project project, final Processor<VirtualFile> processor) {
    switch(getScopeType()) {
        case PROJECT:
            //noinspection unchecked
            ProjectRootManager.getInstance(project).getFileIndex().iterateContent(new MyFileIterator(processor, Conditions.<VirtualFile>alwaysTrue()));
            break;
        case MODULE:
            final Module module = ModuleManager.getInstance(project).findModuleByName(getModuleName());
            assert module != null;
            ModuleRootManager.getInstance(module).getFileIndex().iterateContent(new MyFileIterator(processor, Conditions.<VirtualFile>alwaysTrue()));
            break;
        case DIRECTORY:
            final String dirName = getPath();
            assert dirName != null;
            final VirtualFile virtualFile = findFile(dirName);
            if (virtualFile != null) {
                iterateRecursively(virtualFile, processor, isRecursive());
            }
            break;
        case CUSTOM:
            assert myCustomScope != null;
            final ContentIterator iterator;
            if (myCustomScope instanceof GlobalSearchScope) {
                final GlobalSearchScope searchScope = (GlobalSearchScope) myCustomScope;
                iterator = new MyFileIterator(processor, virtualFile13 -> searchScope.contains(virtualFile13));
                if (searchScope.isSearchInLibraries()) {
                    final OrderEnumerator enumerator = OrderEnumerator.orderEntries(project).withoutModuleSourceEntries().withoutDepModules();
                    final Collection<VirtualFile> libraryFiles = new THashSet<>();
                    Collections.addAll(libraryFiles, enumerator.getClassesRoots());
                    Collections.addAll(libraryFiles, enumerator.getSourceRoots());
                    final Processor<VirtualFile> adapter = virtualFile1 -> iterator.processFile(virtualFile1);
                    for (final VirtualFile file : libraryFiles) {
                        iterateRecursively(file, adapter, true);
                    }
                }
            } else {
                final PsiManager manager = PsiManager.getInstance(project);
                iterator = new MyFileIterator(processor, virtualFile12 -> {
                    final PsiFile element = manager.findFile(virtualFile12);
                    return element != null && PsiSearchScopeUtil.isInScope(myCustomScope, element);
                });
            }
            ProjectRootManager.getInstance(project).getFileIndex().iterateContent(iterator);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) OrderEnumerator(com.intellij.openapi.roots.OrderEnumerator) Tag(com.intellij.util.xmlb.annotations.Tag) ModuleManager(com.intellij.openapi.module.ModuleManager) VirtualFile(com.intellij.openapi.vfs.VirtualFile) THashSet(gnu.trove.THashSet) PsiManager(com.intellij.psi.PsiManager) Comparing(com.intellij.openapi.util.Comparing) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) Conditions(com.intellij.openapi.util.Conditions) Module(com.intellij.openapi.module.Module) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) VfsUtilCore(com.intellij.openapi.vfs.VfsUtilCore) ContentIterator(com.intellij.openapi.roots.ContentIterator) Collection(java.util.Collection) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) Nullable(org.jetbrains.annotations.Nullable) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) PsiSearchScopeUtil(com.intellij.psi.search.PsiSearchScopeUtil) Processor(com.intellij.util.Processor) VirtualFileVisitor(com.intellij.openapi.vfs.VirtualFileVisitor) Attribute(com.intellij.util.xmlb.annotations.Attribute) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) Condition(com.intellij.openapi.util.Condition) ContentIterator(com.intellij.openapi.roots.ContentIterator) PsiManager(com.intellij.psi.PsiManager) THashSet(gnu.trove.THashSet) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) OrderEnumerator(com.intellij.openapi.roots.OrderEnumerator) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module)

Example 47 with GlobalSearchScope

use of com.intellij.psi.search.GlobalSearchScope in project intellij-community by JetBrains.

the class PythonImportUtils method addSymbolImportCandidates.

private static void addSymbolImportCandidates(PyElement node, String refText, @Nullable String asName, AutoImportQuickFix fix, Set<String> seenCandidateNames, PsiFile existingImportFile) {
    Project project = node.getProject();
    List<PsiElement> symbols = new ArrayList<>();
    symbols.addAll(PyClassNameIndex.find(refText, project, true));
    GlobalSearchScope scope = PyProjectScopeBuilder.excludeSdkTestsScope(node);
    if (!isQualifier(node)) {
        symbols.addAll(PyFunctionNameIndex.find(refText, project, scope));
    }
    symbols.addAll(PyVariableNameIndex.find(refText, project, scope));
    if (isPossibleModuleReference(node)) {
        symbols.addAll(findImportableModules(node.getContainingFile(), refText, project, scope));
    }
    if (!symbols.isEmpty()) {
        for (PsiElement symbol : symbols) {
            if (isIndexableTopLevel(symbol)) {
                // we only want top-level symbols
                PsiFileSystemItem srcfile = symbol instanceof PsiFileSystemItem ? ((PsiFileSystemItem) symbol).getParent() : symbol.getContainingFile();
                if (srcfile != null && isAcceptableForImport(node, existingImportFile, srcfile)) {
                    QualifiedName importPath = QualifiedNameFinder.findCanonicalImportPath(symbol, node);
                    if (importPath == null) {
                        continue;
                    }
                    if (symbol instanceof PsiFileSystemItem) {
                        importPath = importPath.removeTail(1);
                    }
                    final String symbolImportQName = importPath.append(refText).toString();
                    if (!seenCandidateNames.contains(symbolImportQName)) {
                        // a new, valid hit
                        fix.addImport(symbol, srcfile, importPath, asName);
                        seenCandidateNames.add(symbolImportQName);
                    }
                }
            }
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) QualifiedName(com.intellij.psi.util.QualifiedName)

Example 48 with GlobalSearchScope

use of com.intellij.psi.search.GlobalSearchScope in project intellij-community by JetBrains.

the class DefaultXmlTagNameProvider method getRootTagsVariants.

private static List<LookupElement> getRootTagsVariants(final XmlTag tag, final List<LookupElement> elements) {
    elements.add(LookupElementBuilder.create("?xml version=\"1.0\" encoding=\"\" ?>").withPresentableText("<?xml version=\"1.0\" encoding=\"\" ?>").withInsertHandler(new InsertHandler<LookupElement>() {

        @Override
        public void handleInsert(InsertionContext context, LookupElement item) {
            int offset = context.getEditor().getCaretModel().getOffset();
            context.getEditor().getCaretModel().moveToOffset(offset - 4);
            AutoPopupController.getInstance(context.getProject()).scheduleAutoPopup(context.getEditor());
        }
    }));
    final FileBasedIndex fbi = FileBasedIndex.getInstance();
    Collection<String> result = new ArrayList<>();
    Processor<String> processor = Processors.cancelableCollectProcessor(result);
    fbi.processAllKeys(XmlNamespaceIndex.NAME, processor, tag.getProject());
    final GlobalSearchScope scope = new EverythingGlobalScope();
    for (final String ns : result) {
        if (ns.startsWith("file://"))
            continue;
        fbi.processValues(XmlNamespaceIndex.NAME, ns, null, new FileBasedIndex.ValueProcessor<XsdNamespaceBuilder>() {

            @Override
            public boolean process(final VirtualFile file, XsdNamespaceBuilder value) {
                List<String> tags = value.getRootTags();
                for (String s : tags) {
                    elements.add(LookupElementBuilder.create(s).withTypeText(ns).withInsertHandler(new XmlTagInsertHandler() {

                        @Override
                        public void handleInsert(InsertionContext context, LookupElement item) {
                            final Editor editor = context.getEditor();
                            final Document document = context.getDocument();
                            final int caretOffset = editor.getCaretModel().getOffset();
                            final RangeMarker caretMarker = document.createRangeMarker(caretOffset, caretOffset);
                            caretMarker.setGreedyToRight(true);
                            XmlFile psiFile = (XmlFile) context.getFile();
                            boolean incomplete = XmlUtil.getTokenOfType(tag, XmlTokenType.XML_TAG_END) == null && XmlUtil.getTokenOfType(tag, XmlTokenType.XML_EMPTY_ELEMENT_END) == null;
                            XmlNamespaceHelper.getHelper(psiFile).insertNamespaceDeclaration(psiFile, editor, Collections.singleton(ns), null, null);
                            editor.getCaretModel().moveToOffset(caretMarker.getEndOffset());
                            XmlTag rootTag = psiFile.getRootTag();
                            if (incomplete) {
                                XmlToken token = XmlUtil.getTokenOfType(rootTag, XmlTokenType.XML_EMPTY_ELEMENT_END);
                                // remove tag end added by smart attribute adder :(
                                if (token != null)
                                    token.delete();
                                PsiDocumentManager.getInstance(context.getProject()).doPostponedOperationsAndUnblockDocument(document);
                                super.handleInsert(context, item);
                            }
                        }
                    }));
                }
                return true;
            }
        }, scope);
    }
    return elements;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlFile(com.intellij.psi.xml.XmlFile) EverythingGlobalScope(com.intellij.psi.search.EverythingGlobalScope) XsdNamespaceBuilder(com.intellij.xml.index.XsdNamespaceBuilder) RangeMarker(com.intellij.openapi.editor.RangeMarker) InsertionContext(com.intellij.codeInsight.completion.InsertionContext) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PrioritizedLookupElement(com.intellij.codeInsight.completion.PrioritizedLookupElement) XmlTagInsertHandler(com.intellij.codeInsight.completion.XmlTagInsertHandler) Document(com.intellij.openapi.editor.Document) XmlToken(com.intellij.psi.xml.XmlToken) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) InsertHandler(com.intellij.codeInsight.completion.InsertHandler) XmlTagInsertHandler(com.intellij.codeInsight.completion.XmlTagInsertHandler) Editor(com.intellij.openapi.editor.Editor) FileBasedIndex(com.intellij.util.indexing.FileBasedIndex) XmlTag(com.intellij.psi.xml.XmlTag)

Example 49 with GlobalSearchScope

use of com.intellij.psi.search.GlobalSearchScope in project intellij-community by JetBrains.

the class GroovyScriptRunConfiguration method getSearchScope.

@Override
public GlobalSearchScope getSearchScope() {
    GlobalSearchScope superScope = super.getSearchScope();
    String path = getScriptPath();
    if (path == null)
        return superScope;
    VirtualFile scriptFile = LocalFileSystem.getInstance().findFileByPath(path);
    if (scriptFile == null)
        return superScope;
    GlobalSearchScope fileScope = GlobalSearchScope.fileScope(getProject(), scriptFile);
    if (superScope == null)
        return fileScope;
    return new DelegatingGlobalSearchScope(fileScope.union(superScope)) {

        @Override
        public int compare(@NotNull VirtualFile file1, @NotNull VirtualFile file2) {
            if (file1.equals(scriptFile))
                return 1;
            if (file2.equals(scriptFile))
                return -1;
            return super.compare(file1, file2);
        }
    };
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) DelegatingGlobalSearchScope(com.intellij.psi.search.DelegatingGlobalSearchScope) NotNull(org.jetbrains.annotations.NotNull) DelegatingGlobalSearchScope(com.intellij.psi.search.DelegatingGlobalSearchScope)

Example 50 with GlobalSearchScope

use of com.intellij.psi.search.GlobalSearchScope in project intellij-community by JetBrains.

the class GrIntroduceParameterProcessor method getForcedType.

@NotNull
@Override
public PsiType getForcedType() {
    final PsiType selectedType = mySettings.getSelectedType();
    if (selectedType != null)
        return selectedType;
    final PsiManager manager = PsiManager.getInstance(myProject);
    final GlobalSearchScope resolveScope = mySettings.getToReplaceIn().getResolveScope();
    return PsiType.getJavaLangObject(manager, resolveScope);
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)485 Project (com.intellij.openapi.project.Project)145 NotNull (org.jetbrains.annotations.NotNull)134 VirtualFile (com.intellij.openapi.vfs.VirtualFile)106 Module (com.intellij.openapi.module.Module)101 Nullable (org.jetbrains.annotations.Nullable)78 PsiClass (com.intellij.psi.PsiClass)53 ArrayList (java.util.ArrayList)37 PsiFile (com.intellij.psi.PsiFile)32 PsiElement (com.intellij.psi.PsiElement)31 THashSet (gnu.trove.THashSet)22 SearchScope (com.intellij.psi.search.SearchScope)19 PsiDirectory (com.intellij.psi.PsiDirectory)18 ContainerUtil (com.intellij.util.containers.ContainerUtil)18 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)17 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)17 com.intellij.psi (com.intellij.psi)17 List (java.util.List)17 StringUtil (com.intellij.openapi.util.text.StringUtil)15 JavaPsiFacade (com.intellij.psi.JavaPsiFacade)15