Search in sources :

Example 1 with ReadActionProcessor

use of com.intellij.openapi.application.ReadActionProcessor in project intellij-community by JetBrains.

the class IndexCacheManagerImpl method processFilesWithWord.

@Override
public boolean processFilesWithWord(@NotNull final Processor<PsiFile> psiFileProcessor, @NotNull final String word, final short occurrenceMask, @NotNull final GlobalSearchScope scope, final boolean caseSensitively) {
    final List<VirtualFile> result = new ArrayList<>(5);
    Processor<VirtualFile> processor = Processors.cancelableCollectProcessor(result);
    collectVirtualFilesWithWord(word, occurrenceMask, scope, caseSensitively, processor);
    if (result.isEmpty())
        return true;
    final Processor<VirtualFile> virtualFileProcessor = new ReadActionProcessor<VirtualFile>() {

        @Override
        public boolean processInReadAction(VirtualFile virtualFile) {
            if (virtualFile.isValid()) {
                final PsiFile psiFile = myPsiManager.findFile(virtualFile);
                return psiFile == null || psiFileProcessor.process(psiFile);
            }
            return true;
        }
    };
    // and then process them not holding indices' read lock.
    for (VirtualFile vFile : result) {
        ProgressIndicatorProvider.checkCanceled();
        if (!virtualFileProcessor.process(vFile)) {
            return false;
        }
    }
    return true;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ArrayList(java.util.ArrayList) PsiFile(com.intellij.psi.PsiFile) ReadActionProcessor(com.intellij.openapi.application.ReadActionProcessor)

Example 2 with ReadActionProcessor

use of com.intellij.openapi.application.ReadActionProcessor in project intellij-community by JetBrains.

the class JavaFindUsagesHelper method addElementUsages.

private static boolean addElementUsages(@NotNull final PsiElement element, @NotNull final FindUsagesOptions options, @NotNull final Processor<UsageInfo> processor) {
    final SearchScope searchScope = options.searchScope;
    final PsiClass[] parentClass = new PsiClass[1];
    if (element instanceof PsiMethod && ReadAction.compute(() -> {
        PsiMethod method = (PsiMethod) element;
        parentClass[0] = method.getContainingClass();
        return method.isConstructor();
    })) {
        PsiMethod method = (PsiMethod) element;
        if (parentClass[0] != null) {
            boolean strictSignatureSearch = !(options instanceof JavaMethodFindUsagesOptions) || !((JavaMethodFindUsagesOptions) options).isIncludeOverloadUsages;
            return MethodReferencesSearch.search(new MethodReferencesSearch.SearchParameters(method, searchScope, strictSignatureSearch, options.fastTrack)).forEach(new ReadActionProcessor<PsiReference>() {

                @Override
                public boolean processInReadAction(final PsiReference ref) {
                    return addResult(ref, options, processor);
                }
            });
        }
        return true;
    }
    final ReadActionProcessor<PsiReference> consumer = new ReadActionProcessor<PsiReference>() {

        @Override
        public boolean processInReadAction(final PsiReference ref) {
            return addResult(ref, options, processor);
        }
    };
    if (element instanceof PsiMethod) {
        final boolean strictSignatureSearch = // field with getter
        !(options instanceof JavaMethodFindUsagesOptions) || !((JavaMethodFindUsagesOptions) options).isIncludeOverloadUsages;
        return MethodReferencesSearch.search(new MethodReferencesSearch.SearchParameters((PsiMethod) element, searchScope, strictSignatureSearch, options.fastTrack)).forEach(consumer);
    }
    return ReferencesSearch.search(new ReferencesSearch.SearchParameters(element, searchScope, false, options.fastTrack)).forEach(consumer);
}
Also used : SearchScope(com.intellij.psi.search.SearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ReadActionProcessor(com.intellij.openapi.application.ReadActionProcessor)

Example 3 with ReadActionProcessor

use of com.intellij.openapi.application.ReadActionProcessor in project intellij-community by JetBrains.

the class GroovyConstructorUsagesSearcher method processConstructorUsages.

static void processConstructorUsages(final PsiMethod constructor, final SearchScope searchScope, final Processor<PsiReference> consumer, final SearchRequestCollector collector, final boolean includeOverloads) {
    if (!constructor.isConstructor())
        return;
    final PsiClass clazz = constructor.getContainingClass();
    if (clazz == null)
        return;
    SearchScope onlyGroovy = GroovyScopeUtil.restrictScopeToGroovyFiles(searchScope, GroovyScopeUtil.getEffectiveScope(constructor));
    Set<PsiClass> processed = collector.getSearchSession().getUserData(LITERALLY_CONSTRUCTED_CLASSES);
    if (processed == null) {
        collector.getSearchSession().putUserData(LITERALLY_CONSTRUCTED_CLASSES, processed = ContainerUtil.newConcurrentSet());
    }
    if (!processed.add(clazz))
        return;
    if (clazz.isEnum() && clazz instanceof GroovyPsiElement) {
        for (PsiField field : clazz.getFields()) {
            if (field instanceof GrEnumConstant) {
                final PsiReference ref = field.getReference();
                if (ref != null && ref.isReferenceTo(constructor)) {
                    if (!consumer.process(ref))
                        return;
                }
            }
        }
    }
    final LiteralConstructorSearcher literalProcessor = new LiteralConstructorSearcher(constructor, consumer, includeOverloads);
    final Processor<GrNewExpression> newExpressionProcessor = grNewExpression -> {
        final PsiMethod resolvedConstructor = grNewExpression.resolveMethod();
        if (includeOverloads || constructor.getManager().areElementsEquivalent(resolvedConstructor, constructor)) {
            return consumer.process(grNewExpression.getReferenceElement());
        }
        return true;
    };
    processGroovyClassUsages(clazz, searchScope, collector, newExpressionProcessor, literalProcessor);
    //this()
    if (clazz instanceof GrTypeDefinition) {
        if (!processConstructors(constructor, consumer, clazz, true)) {
            return;
        }
    }
    //super()
    DirectClassInheritorsSearch.search(clazz, onlyGroovy).forEach(new ReadActionProcessor<PsiClass>() {

        @Override
        public boolean processInReadAction(PsiClass inheritor) {
            if (inheritor instanceof GrTypeDefinition) {
                if (!processConstructors(constructor, consumer, inheritor, false))
                    return false;
            }
            return true;
        }
    });
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) SearchScope(com.intellij.psi.search.SearchScope) ContainerUtil(com.intellij.util.containers.ContainerUtil) ControlFlowUtils(org.jetbrains.plugins.groovy.codeInspection.utils.ControlFlowUtils) GrConstructorInvocation(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrConstructorInvocation) PairProcessor(com.intellij.util.PairProcessor) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) ReadActionProcessor(com.intellij.openapi.application.ReadActionProcessor) MethodReferencesSearch(com.intellij.psi.search.searches.MethodReferencesSearch) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) LightMemberReference(com.intellij.psi.impl.light.LightMemberReference) SearchRequestCollector(com.intellij.psi.search.SearchRequestCollector) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) Key(com.intellij.openapi.util.Key) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) Set(java.util.Set) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) TextRange(com.intellij.openapi.util.TextRange) GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrSafeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrSafeCastExpression) QueryExecutorBase(com.intellij.openapi.application.QueryExecutorBase) GrTypeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrTypeCastExpression) Nullable(org.jetbrains.annotations.Nullable) GrEnumConstant(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant) Processor(com.intellij.util.Processor) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) DirectClassInheritorsSearch(com.intellij.psi.search.searches.DirectClassInheritorsSearch) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrEnumConstant(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant) GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) SearchScope(com.intellij.psi.search.SearchScope)

