Search in sources :

Example 1 with VFileEvent

use of com.intellij.openapi.vfs.newvfs.events.VFileEvent in project intellij-community by JetBrains.

the class HgRepositoryUpdater method after.

@Override
public void after(@NotNull List<? extends VFileEvent> events) {
    // which files in .hg were changed
    boolean branchHeadsChanged = false;
    boolean branchFileChanged = false;
    boolean dirstateFileChanged = false;
    boolean mergeFileChanged = false;
    boolean rebaseFileChanged = false;
    boolean bookmarksFileChanged = false;
    boolean tagsFileChanged = false;
    boolean localTagsFileChanged = false;
    boolean currentBookmarkFileChanged = false;
    boolean mqChanged = false;
    boolean hgIgnoreChanged = false;
    boolean configHgrcChanged = false;
    for (VFileEvent event : events) {
        String filePath = event.getPath();
        if (filePath == null) {
            continue;
        }
        if (myRepositoryFiles.isbranchHeadsFile(filePath)) {
            branchHeadsChanged = true;
        } else if (myRepositoryFiles.isBranchFile(filePath)) {
            branchFileChanged = true;
            DvcsUtil.ensureAllChildrenInVfs(myBranchHeadsDir);
        } else if (myRepositoryFiles.isDirstateFile(filePath)) {
            dirstateFileChanged = true;
        } else if (myRepositoryFiles.isMergeFile(filePath)) {
            mergeFileChanged = true;
        } else if (myRepositoryFiles.isRebaseFile(filePath)) {
            rebaseFileChanged = true;
        } else if (myRepositoryFiles.isBookmarksFile(filePath)) {
            bookmarksFileChanged = true;
        } else if (myRepositoryFiles.isTagsFile(filePath)) {
            tagsFileChanged = true;
        } else if (myRepositoryFiles.isLocalTagsFile(filePath)) {
            localTagsFileChanged = true;
        } else if (myRepositoryFiles.isCurrentBookmarksFile(filePath)) {
            currentBookmarkFileChanged = true;
        } else if (myRepositoryFiles.isMqFile(filePath)) {
            mqChanged = true;
            if (myMqDir == null) {
                myMqDir = VcsUtil.getVirtualFile(myRepositoryFiles.getMQDirPath());
            }
            DvcsUtil.ensureAllChildrenInVfs(myMqDir);
        } else if (myRepositoryFiles.isConfigHgrcFile(filePath)) {
            configHgrcChanged = true;
        } else if (myRepositoryFiles.isHgIgnore(filePath)) {
            hgIgnoreChanged = true;
        }
    }
    if (branchHeadsChanged || branchFileChanged || dirstateFileChanged || mergeFileChanged || rebaseFileChanged || bookmarksFileChanged || currentBookmarkFileChanged || tagsFileChanged || localTagsFileChanged || mqChanged) {
        myUpdateQueue.queue(new MyUpdater("hgrepositoryUpdate"));
    }
    if (configHgrcChanged) {
        myUpdateConfigQueue.queue(new MyUpdater("hgconfigUpdate"));
    }
    if (dirstateFileChanged || hgIgnoreChanged) {
        myRepository.getLocalIgnoredHolder().startRescan();
        final VirtualFile root = myRepository.getRoot();
        myDirtyScopeManager.dirDirtyRecursively(root);
        if (dirstateFileChanged) {
            //update async incoming/outgoing model
            myProject.getMessageBus().syncPublisher(HgVcs.REMOTE_TOPIC).update(myProject, root);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VFileEvent(com.intellij.openapi.vfs.newvfs.events.VFileEvent)

Example 2 with VFileEvent

use of com.intellij.openapi.vfs.newvfs.events.VFileEvent in project intellij-community by JetBrains.

the class JarFileSystemTest method testJarRefresh.

@Test
public void testJarRefresh() throws IOException {
    File jar = IoTestUtil.createTestJar(tempDir.newFile("test.jar"));
    assertTrue(jar.setLastModified(jar.lastModified() - 1000));
    VirtualFile vFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(jar);
    assertNotNull(vFile);
    VirtualFile jarRoot = findByPath(jar.getPath() + JarFileSystem.JAR_SEPARATOR);
    assertThat(Stream.of(jarRoot.getChildren()).map(VirtualFile::getName)).containsExactly("META-INF");
    VirtualFile entry = findByPath(jar.getPath() + JarFileSystem.JAR_SEPARATOR + JarFile.MANIFEST_NAME);
    assertEquals("", VfsUtilCore.loadText(entry));
    Ref<Boolean> updated = Ref.create(false);
    ApplicationManager.getApplication().getMessageBus().connect(getTestRootDisposable()).subscribe(VirtualFileManager.VFS_CHANGES, new BulkFileListener() {

        @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());
    assertEquals("update", VfsUtilCore.loadText(entry));
    assertThat(Stream.of(jarRoot.getChildren()).map(VirtualFile::getName)).containsExactlyInAnyOrder("META-INF", "some.txt");
    VirtualFile newEntry = findByPath(jar.getPath() + JarFileSystem.JAR_SEPARATOR + "some.txt");
    assertEquals("some text", VfsUtilCore.loadText(newEntry));
}
Also used : VFileEvent(com.intellij.openapi.vfs.newvfs.events.VFileEvent) VFileContentChangeEvent(com.intellij.openapi.vfs.newvfs.events.VFileContentChangeEvent) BulkFileListener(com.intellij.openapi.vfs.newvfs.BulkFileListener) JarFile(java.util.jar.JarFile) Test(org.junit.Test)

Example 3 with VFileEvent

use of com.intellij.openapi.vfs.newvfs.events.VFileEvent in project intellij-community by JetBrains.

the class LocalFileSystemTest method doTestInterruptedRefresh.

public static void doTestInterruptedRefresh(@NotNull File top) throws Exception {
    for (int i = 1; i <= 3; i++) {
        File sub = IoTestUtil.createTestDir(top, "sub_" + i);
        for (int j = 1; j <= 3; j++) {
            IoTestUtil.createTestDir(sub, "sub_" + j);
        }
    }
    Files.walkFileTree(top.toPath(), new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            for (int k = 1; k <= 3; k++) {
                IoTestUtil.createTestFile(dir.toFile(), "file_" + k, ".");
            }
            return FileVisitResult.CONTINUE;
        }
    });
    VirtualFile topDir = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(top);
    assertNotNull(topDir);
    Set<VirtualFile> files = ContainerUtil.newHashSet();
    VfsUtilCore.processFilesRecursively(topDir, file -> {
        if (!file.isDirectory())
            files.add(file);
        return true;
    });
    // 13 dirs of 3 files
    assertEquals(39, files.size());
    topDir.refresh(false, true);
    Set<VirtualFile> processed = ContainerUtil.newHashSet();
    MessageBusConnection connection = ApplicationManager.getApplication().getMessageBus().connect();
    connection.subscribe(VirtualFileManager.VFS_CHANGES, new BulkFileListener() {

        @Override
        public void after(@NotNull List<? extends VFileEvent> events) {
            events.forEach(e -> processed.add(e.getFile()));
        }
    });
    try {
        files.forEach(f -> IoTestUtil.updateFile(new File(f.getPath()), "+++"));
        ((NewVirtualFile) topDir).markDirtyRecursively();
        RefreshWorker.setCancellingCondition(file -> file.getPath().endsWith(top.getName() + "/sub_2/file_2"));
        topDir.refresh(false, true);
        assertThat(processed.size()).isGreaterThan(0).isLessThan(files.size());
        RefreshWorker.setCancellingCondition(null);
        topDir.refresh(false, true);
        assertThat(processed).isEqualTo(files);
    } finally {
        connection.disconnect();
        RefreshWorker.setCancellingCondition(null);
    }
}
Also used : Path(java.nio.file.Path) VfsRootAccess(com.intellij.openapi.vfs.newvfs.impl.VfsRootAccess) java.util(java.util) WriteAction(com.intellij.openapi.application.WriteAction) ArrayUtil(com.intellij.util.ArrayUtil) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) FileAttributes(com.intellij.openapi.util.io.FileAttributes) ContainerUtil(com.intellij.util.containers.ContainerUtil) FileSystemUtil(com.intellij.openapi.util.io.FileSystemUtil) MessageBusConnection(com.intellij.util.messages.MessageBusConnection) com.intellij.openapi.vfs.newvfs(com.intellij.openapi.vfs.newvfs) PersistentFSImpl(com.intellij.openapi.vfs.newvfs.persistent.PersistentFSImpl) VFileCreateEvent(com.intellij.openapi.vfs.newvfs.events.VFileCreateEvent) FileUtil(com.intellij.openapi.util.io.FileUtil) VFileDeleteEvent(com.intellij.openapi.vfs.newvfs.events.VFileDeleteEvent) Path(java.nio.file.Path) VFileEvent(com.intellij.openapi.vfs.newvfs.events.VFileEvent) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) RefreshWorker(com.intellij.openapi.vfs.newvfs.persistent.RefreshWorker) PlatformTestUtil(com.intellij.testFramework.PlatformTestUtil) Files(java.nio.file.Files) StringUtil(com.intellij.openapi.util.text.StringUtil) PlatformTestCase(com.intellij.testFramework.PlatformTestCase) PersistentFS(com.intellij.openapi.vfs.newvfs.persistent.PersistentFS) IOException(java.io.IOException) VirtualDirectoryImpl(com.intellij.openapi.vfs.newvfs.impl.VirtualDirectoryImpl) VFileContentChangeEvent(com.intellij.openapi.vfs.newvfs.events.VFileContentChangeEvent) SystemInfo(com.intellij.openapi.util.SystemInfo) File(java.io.File) FileVisitResult(java.nio.file.FileVisitResult) ApplicationManager(com.intellij.openapi.application.ApplicationManager) IoTestUtil(com.intellij.openapi.util.io.IoTestUtil) com.intellij.openapi.vfs(com.intellij.openapi.vfs) GeneralSettings(com.intellij.ide.GeneralSettings) VirtualFileSystemEntry(com.intellij.openapi.vfs.newvfs.impl.VirtualFileSystemEntry) ObjectUtils(com.intellij.util.ObjectUtils) NotNull(org.jetbrains.annotations.NotNull) MessageBusConnection(com.intellij.util.messages.MessageBusConnection) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) File(java.io.File)

