com.intellij.openapi.vfs.newvfs.events.VFileContentChangeEvent Java Examples

The following examples show how to use com.intellij.openapi.vfs.newvfs.events.VFileContentChangeEvent. 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 File: JarMappings.java    From intellij-pants-plugin with Apache License 2.0 6 votes vote down vote up
public JarMappings(Project project) {
  this.project = project;

  project.getMessageBus().connect().subscribe(VirtualFileManager.VFS_CHANGES, new BulkFileListener() {
    @Override
    public void after(@NotNull List<? extends VFileEvent> events) {
      events.forEach(event -> {
        if (event instanceof VFileContentChangeEvent &&
            event.getFile() != null &&
            event.getFile().equals(librariesFile())) {
          librariesFileIsUpToDate = false;
        }
      });
    }
  });
}
 
Example #2
Source File: FileDocumentManagerImplTest.java    From consulo with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("UnusedDeclaration")
public void _testContentChanged_reloadChangedDocumentOnSave() throws Exception {
  final MockVirtualFile file = new MockVirtualFile("test.txt", "test\rtest") {
    @Override
    public void refresh(boolean asynchronous, boolean recursive, Runnable postRunnable) {
      long oldStamp = getModificationStamp();
      setModificationStamp(LocalTimeCounter.currentTime());
      myDocumentManager.contentsChanged(new VFileContentChangeEvent(null, this, oldStamp, getModificationStamp(), true));
    }
  };
  Document document = myDocumentManager.getDocument(file);
  assertNotNull(file.toString(), document);
  document.insertString(0, "zzz");
  file.setContent(null, "xxx", false);

  myReloadFromDisk = Boolean.TRUE;
  myDocumentManager.saveAllDocuments();
  long fileStamp = file.getModificationStamp();

  assertEquals("xxx", document.getText());
  assertEquals(file.getModificationStamp(), document.getModificationStamp());
  assertEquals(file.getModificationStamp(), fileStamp);
  assertEquals(0, myDocumentManager.getUnsavedDocuments().length);
}
 
Example #3
Source File: MemoryDiskConflictResolver.java    From consulo with Apache License 2.0 6 votes vote down vote up
void beforeContentChange(@Nonnull VFileContentChangeEvent event) {
  if (event.isFromSave()) return;

  VirtualFile file = event.getFile();
  if (!file.isValid() || hasConflict(file)) return;

  Document document = FileDocumentManager.getInstance().getCachedDocument(file);
  if (document == null || !FileDocumentManager.getInstance().isDocumentUnsaved(document)) return;

  long documentStamp = document.getModificationStamp();
  long oldFileStamp = event.getOldModificationStamp();
  if (documentStamp != oldFileStamp) {
    LOG.info("reload " + file.getName() + " from disk?");
    LOG.info("  documentStamp:" + documentStamp);
    LOG.info("  oldFileStamp:" + oldFileStamp);
    if (myConflicts.isEmpty()) {
      if (ApplicationManager.getApplication().isUnitTestMode()) {
        myConflictAppeared = new Throwable();
      }
      ApplicationManager.getApplication().invokeLater(this::processConflicts);
    }
    myConflicts.add(file);
  }
}
 
Example #4
Source File: QuarkusLanguageClient.java    From intellij-quarkus with Eclipse Public License 2.0 5 votes vote down vote up
private void filter(VFileEvent event, Set<String> uris) {
  VirtualFile file = event.getFile();
  if (file != null && file.exists() && "java".equalsIgnoreCase(file.getExtension())) {
    Module module = ProjectFileIndex.getInstance(getProject()).getModuleForFile(file);
    if (module != null && (event instanceof VFileCreateEvent || event instanceof VFileContentChangeEvent || event instanceof VFileDeleteEvent)) {
      uris.add(PsiUtilsImpl.getProjectURI(module));
    }
  }
}
 
Example #5
Source File: BulkSymbolTableBuildingChangeListener.java    From intellij with Apache License 2.0 5 votes vote down vote up
@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 #6
Source File: FileDocumentManagerImplTest.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void testContentChanged_ignoreEventsFromSelfOnSave() throws Exception {
  final VirtualFile file = new MockVirtualFile("test.txt", "test\rtest") {
    @Nonnull
    @Override
    public OutputStream getOutputStream(final Object requestor, final long newModificationStamp, long newTimeStamp) throws IOException {
      final VirtualFile self = this;
      return new ByteArrayOutputStream() {
        @Override
        public void close() throws IOException {
          super.close();
          long oldStamp = getModificationStamp();
          setModificationStamp(newModificationStamp);
          setText(toString());
          myDocumentManager.contentsChanged(new VFileContentChangeEvent(requestor, self, oldStamp, getModificationStamp(), false));
        }
      };
    }
  };
  final Document document = myDocumentManager.getDocument(file);
  assertNotNull(file.toString(), document);
  WriteCommandAction.runWriteCommandAction(myProject, new Runnable() {
    @Override
    public void run() {
      document.insertString(0, "xxx");
    }
  });

  final long stamp = document.getModificationStamp();

  myDocumentManager.saveAllDocuments();
  assertEquals(stamp, document.getModificationStamp());
}
 
Example #7
Source File: FileDocumentManagerImplTest.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void testContentChanged_doNotReloadChangedDocumentOnSave() throws Exception {
  final MockVirtualFile file = new MockVirtualFile("test.txt", "test") {
    @Override
    public void refresh(boolean asynchronous, boolean recursive, Runnable postRunnable) {
      long oldStamp = getModificationStamp();
      setModificationStamp(LocalTimeCounter.currentTime());
      myDocumentManager.contentsChanged(new VFileContentChangeEvent(null, this, oldStamp, getModificationStamp(), true));
    }
  };

  myReloadFromDisk = Boolean.FALSE;
  final Document document = myDocumentManager.getDocument(file);
  assertNotNull(file.toString(), document);
  WriteCommandAction.runWriteCommandAction(myProject, new Runnable() {
    @Override
    public void run() {
      document.insertString(0, "old ");
    }
  });

  long documentStamp = document.getModificationStamp();

  file.setContent(null, "xxx", false);

  myDocumentManager.saveAllDocuments();

  assertEquals("old test", document.getText());
  assertEquals(file.getModificationStamp(), document.getModificationStamp());
  assertTrue(Arrays.equals("old test".getBytes("UTF-8"), file.contentsToByteArray()));
  assertEquals(documentStamp, document.getModificationStamp());
}
 
Example #8
Source File: FileDocumentManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void contentsChanged(VFileContentChangeEvent event) {
  VirtualFile virtualFile = event.getFile();
  Document document = getCachedDocument(virtualFile);

  if (event.isFromSave()) return;

  if (document == null || isBinaryWithDecompiler(virtualFile)) {
    myMultiCaster.fileWithNoDocumentChanged(virtualFile); // This will generate PSI event at FileManagerImpl
  }

  if (document != null && (document.getModificationStamp() == event.getOldModificationStamp() || !isDocumentUnsaved(document))) {
    reloadFromDisk(document);
  }
}
 
Example #9
Source File: EncodingProjectManagerImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static void reload(@Nonnull final VirtualFile virtualFile) {
  ApplicationManager.getApplication().runWriteAction(() -> {
    FileDocumentManager documentManager = FileDocumentManager.getInstance();
    ((FileDocumentManagerImpl)documentManager).contentsChanged(new VFileContentChangeEvent(null, virtualFile, 0, 0, false));
  });
}