private void replaceUsagesUnderCommand(@NotNull ReplaceContext replaceContext, @NotNull Set<? extends Usage> usagesSet,
                                         boolean replaceAll) {
    if (usagesSet.isEmpty()) {
      return;
    }

    final List<Usage> usages = new ArrayList<>(usagesSet);
    usages.sort(UsageViewImpl.USAGE_COMPARATOR_BY_FILE_AND_OFFSET);

    if (!ensureUsagesWritable(usages)) return;

    Runnable runnable = () -> {
      final boolean success = replaceUsages(replaceContext, usages);
      final UsageView usageView = replaceContext.getUsageView();

      if (closeUsageViewIfEmpty(usageView, success)) return;
      IdeFocusManager.getGlobalInstance().doWhenFocusSettlesDown(
        () -> IdeFocusManager.getGlobalInstance().requestFocus(usageView.getPreferredFocusableComponent(), true));
    };
    if (replaceAll) {
      FindModel findModel = replaceContext.getFindModel();
      LocalHistoryAction action =
        LocalHistory.getInstance().startAction(FindBundle.message("find.in.files.replace.all.local.history.action",
                                                                  findModel.getStringToFind(),
                                                                  findModel.getStringToReplace(),
                                                                  FindInProjectUtil.getTitleForScope(findModel)));
      try {
        CommandProcessor.getInstance().executeCommand(myProject, runnable, FindBundle.message("find.replace.command"), null);
      }
      finally {
        action.finish();
      }
    }
    else {
      CommandProcessor.getInstance().executeCommand(myProject, runnable, FindBundle.message("find.replace.command"), null);
    }

    replaceContext.invalidateExcludedSetCache();
  }
private void replaceUsagesUnderCommand(@NotNull ReplaceContext replaceContext, @NotNull Set<? extends Usage> usagesSet,
                                         boolean replaceAll)