Search in sources :

Example 1 with BulkFileListener

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

the class RefResolveServiceImpl method initListeners.

private void initListeners(@NotNull MessageBus messageBus, @NotNull PsiManager psiManager) {
    messageBus.connect().subscribe(VirtualFileManager.VFS_CHANGES, new BulkFileListener() {

        @Override
        public void after(@NotNull List<? extends VFileEvent> events) {
            fileCount.set(0);
            List<VirtualFile> files = ContainerUtil.mapNotNull(events, new Function<VFileEvent, VirtualFile>() {

                @Override
                public VirtualFile fun(VFileEvent event) {
                    return event.getFile();
                }
            });
            queue(files, "VFS events " + events.size());
        }
    });
    psiManager.addPsiTreeChangeListener(new PsiTreeChangeAdapter() {

        @Override
        public void childrenChanged(@NotNull PsiTreeChangeEvent event) {
            PsiFile file = event.getFile();
            VirtualFile virtualFile = PsiUtilCore.getVirtualFile(file);
            if (virtualFile != null) {
                queue(Collections.singletonList(virtualFile), event);
            }
        }

        @Override
        public void propertyChanged(@NotNull PsiTreeChangeEvent event) {
            childrenChanged(event);
        }
    });
    messageBus.connect().subscribe(DumbService.DUMB_MODE, new DumbService.DumbModeListener() {

        @Override
        public void enteredDumbMode() {
            disable();
        }

        @Override
        public void exitDumbMode() {
            enable();
        }
    });
    messageBus.connect().subscribe(PowerSaveMode.TOPIC, new PowerSaveMode.Listener() {

        @Override
        public void powerSaveStateChanged() {
            if (PowerSaveMode.isEnabled()) {
                enable();
            } else {
                disable();
            }
        }
    });
    myApplication.addApplicationListener(new ApplicationAdapter() {

        @Override
        public void beforeWriteActionStart(@NotNull Object action) {
            disable();
        }

        @Override
        public void writeActionFinished(@NotNull Object action) {
            enable();
        }

        @Override
        public void applicationExiting() {
            disable();
        }
    }, this);
    VirtualFileManager.getInstance().addVirtualFileManagerListener(new VirtualFileManagerListener() {

        @Override
        public void beforeRefreshStart(boolean asynchronous) {
            disable();
        }

        @Override
        public void afterRefreshFinish(boolean asynchronous) {
            enable();
        }
    }, this);
    HeavyProcessLatch.INSTANCE.addListener(new HeavyProcessLatch.HeavyProcessListener() {

        @Override
        public void processStarted() {
        }

        @Override
        public void processFinished() {
            wakeUp();
        }
    }, this);
}
Also used : NewVirtualFile(com.intellij.openapi.vfs.newvfs.NewVirtualFile) BulkFileListener(com.intellij.openapi.vfs.newvfs.BulkFileListener) PowerSaveMode(com.intellij.ide.PowerSaveMode) Function(com.intellij.util.Function) VFileEvent(com.intellij.openapi.vfs.newvfs.events.VFileEvent) HeavyProcessLatch(com.intellij.util.io.storage.HeavyProcessLatch) ApplicationAdapter(com.intellij.openapi.application.ApplicationAdapter)

Example 2 with BulkFileListener

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

the class JrtFileSystemImpl method checkSubscription.

private void checkSubscription() {
    if (mySubscribed.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) {
            Set<VirtualFile> toRefresh = null;
            for (VFileEvent event : events) {
                if (event.getFileSystem() instanceof LocalFileSystem && event instanceof VFileContentChangeEvent) {
                    VirtualFile file = event.getFile();
                    if (file != null && "release".equals(file.getName())) {
                        String homePath = file.getParent().getPath();
                        ArchiveHandler handler = myHandlers.remove(homePath);
                        if (handler != null) {
                            handler.dispose();
                            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);
            }
        }
    });
}
Also used : NewVirtualFile(com.intellij.openapi.vfs.newvfs.NewVirtualFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ArchiveHandler(com.intellij.openapi.vfs.impl.ArchiveHandler) Set(java.util.Set) VFileEvent(com.intellij.openapi.vfs.newvfs.events.VFileEvent) VFileContentChangeEvent(com.intellij.openapi.vfs.newvfs.events.VFileContentChangeEvent) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) BulkFileListener(com.intellij.openapi.vfs.newvfs.BulkFileListener) Application(com.intellij.openapi.application.Application)