Example 4 with ReadActionProcessor

use of com.intellij.openapi.application.ReadActionProcessor in project intellij-community by JetBrains.

the class FindUsagesHandler method processElementUsages.

public boolean processElementUsages(@NotNull final PsiElement element, @NotNull final Processor<UsageInfo> processor, @NotNull final FindUsagesOptions options) {
    final ReadActionProcessor<PsiReference> refProcessor = new ReadActionProcessor<PsiReference>() {

        @Override
        public boolean processInReadAction(final PsiReference ref) {
            TextRange rangeInElement = ref.getRangeInElement();
            return processor.process(new UsageInfo(ref.getElement(), rangeInElement.getStartOffset(), rangeInElement.getEndOffset(), false));
        }
    };
    final SearchScope scope = options.searchScope;
    final boolean searchText = options.isSearchForTextOccurrences && scope instanceof GlobalSearchScope;
    if (options.isUsages) {
        boolean success = ReferencesSearch.search(new ReferencesSearch.SearchParameters(element, scope, false, options.fastTrack)).forEach(refProcessor);
        if (!success)
            return false;
    }
    if (searchText) {
        if (options.fastTrack != null) {
            options.fastTrack.searchCustom(consumer -> processUsagesInText(element, processor, (GlobalSearchScope) scope));
        } else {
            return processUsagesInText(element, processor, (GlobalSearchScope) scope);
        }
    }
    return true;
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) UsageInfo(com.intellij.usageView.UsageInfo) ReadActionProcessor(com.intellij.openapi.application.ReadActionProcessor)