Example 4 with VFileEvent

use of com.intellij.openapi.vfs.newvfs.events.VFileEvent in project intellij-community by JetBrains.

the class LocalFileSystemTest method testFileContentChangeEvents.

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() {

        @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]);
}
Also used : MessageBusConnection(com.intellij.util.messages.MessageBusConnection) VFileEvent(com.intellij.openapi.vfs.newvfs.events.VFileEvent) VFileContentChangeEvent(com.intellij.openapi.vfs.newvfs.events.VFileContentChangeEvent) File(java.io.File)

Example 5 with VFileEvent

use of com.intellij.openapi.vfs.newvfs.events.VFileEvent in project intellij-community by JetBrains.

the class VfsImplUtil method checkSubscription.

private static void checkSubscription() {
    if (ourSubscribed.getAndSet(true))
        return;
    Application app = ApplicationManager.getApplication();
    app.getMessageBus().connect(app).subscribe(VirtualFileManager.VFS_CHANGES, new BulkFileListener() {

        @Override
        public void after(@NotNull List<? extends VFileEvent> events) {
            InvalidationState state = null;
            synchronized (ourLock) {
                for (VFileEvent event : events) {
                    if (!(event.getFileSystem() instanceof LocalFileSystem))
                        continue;
                    // created file should not invalidate + getFile is costly
                    if (event instanceof VFileCreateEvent)
                        continue;
                    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) {
                            // defensive copying; original may be updated on invalidation
                            affectedPaths = ContainerUtil.newArrayList(affectedPaths);
                            for (String affectedPath : affectedPaths) {
                                state = InvalidationState.invalidate(state, affectedPath);
                            }
                        }
                    }
                }
            }
            if (state != null)
                state.scheduleRefresh();
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VFilePropertyChangeEvent(com.intellij.openapi.vfs.newvfs.events.VFilePropertyChangeEvent) VFileCreateEvent(com.intellij.openapi.vfs.newvfs.events.VFileCreateEvent) VFileMoveEvent(com.intellij.openapi.vfs.newvfs.events.VFileMoveEvent) VFileEvent(com.intellij.openapi.vfs.newvfs.events.VFileEvent) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) Collection(java.util.Collection) Application(com.intellij.openapi.application.Application)

Aggregations

VFileEvent (com.intellij.openapi.vfs.newvfs.events.VFileEvent)18 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 VFileCreateEvent (com.intellij.openapi.vfs.newvfs.events.VFileCreateEvent)8 VFileContentChangeEvent (com.intellij.openapi.vfs.newvfs.events.VFileContentChangeEvent)6 BulkFileListener (com.intellij.openapi.vfs.newvfs.BulkFileListener)5 MessageBusConnection (com.intellij.util.messages.MessageBusConnection)5 File (java.io.File)4 VFileDeleteEvent (com.intellij.openapi.vfs.newvfs.events.VFileDeleteEvent)3 VFileMoveEvent (com.intellij.openapi.vfs.newvfs.events.VFileMoveEvent)3 VFilePropertyChangeEvent (com.intellij.openapi.vfs.newvfs.events.VFilePropertyChangeEvent)3 GeneralSettings (com.intellij.ide.GeneralSettings)2 Application (com.intellij.openapi.application.Application)2 ApplicationManager (com.intellij.openapi.application.ApplicationManager)2 WriteAction (com.intellij.openapi.application.WriteAction)2 SystemInfo (com.intellij.openapi.util.SystemInfo)2 FileAttributes (com.intellij.openapi.util.io.FileAttributes)2 FileSystemUtil (com.intellij.openapi.util.io.FileSystemUtil)2 FileUtil (com.intellij.openapi.util.io.FileUtil)2 IoTestUtil (com.intellij.openapi.util.io.IoTestUtil)2 StringUtil (com.intellij.openapi.util.text.StringUtil)2