intellij idea 编写插件
先从编写一个简单的单个点击事件开始,通过在菜单栏添加一个新功能然后点击弹出一个弹出框显示当前的文件名
首先新建一个IDE Plugin的项目(本文是在Intellij 2023.2.3 Community版本创建)
然后在src->main->resources->META-INF->plugin.xml文件添加一个action,表示要添加的点击事件,如果没有actions标签,需要将action放在actions标签里面:
<actions>
<action id="com.example.demo.PopupDialogAction" class="com.example.demo.PopupDialogAction"
text="Action Basics Plugin: Pop Dialog Action" description="SDK action example">
<add-to-group group-id="ToolsMenu" anchor="first"/>
<override-text place="MainMenu" text="Preview OFD"/>
<keyboard-shortcut first-keystroke="control alt A" second-keystroke="C" keymap="$default"/>
<mouse-shortcut keystroke="control button3 doubleClick" keymap="$default"/>
</action>
</actions>
在com.example.demo包下创建PopupDialogAction类,该类继承自AnAction,实现最简单的弹窗功能:
class PopupDialogAction: AnAction {
constructor(): super()
constructor(text: String, description: String, icon: Icon): super(text, description, icon)
override fun actionPerformed(e: AnActionEvent) {
// 获取档期按项目的信息
val currentProject = e.project
val msg = StringBuilder(e.presentation.text + " Selected")
val ele = e.getData(CommonDataKeys.NAVIGATABLE)
ele?.apply {
msg.append("\nSelected element: ").append(ele)
}
// dialog的标题
val title = e.presentation.description
// 弹出一个dialog,显示内容
Messages.showMessageDialog(currentProject, msg.toString(), title, Messages.getInformationIcon())
}
override fun getActionUpdateThread(): ActionUpdateThread {
return ActionUpdateThread.BGT
}
override fun update(e: AnActionEvent) {
// 选中的项目不为空
e.presentation.isEnabledAndVisible = e.project != null
}
}
上面就是一个完整简单的action点击事件插件,然后运行 run plugin
运行了一个新的插件项目窗口,如果没有项目则自己新建一个就行
可以看到有了新加的action,当我们点击action的时候就会弹窗了
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通