Java Code Examples for com.intellij.openapi.vfs.newvfs.events.VFilePropertyChangeEvent

The following examples show how to use com.intellij.openapi.vfs.newvfs.events.VFilePropertyChangeEvent. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: consulo   Source File: FileContentUtilCore.java    License: Apache License 2.0 6 votes vote down vote up
@RequiredUIAccess
public static void reparseFiles(@Nonnull final Collection<? extends VirtualFile> files) {
  ApplicationManager.getApplication().runWriteAction(new Runnable() {
    @Override
    public void run() {
      // files must be processed under one write action to prevent firing event for invalid files.
      final Set<VFilePropertyChangeEvent> events = new THashSet<VFilePropertyChangeEvent>();
      for (VirtualFile file : files) {
        saveOrReload(file, events);
      }

      BulkFileListener publisher = ApplicationManager.getApplication().getMessageBus().syncPublisher(VirtualFileManager.VFS_CHANGES);
      List<VFileEvent> eventList = new ArrayList<VFileEvent>(events);
      publisher.before(eventList);
      publisher.after(eventList);
    }
  });
}
 
Example 2
Source Project: consulo   Source File: FileContentUtilCore.java    License: Apache License 2.0 6 votes vote down vote up
@RequiredWriteAction
private static void saveOrReload(VirtualFile file, @Nonnull Collection<VFilePropertyChangeEvent> events) {
  if (file == null || file.isDirectory() || !file.isValid()) {
    return;
  }

  FileDocumentManager documentManager = FileDocumentManager.getInstance();
  if (documentManager.isFileModified(file)) {
    Document document = documentManager.getDocument(file);
    if (document != null) {
      documentManager.saveDocument(document);
    }
  }

  events.add(new VFilePropertyChangeEvent(FORCE_RELOAD_REQUESTOR, file, VirtualFile.PROP_NAME, file.getName(), file.getName(), false));
}
 
Example 3
Source Project: consulo   Source File: VirtualFileManagerImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void notifyPropertyChanged(@Nonnull final VirtualFile virtualFile, @Nonnull final String property, final Object oldValue, final Object newValue) {
  final Application application = ApplicationManager.getApplication();
  final Runnable runnable = new Runnable() {
    @Override
    public void run() {
      if (virtualFile.isValid() && !application.isDisposed()) {
        application.runWriteAction(new Runnable() {
          @Override
          public void run() {
            List<VFilePropertyChangeEvent> events = Collections.singletonList(new VFilePropertyChangeEvent(this, virtualFile, property, oldValue, newValue, false));
            BulkFileListener listener = application.getMessageBus().syncPublisher(VirtualFileManager.VFS_CHANGES);
            listener.before(events);
            listener.after(events);
          }
        });
      }
    }
  };
  application.invokeLater(runnable, ModalityState.NON_MODAL);
}
 
Example 4
@Override
public void after(List<? extends VFileEvent> events) {
  if (!enabled) {
    return;
  }
  for (VFileEvent event : events) {
    VirtualFile modifiedFile = null;
    // Skip delete events.
    if (event instanceof VFileContentChangeEvent || event instanceof VFileCreateEvent) {
      modifiedFile = event.getFile();
    } else if (event instanceof VFileCopyEvent) {
      VFileCopyEvent copyEvent = (VFileCopyEvent) event;
      modifiedFile = copyEvent.getNewParent();
    } else if (event instanceof VFileMoveEvent) {
      VFileMoveEvent moveEvent = (VFileMoveEvent) event;
      modifiedFile = moveEvent.getNewParent();
    } else if (event instanceof VFilePropertyChangeEvent) {
      VFilePropertyChangeEvent propEvent = (VFilePropertyChangeEvent) event;
      // Check for file renames (sometimes we get property change events without the name
      // actually changing though)
      if (propEvent.getPropertyName().equals(VirtualFile.PROP_NAME)
          && !propEvent.getOldValue().equals(propEvent.getNewValue())) {
        modifiedFile = propEvent.getFile();
      }
    }
    if (SymbolTableProvider.isSourceFile(project, modifiedFile)) {
      queueChange(modifiedFile);
    }
  }
}
 
