Search in sources :

Example 1 with DelegatingGlobalSearchScope

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

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

the class CodeFragmentFactoryContextWrapper method prepareResolveScope.

private static JavaCodeFragment prepareResolveScope(JavaCodeFragment codeFragment) {
    GlobalSearchScope originalResolveScope = codeFragment.getResolveScope();
    codeFragment.forceResolveScope(new DelegatingGlobalSearchScope(GlobalSearchScope.allScope(codeFragment.getProject())) {

        final Comparator<VirtualFile> myScopeComparator = Comparator.comparing(originalResolveScope::contains).thenComparing(super::compare);

        @Override
        public int compare(@NotNull VirtualFile file1, @NotNull VirtualFile file2) {
            // prefer files from the original resolve scope
            return myScopeComparator.compare(file1, file2);
        }
    });
    return codeFragment;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DelegatingGlobalSearchScope(com.intellij.psi.search.DelegatingGlobalSearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) DelegatingGlobalSearchScope(com.intellij.psi.search.DelegatingGlobalSearchScope)

Example 3 with DelegatingGlobalSearchScope

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

the class LibraryScopeCache method calcLibraryScope.

@NotNull
private GlobalSearchScope calcLibraryScope(@NotNull List<OrderEntry> orderEntries) {
    List<Module> modulesLibraryUsedIn = new ArrayList<>();
    LibraryOrderEntry lib = null;
    for (OrderEntry entry : orderEntries) {
        if (entry instanceof JdkOrderEntry) {
            return getScopeForSdk((JdkOrderEntry) entry);
        }
        if (entry instanceof LibraryOrderEntry) {
            lib = (LibraryOrderEntry) entry;
            modulesLibraryUsedIn.add(entry.getOwnerModule());
        } else if (entry instanceof ModuleOrderEntry) {
            modulesLibraryUsedIn.add(entry.getOwnerModule());
        }
    }
    Comparator<Module> comparator = (o1, o2) -> o1.getName().compareTo(o2.getName());
    Collections.sort(modulesLibraryUsedIn, comparator);
    List<Module> uniquesList = ContainerUtil.removeDuplicatesFromSorted(modulesLibraryUsedIn, comparator);
    GlobalSearchScope allCandidates = uniquesList.isEmpty() ? myLibrariesOnlyScope : getScopeForLibraryUsedIn(uniquesList);
    if (lib != null) {
        final LibraryRuntimeClasspathScope preferred = new LibraryRuntimeClasspathScope(myProject, lib);
        // prefer current library
        return new DelegatingGlobalSearchScope(allCandidates, preferred) {

            @Override
            public int compare(@NotNull VirtualFile file1, @NotNull VirtualFile file2) {
                boolean c1 = preferred.contains(file1);
                boolean c2 = preferred.contains(file2);
                if (c1 && !c2)
                    return 1;
                if (c2 && !c1)
                    return -1;
                return super.compare(file1, file2);
            }
        };
    }
    return allCandidates;
}
Also used : java.util(java.util) LibraryRuntimeClasspathScope(com.intellij.openapi.module.impl.scopes.LibraryRuntimeClasspathScope) ModulesScope(com.intellij.openapi.module.impl.scopes.ModulesScope) ModuleManager(com.intellij.openapi.module.ModuleManager) VirtualFile(com.intellij.openapi.vfs.VirtualFile) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ConcurrencyUtil(com.intellij.util.ConcurrencyUtil) THashSet(gnu.trove.THashSet) ContainerUtil(com.intellij.util.containers.ContainerUtil) TObjectHashingStrategy(gnu.trove.TObjectHashingStrategy) ConcurrentMap(java.util.concurrent.ConcurrentMap) JdkScope(com.intellij.openapi.module.impl.scopes.JdkScope) com.intellij.openapi.roots(com.intellij.openapi.roots) DelegatingGlobalSearchScope(com.intellij.psi.search.DelegatingGlobalSearchScope) Nullable(org.jetbrains.annotations.Nullable) ServiceManager(com.intellij.openapi.components.ServiceManager) Project(com.intellij.openapi.project.Project) ConcurrentFactoryMap(com.intellij.util.containers.ConcurrentFactoryMap) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull) SdkResolveScopeProvider(com.intellij.psi.SdkResolveScopeProvider) VirtualFile(com.intellij.openapi.vfs.VirtualFile) NotNull(org.jetbrains.annotations.NotNull) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) DelegatingGlobalSearchScope(com.intellij.psi.search.DelegatingGlobalSearchScope) LibraryRuntimeClasspathScope(com.intellij.openapi.module.impl.scopes.LibraryRuntimeClasspathScope) Module(com.intellij.openapi.module.Module) DelegatingGlobalSearchScope(com.intellij.psi.search.DelegatingGlobalSearchScope) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with DelegatingGlobalSearchScope

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

