Search in sources :

Example 16 with SearchScope

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

the class JavaClassInheritorsSearcher method getOrComputeSubClasses.

@NotNull
private static Iterable<PsiClass> getOrComputeSubClasses(@NotNull Project project, @NotNull PsiClass baseClass, @NotNull SearchScope searchScopeForNonPhysical) {
    ConcurrentMap<PsiClass, Iterable<PsiClass>> map = HighlightingCaches.getInstance(project).ALL_SUB_CLASSES;
    Iterable<PsiClass> cached = map.get(baseClass);
    if (cached == null) {
        // returns lazy collection of subclasses. Each call to next() leads to calculation of next batch of subclasses.
        Function<PsiAnchor, PsiClass> converter = anchor -> ReadAction.compute(() -> (PsiClass) anchor.retrieve());
        Predicate<PsiClass> applicableFilter = candidate -> !(candidate instanceof PsiAnonymousClass) && candidate != null && !candidate.hasModifierProperty(PsiModifier.FINAL);
        // for non-physical elements ignore the cache completely because non-physical elements created so often/unpredictably so I can't figure out when to clear caches in this case
        boolean isPhysical = ReadAction.compute(baseClass::isPhysical);
        SearchScope scopeToUse = isPhysical ? GlobalSearchScope.allScope(project) : searchScopeForNonPhysical;
        LazyConcurrentCollection.MoreElementsGenerator<PsiAnchor, PsiClass> generator = (candidate, processor) -> DirectClassInheritorsSearch.search(candidate, scopeToUse).forEach(subClass -> {
            ProgressManager.checkCanceled();
            PsiAnchor pointer = ReadAction.compute(() -> PsiAnchor.create(subClass));
            processor.consume(pointer);
            return true;
        });
        PsiAnchor seed = ReadAction.compute(() -> PsiAnchor.create(baseClass));
        // lazy collection: store underlying queue as PsiAnchors, generate new elements by running direct inheritors
        Iterable<PsiClass> computed = new LazyConcurrentCollection<>(seed, converter, applicableFilter, generator);
        // make sure concurrent calls of this method always return the same collection to avoid expensive duplicate work
        cached = isPhysical ? ConcurrencyUtil.cacheOrGet(map, baseClass, computed) : computed;
    }
    return cached;
}
Also used : ProgressManager(com.intellij.openapi.progress.ProgressManager) VirtualFile(com.intellij.openapi.vfs.VirtualFile) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ConcurrencyUtil(com.intellij.util.ConcurrencyUtil) SearchScope(com.intellij.psi.search.SearchScope) ReadAction(com.intellij.openapi.application.ReadAction) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) Predicate(com.intellij.util.containers.Predicate) QueryExecutorBase(com.intellij.openapi.application.QueryExecutorBase) ProgressIndicatorProvider(com.intellij.openapi.progress.ProgressIndicatorProvider) ConcurrentMap(java.util.concurrent.ConcurrentMap) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) PsiSearchScopeUtil(com.intellij.psi.search.PsiSearchScopeUtil) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) Function(com.intellij.util.Function) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) Project(com.intellij.openapi.project.Project) com.intellij.psi(com.intellij.psi) AllClassesSearch(com.intellij.psi.search.searches.AllClassesSearch) ClassInheritorsSearch(com.intellij.psi.search.searches.ClassInheritorsSearch) NotNull(org.jetbrains.annotations.NotNull) DirectClassInheritorsSearch(com.intellij.psi.search.searches.DirectClassInheritorsSearch) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) NotNull(org.jetbrains.annotations.NotNull)

Example 17 with SearchScope

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

the class JavaDirectInheritorsSearcher method performSearchUsingCompilerIndices.

private static CompilerDirectHierarchyInfo performSearchUsingCompilerIndices(@NotNull DirectClassInheritorsSearch.SearchParameters parameters, @NotNull SearchScope useScope, @NotNull Project project) {
    if (!(useScope instanceof GlobalSearchScope))
        return null;
    SearchScope scope = parameters.getScope();
    if (!(scope instanceof GlobalSearchScope))
        return null;
    PsiClass searchClass = ReadAction.compute(() -> (PsiClass) PsiUtil.preferCompiledElement(parameters.getClassToProcess()));
    final CompilerReferenceService compilerReferenceService = CompilerReferenceService.getInstance(project);
    return compilerReferenceService.getDirectInheritors(searchClass, (GlobalSearchScope) useScope, (GlobalSearchScope) scope, JavaFileType.INSTANCE);
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) CompilerReferenceService(com.intellij.compiler.CompilerReferenceService)