Example 5
Source Project: azure-devops-intellij   Source File: RenameFileDirectory.java    License: MIT License 4 votes vote down vote up
public static void execute(final PsiElement element, final String newName, final UsageInfo[] usages,
                           @Nullable final RefactoringElementListener listener) throws IncorrectOperationException {
    try {
        final VirtualFile virtualFile;
        if (element instanceof PsiFile) {
            logger.info("Renaming file...");
            virtualFile = ((PsiFile) element).getVirtualFile();
        } else if (element instanceof PsiDirectory) {
            logger.info("Renaming directory...");
            virtualFile = ((PsiDirectory) element).getVirtualFile();
        } else {
            // should never reach here since we check if file/directory before making a rename
            logger.warn("RenameFile: failed to find proper object to rename: " + element.getClass());
            throw new IncorrectOperationException("Can't perform rename on objects other than files and directories");
        }

        final String currentPath = virtualFile.getPath();
        final String parentDirectory = virtualFile.getParent().getPath();
        final String newPath = Path.combine(parentDirectory, newName);
        final Project project = element.getProject();

        // a single file may have 0, 1, or 2 pending changes to it
        // 0 - file has not been touched in the local workspace
        // 1 - file has versioned OR unversioned changes
        // 2 - file has versioned AND unversioned changes (rare but can happen)
        final List<PendingChange> pendingChanges = new ArrayList<>(2);
        pendingChanges.addAll(
                CommandUtils.getStatusForFiles(
                        project,
                        TFSVcs.getInstance(project).getServerContext(true),
                        ImmutableList.of(currentPath)));

        // ** Rename logic **
        // If 1 change and it's an add that means it's a new unversioned file so rename thru the file system
        // Anything else can be renamed
        // Deleted files should not be at this point since IDE disables rename option for them
        if (pendingChanges.size() == 1 && pendingChanges.get(0).getChangeTypes().contains(ServerStatusType.ADD)) {
            logger.info("Renaming unversioned file thru file system");
            RenameUtil.doRenameGenericNamedElement(element, newName, usages, listener);
        } else {
            logger.info("Renaming file thru tf commandline");
            CommandUtils.renameFile(TFSVcs.getInstance(project).getServerContext(true), currentPath, newPath);

            // this alerts that a rename has taken place so any additional processing can take place
            final VFileEvent event = new VFilePropertyChangeEvent(element.getManager(), virtualFile, "name", currentPath, newName, false);
            PersistentFS.getInstance().processEvents(Collections.singletonList(event));
        }
    } catch (Throwable t) {
        logger.warn("renameElement experienced a failure while trying to rename a file", t);
        throw new IncorrectOperationException(t);
    }

    if (listener != null) {
        listener.elementRenamed(element);
    }
}
 
Example 6
Source Project: consulo   Source File: VfsImplUtil.java    License: Apache License 2.0 4 votes vote down vote up
private static void checkSubscription() {
  if (ourSubscribed.getAndSet(true)) return;

  Application app = ApplicationManager.getApplication();
  app.getMessageBus().connect(app).subscribe(VirtualFileManager.VFS_CHANGES, new BulkFileListener.Adapter() {
    @Override
    public void after(@Nonnull List<? extends VFileEvent> events) {
      InvalidationState state = null;

      synchronized (ourLock) {
        for (VFileEvent event : events) {
          if (!(event.getFileSystem() instanceof LocalFileSystem)) continue;

          if (event instanceof VFileCreateEvent) continue; // created file should not invalidate + getFile is costly

          if (event instanceof VFilePropertyChangeEvent && !VirtualFile.PROP_NAME.equals(((VFilePropertyChangeEvent)event).getPropertyName())) {
            continue;
          }

          String path = event.getPath();
          if (event instanceof VFilePropertyChangeEvent) {
            path = ((VFilePropertyChangeEvent)event).getOldPath();
          }
          else if (event instanceof VFileMoveEvent) {
            path = ((VFileMoveEvent)event).getOldPath();
          }

          VirtualFile file = event.getFile();
          if (file == null || !file.isDirectory()) {
            state = InvalidationState.invalidate(state, path);
          }
          else {
            Collection<String> affectedPaths = ourDominatorsMap.get(path);
            if (affectedPaths != null) {
              affectedPaths = ContainerUtil.newArrayList(affectedPaths);  // defensive copying; original may be updated on invalidation
              for (String affectedPath : affectedPaths) {
                state = InvalidationState.invalidate(state, affectedPath);
              }
            }
          }
        }
      }

      if (state != null) state.scheduleRefresh();
    }
  });
}
 
