@Override
  public void propertyChanged(final VirtualFilePropertyEvent event) {
    if (VirtualFile.PROP_WRITABLE.equals(event.getPropertyName())) {
      final VirtualFile file = event.getFile();
      final Document document = getCachedDocument(file);
      if (document == null) return;

      ApplicationManager.getApplication()
          .runWriteAction(
              new ExternalChangeAction() {
                @Override
                public void run() {
                  document.setReadOnly(!event.getFile().isWritable());
                }
              });
      // myUnsavedDocuments.remove(document); //?
    }
  }
Esempio n. 2
0
 private VirtualFileSystemEntry applyEvent(@NotNull VFileEvent event) {
   try {
     if (event instanceof VFileCreateEvent) {
       final VFileCreateEvent createEvent = (VFileCreateEvent) event;
       return executeCreateChild(createEvent.getParent(), createEvent.getChildName());
     } else if (event instanceof VFileDeleteEvent) {
       final VFileDeleteEvent deleteEvent = (VFileDeleteEvent) event;
       executeDelete(deleteEvent.getFile());
     } else if (event instanceof VFileContentChangeEvent) {
       final VFileContentChangeEvent contentUpdateEvent = (VFileContentChangeEvent) event;
       executeTouch(
           contentUpdateEvent.getFile(),
           contentUpdateEvent.isFromRefresh(),
           contentUpdateEvent.getModificationStamp());
     } else if (event instanceof VFileCopyEvent) {
       final VFileCopyEvent copyEvent = (VFileCopyEvent) event;
       return executeCreateChild(copyEvent.getNewParent(), copyEvent.getNewChildName());
     } else if (event instanceof VFileMoveEvent) {
       final VFileMoveEvent moveEvent = (VFileMoveEvent) event;
       executeMove(moveEvent.getFile(), moveEvent.getNewParent());
     } else if (event instanceof VFilePropertyChangeEvent) {
       final VFilePropertyChangeEvent propertyChangeEvent = (VFilePropertyChangeEvent) event;
       if (VirtualFile.PROP_NAME.equals(propertyChangeEvent.getPropertyName())) {
         executeRename(propertyChangeEvent.getFile(), (String) propertyChangeEvent.getNewValue());
       } else if (VirtualFile.PROP_WRITABLE.equals(propertyChangeEvent.getPropertyName())) {
         executeSetWritable(
             propertyChangeEvent.getFile(),
             ((Boolean) propertyChangeEvent.getNewValue()).booleanValue());
       } else if (VirtualFile.PROP_HIDDEN.equals(propertyChangeEvent.getPropertyName())) {
         executeSetHidden(
             propertyChangeEvent.getFile(),
             ((Boolean) propertyChangeEvent.getNewValue()).booleanValue());
       } else if (VirtualFile.PROP_SYMLINK_TARGET.equals(propertyChangeEvent.getPropertyName())) {
         executeSetTarget(
             propertyChangeEvent.getFile(), (String) propertyChangeEvent.getNewValue());
       }
     }
   } catch (Exception e) {
     // Exception applying single event should not prevent other events from applying.
     LOG.error(e);
   }
   return null;
 }
  @Override
  public void propertyChanged(@NotNull final VirtualFilePropertyEvent event) {
    final String propertyName = event.getPropertyName();
    final VirtualFile vFile = event.getFile();

    final FileViewProvider oldFileViewProvider = myFileManager.findCachedViewProvider(vFile);
    final PsiFile oldPsiFile;
    if (oldFileViewProvider instanceof SingleRootFileViewProvider) {
      oldPsiFile =
          ((SingleRootFileViewProvider) oldFileViewProvider)
              .getCachedPsi(oldFileViewProvider.getBaseLanguage());
    } else {
      oldPsiFile = null;
    }

    VirtualFile parent = vFile.getParent();
    final PsiDirectory parentDir =
        oldPsiFile != null && parent != null
            ? myFileManager.findDirectory(parent)
            : getCachedDirectory(parent);

    if (oldFileViewProvider
            != null // there is no need to rebuild if there were no PSI in the first place
        && FileContentUtilCore.FORCE_RELOAD_REQUESTOR.equals(event.getRequestor())) {
      myFileManager.forceReload(vFile);
      return;
    }

    // do not suppress reparse request for light files
    if (parentDir == null) {
      boolean fire = VirtualFile.PROP_NAME.equals(propertyName) && vFile.isDirectory();
      if (fire) {
        PsiDirectory psiDir = myFileManager.getCachedDirectory(vFile);
        fire = psiDir != null;
      }
      if (!fire && !VirtualFile.PROP_WRITABLE.equals(propertyName)) {
        handleVfsChangeWithoutPsi(vFile);
        return;
      }
    }

    ((SmartPointerManagerImpl) SmartPointerManager.getInstance(myManager.getProject()))
        .fastenBelts(vFile, 0, null);
    ApplicationManager.getApplication()
        .runWriteAction(
            new ExternalChangeAction() {
              @Override
              public void run() {
                PsiTreeChangeEventImpl treeEvent = new PsiTreeChangeEventImpl(myManager);
                treeEvent.setParent(parentDir);

                if (VirtualFile.PROP_NAME.equals(propertyName)) {
                  if (vFile.isDirectory()) {
                    PsiDirectory psiDir = myFileManager.getCachedDirectory(vFile);
                    if (psiDir != null) {
                      if (myFileTypeManager.isFileIgnored(vFile)) {
                        myFileManager.removeFilesAndDirsRecursively(vFile);

                        treeEvent.setChild(psiDir);
                        myManager.childRemoved(treeEvent);
                      } else {
                        treeEvent.setElement(psiDir);
                        treeEvent.setPropertyName(PsiTreeChangeEvent.PROP_DIRECTORY_NAME);
                        treeEvent.setOldValue(event.getOldValue());
                        treeEvent.setNewValue(event.getNewValue());
                        myManager.propertyChanged(treeEvent);
                      }
                    } else {
                      PsiDirectory psiDir1 = myFileManager.findDirectory(vFile);
                      if (psiDir1 != null) {
                        treeEvent.setChild(psiDir1);
                        myManager.childAdded(treeEvent);
                      }
                    }
                  } else {
                    final FileViewProvider fileViewProvider =
                        myFileManager.createFileViewProvider(vFile, true);
                    final PsiFile newPsiFile =
                        fileViewProvider.getPsi(fileViewProvider.getBaseLanguage());
                    if (oldPsiFile != null) {
                      if (newPsiFile == null) {
                        myFileManager.setViewProvider(vFile, null);

                        treeEvent.setChild(oldPsiFile);
                        myManager.childRemoved(treeEvent);
                      } else if (!FileManagerImpl.areViewProvidersEquivalent(
                          fileViewProvider, oldFileViewProvider)) {
                        myFileManager.setViewProvider(vFile, fileViewProvider);

                        treeEvent.setOldChild(oldPsiFile);
                        treeEvent.setNewChild(newPsiFile);
                        myManager.childReplaced(treeEvent);
                      } else {
                        if (oldPsiFile instanceof PsiFileImpl) {
                          ((PsiFileImpl) oldPsiFile).clearCaches();
                        }
                        treeEvent.setElement(oldPsiFile);
                        treeEvent.setPropertyName(PsiTreeChangeEvent.PROP_FILE_NAME);
                        treeEvent.setOldValue(event.getOldValue());
                        treeEvent.setNewValue(event.getNewValue());
                        myManager.propertyChanged(treeEvent);
                      }
                    } else if (newPsiFile != null) {
                      myFileManager.setViewProvider(vFile, fileViewProvider);
                      if (parentDir != null) {
                        treeEvent.setChild(newPsiFile);
                        myManager.childAdded(treeEvent);
                      }
                    }
                  }
                } else if (VirtualFile.PROP_WRITABLE.equals(propertyName)) {
                  if (oldPsiFile == null) return;

                  treeEvent.setElement(oldPsiFile);
                  treeEvent.setPropertyName(PsiTreeChangeEvent.PROP_WRITABLE);
                  treeEvent.setOldValue(event.getOldValue());
                  treeEvent.setNewValue(event.getNewValue());
                  myManager.propertyChanged(treeEvent);
                } else if (VirtualFile.PROP_ENCODING.equals(propertyName)) {
                  if (oldPsiFile == null) return;

                  treeEvent.setElement(oldPsiFile);
                  treeEvent.setPropertyName(VirtualFile.PROP_ENCODING);
                  treeEvent.setOldValue(event.getOldValue());
                  treeEvent.setNewValue(event.getNewValue());
                  myManager.propertyChanged(treeEvent);
                }
              }
            });
  }