@Override
  public final synchronized void formatDocument(@NotNull Document document,
                                                @NotNull List<TextRange> formattingRanges,
                                                @NotNull FormattingContext formattingContext,
                                                boolean canChangeWhiteSpaceOnly,
                                                boolean quickFormat) {
    AsyncFormattingRequest currRequest = findPendingRequest(document);
    boolean forceSync = Boolean.TRUE.equals(document.getUserData(FORMAT_DOCUMENT_SYNCHRONOUSLY));
    if (currRequest != null) {
      if (!((FormattingRequestImpl)currRequest).cancel()) {
        LOG.warn("Pending request can't be cancelled");
        return;
      }
    }
    FormattingRequestImpl formattingRequest = new FormattingRequestImpl(formattingContext, document, formattingRanges,
                                                                         canChangeWhiteSpaceOnly, quickFormat);
    FormattingTask formattingTask = createFormattingTask(formattingRequest);
    if (formattingTask != null) {
      formattingRequest.setTask(formattingTask);
      myPendingRequests.add(formattingRequest);
      if (forceSync || ApplicationManager.getApplication().isHeadlessEnvironment()) {
        runAsyncFormat(formattingRequest, null);
      }
      else {
        if (formattingTask.isRunUnderProgress()) {
          new FormattingProgressTask(formattingRequest)
            .setCancelText(CodeStyleBundle.message("async.formatting.service.cancel", getName()))
            .queue();
        }
        else {
          ApplicationManager.getApplication().executeOnPooledThread(() -> runAsyncFormat(formattingRequest, null));
        }
      }
    }
  }
@Override
  public final synchronized void formatDocument(@NotNull Document document,
                                                @NotNull List<TextRange> formattingRanges,
                                                @NotNull FormattingContext formattingContext,
                                                boolean canChangeWhiteSpaceOnly,
                                                boolean quickFormat)