Example 3 with BulkFileListener

use of com.intellij.openapi.vfs.newvfs.BulkFileListener 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 4 with BulkFileListener

use of com.intellij.openapi.vfs.newvfs.BulkFileListener in project intellij-leiningen-plugin by derkork.

the class LeiningenProjectsManagerWatcher method start.

public synchronized void start() {
    final MessageBusConnection myBusConnection = project.getMessageBus().connect(myQueue);
    myBusConnection.subscribe(VirtualFileManager.VFS_CHANGES, new BulkFileListener() {

        public void before(List<? extends VFileEvent> vFileEvents) {
        }

        public void after(List<? extends VFileEvent> vFileEvents) {
            for (VFileEvent vFileEvent : vFileEvents) {
                //                    }
                if (vFileEvent instanceof VFileCreateEvent) {
                    if (isRelevant(vFileEvent.getPath())) {
                        manager.importLeiningenProject(vFileEvent.getFileSystem().findFileByPath(vFileEvent.getPath()), project);
                    }
                }
            }
        }

        private boolean isRelevant(String path) {
            return path != null && path.endsWith(LeiningenConstants.PROJECT_CLJ);
        }
    });
    myBusConnection.subscribe(ProjectTopics.PROJECT_ROOTS, new ModuleRootListener() {

        public void beforeRootsChange(ModuleRootEvent moduleRootEvent) {
        }

        public void rootsChanged(ModuleRootEvent moduleRootEvent) {
        }
    });
    myQueue.activate();
}
Also used : MessageBusConnection(com.intellij.util.messages.MessageBusConnection) VFileEvent(com.intellij.openapi.vfs.newvfs.events.VFileEvent) BulkFileListener(com.intellij.openapi.vfs.newvfs.BulkFileListener) ModuleRootEvent(com.intellij.openapi.roots.ModuleRootEvent) VFileCreateEvent(com.intellij.openapi.vfs.newvfs.events.VFileCreateEvent) ModuleRootListener(com.intellij.openapi.roots.ModuleRootListener)

Example 5 with BulkFileListener

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

the class VirtualFileManagerImpl method notifyPropertyChanged.

@Override
public void notifyPropertyChanged(@NotNull final VirtualFile virtualFile, @NotNull 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);
}
Also used : VFilePropertyChangeEvent(com.intellij.openapi.vfs.newvfs.events.VFilePropertyChangeEvent) BulkFileListener(com.intellij.openapi.vfs.newvfs.BulkFileListener) ArrayList(java.util.ArrayList) List(java.util.List) Application(com.intellij.openapi.application.Application)

Aggregations

BulkFileListener (com.intellij.openapi.vfs.newvfs.BulkFileListener)10 VFileEvent (com.intellij.openapi.vfs.newvfs.events.VFileEvent)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 VFileContentChangeEvent (com.intellij.openapi.vfs.newvfs.events.VFileContentChangeEvent)3 Application (com.intellij.openapi.application.Application)2 ModuleRootEvent (com.intellij.openapi.roots.ModuleRootEvent)2 ModuleRootListener (com.intellij.openapi.roots.ModuleRootListener)2 LocalFileSystem (com.intellij.openapi.vfs.LocalFileSystem)2 NewVirtualFile (com.intellij.openapi.vfs.newvfs.NewVirtualFile)2 VFilePropertyChangeEvent (com.intellij.openapi.vfs.newvfs.events.VFilePropertyChangeEvent)2 MessageBusConnection (com.intellij.util.messages.MessageBusConnection)2 ArrayList (java.util.ArrayList)2 PowerSaveMode (com.intellij.ide.PowerSaveMode)1 ApplicationAdapter (com.intellij.openapi.application.ApplicationAdapter)1 FileTypeEvent (com.intellij.openapi.fileTypes.FileTypeEvent)1 FileTypeListener (com.intellij.openapi.fileTypes.FileTypeListener)1 OrderRootType (com.intellij.openapi.roots.OrderRootType)1 VirtualFileManager (com.intellij.openapi.vfs.VirtualFileManager)1 ArchiveHandler (com.intellij.openapi.vfs.impl.ArchiveHandler)1 VFileCreateEvent (com.intellij.openapi.vfs.newvfs.events.VFileCreateEvent)1