private DocumentEx getCachedDocument(PsiFile psiFile, boolean force) {
   final DocumentEx document = (DocumentEx) myPsiDocumentManager.getCachedDocument(psiFile);
   if (document == null
       || document instanceof DocumentWindow
       || !force && getTransaction(document) == null) {
     return null;
   }
   return document;
 }
  private void checkPsiModificationAllowed(@NotNull final PsiTreeChangeEvent event) {
    if (!toProcessPsiEvent()) return;
    final PsiFile psiFile = event.getFile();
    if (!(psiFile instanceof PsiFileEx) || !((PsiFileEx) psiFile).isContentsLoaded()) return;

    final Document document = myPsiDocumentManager.getCachedDocument(psiFile);
    if (document != null && myPsiDocumentManager.isUncommited(document)) {
      throw new IllegalStateException("Attempt to modify PSI for non-committed Document!");
    }
  }
  private void doSync(
      @NotNull final PsiTreeChangeEvent event,
      boolean force,
      @NotNull final DocSyncAction syncAction) {
    if (!toProcessPsiEvent()) return;
    final PsiFile psiFile = event.getFile();
    if (psiFile == null || psiFile.getNode() == null) return;

    final DocumentEx document = (DocumentEx) myPsiDocumentManager.getCachedDocument(psiFile);
    if (document == null || document instanceof DocumentWindow) return;
    if (!force && getTransaction(document) == null) {
      return;
    }

    TextBlock textBlock = TextBlock.get(psiFile);

    if (!textBlock.isEmpty()) {
      throw new IllegalStateException("Attempt to modify PSI for non-committed Document!");
    }

    textBlock.performAtomically(
        new Runnable() {
          @Override
          public void run() {
            syncAction.syncDocument(document, (PsiTreeChangeEventImpl) event);
          }
        });

    final boolean insideTransaction = myTransactionsMap.containsKey(document);
    if (!insideTransaction) {
      document.setModificationStamp(psiFile.getViewProvider().getModificationStamp());
      if (LOG.isDebugEnabled()) {
        PsiDocumentManagerBase.checkConsistency(psiFile, document);
      }
    }

    psiFile.getViewProvider().contentsSynchronized();
  }