private void doRefactoring(@NotNull final Collection<UsageInfo> usageInfoSet) {
   for (Iterator<UsageInfo> iterator = usageInfoSet.iterator(); iterator.hasNext();) {
      UsageInfo usageInfo = iterator.next();
      final PsiElement element = usageInfo.getElement();
      if (element == null || !isToBeChanged(usageInfo)) {
        iterator.remove();
      }
    }

    String commandName = getCommandName();
    LocalHistoryAction action = LocalHistory.getInstance().startAction(commandName);

    final UsageInfo[] writableUsageInfos = usageInfoSet.toArray(UsageInfo.EMPTY_ARRAY);
    final String refactoringId = getRefactoringId();
    try {
      if (refactoringId != null) {
        RefactoringEventData data = getBeforeData();
        if (data != null) {
          data.addUsages(Arrays.asList(writableUsageInfos));
        }
        myProject.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(refactoringId, data);
      }

      PsiDocumentManager.getInstance(myProject).commitAllDocuments();
      RefactoringListenerManagerImpl listenerManager = (RefactoringListenerManagerImpl)RefactoringListenerManager.getInstance(myProject);
      myTransaction = listenerManager.startTransaction();
      final Map<RefactoringHelper, Object> preparedData = new LinkedHashMap<>();
      final Runnable prepareHelpersRunnable = () -> {
        RefactoringEventData data = ReadAction.compute(() -> getBeforeData());
        PsiElement[] elements = data != null ? data.getUserData(RefactoringEventData.PSI_ELEMENT_ARRAY_KEY) : null;
        PsiElement primaryElement = data != null ? data.getUserData(RefactoringEventData.PSI_ELEMENT_KEY) : null;
        PsiElement[] allElements = elements != null ? ArrayUtil.append(elements, primaryElement) : new PsiElement[]{primaryElement};
        for (final RefactoringHelper helper : RefactoringHelper.EP_NAME.getExtensionList()) {
          Object operation = ReadAction.compute(() -> {
            return helper.prepareOperation(writableUsageInfos, ContainerUtil.filter(allElements, e -> e != null));
          });
          preparedData.put(helper, operation);
        }
      };

      ProgressManager.getInstance().runProcessWithProgressSynchronously(prepareHelpersRunnable,
                                                                        RefactoringBundle.message("refactoring.prepare.progress"), false, myProject);

      if (refactoringId != null) {
        UndoManager.getInstance(myProject).undoableActionPerformed(new UndoRefactoringAction(myProject, refactoringId));
      }

      ApplicationEx app = ApplicationManagerEx.getApplicationEx();
      if (Registry.is("run.refactorings.under.progress")) {
        if (!app.runWriteActionWithCancellableProgressInDispatchThread(commandName, myProject, null,
                                                                       indicator -> performRefactoring(writableUsageInfos))) {
          return;
        }
      }
      else {
        app.runWriteAction(() -> performRefactoring(writableUsageInfos));
      }

      DumbService.getInstance(myProject).completeJustSubmittedTasks();

      for(Map.Entry<RefactoringHelper, Object> e: preparedData.entrySet()) {
        //noinspection unchecked
        e.getKey().performOperation(myProject, e.getValue());
      }
      myTransaction.commit();
      if (Registry.is("run.refactorings.under.progress")) {
        if (!app.runWriteActionWithCancellableProgressInDispatchThread(commandName, myProject, null,
                                                                       indicator -> performPsiSpoilingRefactoring())) {
          return;
        }
      }
      else {
        app.runWriteAction(this::performPsiSpoilingRefactoring);
      }
    }
    finally {
      if (refactoringId != null) {
        myProject.getMessageBus()
          .syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone(refactoringId, getAfterData(writableUsageInfos));
      }
      action.finish();
      myUsageView = null;
    }

    int count = writableUsageInfos.length;
    if (count > 0) {
      RefactoringUiService.getInstance().setStatusBarInfo(myProject, RefactoringBundle.message("statusBar.refactoring.result", count));
    }
    else {
      if (!isPreviewUsages(writableUsageInfos)) {
        RefactoringUiService.getInstance().setStatusBarInfo(myProject, RefactoringBundle.message("statusBar.noUsages"));
      }
    }
  }
private void doRefactoring(@NotNull final Collection<UsageInfo> usageInfoSet)