the class UnusedDeclarationInspectionBase method runInspection.

@Override
public void runInspection(@NotNull final AnalysisScope scope, @NotNull InspectionManager manager, @NotNull final GlobalInspectionContext globalContext, @NotNull ProblemDescriptionsProcessor problemDescriptionsProcessor) {
    globalContext.getRefManager().iterate(new RefJavaVisitor() {

        @Override
        public void visitElement(@NotNull final RefEntity refEntity) {
            if (refEntity instanceof RefElementImpl) {
                final RefElementImpl refElement = (RefElementImpl) refEntity;
                if (!refElement.isSuspicious())
                    return;
                PsiFile file = refElement.getContainingFile();
                if (file == null)
                    return;
                final boolean isSuppressed = refElement.isSuppressed(getShortName(), ALTERNATIVE_ID);
                if (isSuppressed || !((GlobalInspectionContextBase) globalContext).isToCheckFile(file, UnusedDeclarationInspectionBase.this)) {
                    if (isSuppressed || !scope.contains(file)) {
                        getEntryPointsManager(globalContext).addEntryPoint(refElement, false);
                    }
                }
            }
        }
    });
    if (isAddNonJavaUsedEnabled()) {
        checkForReachableRefs(globalContext);
        final StrictUnreferencedFilter strictUnreferencedFilter = new StrictUnreferencedFilter(this, globalContext);
        ProgressManager.getInstance().runProcess(new Runnable() {

            @Override
            public void run() {
                final RefManager refManager = globalContext.getRefManager();
                final PsiSearchHelper helper = PsiSearchHelper.SERVICE.getInstance(refManager.getProject());
                refManager.iterate(new RefJavaVisitor() {

                    @Override
                    public void visitElement(@NotNull final RefEntity refEntity) {
                        if (refEntity instanceof RefClass && strictUnreferencedFilter.accepts((RefClass) refEntity)) {
                            findExternalClassReferences((RefClass) refEntity);
                        } else if (refEntity instanceof RefMethod) {
                            RefMethod refMethod = (RefMethod) refEntity;
                            if (refMethod.isConstructor() && strictUnreferencedFilter.accepts(refMethod)) {
                                findExternalClassReferences(refMethod.getOwnerClass());
                            }
                        }
                    }

                    private void findExternalClassReferences(final RefClass refElement) {
                        final PsiClass psiClass = refElement.getElement();
                        String qualifiedName = psiClass != null ? psiClass.getQualifiedName() : null;
                        if (qualifiedName != null) {
                            final GlobalSearchScope projectScope = GlobalSearchScope.projectScope(globalContext.getProject());
                            final PsiNonJavaFileReferenceProcessor processor = (file, startOffset, endOffset) -> {
                                getEntryPointsManager(globalContext).addEntryPoint(refElement, false);
                                return false;
                            };
                            final DelegatingGlobalSearchScope globalSearchScope = new DelegatingGlobalSearchScope(projectScope) {

                                @Override
                                public boolean contains(@NotNull VirtualFile file) {
                                    return file.getFileType() != JavaFileType.INSTANCE && super.contains(file);
                                }
                            };
                            if (helper.processUsagesInNonJavaFiles(qualifiedName, processor, globalSearchScope)) {
                                final PsiReference reference = ReferencesSearch.search(psiClass, globalSearchScope).findFirst();
                                if (reference != null) {
                                    getEntryPointsManager(globalContext).addEntryPoint(refElement, false);
                                    for (PsiMethod method : psiClass.getMethods()) {
                                        final RefElement refMethod = refManager.getReference(method);
                                        if (refMethod != null) {
                                            getEntryPointsManager(globalContext).addEntryPoint(refMethod, false);
                                        }
                                    }
                                }
                            }
                        }
                    }
                });
            }
        }, null);
    }
    myProcessedSuspicious = new HashSet<>();
    myPhase = 1;
}
Also used : GroupNames(com.intellij.codeInsight.daemon.GroupNames) PsiClassImplUtil(com.intellij.psi.impl.PsiClassImplUtil) java.util(java.util) JobDescriptor(com.intellij.codeInspection.ex.JobDescriptor) VirtualFile(com.intellij.openapi.vfs.VirtualFile) HashMap(com.intellij.util.containers.HashMap) ToolExtensionPoints(com.intellij.ToolExtensionPoints) InvalidDataException(com.intellij.openapi.util.InvalidDataException) ContainerUtil(com.intellij.util.containers.ContainerUtil) UnusedSymbolLocalInspectionBase(com.intellij.codeInspection.unusedSymbol.UnusedSymbolLocalInspectionBase) DelegatingGlobalSearchScope(com.intellij.psi.search.DelegatingGlobalSearchScope) Project(com.intellij.openapi.project.Project) com.intellij.codeInspection.reference(com.intellij.codeInspection.reference) Logger(com.intellij.openapi.diagnostic.Logger) RefFilter(com.intellij.codeInspection.util.RefFilter) Extensions(com.intellij.openapi.extensions.Extensions) ProgressManager(com.intellij.openapi.progress.ProgressManager) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) PsiSearchHelper(com.intellij.psi.search.PsiSearchHelper) ExtensionPoint(com.intellij.openapi.extensions.ExtensionPoint) AnalysisScope(com.intellij.analysis.AnalysisScope) HighlightInfoType(com.intellij.codeInsight.daemon.impl.HighlightInfoType) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) com.intellij.codeInspection(com.intellij.codeInspection) JavaFileType(com.intellij.ide.highlighter.JavaFileType) PsiNonJavaFileReferenceProcessor(com.intellij.psi.search.PsiNonJavaFileReferenceProcessor) TestOnly(org.jetbrains.annotations.TestOnly) Nullable(org.jetbrains.annotations.Nullable) EntryPointsManager(com.intellij.codeInspection.ex.EntryPointsManager) PsiMethodUtil(com.intellij.psi.util.PsiMethodUtil) ApplicationManager(com.intellij.openapi.application.ApplicationManager) GlobalInspectionContextBase(com.intellij.codeInspection.ex.GlobalInspectionContextBase) HighlightUtilBase(com.intellij.codeInsight.daemon.impl.analysis.HighlightUtilBase) com.intellij.psi(com.intellij.psi) WriteExternalException(com.intellij.openapi.util.WriteExternalException) NotNull(org.jetbrains.annotations.NotNull) Element(org.jdom.Element) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiNonJavaFileReferenceProcessor(com.intellij.psi.search.PsiNonJavaFileReferenceProcessor) NotNull(org.jetbrains.annotations.NotNull) PsiSearchHelper(com.intellij.psi.search.PsiSearchHelper) DelegatingGlobalSearchScope(com.intellij.psi.search.DelegatingGlobalSearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) DelegatingGlobalSearchScope(com.intellij.psi.search.DelegatingGlobalSearchScope)

