Search in sources :

Example 1 with EverythingGlobalScope

use of com.intellij.psi.search.EverythingGlobalScope in project android-butterknife-zelezny by avast.

the class Utils method resolveLayoutResourceFile.

private static PsiFile resolveLayoutResourceFile(PsiElement element, Project project, String name) {
    // restricting the search to the current module - searching the whole project could return wrong layouts
    Module module = ModuleUtil.findModuleForPsiElement(element);
    PsiFile[] files = null;
    if (module != null) {
        // first omit libraries, it might cause issues like (#103)
        GlobalSearchScope moduleScope = module.getModuleWithDependenciesScope();
        files = FilenameIndex.getFilesByName(project, name, moduleScope);
        if (files == null || files.length <= 0) {
            // now let's do a fallback including the libraries
            moduleScope = module.getModuleWithDependenciesAndLibrariesScope(false);
            files = FilenameIndex.getFilesByName(project, name, moduleScope);
        }
    }
    if (files == null || files.length <= 0) {
        // fallback to search through the whole project
        // useful when the project is not properly configured - when the resource directory is not configured
        files = FilenameIndex.getFilesByName(project, name, new EverythingGlobalScope(project));
        if (files.length <= 0) {
            //no matching files
            return null;
        }
    }
    // we need to resolve R class properly and find the proper layout for the R class
    for (PsiFile file : files) {
        log.info("Resolved layout resource file for name [" + name + "]: " + file.getVirtualFile());
    }
    return files[0];
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) EverythingGlobalScope(com.intellij.psi.search.EverythingGlobalScope) Module(com.intellij.openapi.module.Module)

Example 2 with EverythingGlobalScope

use of com.intellij.psi.search.EverythingGlobalScope 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 3 with EverythingGlobalScope

use of com.intellij.psi.search.EverythingGlobalScope in project kotlin by JetBrains.

the class SourceNavigationHelper method createLibraryOrSourcesScope.

@NotNull
private static GlobalSearchScope createLibraryOrSourcesScope(@NotNull KtNamedDeclaration declaration, @NotNull NavigationKind navigationKind) {
    KtFile containingFile = declaration.getContainingKtFile();
    VirtualFile libraryFile = containingFile.getVirtualFile();
    if (libraryFile == null)
        return GlobalSearchScope.EMPTY_SCOPE;
    boolean includeLibrarySources = navigationKind == NavigationKind.CLASS_FILES_TO_SOURCES;
    if (ProjectRootsUtil.isInContent(declaration, false, includeLibrarySources, !includeLibrarySources, true)) {
        return GlobalSearchScope.EMPTY_SCOPE;
    }
    Project project = declaration.getProject();
    return includeLibrarySources ? KotlinSourceFilterScope.librarySources(new EverythingGlobalScope(project), project) : KotlinSourceFilterScope.libraryClassFiles(new EverythingGlobalScope(project), project);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) EverythingGlobalScope(com.intellij.psi.search.EverythingGlobalScope) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with EverythingGlobalScope

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

the class JavaDocInfoGenerator method elementHasSourceCode.

private boolean elementHasSourceCode() {
    PsiFileSystemItem[] items;
    if (myElement instanceof PsiDirectory) {
        final PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage((PsiDirectory) myElement);
        if (aPackage == null)
            return false;
        items = aPackage.getDirectories(new EverythingGlobalScope(myProject));
    } else if (myElement instanceof PsiPackage) {
        items = ((PsiPackage) myElement).getDirectories(new EverythingGlobalScope(myProject));
    } else {
        PsiFile containingFile = myElement.getNavigationElement().getContainingFile();
        if (containingFile == null)
            return false;
        items = new PsiFileSystemItem[] { containingFile };
    }
    ProjectFileIndex projectFileIndex = ProjectFileIndex.SERVICE.getInstance(myProject);
    for (PsiFileSystemItem item : items) {
        VirtualFile file = item.getVirtualFile();
        if (file != null && projectFileIndex.isInSource(file))
            return true;
    }
    return false;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) EverythingGlobalScope(com.intellij.psi.search.EverythingGlobalScope)

Example 5 with EverythingGlobalScope

use of com.intellij.psi.search.EverythingGlobalScope in project android-butterknife-zelezny by avast.

the class InjectWriter method generateInjects.

protected void generateInjects(@NotNull IButterKnife butterKnife) {
    PsiClass activityClass = JavaPsiFacade.getInstance(mProject).findClass("android.app.Activity", new EverythingGlobalScope(mProject));
    PsiClass fragmentClass = JavaPsiFacade.getInstance(mProject).findClass("android.app.Fragment", new EverythingGlobalScope(mProject));
    PsiClass supportFragmentClass = JavaPsiFacade.getInstance(mProject).findClass("android.support.v4.app.Fragment", new EverythingGlobalScope(mProject));
    // Check for Activity class
    if (activityClass != null && mClass.isInheritor(activityClass, true)) {
        generateActivityBind(butterKnife);
    // Check for Fragment class
    } else if ((fragmentClass != null && mClass.isInheritor(fragmentClass, true)) || (supportFragmentClass != null && mClass.isInheritor(supportFragmentClass, true))) {
        generateFragmentBindAndUnbind(butterKnife);
    }
}
Also used : EverythingGlobalScope(com.intellij.psi.search.EverythingGlobalScope)

Aggregations

EverythingGlobalScope (com.intellij.psi.search.EverythingGlobalScope)9 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)3 Module (com.intellij.openapi.module.Module)2 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)2 NotNull (org.jetbrains.annotations.NotNull)2 InsertHandler (com.intellij.codeInsight.completion.InsertHandler)1 InsertionContext (com.intellij.codeInsight.completion.InsertionContext)1 PrioritizedLookupElement (com.intellij.codeInsight.completion.PrioritizedLookupElement)1 XmlTagInsertHandler (com.intellij.codeInsight.completion.XmlTagInsertHandler)1 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 SourcePosition (com.intellij.debugger.SourcePosition)1 ASTNode (com.intellij.lang.ASTNode)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 RangeMarker (com.intellij.openapi.editor.RangeMarker)1 ExternalModuleBuildGlobalSearchScope (com.intellij.openapi.externalSystem.psi.search.ExternalModuleBuildGlobalSearchScope)1 Project (com.intellij.openapi.project.Project)1 PsiClass (com.intellij.psi.PsiClass)1 PsiFile (com.intellij.psi.PsiFile)1