Example 18 with SearchScope

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

the class JavaOverridingMethodsSearcher method execute.

@Override
public boolean execute(@NotNull final OverridingMethodsSearch.SearchParameters parameters, @NotNull final Processor<PsiMethod> consumer) {
    final PsiMethod method = parameters.getMethod();
    Project project = ReadAction.compute(method::getProject);
    final SearchScope searchScope = parameters.getScope();
    if (searchScope instanceof LocalSearchScope) {
        return processLocalScope((LocalSearchScope) searchScope, method, project, consumer);
    }
    Iterable<PsiMethod> cached = HighlightingCaches.getInstance(project).OVERRIDING_METHODS.get(method);
    if (cached == null) {
        cached = compute(method, project);
        // for non-physical elements ignore the cache completely because non-physical elements created so often/unpredictably so I can't figure out when to clear caches in this case
        if (ReadAction.compute(method::isPhysical)) {
            HighlightingCaches.getInstance(project).OVERRIDING_METHODS.put(method, cached);
        }
    }
    for (final PsiMethod subMethod : cached) {
        ProgressManager.checkCanceled();
        if (!ReadAction.compute(() -> PsiSearchScopeUtil.isInScope(searchScope, subMethod))) {
            continue;
        }
        if (!consumer.process(subMethod) || !parameters.isCheckDeep()) {
            return false;
        }
    }
    return true;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope)

Example 19 with SearchScope

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

the class AnnotatedElementsSearcher method execute.

@Override
public boolean execute(@NotNull final AnnotatedElementsSearch.Parameters p, @NotNull final Processor<PsiModifierListOwner> consumer) {
    final PsiClass annClass = p.getAnnotationClass();
    if (!annClass.isAnnotationType())
        throw new IllegalArgumentException("Annotation type should be passed to annotated members search but got: " + annClass);
    String annotationFQN = ApplicationManager.getApplication().runReadAction(new Computable<String>() {

        @Override
        public String compute() {
            return annClass.getQualifiedName();
        }
    });
    if (annotationFQN == null)
        throw new IllegalArgumentException("FQN is null for " + annClass);
    final PsiManager psiManager = ApplicationManager.getApplication().runReadAction(new Computable<PsiManager>() {

        @Override
        public PsiManager compute() {
            return annClass.getManager();
        }
    });
    final SearchScope useScope = p.getScope();
    final Class<? extends PsiModifierListOwner>[] types = p.getTypes();
    for (final PsiAnnotation ann : getAnnotationCandidates(annClass, useScope, psiManager.getProject())) {
        final PsiModifierListOwner candidate = ApplicationManager.getApplication().runReadAction(new Computable<PsiModifierListOwner>() {

            @Override
            public PsiModifierListOwner compute() {
                PsiElement parent = ann.getContext();
                if (!(parent instanceof PsiModifierList)) {
                    // Can be a PsiNameValuePair, if annotation is used to annotate annotation parameters
                    return null;
                }
                final PsiElement owner = parent.getParent();
                if (!isInstanceof(owner, types)) {
                    return null;
                }
                if (p.isApproximate()) {
                    return (PsiModifierListOwner) owner;
                }
                final PsiJavaCodeReferenceElement ref = ann.getNameReferenceElement();
                if (ref == null || !psiManager.areElementsEquivalent(ref.resolve(), annClass)) {
                    return null;
                }
                return (PsiModifierListOwner) owner;
            }
        });
        if (candidate != null && !consumer.process(candidate)) {
            return false;
        }
    }
    return true;
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope)

Example 20 with SearchScope

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

the class JavaCompilingVisitor method buildDescendants.

