Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect file changes in Intellij-idea?

I want to build a simple idea plugin, which will detect the changes of a kind of file, then convert them to another format.

Current, I use such code to do this:

VirtualFileManager.getInstance().addVirtualFileListener(new VirtualFileAdapter() {
    @Override
    public void contentsChanged(VirtualFileEvent event) {
         // do something
    }
});

It works, but not efficient.

I found this article says:

The most efficient way to listen to VFS events is to implement the BulkFileListener interface and to subscribe with it to the VirtualFileManager.VFS_CHANGES topic.

But I can't find any example to implement it. How to do that?

like image 803
Freewind Avatar asked Jul 04 '11 15:07

Freewind


1 Answers

I guess you'll have found the answer by now, but for others it seems to work like this

public class A implements ApplicationComponent, BulkFileListener {

    private final MessageBusConnection connection;

    public A() {
        connection = ApplicationManager.getApplication().getMessageBus().connect();
    }

    public void initComponent() {
        connection.subscribe(VirtualFileManager.VFS_CHANGES, this);
    }

    public void disposeComponent() {
        connection.disconnect();
    }

    public void before(List<? extends VFileEvent> events) {
        // ...
    }

    public void after(List<? extends VFileEvent> events) {
        // ...
    }

    ...
}
like image 59
Nils Olsson Avatar answered Oct 15 '22 14:10

Nils Olsson