开发者

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 sa开发者_如何学Goys:

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?


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) {
        // ...
    }

    ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