public static void deleteString(@NotNull Document document, int startOffset, int endOffset) {
    try {
      document.deleteString(startOffset, endOffset);
    }
    catch (ReadOnlyFragmentModificationException ex) {
      RangeMarker block = ex.getGuardedBlock();
      if (block.getEndOffset() < endOffset) {
        throw ex;
      }

      CharSequence blockPrefix = document.getImmutableCharSequence().subSequence(block.getStartOffset(), endOffset);
      CharSequence textBefore =
        document.getImmutableCharSequence().subSequence(Math.max(0, startOffset - blockPrefix.length()), startOffset);

      if (!blockPrefix.isEmpty() && blockPrefix.toString().equals(textBefore.toString())) {
        deleteString(document, startOffset - blockPrefix.length(), endOffset - blockPrefix.length());
      }
      else {
        throw ex;
      }
    }
  }
public static void deleteString(@NotNull Document document, int startOffset, int endOffset)