Example 7
Source Project: consulo   Source File: VcsDirtyScopeVfsListener.java    License: Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public ChangeApplier prepareChange(@Nonnull List<? extends VFileEvent> events) {
  if (myForbid || !myVcsManager.hasAnyMappings()) return null;
  final FilesAndDirs dirtyFilesAndDirs = new FilesAndDirs();
  // collect files and directories - sources of events
  for (VFileEvent event : events) {
    ProgressManager.checkCanceled();

    final boolean isDirectory;
    if (event instanceof VFileCreateEvent) {
      if (!((VFileCreateEvent)event).getParent().isInLocalFileSystem()) {
        continue;
      }
      isDirectory = ((VFileCreateEvent)event).isDirectory();
    }
    else {
      final VirtualFile file = Objects.requireNonNull(event.getFile(), "All events but VFileCreateEvent have @NotNull getFile()");
      if (!file.isInLocalFileSystem()) {
        continue;
      }
      isDirectory = file.isDirectory();
    }

    if (event instanceof VFileMoveEvent) {
      add(myVcsManager, dirtyFilesAndDirs, VcsUtil.getFilePath(((VFileMoveEvent)event).getOldPath(), isDirectory));
      add(myVcsManager, dirtyFilesAndDirs, VcsUtil.getFilePath(((VFileMoveEvent)event).getNewPath(), isDirectory));
    }
    else if (event instanceof VFilePropertyChangeEvent && ((VFilePropertyChangeEvent)event).isRename()) {
      // if a file was renamed, then the file is dirty and its parent directory is dirty too;
      // if a directory was renamed, all its children are recursively dirty, the parent dir is also dirty but not recursively.
      FilePath oldPath = VcsUtil.getFilePath(((VFilePropertyChangeEvent)event).getOldPath(), isDirectory);
      FilePath newPath = VcsUtil.getFilePath(((VFilePropertyChangeEvent)event).getNewPath(), isDirectory);
      // the file is dirty recursively
      add(myVcsManager, dirtyFilesAndDirs, oldPath);
      add(myVcsManager, dirtyFilesAndDirs, newPath);
      FilePath parentPath = oldPath.getParentPath();
      if (parentPath != null) {
        addAsFiles(myVcsManager, dirtyFilesAndDirs, parentPath); // directory is dirty alone
      }
    }
    else {
      add(myVcsManager, dirtyFilesAndDirs, VcsUtil.getFilePath(event.getPath(), isDirectory));
    }
  }

  return new ChangeApplier() {
    @Override
    public void afterVfsChange() {
      markDirtyOnPooled(dirtyFilesAndDirs);
    }
  };
}
 
Example 8
Source Project: consulo   Source File: GistManagerImpl.java    License: Apache License 2.0 4 votes vote down vote up
private static boolean shouldDropCache(VFileEvent e) {
  if (!(e instanceof VFilePropertyChangeEvent)) return false;

  String propertyName = ((VFilePropertyChangeEvent)e).getPropertyName();
  return propertyName.equals(VirtualFile.PROP_NAME) || propertyName.equals(VirtualFile.PROP_ENCODING);
}