我想构建一个简单的idea插件,它将检测一种文件的变化,然后将它们转换为另一种格式。
当前,我使用这样的代码来执行此操作:
VirtualFileManager.getInstance().addVirtualFileListener(new VirtualFileAdapter() {
@Override
public void contentsChanged(VirtualFileEvent event) {
// do something
}
});
它可以工作,但效率不高。
我找到了this article说:
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.
但我找不到任何实现它的例子。如何做到这一点?
最佳答案
我想你现在已经找到答案了,但对其他人来说似乎是这样的
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) {
// ...
}
...
}
关于plugins - Intellij-idea中如何检测文件变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6573364/