<commit_before>/*
 * @author max
 */
package com.intellij.psi.impl.search;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ContentIterator;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.psi.impl.PsiManagerImpl;
import com.intellij.psi.impl.cache.RepositoryManager;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.LocalSearchScope;
import com.intellij.psi.search.SearchScope;
import com.intellij.psi.search.searches.AllClassesSearch;
import com.intellij.util.Processor;
import com.intellij.util.QueryExecutor;

public class AllClassesSearchExecutor implements QueryExecutor<PsiClass, AllClassesSearch.SearchParameters> {
  public boolean execute(final AllClassesSearch.SearchParameters queryParameters, final Processor<PsiClass> consumer) {
    SearchScope scope = queryParameters.getScope();

    if (scope instanceof GlobalSearchScope) {
      return processAllClassesInGlobalScope((GlobalSearchScope)scope, consumer, queryParameters.getProject());
    }

    PsiElement[] scopeRoots = ((LocalSearchScope)scope).getScope();
    for (final PsiElement scopeRoot : scopeRoots) {
      if (!processScopeRootForAllClasses(scopeRoot, consumer)) return false;
    }
    return true;
  }

  private static boolean processAllClassesInGlobalScope(final GlobalSearchScope searchScope, final Processor<PsiClass> processor, final Project project) {
    final PsiManagerImpl psiManager = (PsiManagerImpl)PsiManager.getInstance(project);
    final RepositoryManager repositoryManager = psiManager.getRepositoryManager();
    repositoryManager.updateAll();

    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(psiManager.getProject()).getFileIndex();
    return fileIndex.iterateContent(new ContentIterator() {
      public boolean processFile(final VirtualFile fileOrDir) {
        return ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {
          public Boolean compute() {
            if (!fileOrDir.isDirectory() && searchScope.contains(fileOrDir)) {
              final PsiFile psiFile = psiManager.findFile(fileOrDir);
              if (psiFile instanceof PsiJavaFile) {
                long fileId = repositoryManager.getFileId(fileOrDir);
                if (fileId >= 0) {
                  long[] allClasses = repositoryManager.getFileView().getAllClasses(fileId);
                  for (long allClass : allClasses) {
                    PsiClass psiClass = (PsiClass)psiManager.getRepositoryElementsManager().findOrCreatePsiElementById(allClass);
                    if (!processor.process(psiClass)) return false;
                  }
                }
                else {
                  if (!processScopeRootForAllClasses(psiFile, processor)) return false;
                }
              }
            }
            return true;
          }
        }).booleanValue();
      }
    });
  }

  private static boolean processScopeRootForAllClasses(PsiElement scopeRoot, final Processor<PsiClass> processor) {
    if (scopeRoot == null) return true;
    final boolean[] stopped = new boolean[]{false};

    scopeRoot.accept(new JavaRecursiveElementVisitor() {
      @Override public void visitReferenceExpression(PsiReferenceExpression expression) {
        if (!stopped[0]) {
          visitElement(expression);
        }<commit_msg>intergrate GRVY-996<commit_after>/*
 * @author max
 */
package com.intellij.psi.impl.search;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ContentIterator;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.psi.impl.PsiManagerImpl;
import com.intellij.psi.impl.cache.RepositoryManager;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.LocalSearchScope;
import com.intellij.psi.search.SearchScope;
import com.intellij.psi.search.searches.AllClassesSearch;
import com.intellij.util.Processor;
import com.intellij.util.QueryExecutor;

public class AllClassesSearchExecutor implements QueryExecutor<PsiClass, AllClassesSearch.SearchParameters> {
  public boolean execute(final AllClassesSearch.SearchParameters queryParameters, final Processor<PsiClass> consumer) {
    SearchScope scope = queryParameters.getScope();

    if (scope instanceof GlobalSearchScope) {
      return processAllClassesInGlobalScope((GlobalSearchScope)scope, consumer, queryParameters.getProject());
    }

    PsiElement[] scopeRoots = ((LocalSearchScope)scope).getScope();
    for (final PsiElement scopeRoot : scopeRoots) {
      if (!processScopeRootForAllClasses(scopeRoot, consumer)) return false;
    }
    return true;
  }

  private static boolean processAllClassesInGlobalScope(final GlobalSearchScope searchScope, final Processor<PsiClass> processor, final Project project) {
    final PsiManagerImpl psiManager = (PsiManagerImpl)PsiManager.getInstance(project);
    final RepositoryManager repositoryManager = psiManager.getRepositoryManager();
    repositoryManager.updateAll();

    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(psiManager.getProject()).getFileIndex();
    return fileIndex.iterateContent(new ContentIterator() {
      public boolean processFile(final VirtualFile fileOrDir) {
        return ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {
          public Boolean compute() {
            if (!fileOrDir.isDirectory() && searchScope.contains(fileOrDir)) {
              final PsiFile psiFile = psiManager.findFile(fileOrDir);
              if (psiFile instanceof PsiJavaFile) {
                long fileId = repositoryManager.getFileId(fileOrDir);
                if (fileId >= 0) {
                  long[] allClasses = repositoryManager.getFileView().getAllClasses(fileId);
                  for (long allClass : allClasses) {
                    PsiClass psiClass = (PsiClass)psiManager.getRepositoryElementsManager().findOrCreatePsiElementById(allClass);
                    if (!processor.process(psiClass)) return false;
                  }
                }
                else {
                  if (!processScopeRootForAllClasses(psiFile, processor)) return false;
                }
              } else if (psiFile instanceof PsiClassOwner) {
                for (PsiClass aClass : ((PsiClassOwner)psiFile).getClasses()) {
                  if (!processor.process(aClass)) return false;
                }
              }
            }
            return true;
          }
        }).booleanValue();
      }
    });
  }

  private static boolean processScopeRootForAllClasses(PsiElement scopeRoot, final Processor<PsiClass> processor) {
    if (scopeRoot == null) return true;
    final boolean[] stopped = new boolean[]{false};

    scopeRoot.accept(new JavaRecursiveElementVisitor() {
      @Override public void visitReferenceExpression(PsiReferenceExpression expression) {
        if (!stopped[0]) {
          visitElement(expression);
        }
{'fraction_non_alphanumeric': 0.09202206376967577, 'fraction_numerical': 0.0009417462666487286, 'mean_word_length': 3.56353591160221, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6743564', 'n_tokens_mistral': 1974, 'n_tokens_neox': 1919, 'n_words': 382}