com.intellij.openapi.application.ReadActionProcessor Java Examples

The following examples show how to use com.intellij.openapi.application.ReadActionProcessor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PsiSearchHelperImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
private static Processor<PsiElement> localProcessor(@Nonnull final BulkOccurrenceProcessor processor,
                                                    @Nonnull final ProgressIndicator progress,
                                                    @Nonnull final StringSearcher searcher) {
  return new ReadActionProcessor<PsiElement>() {
    @Override
    public boolean processInReadAction(PsiElement scopeElement) {
      if (scopeElement instanceof PsiCompiledElement) {
        // can't scan text of the element
        return true;
      }

      return scopeElement.isValid() &&
             processor.execute(scopeElement, LowLevelSearchUtil.getTextOccurrencesInScope(scopeElement, searcher, progress), searcher);
    }

    @Override
    public String toString() {
      return processor.toString();
    }
  };
}
 
Example #2
Source File: QuerySearchRequest.java    From consulo with Apache License 2.0 6 votes vote down vote up
public QuerySearchRequest(@Nonnull Query<PsiReference> query,
                          @Nonnull final SearchRequestCollector collector,
                          boolean inReadAction,
                          @Nonnull final PairProcessor<? super PsiReference, ? super SearchRequestCollector> processor) {
  this.query = query;
  this.collector = collector;
  if (inReadAction) {
    this.processor = new ReadActionProcessor<PsiReference>() {
      @Override
      public boolean processInReadAction(PsiReference psiReference) {
        return processor.process(psiReference, collector);
      }
    };
  }
  else {
    this.processor = psiReference -> processor.process(psiReference, collector);
  }
}
 
Example #3
Source File: CSharpChangeSignatureUsageProcessor.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public UsageInfo[] findUsages(@Nonnull final ChangeInfo info)
{
	if(!(info instanceof CSharpChangeInfo))
	{
		return UsageInfo.EMPTY_ARRAY;
	}
	final List<UsageInfo> list = new ArrayList<UsageInfo>();

	final ReadActionProcessor<PsiReference> refProcessor = new ReadActionProcessor<PsiReference>()
	{
		@RequiredReadAction
		@Override
		public boolean processInReadAction(final PsiReference ref)
		{
			final PsiElement resolve = ref.resolve();
			if(resolve != info.getMethod())
			{
				return true;
			}
			TextRange rangeInElement = ref.getRangeInElement();
			list.add(new UsageInfo(ref.getElement(), rangeInElement.getStartOffset(), rangeInElement.getEndOffset(), false));
			return true;
		}
	};

	ReferencesSearch.search(new ReferencesSearch.SearchParameters(info.getMethod(), info.getMethod().getResolveScope(), false)).forEach(refProcessor);
	return list.toArray(UsageInfo.EMPTY_ARRAY);
}
 
Example #4
Source File: HaxeFindUsagesHandler.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
@NotNull
private static ReadActionProcessor<PsiReference> getSimpleSearchProcessor(@NotNull Processor<UsageInfo> processor) {
  return 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));
    }
  };
}
 
Example #5
Source File: HaxeFindUsagesHandler.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
@NotNull
private static ReadActionProcessor<PsiReference> getConstructorSearchProcessor(@NotNull Processor<UsageInfo> processor) {
  return new ReadActionProcessor<PsiReference>() {
    @Override
    public boolean processInReadAction(PsiReference reference) {
      PsiElement refElement = reference.getElement();
      final PsiElement parent = refElement.getParent();
      if (parent instanceof HaxeType && parent.getParent() instanceof HaxeNewExpression) {
        TextRange rangeInElement = reference.getRangeInElement();
        processor.process(new UsageInfo(refElement, rangeInElement.getStartOffset(), rangeInElement.getEndOffset(), false));
      }
      return true;
    }
  };
}
 
Example #6
Source File: AbstractQuery.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static <T> Query<T> wrapInReadAction(@Nonnull final Query<? extends T> query) {
  return new AbstractQuery<T>() {
    @Override
    protected boolean processResults(@Nonnull Processor<? super T> consumer) {
      return AbstractQuery.delegateProcessResults(query, ReadActionProcessor.wrapInReadAction(consumer));
    }
  };
}
 
Example #7
Source File: IndexCacheManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean processFilesWithWord(@Nonnull final Processor<PsiFile> psiFileProcessor, @Nonnull final String word, final short occurrenceMask, @Nonnull final GlobalSearchScope scope, final boolean caseSensitively) {
  final List<VirtualFile> vFiles = new ArrayList<VirtualFile>(5);
  collectVirtualFilesWithWord(new CommonProcessors.CollectProcessor<VirtualFile>(vFiles), word, occurrenceMask, scope, caseSensitively);
  if (vFiles.isEmpty()) return true;

  final Processor<VirtualFile> virtualFileProcessor = new ReadActionProcessor<VirtualFile>() {
    @RequiredReadAction
    @Override
    public boolean processInReadAction(VirtualFile virtualFile) {
      if (virtualFile.isValid()) {
        final PsiFile psiFile = myPsiManager.findFile(virtualFile);
        return psiFile == null || psiFileProcessor.process(psiFile);
      }
      return true;
    }
  };


  // IMPORTANT!!!
  // Since implementation of virtualFileProcessor.process() may call indices directly or indirectly,
  // we cannot call it inside FileBasedIndex.processValues() method
  // If we do, deadlocks are possible (IDEADEV-42137). So first we obtain files with the word specified,
  // and then process them not holding indices' read lock.
  for (VirtualFile vFile : vFiles) {
    ProgressIndicatorProvider.checkCanceled();
    if (!virtualFileProcessor.process(vFile)) {
      return false;
    }
  }
  return true;
}
 
Example #8
Source File: FindUsagesHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
public boolean processElementUsages(@Nonnull final PsiElement element,
                                    @Nonnull final Processor<UsageInfo> processor,
                                    @Nonnull 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;
}
 
Example #9
Source File: HaxeFindUsagesHandler.java    From intellij-haxe with Apache License 2.0 4 votes vote down vote up
@Override
public boolean processElementUsages(@NotNull PsiElement element,
                                    @NotNull Processor<UsageInfo> processor,
                                    @NotNull FindUsagesOptions options) {
  final SearchScope scope = options.searchScope;

  final boolean searchText = options.isSearchForTextOccurrences && scope instanceof GlobalSearchScope;

  if (options.isUsages) {
    ReadActionProcessor<PsiReference> searchProcessor = null;
    PsiElement searchElement = element;
    boolean fastTrack = true;

    if (element instanceof HaxeMethodDeclaration) {
      final HaxeMethodDeclaration method = (HaxeMethodDeclaration)element;
      final HaxeMethodModel methodModel = method.getModel();
      final HaxeClassModel declaringClass = methodModel.getDeclaringClass();

      if (method.isConstructor()) {
        searchElement = declaringClass.haxeClass;
        searchProcessor = getConstructorSearchProcessor(processor);
        fastTrack = false;
      }
    }

    if (searchProcessor == null) {
      searchProcessor = getSimpleSearchProcessor(processor);
    }

    final ReferencesSearch.SearchParameters parameters =
      new ReferencesSearch.SearchParameters(searchElement, scope, false, fastTrack ? options.fastTrack : null);
    final boolean success = ReferencesSearch.search(parameters).forEach(searchProcessor);

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