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


    官方文档 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...
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

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

    通过Plugin.xml进行注册

    EditorPopupMenu 编辑器右键菜单的GroupId

    创建单个菜单

    <actions>
            <action id="icu.weboys.sundriesplugin.core.translate.TranslateAction"
                    class="icu.weboys.sundriesplugin.core.translate.TranslateAction" text="Translate" description="翻译">
                <add-to-group group-id="EditorPopupMenu" anchor="first"/>
            action>
        actions>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在Anactions标签内加入一个 action

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

    创建菜单组

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

    
    
    • 1

    其Group中属性与Action属性相同

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

    例如

    <actions>
            <group popup="true" class="icu.weboys.sundriesplugin.core.quicksearch.QuickSearchGroup" id="quickstart.QuickSearchGroup" text="在Web中搜索..." icon="AllIcons.Actions.Search" description="Quick search">
                <add-to-group group-id="EditorPopupMenu" anchor="first"/>
            group>
    actions>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    如何给其添加子菜单

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

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

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

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

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

    <add-to-group group-id="quickstart.QuickSearchGroup" anchor="first"/>
    
    • 1

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

    <actions>
            <action id="icu.weboys.sundriesplugin.core.translate.TranslateAction"
                    class="icu.weboys.sundriesplugin.core.translate.TranslateAction" text="Translate" description="翻译">
                <add-to-group group-id="quickstart.QuickSearchGroup" anchor="first"/>
            action>
        actions>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

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

    通过代码创建菜单组

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

    <group popup="true" class="icu.weboys.sundriesplugin.core.quicksearch.QuickSearchGroup" id="quickstart.QuickSearchGroup" text="在Web中搜索..." icon="AllIcons.Actions.Search" description="Quick search">
         <add-to-group group-id="EditorPopupMenu" anchor="first"/>
    group>
    
    • 1
    • 2
    • 3

    然后我们创建一个类 并继承 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<AnAction> {
            // return Array;
            // 一个AnAction对象的数组
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    我们在这个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<@ActionText String> 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);

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

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

    例如

    package icu.weboys.sundriesplugin.core.quicksearch
    
    class QuickSearchAction(name:String,des:String,icon:ICON) : AnAction(name,des,icon){
        override fun actionPerformed(e: AnActionEvent) {
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

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


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

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

  • 相关阅读:
    【kubernetes】带你入门k8s中的HPA
    大数据-hadoop常用命令
    车辆管理系统(asp.net+SqlServer)
    文字转音频软件哪个好用?这3款文字转换音频的软件识别率很高
    js中将字符串转换为正则
    Leetcode刷题【hot100】盛最多水的容器
    LockSupport从入门到深入理解
    Pytest测试框架一键动态切换环境思路及方案
    S7协议规范常量
    Linux上如何部署SpringBoot项目——手把手教会你
  • 原文地址:https://blog.csdn.net/qq_32447361/article/details/126824493