private static List<PsiClass> buildDescendants(String className, boolean includeSelf, OptimizingSearchHelper searchHelper, CompileContext context) {
    if (!searchHelper.doOptimizing())
        return Collections.emptyList();
    final SearchScope scope = context.getOptions().getScope();
    if (!(scope instanceof GlobalSearchScope))
        return Collections.emptyList();
    final PsiShortNamesCache cache = PsiShortNamesCache.getInstance(context.getProject());
    final PsiClass[] classes = cache.getClassesByName(className, (GlobalSearchScope) scope);
    final List<PsiClass> results = new ArrayList<>();
    final Processor<PsiClass> processor = aClass -> {
        results.add(aClass);
        return true;
    };
    for (PsiClass aClass : classes) {
        ClassInheritorsSearch.search(aClass, scope, true).forEach(processor);
    }
    if (includeSelf) {
        Collections.addAll(results, classes);
    }
    return results;
}
Also used : MalformedPatternException(com.intellij.structuralsearch.MalformedPatternException) CommentMatchingStrategy(com.intellij.structuralsearch.impl.matcher.strategies.CommentMatchingStrategy) ExprMatchingStrategy(com.intellij.structuralsearch.impl.matcher.strategies.ExprMatchingStrategy) JavaDocMatchingStrategy(com.intellij.structuralsearch.impl.matcher.strategies.JavaDocMatchingStrategy) NonNls(org.jetbrains.annotations.NonNls) SearchScope(com.intellij.psi.search.SearchScope) com.intellij.structuralsearch.impl.matcher.handlers(com.intellij.structuralsearch.impl.matcher.handlers) com.intellij.structuralsearch.impl.matcher.filters(com.intellij.structuralsearch.impl.matcher.filters) ArrayList(java.util.ArrayList) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) RegExpPredicate(com.intellij.structuralsearch.impl.matcher.predicates.RegExpPredicate) Matcher(java.util.regex.Matcher) JavaCompiledPattern(com.intellij.structuralsearch.impl.matcher.JavaCompiledPattern) PsiShortNamesCache(com.intellij.psi.search.PsiShortNamesCache) DocValuesIterator(com.intellij.structuralsearch.impl.matcher.iterators.DocValuesIterator) NodeIterator(com.intellij.dupLocator.iterators.NodeIterator) MatchingStrategy(com.intellij.structuralsearch.impl.matcher.strategies.MatchingStrategy) SSRBundle(com.intellij.structuralsearch.SSRBundle) CompiledPattern(com.intellij.structuralsearch.impl.matcher.CompiledPattern) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) Nullable(org.jetbrains.annotations.Nullable) PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) List(java.util.List) Processor(com.intellij.util.Processor) com.intellij.psi(com.intellij.psi) ClassInheritorsSearch(com.intellij.psi.search.searches.ClassInheritorsSearch) UnsupportedPatternException(com.intellij.structuralsearch.UnsupportedPatternException) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) PsiShortNamesCache(com.intellij.psi.search.PsiShortNamesCache) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ArrayList(java.util.ArrayList)

Aggregations

SearchScope (com.intellij.psi.search.SearchScope)90 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)39 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)36 Project (com.intellij.openapi.project.Project)21 NotNull (org.jetbrains.annotations.NotNull)21 VirtualFile (com.intellij.openapi.vfs.VirtualFile)18 PsiElement (com.intellij.psi.PsiElement)16 PsiClass (com.intellij.psi.PsiClass)8 Processor (com.intellij.util.Processor)8 com.intellij.psi (com.intellij.psi)6 PsiMethod (com.intellij.psi.PsiMethod)6 HashMap (com.intellij.util.containers.HashMap)6 ClassInheritorsSearch (com.intellij.psi.search.searches.ClassInheritorsSearch)5 Nullable (org.jetbrains.annotations.Nullable)5 AnalysisScope (com.intellij.analysis.AnalysisScope)4 ProgressManager (com.intellij.openapi.progress.ProgressManager)4 TextRange (com.intellij.openapi.util.TextRange)4 PsiFile (com.intellij.psi.PsiFile)4 PsiSearchHelper (com.intellij.psi.search.PsiSearchHelper)4 QueryExecutor (com.intellij.util.QueryExecutor)4