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;
}
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);
}
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;
}
});
}
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;
}
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);
}
Aggregations