Example 5 with DelegatingGlobalSearchScope

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

the class UsedIconsListingAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    final Project project = LangDataKeys.PROJECT.getData(e.getDataContext());
    final MultiMap<String, PsiExpression> calls = new MultiMap<>();
    final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
    Processor<PsiReference> consumer = new Processor<PsiReference>() {

        @Override
        public boolean process(PsiReference reference) {
            PsiCallExpression call = PsiTreeUtil.getParentOfType(reference.getElement(), PsiCallExpression.class, false);
            if (call == null)
                return true;
            if (call.getArgumentList() == null)
                return true;
            if (call.getArgumentList().getExpressions() == null)
                return true;
            PsiFile file = reference.getElement().getContainingFile();
            if ("AllIcons.java".equals(file.getName()))
                return true;
            PsiClass container = PsiUtil.getTopLevelClass(reference.getElement());
            if (container != null && container.getQualifiedName().startsWith("icons."))
                return true;
            for (PsiExpression arg : call.getArgumentList().getExpressions()) {
                if (arg instanceof PsiLiteralExpression) {
                    Object value = ((PsiLiteralExpression) arg).getValue();
                    processValue(value, call, file);
                } else {
                    Object value = psiFacade.getConstantEvaluationHelper().computeConstantExpression(arg, false);
                    processValue(value, call, file);
                }
            }
            return true;
        }

        private void processValue(Object value, PsiCallExpression call, PsiFile file) {
            if (value instanceof String) {
                String str = StringUtil.unquoteString((String) value, '\"');
                if (!str.startsWith("/")) {
                    if (file instanceof PsiClassOwner) {
                        str = "/" + ((PsiClassOwner) file).getPackageName().replace('.', '/') + "/" + str;
                    }
                }
                calls.putValue(str, call);
            }
        }
    };
    GlobalSearchScope allScope = GlobalSearchScope.allScope(project);
    PsiClass iconLoader = psiFacade.findClass("com.intellij.openapi.util.IconLoader", allScope);
    PsiMethod getIconMethod = iconLoader.findMethodsByName("getIcon", false)[0];
    PsiMethod findIconMethod = iconLoader.findMethodsByName("findIcon", false)[0];
    if (true) {
        MethodReferencesSearch.search(getIconMethod, false).forEach(consumer);
        MethodReferencesSearch.search(findIconMethod, false).forEach(consumer);
    }
    final ProjectFileIndex index = ProjectRootManager.getInstance(project).getFileIndex();
    if (true) {
        PsiClass javaeeIcons = psiFacade.findClass("com.intellij.javaee.oss.JavaeeIcons", allScope);
        MethodReferencesSearch.search(javaeeIcons.findMethodsByName("getIcon", false)[0], false).forEach(consumer);
        MethodReferencesSearch.search(findIconMethod, false).forEach(consumer);
    }
    final List<XmlAttribute> xmlAttributes = new ArrayList<>();
    PsiSearchHelper.SERVICE.getInstance(project).processAllFilesWithWordInText("icon", new DelegatingGlobalSearchScope(GlobalSearchScope.projectScope(project)) {

        @Override
        public boolean contains(@NotNull VirtualFile file) {
            return super.contains(file) && file.getFileType() == XmlFileType.INSTANCE && index.isInSource(file);
        }
    }, new Processor<PsiFile>() {

        @Override
        public boolean process(PsiFile file) {
            file.accept(new XmlRecursiveElementVisitor() {

                @Override
                public void visitXmlTag(XmlTag tag) {
                    super.visitXmlTag(tag);
                    String icon = tag.getAttributeValue("icon");
                    if (icon != null) {
                        xmlAttributes.add(tag.getAttribute("icon"));
                    }
                }
            });
            return true;
        }
    }, true);
    PsiClass presentation = psiFacade.findClass("com.intellij.ide.presentation.Presentation", allScope);
    final MultiMap<String, PsiAnnotation> annotations = new MultiMap<>();
    AnnotationTargetsSearch.search(presentation).forEach(owner -> {
        PsiAnnotation annotation = owner.getModifierList().findAnnotation("com.intellij.ide.presentation.Presentation");
        PsiAnnotationMemberValue icon = annotation.findAttributeValue("icon");
        if (icon instanceof PsiLiteralExpression) {
            Object value = ((PsiLiteralExpression) icon).getValue();
            if (value instanceof String) {
                annotations.putValue((String) value, annotation);
            }
        }
        return true;
    });
    doReplacements(project, calls, xmlAttributes, annotations, psiFacade.findClass("com.intellij.icons.AllIcons", allScope));
    for (PsiClass iconClass : psiFacade.findPackage("icons").getClasses(allScope)) {
        if (iconClass.getName().endsWith("Icons")) {
            doReplacements(project, calls, xmlAttributes, annotations, iconClass);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Processor(com.intellij.util.Processor) XmlAttribute(com.intellij.psi.xml.XmlAttribute) MultiMap(com.intellij.util.containers.MultiMap) DelegatingGlobalSearchScope(com.intellij.psi.search.DelegatingGlobalSearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Project(com.intellij.openapi.project.Project) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) DelegatingGlobalSearchScope(com.intellij.psi.search.DelegatingGlobalSearchScope) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 DelegatingGlobalSearchScope (com.intellij.psi.search.DelegatingGlobalSearchScope)5 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)5 Project (com.intellij.openapi.project.Project)3 NotNull (org.jetbrains.annotations.NotNull)3 ContainerUtil (com.intellij.util.containers.ContainerUtil)2 java.util (java.util)2 ToolExtensionPoints (com.intellij.ToolExtensionPoints)1 AnalysisScope (com.intellij.analysis.AnalysisScope)1 GroupNames (com.intellij.codeInsight.daemon.GroupNames)1 HighlightInfoType (com.intellij.codeInsight.daemon.impl.HighlightInfoType)1 HighlightUtilBase (com.intellij.codeInsight.daemon.impl.analysis.HighlightUtilBase)1 com.intellij.codeInspection (com.intellij.codeInspection)1 EntryPointsManager (com.intellij.codeInspection.ex.EntryPointsManager)1 GlobalInspectionContextBase (com.intellij.codeInspection.ex.GlobalInspectionContextBase)1 JobDescriptor (com.intellij.codeInspection.ex.JobDescriptor)1 com.intellij.codeInspection.reference (com.intellij.codeInspection.reference)1 UnusedSymbolLocalInspectionBase (com.intellij.codeInspection.unusedSymbol.UnusedSymbolLocalInspectionBase)1 RefFilter (com.intellij.codeInspection.util.RefFilter)1 JavaFileType (com.intellij.ide.highlighter.JavaFileType)1