IntelliJ Plugin开发 (一) 创建一个编辑器上的右键菜单

您所在的位置:网站首页 idea右键菜单怎么取消 IntelliJ Plugin开发 (一) 创建一个编辑器上的右键菜单

IntelliJ Plugin开发 (一) 创建一个编辑器上的右键菜单

2024-01-18 14:22| 来源: 网络整理| 查看: 265

官方文档 IDE Plugin SDK Doc

JDK版本: 17 IDEA版本: IU-2022.2.1 Gradle版本:7.5 编程语言:Java和kotlin

AnAction是Idea插件的操作类 通过AnAction,可以使我们的插件在Idea创建相应的操作,例如 新建文件,打开文件以及打开一个窗口等,并将其添加到 菜单栏,工具栏,右键菜单等地方

创建一个AnAction对象 package icu.weboys.sundriesplugin.core.translate class TranslateAction : AnAction(){ override fun actionPerformed(e: AnActionEvent) { //Todo... } }

新建一个类后继承 AnAction,并实现 actionPerformed方法即可完成一个简单的操作对象,接下来就是让这个操作与Idea的菜单进行绑定

通过Plugin.xml进行注册

EditorPopupMenu 编辑器右键菜单的GroupId

创建单个菜单

在Anactions标签内加入一个 action

属性说明id当前操作的id,可自定义填写,或写当前绑定的AnAction对象的包名.类名/类名class绑定的AnAction对象text当前这个菜单要显示什么内容description当前菜单的介绍icon菜单旁边要显示什么图标 创建菜单组

菜单组即为 当前菜单下有N个子菜单

其Group中属性与Action属性相同

属性说明id当前操作的id,可自定义填写,或写当前绑定的AnAction对象的包名.类名/类名class绑定的AnAction对象text当前这个菜单要显示什么内容description当前菜单的介绍icon菜单旁边要显示什么图标 例如 如何给其添加子菜单

在这里正好说一下上方的 add-to-group 这个标签

属性说明group-id菜单组Idanchor添加的位置 可选值 first,last ,before 啥效果自己摸索吧

从名字上就可以知道它的作用就是将 当前这个Action添加到一个组

在上面我们创建了一个 名字叫做 “在Web中搜索…”的Group 和 一个 翻译的菜单 (从自己项目中拷贝的代码,不同功能的,主要是上面那个是动态添加的,没有现成的 将就点看吧)

这个是添加到 [在Web中搜索]这个组的

我们将它添加到 翻译的 标签内

这样我们在预览时,可以看到,当鼠标点击 在web中搜索这个菜单上时,后面会显示出 翻译的菜单,当点击这个菜单后,会执行我们的actionPerformed这个方法

通过代码创建菜单组

首先我们还是要在Plugin.xml中注册一个 group

然后我们创建一个类 并继承 ActionGroup 对象

package icu.weboys.sundriesplugin.core.quicksearch import com.intellij.openapi.actionSystem.ActionGroup import com.intellij.openapi.actionSystem.AnAction import com.intellij.openapi.actionSystem.AnActionEvent import icu.weboys.sundriesplugin.config.QsConfigFactory class QuickSearchGroup : ActionGroup() { override fun getChildren(e: AnActionEvent?): Array { // return Array; // 一个AnAction对象的数组 } }

我们在这个getChildren方法中返回的 这个数组,就是它的子菜单 这样插件启用用,会把这些子菜单添加到在web中搜索的下面

设置子菜单的图标文字说明等内容

但是用到最开始创建的AnAction对象,而且我们没有在plugin.xml中搞它的文字和图标,所以我们需要在代码中设置

先看看 AnAction的构造方法 public AnAction() { // avoid eagerly creating template presentation } /** * Creates a new action with {@code icon} provided. Its text, description set to {@code null}. * * @param icon Default icon to appear in toolbars and menus (Note some platform don't have icons in menu). */ public AnAction(@Nullable Icon icon) { this(Presentation.NULL_STRING, Presentation.NULL_STRING, icon); } /** * Creates a new action with the specified text. Description and icon are * set to {@code null}. * * @param text Serves as a tooltip when the presentation is a button and the name of the * menu item when the presentation is a menu item. */ public AnAction(@Nullable @ActionText String text) { this(text, null, null); } /** * Creates a new action with the specified text. Description and icon are * set to {@code null}. * * @param dynamicText Serves as a tooltip when the presentation is a button and the name of the * menu item when the presentation is a menu item. *

* Use it if you need to localize action text. */ public AnAction(@NotNull Supplier dynamicText) { this(dynamicText, Presentation.NULL_STRING, null); } /** * Constructs a new action with the specified text, description and icon. * * @param text Serves as a tooltip when the presentation is a button and the name of the * menu item when the presentation is a menu item * @param description Describes current action, this description will appear on * the status bar when presentation has focus * @param icon Action's icon */ public AnAction(@Nullable @ActionText String text, @Nullable @ActionDescription String description, @Nullable Icon icon) { this(() -> text, () -> description, icon);

看完后应该就不用说咋设置了吧

例如 package icu.weboys.sundriesplugin.core.quicksearch class QuickSearchAction(name:String,des:String,icon:ICON) : AnAction(name,des,icon){ override fun actionPerformed(e: AnActionEvent) { } }

这些就是怎么在插件中创建菜单的教程了

代码都是用的这个仓库的,这个是最近在写的一个idea工具插件,现在已经完成了 在web中搜索…(貌似有很多这种右键选中文本然后在浏览器中打开搜索的.),翻译的功能还在写中,差个配置ui就可以用 百度翻译了,等写完ui后在写其他翻译源

主要是写来自己用,所以都是写自己会用到的功能😂 仓库地址



【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3