public void searchAndShowUsages(
      @NotNull UsageViewManager manager,
      @NotNull Factory<UsageSearcher> usageSearcherFactory,
      @NotNull FindModel findModelCopy,
      @NotNull FindManager findManager) {
    final UsageViewPresentation presentation =
        FindInProjectUtil.setupViewPresentation(true, findModelCopy);
    final FindUsagesProcessPresentation processPresentation =
        FindInProjectUtil.setupProcessPresentation(myProject, true, presentation);

    searchAndShowUsages(
        manager,
        usageSearcherFactory,
        findModelCopy,
        presentation,
        processPresentation,
        findManager);
  }
  private static boolean isMaskValid(@NotNull String mask) {
    try {
      FindInProjectUtil.createFileMaskRegExp(mask);
    } catch (PatternSyntaxException e) {
      return false;
    }

    return true;
  }
  FindInProjectTask(
      @NotNull final FindModel findModel,
      @NotNull final Project project,
      @Nullable final PsiDirectory psiDirectory) {
    myFindModel = findModel;
    myProject = project;
    myPsiDirectory = psiDirectory;
    myPsiManager = PsiManager.getInstance(project);

    final String moduleName = findModel.getModuleName();
    myModule =
        moduleName == null
            ? null
            : ApplicationManager.getApplication()
                .runReadAction(
                    new Computable<Module>() {
                      @Override
                      public Module compute() {
                        return ModuleManager.getInstance(project).findModuleByName(moduleName);
                      }
                    });
    myFileIndex =
        myModule == null
            ? ProjectRootManager.getInstance(project).getFileIndex()
            : ModuleRootManager.getInstance(myModule).getFileIndex();

    final String filter = findModel.getFileFilter();
    final Pattern pattern = FindInProjectUtil.createFileMaskRegExp(filter);

    //noinspection unchecked
    myFileMask =
        pattern == null
            ? Condition.TRUE
            : new Condition<VirtualFile>() {
              @Override
              public boolean value(VirtualFile file) {
                return file != null && pattern.matcher(file.getName()).matches();
              }
            };

    final ProgressIndicator progress = ProgressManager.getInstance().getProgressIndicator();
    myProgress = progress != null ? progress : new EmptyProgressIndicator();
  }
  public void replaceInProject(@NotNull DataContext dataContext) {
    final FindManager findManager = FindManager.getInstance(myProject);
    final FindModel findModel = (FindModel) findManager.getFindInProjectModel().clone();
    findModel.setReplaceState(true);
    FindInProjectUtil.setDirectoryName(findModel, dataContext);

    Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
    FindUtil.initStringToFindWithSelection(findModel, editor);

    findManager.showFindDialog(
        findModel,
        new Runnable() {
          @Override
          public void run() {
            final PsiDirectory psiDirectory =
                FindInProjectUtil.getPsiDirectory(findModel, myProject);
            if (!findModel.isProjectScope()
                && psiDirectory == null
                && findModel.getModuleName() == null
                && findModel.getCustomScope() == null) {
              return;
            }

            UsageViewManager manager = UsageViewManager.getInstance(myProject);

            if (manager == null) return;
            findManager.getFindInProjectModel().copyFrom(findModel);
            final FindModel findModelCopy = (FindModel) findModel.clone();

            final UsageViewPresentation presentation =
                FindInProjectUtil.setupViewPresentation(true, findModelCopy);
            final FindUsagesProcessPresentation processPresentation =
                FindInProjectUtil.setupProcessPresentation(myProject, true, presentation);

            UsageSearcherFactory factory =
                new UsageSearcherFactory(findModelCopy, psiDirectory, processPresentation);
            searchAndShowUsages(
                manager, factory, findModelCopy, presentation, processPresentation, findManager);
          }
        });
  }
  private void searchInFiles(
      @NotNull Collection<PsiFile> psiFiles,
      @NotNull FindUsagesProcessPresentation processPresentation,
      @NotNull final Processor<UsageInfo> consumer) {
    int i = 0;
    long totalFilesSize = 0;
    int count = 0;

    for (final PsiFile psiFile : psiFiles) {
      final VirtualFile virtualFile = psiFile.getVirtualFile();
      final int index = i++;
      if (virtualFile == null) continue;

      long fileLength = UsageViewManagerImpl.getFileLength(virtualFile);
      if (fileLength == -1) continue; // Binary or invalid

      final boolean skipProjectFile =
          ProjectCoreUtil.isProjectOrWorkspaceFile(virtualFile)
              && !myFindModel.isSearchInProjectFiles();
      if (skipProjectFile && !Registry.is("find.search.in.project.files")) continue;

      if (fileLength > SINGLE_FILE_SIZE_LIMIT) {
        myLargeFiles.add(psiFile);
        continue;
      }

      myProgress.checkCanceled();
      myProgress.setFraction((double) index / psiFiles.size());
      String text =
          FindBundle.message(
              "find.searching.for.string.in.file.progress",
              myFindModel.getStringToFind(),
              virtualFile.getPresentableUrl());
      myProgress.setText(text);
      myProgress.setText2(
          FindBundle.message("find.searching.for.string.in.file.occurrences.progress", count));

      int countInFile =
          FindInProjectUtil.processUsagesInFile(
              psiFile,
              myFindModel,
              new Processor<UsageInfo>() {
                @Override
                public boolean process(UsageInfo info) {
                  return skipProjectFile || consumer.process(info);
                }
              });

      if (countInFile > 0 && skipProjectFile) {
        processPresentation.projectFileUsagesFound(
            new Runnable() {
              @Override
              public void run() {
                FindModel model = myFindModel.clone();
                model.setSearchInProjectFiles(true);
                FindInProjectManager.getInstance(myProject).startFindInProject(model);
              }
            });
        continue;
      }

      count += countInFile;
      if (countInFile > 0) {
        totalFilesSize += fileLength;
        if (totalFilesSize > FILES_SIZE_LIMIT && !myWarningShown) {
          myWarningShown = true;
          String message =
              FindBundle.message(
                  "find.excessive.total.size.prompt",
                  UsageViewManagerImpl.presentableSize(totalFilesSize),
                  ApplicationNamesInfo.getInstance().getProductName());
          UsageLimitUtil.showAndCancelIfAborted(
              myProject, message, processPresentation.getUsageViewPresentation());
        }
      }
    }
  }