private static <E extends IncorrectOperationException> void runWriteAction(@NotNull Project project,
                                                                             @NotNull @NlsContexts.ProgressTitle String title,
                                                                             @NotNull ThrowableConsumer<@Nullable ProgressIndicator, E> consumer) throws E {
    if (Registry.is("run.refactorings.under.progress")){
      ApplicationEx application = ApplicationManagerEx.getApplicationEx();
      AtomicReference<Throwable> thrown = new AtomicReference<>();
      if (!application.runWriteActionWithCancellableProgressInDispatchThread(title, project, null, progress -> {
        try {
          consumer.consume(progress);
        }
        catch (ProcessCanceledException e) {
          throw e;
        }
        catch (Throwable e) {
          thrown.set(e);
        }
      })) {
        return;
      }
      Throwable throwable = thrown.get();
      if (throwable instanceof IncorrectOperationException) {
        throw (IncorrectOperationException) throwable;
      } else if (throwable != null) {
        throw new IncorrectOperationException(throwable);
      }
    }
    else {
      WriteAction.run(() -> consumer.consume(null));
    }
  }
private static <E extends IncorrectOperationException> void runWriteAction(@NotNull Project project,
                                                                             @NotNull @NlsContexts.ProgressTitle String title,
                                                                             @NotNull ThrowableConsumer<@Nullable ProgressIndicator, E> consumer)