@Override
    public void rootsChanged(final ModuleRootEvent event) {
      myFileManager.dispatchPendingEvents();

      if (!myFileManager.isInitialized()) return;
      if (event.isCausedByFileTypesChange()) return;
      ApplicationManager.getApplication()
          .runWriteAction(
              new ExternalChangeAction() {
                @Override
                public void run() {
                  depthCounter--;
                  assert depthCounter >= 0 : depthCounter;
                  if (depthCounter > 0) return;

                  myFileManager.removeInvalidFilesAndDirs(true);

                  PsiTreeChangeEventImpl treeEvent = new PsiTreeChangeEventImpl(myManager);
                  treeEvent.setPropertyName(PsiTreeChangeEvent.PROP_ROOTS);
                  final VirtualFile[] contentRoots = myProjectRootManager.getContentRoots();
                  treeEvent.setNewValue(contentRoots);
                  LOG.assertTrue(myOldContentRoots != null);
                  treeEvent.setOldValue(myOldContentRoots);
                  myOldContentRoots = null;
                  myManager.propertyChanged(treeEvent);
                }
              });
    }
    @Override
    public void beforeRootsChange(final ModuleRootEvent event) {
      if (!myFileManager.isInitialized()) return;
      if (event.isCausedByFileTypesChange()) return;
      ApplicationManager.getApplication()
          .runWriteAction(
              new ExternalChangeAction() {
                @Override
                public void run() {
                  depthCounter++;
                  if (depthCounter > 1) return;

                  PsiTreeChangeEventImpl treeEvent = new PsiTreeChangeEventImpl(myManager);
                  treeEvent.setPropertyName(PsiTreeChangeEvent.PROP_ROOTS);
                  final VirtualFile[] contentRoots = myProjectRootManager.getContentRoots();
                  LOG.assertTrue(myOldContentRoots == null);
                  myOldContentRoots = contentRoots;
                  treeEvent.setOldValue(contentRoots);
                  myManager.beforePropertyChange(treeEvent);
                }
              });
    }