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);
          }
        });
  }
  public void searchAndShowUsages(
      @NotNull UsageViewManager manager,
      @NotNull Factory<UsageSearcher> usageSearcherFactory,
      @NotNull final FindModel findModelCopy,
      @NotNull UsageViewPresentation presentation,
      @NotNull FindUsagesProcessPresentation processPresentation,
      final FindManager findManager) {
    presentation.setMergeDupLinesAvailable(false);
    final ReplaceContext[] context = new ReplaceContext[1];
    manager.searchAndShowUsages(
        new UsageTarget[] {
          new FindInProjectUtil.StringUsageTarget(myProject, findModelCopy.getStringToFind())
        },
        usageSearcherFactory,
        processPresentation,
        presentation,
        new UsageViewManager.UsageViewStateListener() {
          @Override
          public void usageViewCreated(@NotNull UsageView usageView) {
            context[0] = new ReplaceContext(usageView, findModelCopy);
            addReplaceActions(context[0]);
          }

          @Override
          public void findingUsagesFinished(final UsageView usageView) {
            if (context[0] != null && findManager.getFindInProjectModel().isPromptOnReplace()) {
              SwingUtilities.invokeLater(
                  new Runnable() {
                    @Override
                    public void run() {
                      replaceWithPrompt(context[0]);
                      context[0].invalidateExcludedSetCache();
                    }
                  });
            }
          }
        });
  }