Example 5 with ReadActionProcessor

use of com.intellij.openapi.application.ReadActionProcessor in project intellij-community by JetBrains.

the class PsiSearchHelperImpl method bulkProcessElementsWithWord.

private boolean bulkProcessElementsWithWord(@NotNull SearchScope searchScope, @NotNull final String text, final short searchContext, @NotNull EnumSet<Options> options, @Nullable String containerName, @NotNull final BulkOccurrenceProcessor processor) {
    if (text.isEmpty()) {
        throw new IllegalArgumentException("Cannot search for elements with empty text");
    }
    final ProgressIndicator progress = getOrCreateIndicator();
    if (searchScope instanceof GlobalSearchScope) {
        StringSearcher searcher = new StringSearcher(text, options.contains(Options.CASE_SENSITIVE_SEARCH), true, searchContext == UsageSearchContext.IN_STRINGS, options.contains(Options.PROCESS_ONLY_JAVA_IDENTIFIERS_IF_POSSIBLE));
        return processElementsWithTextInGlobalScope(processor, (GlobalSearchScope) searchScope, searcher, searchContext, options.contains(Options.CASE_SENSITIVE_SEARCH), containerName, progress);
    }
    LocalSearchScope scope = (LocalSearchScope) searchScope;
    PsiElement[] scopeElements = scope.getScope();
    final StringSearcher searcher = new StringSearcher(text, options.contains(Options.CASE_SENSITIVE_SEARCH), true, searchContext == UsageSearchContext.IN_STRINGS, options.contains(Options.PROCESS_ONLY_JAVA_IDENTIFIERS_IF_POSSIBLE));
    ReadActionProcessor<PsiElement> localProcessor = new ReadActionProcessor<PsiElement>() {

        @Override
        public boolean processInReadAction(PsiElement scopeElement) {
            if (!scopeElement.isValid())
                return true;
            if (!scopeElement.isPhysical() || scopeElement instanceof PsiCompiledElement) {
                scopeElement = scopeElement.getNavigationElement();
            }
            if (scopeElement instanceof PsiCompiledElement) {
                // can't scan text of the element
                return true;
            }
            if (scopeElement.getTextRange() == null) {
                // clients can put whatever they want to the LocalSearchScope. Skip what we can't process.
                LOG.debug("Element " + scopeElement + " of class " + scopeElement.getClass() + " has null range");
                return true;
            }
            return processor.execute(scopeElement, LowLevelSearchUtil.getTextOccurrencesInScope(scopeElement, searcher, progress), searcher);
        }

        @Override
        public String toString() {
            return processor.toString();
        }
    };
    return JobLauncher.getInstance().invokeConcurrentlyUnderProgress(Arrays.asList(scopeElements), progress, true, true, localProcessor);
}
Also used : EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) StringSearcher(com.intellij.util.text.StringSearcher) ReadActionProcessor(com.intellij.openapi.application.ReadActionProcessor)

Aggregations

ReadActionProcessor (com.intellij.openapi.application.ReadActionProcessor)6 SearchScope (com.intellij.psi.search.SearchScope)3 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 TextRange (com.intellij.openapi.util.TextRange)2 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)2 NotNull (org.jetbrains.annotations.NotNull)2 PluginException (com.intellij.diagnostic.PluginException)1 ChooseByNameContributor (com.intellij.navigation.ChooseByNameContributor)1 ChooseByNameContributorEx (com.intellij.navigation.ChooseByNameContributorEx)1 QueryExecutorBase (com.intellij.openapi.application.QueryExecutorBase)1 EmptyProgressIndicator (com.intellij.openapi.progress.EmptyProgressIndicator)1 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)1 IndexNotReadyException (com.intellij.openapi.project.IndexNotReadyException)1 Key (com.intellij.openapi.util.Key)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 com.intellij.psi (com.intellij.psi)1 PsiFile (com.intellij.psi.PsiFile)1 PsiReference (com.intellij.psi.PsiReference)1 LightMemberReference (com.intellij.psi.impl.light.LightMemberReference)1 SearchRequestCollector (com.intellij.psi.search.SearchRequestCollector)1