private void checkSubscription() { if (mySubscribed.getAndSet(true)) return; Application app = ApplicationManager.getApplication(); app.getMessageBus().connect(app).subscribe(VirtualFileManager.VFS_CHANGES, new BulkFileListener.Adapter() { @Override public void after(@NotNull List<? extends VFileEvent> events) { Set<VirtualFile> toRefresh = null; for (VFileEvent event : events) { if (event.getFileSystem() instanceof LocalFileSystem && event instanceof VFileContentChangeEvent) { VirtualFile file = event.getFile(); if (file != null && "jimage".equals(file.getExtension())) { String homePath = file.getParent().getParent().getParent().getPath(); if (myHandlers.remove(homePath) != null) { VirtualFile root = findFileByPath(composeRootPath(homePath)); if (root != null) { ((NewVirtualFile)root).markDirtyRecursively(); if (toRefresh == null) toRefresh = ContainerUtil.newHashSet(); toRefresh.add(root); } } } } } if (toRefresh != null) { boolean async = !ApplicationManager.getApplication().isUnitTestMode(); RefreshQueue.getInstance().refresh(async, true, null, toRefresh); } } }); }
public void testJarRefresh() throws IOException { File jar = IoTestUtil.createTestJar(); assertTrue(jar.setLastModified(jar.lastModified() - 1000)); VirtualFile vFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(jar); assertNotNull(vFile); VirtualFile jarRoot = findByPath(jar.getPath() + JarFileSystem.JAR_SEPARATOR); assertEquals(1, jarRoot.getChildren().length); final VirtualFile entry = findByPath(jar.getPath() + JarFileSystem.JAR_SEPARATOR + JarFile.MANIFEST_NAME); assertContent(entry, ""); final Ref<Boolean> updated = Ref.create(false); ApplicationManager.getApplication().getMessageBus().connect(myTestRootDisposable).subscribe( VirtualFileManager.VFS_CHANGES, new BulkFileListener.Adapter() { @Override public void before(@NotNull List<? extends VFileEvent> events) { for (VFileEvent event : events) { if (event instanceof VFileContentChangeEvent && entry.equals(event.getFile())) { updated.set(true); break; } } } } ); IoTestUtil.createTestJar(jar, JarFile.MANIFEST_NAME, "update", "some.txt", "some text"); vFile.refresh(false, false); assertTrue(updated.get()); assertTrue(entry.isValid()); assertContent(entry, "update"); assertEquals(2, jarRoot.getChildren().length); VirtualFile newEntry = findByPath(jar.getPath() + JarFileSystem.JAR_SEPARATOR + "some.txt"); assertContent(newEntry, "some text"); }
public void testFileContentChangeEvents() throws IOException { File file = IoTestUtil.createTestFile("file.txt"); long stamp = file.lastModified(); VirtualFile vFile = myFS.refreshAndFindFileByIoFile(file); assertNotNull(vFile); int[] updated = {0}; MessageBusConnection connection = ApplicationManager.getApplication().getMessageBus().connect(getTestRootDisposable()); connection.subscribe(VirtualFileManager.VFS_CHANGES, new BulkFileListener.Adapter() { @Override public void after(@NotNull List<? extends VFileEvent> events) { for (VFileEvent event : events) { if (event instanceof VFileContentChangeEvent && vFile.equals(event.getFile())) { updated[0]++; break; } } } }); FileUtil.writeToFile(file, "content"); assertTrue(file.setLastModified(stamp)); vFile.refresh(false, false); assertEquals(1, updated[0]); FileUtil.writeToFile(file, "more content"); assertTrue(file.setLastModified(stamp)); vFile.refresh(false, false); assertEquals(2, updated[0]); }
public VFSTestFrameworkListener() { myService = PyTestFrameworkService.getInstance(); MessageBus messageBus = ApplicationManager.getApplication().getMessageBus(); messageBus.connect().subscribe(VirtualFileManager.VFS_CHANGES, new BulkFileListener.Adapter() { @Override public void after(@NotNull List<? extends VFileEvent> events) { for (VFileEvent event : events) { if (!(event.getFileSystem() instanceof LocalFileSystem) || event instanceof VFileContentChangeEvent) continue; final String path = event.getPath(); boolean containsNose = path.contains(PyNames.NOSE_TEST); boolean containsPy = path.contains("py-1") || path.contains(PyNames.PY_TEST); boolean containsAt = path.contains(PyNames.AT_TEST); if (!containsAt && !containsNose && !containsPy) continue; for (Sdk sdk : PythonSdkType.getAllSdks()) { if (PySdkUtil.isRemote(sdk)) { continue; } for (VirtualFile virtualFile : sdk.getRootProvider().getFiles(OrderRootType.CLASSES)) { String root = virtualFile.getCanonicalPath(); if (root != null && path.contains(root)) { if (containsNose) { updateTestFrameworks(sdk, PyNames.NOSE_TEST); return; } else if (containsPy) { updateTestFrameworks(sdk, PyNames.PY_TEST); return; } else { updateTestFrameworks(sdk, PyNames.AT_TEST); return; } } } } } } }); }
public static void imitateEvent(VirtualFile dir) { final VirtualFile child = dir.findChild(".svn"); assertNotNull(child); final VirtualFile wcdb = child.findChild("wc.db"); assertNotNull(wcdb); final BulkFileListener listener = ApplicationManager.getApplication().getMessageBus().syncPublisher(VirtualFileManager.VFS_CHANGES); final VFileContentChangeEvent event = new VFileContentChangeEvent(null, wcdb, wcdb.getModificationStamp() - 1, wcdb.getModificationStamp(), true); final List<VFileContentChangeEvent> events = Collections.singletonList(event); listener.before(events); listener.after(events); }
@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(modifiedFile)) { queueChange(modifiedFile); } } }