Search in sources :

Example 31 with SearchScope

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

the class NonPhysicalReferenceSearcher method processQuery.

public void processQuery(@NotNull ReferencesSearch.SearchParameters queryParameters, @NotNull Processor<PsiReference> consumer) {
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        return;
    }
    final SearchScope scope = queryParameters.getScopeDeterminedByUser();
    final PsiElement element = queryParameters.getElementToSearch();
    final PsiFile containingFile = element.getContainingFile();
    if (!(scope instanceof GlobalSearchScope) && !isApplicableTo(containingFile)) {
        return;
    }
    final LocalSearchScope currentScope;
    if (scope instanceof LocalSearchScope) {
        if (queryParameters.isIgnoreAccessScope()) {
            return;
        }
        currentScope = (LocalSearchScope) scope;
    } else {
        currentScope = null;
    }
    Project project = element.getProject();
    if (!project.isInitialized()) {
        // skip default and other projects that look weird
        return;
    }
    final PsiManager psiManager = PsiManager.getInstance(project);
    for (VirtualFile virtualFile : FileEditorManager.getInstance(project).getOpenFiles()) {
        if (virtualFile.getFileType().isBinary()) {
            continue;
        }
        PsiFile file = psiManager.findFile(virtualFile);
        if (isApplicableTo(file)) {
            final LocalSearchScope fileScope = new LocalSearchScope(file);
            final LocalSearchScope searchScope = currentScope == null ? fileScope : fileScope.intersectWith(currentScope);
            ReferencesSearch.searchOptimized(element, searchScope, true, queryParameters.getOptimizer(), consumer);
        }
    }
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope)

Example 32 with SearchScope

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

the class JavaClassInheritorsSearcher method processInheritors.

private static boolean processInheritors(@NotNull final ClassInheritorsSearch.SearchParameters parameters, @NotNull final Processor<PsiClass> consumer) {
    @NotNull final PsiClass baseClass = parameters.getClassToProcess();
    if (baseClass instanceof PsiAnonymousClass || isFinal(baseClass))
        return true;
    final SearchScope searchScope = parameters.getScope();
    Project project = PsiUtilCore.getProjectInReadAction(baseClass);
    if (isJavaLangObject(baseClass)) {
        return AllClassesSearch.search(searchScope, project, parameters.getNameCondition()).forEach(aClass -> {
            ProgressManager.checkCanceled();
            return isJavaLangObject(aClass) || consumer.process(aClass);
        });
    }
    if (searchScope instanceof LocalSearchScope) {
        return processLocalScope(project, parameters, (LocalSearchScope) searchScope, baseClass, consumer);
    }
    Iterable<PsiClass> cached = getOrComputeSubClasses(project, baseClass, searchScope);
    for (final PsiClass subClass : cached) {
        ProgressManager.checkCanceled();
        if (subClass instanceof PsiAnonymousClass && !parameters.isIncludeAnonymous()) {
            continue;
        }
        if (ReadAction.compute(() -> checkCandidate(subClass, parameters) && !consumer.process(subClass))) {
            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) NotNull(org.jetbrains.annotations.NotNull)

Example 33 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 34 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 35 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)

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