简介
IntelliJ插件开发 - 消息文章中介绍了消息的使用方法,除了自定义消息外,开发插件过程中需要经常与系统内置消息打交道,例如:插件被加载,卸载,用户输入了一些命令等。
使用方法
找到自己需要监听的内置消息后,需要辨别它是projectLevel还是applicationLevel,可以通过消息类内部的注解来看,例如LookupManagerListener :
public interface LookupManagerListener {
void activeLookupChanged(@Nullable Lookup oldLookup, @Nullable Lookup newLookup);
@Topic.ProjectLevel
Topic<LookupManagerListener> TOPIC = Topic.create("lookup manager listener", LookupManagerListener.class);
}
它使用了@Topic.ProjectLevel
,应当把监听配置放到projectListeners
中。
<idea-plugin>
<projectListeners>
<listener class="com.demo.MyLookupManagerListener" topic="com.intellij.codeInsight.lookup.LookupManagerListener"/>/>
</projectListeners>
</idea-plugin>
监听类MyLookupManagerListener的实现:
public class MyLookupManagerListener implements LookupManagerListener {
@Override
public void activeLookupChanged(@Nullable Lookup oldLookup, @Nullable Lookup newLookup){
// add your logic
}
}
常用内置消息
LookupManagerListener
com.intellij.codeInsight.lookup.LookupManagerListener
系统内置补全监听器,提供了activeLookupChanged方法,可以监听内置提示变更事件(你敲一个变量名的时候,系统可能会把符合的变量以popup的形式弹出来)。此监听器可以监听系统提示popup显示了,获取取消了事件。
FileEditorManagerListener
com.intellij.openapi.fileEditor.FileEditorManagerListener
编辑器事件监听器,可以监听文件打开、关闭、切换了文件。
CommandListener
com.intellij.openapi.command.CommandListener
监听命令事件,任何按钮点击都是一个command。
DynamicPluginListener
com.intellij.ide.plugins.DynamicPluginListener
监听插件装载、卸载事件。
AnActionListener
com.intellij.openapi.actionSystem.ex.AnActionListener
监听AnAction事件(idea中的菜单),例如CutAction、UndoAction或者自己实现的AnAction