public boolean commitTransaction(final Document document) {
    ApplicationManager.getApplication().assertIsDispatchThread();
    final DocumentChangeTransaction documentChangeTransaction = removeTransaction(document);
    if (documentChangeTransaction == null) return false;
    final PsiElement changeScope = documentChangeTransaction.getChangeScope();
    try {
      mySyncDocument = document;

      final PsiTreeChangeEventImpl fakeEvent = new PsiTreeChangeEventImpl(changeScope.getManager());
      fakeEvent.setParent(changeScope);
      fakeEvent.setFile(changeScope.getContainingFile());
      doSync(
          fakeEvent,
          true,
          new DocSyncAction() {
            @Override
            public void syncDocument(Document document, PsiTreeChangeEventImpl event) {
              doCommitTransaction(document, documentChangeTransaction);
            }
          });
      myBus
          .syncPublisher(PsiDocumentTransactionListener.TOPIC)
          .transactionCompleted(document, (PsiFile) changeScope);
    } finally {
      mySyncDocument = null;
    }
    return true;
  }
 private void replaceString(
     Document document,
     int startOffset,
     int endOffset,
     String s,
     @Nullable PsiElement replacement) {
   final DocumentChangeTransaction documentChangeTransaction = getTransaction(document);
   if (documentChangeTransaction != null) {
     documentChangeTransaction.replace(startOffset, endOffset - startOffset, s, replacement);
   }
 }
  private static void doCommitTransaction(
      @NotNull Document document, @NotNull DocumentChangeTransaction documentChangeTransaction) {
    DocumentEx ex = (DocumentEx) document;
    ex.suppressGuardedExceptions();
    try {
      boolean isReadOnly = !document.isWritable();
      ex.setReadOnly(false);
      final Set<Pair<MutableTextRange, StringBuffer>> affectedFragments =
          documentChangeTransaction.getAffectedFragments();
      for (final Pair<MutableTextRange, StringBuffer> pair : affectedFragments) {
        final StringBuffer replaceBuffer = pair.getSecond();
        final MutableTextRange range = pair.getFirst();
        if (replaceBuffer.length() == 0) {
          ex.deleteString(range.getStartOffset(), range.getEndOffset());
        } else if (range.getLength() == 0) {
          ex.insertString(range.getStartOffset(), replaceBuffer);
        } else {
          ex.replaceString(range.getStartOffset(), range.getEndOffset(), replaceBuffer);
        }
      }

      ex.setReadOnly(isReadOnly);
      // if(documentChangeTransaction.getChangeScope() != null) {
      //
      // LOG.assertTrue(document.getText().equals(documentChangeTransaction.getChangeScope().getText()),
      //                 "Psi to document synchronization failed (send to IK)");
      // }
    } finally {
      ex.unSuppressGuardedExceptions();
    }
  }
 private void deleteString(Document document, int startOffset, int endOffset) {
   final DocumentChangeTransaction documentChangeTransaction = getTransaction(document);
   if (documentChangeTransaction != null) {
     documentChangeTransaction.replace(startOffset, endOffset - startOffset, "");
   }
 }
 public void insertString(Document document, int offset, String s) {
   final DocumentChangeTransaction documentChangeTransaction = getTransaction(document);
   if (documentChangeTransaction != null) {
     documentChangeTransaction.replace(offset, 0, s);
   }
 }
 public void replaceString(Document document, int startOffset, int endOffset, String s) {
   final DocumentChangeTransaction documentChangeTransaction = getTransaction(document);
   if (documentChangeTransaction != null) {
     documentChangeTransaction.replace(startOffset, endOffset - startOffset, s);
   }
 }