实现 ApplicationComponent,BulkFileListener 接口,当然由于是 ApplicationComponent,因此需要在 plugin.xml 加上相关配置。
plugin.xml 添加配置:
<application-components> <component> <implementation-class>com.baiguiren.yaml.YamlApplicationComponent</implementation-class> </component> </application-components>
下面是文件内容:
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) { // ... } }
示例:
package com.baiguiren.yaml; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.components.ApplicationComponent; import com.intellij.openapi.vfs.VirtualFileManager; import com.intellij.openapi.vfs.newvfs.BulkFileListener; import com.intellij.openapi.vfs.newvfs.events.VFileEvent; import com.intellij.util.messages.MessageBusConnection; import org.jetbrains.annotations.NotNull; import java.util.HashSet; import java.util.List; public class YamlApplicationComponent implements ApplicationComponent, BulkFileListener { @SuppressWarnings("WeakerAccess") public static HashSet<String > dirtyProjects = new HashSet<>(); private final MessageBusConnection connection; public YamlApplicationComponent() { connection = ApplicationManager.getApplication().getMessageBus().connect(); } @Override public void initComponent() { connection.subscribe(VirtualFileManager.VFS_CHANGES, this); } @Override public void disposeComponent() { connection.disconnect(); } @Override public void before(@NotNull List<? extends VFileEvent> events) { } @Override public void after(@NotNull List<? extends VFileEvent> events) { for (VFileEvent event : events) { if (isLaravelProjectEvent(event)) { String projectName = getProjectName(event); if (!projectName.equals("")) { dirtyProjects.add(projectName); } } } } private boolean isLaravelProjectEvent(VFileEvent event) { return event.getPath().endsWith("code-gen/fields.txt"); } private String getProjectName(VFileEvent event) { String[] paths = event.getPath().split("/"); if (paths.length > 3) { return paths[paths.length - 3]; } return ""; } @SuppressWarnings("WeakerAccess") public static boolean projectIsDirty(String projectName) { return dirtyProjects.contains(projectName); } }
如上,我们可以通过 event 参数获取文件所在路径等信息。