Jetbrain IDE插件开发入门

Jetbrain号称IDE界的Adobe,各个平台、各个环境下的IDE无论从易用性还是外观上都是十分优秀的,这份笔记将会记录如何在JetBrains中编写简单的插件。

一、创建项目

  1. 安装JetBrains IDEA

  2. 新建项目,选择IntelliJ Platform Plugin,这里我们选择开发IDEA的插件,所以Project SDK选择IDEA的安装目录

    create

二、实现

  1. 创建好项目后,打开resources目录下的plugins.xml,在这里我们要注册我们的插件

    加一条<depends>com.intellij.modules.platform</depends>,后面打包的时候需要

    <actions>中注册自己的插件

    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
    52
    <idea-plugin>
    <id>com.ph0en1x.Registration</id>
    <name>Jetbrain Plugin</name>
    <version>1.0</version>
    <vendor email="support@yourcompany.com" url="https://www.ph0en1x.space">Ph0en1x Notebook</vendor>

    <description><![CDATA[
    Jetbrain Plugin Demo
    ]]></description>

    <change-notes><![CDATA[
    Add change notes here.<br>
    <em>most HTML tags may be used</em>
    ]]>
    </change-notes>

    <!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description -->
    <idea-version since-build="173.0"/>

    <!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
    on how to target different products -->
    <!-- uncomment to enable plugin in all products
    <depends>com.intellij.modules.lang</depends>
    -->

    <depends>com.intellij.modules.platform</depends>
    <!--<application-components>-->
    <!--<component>-->
    <!--<implementation-class>com.ph0en1x.Registration</implementation-class>-->
    <!--</component>-->
    <!--</application-components>-->

    <extensions defaultExtensionNs="com.intellij">
    <!-- Add your extensions here -->
    </extensions>

    <actions>
    <!-- Add your actions here -->
    <group id="HelloWorld" text="Ph0en1x Menu">
    <add-to-group group-id="MainMenu" anchor="last" />
    </group>

    <action class="com.ph0en1x.MenuAction" id="menuAction1" text="显示对话框">
    <add-to-group group-id="HelloWorld" anchor="first" />
    </action>

    <action class="com.ph0en1x.MenuAction2" id="menuAction2" text="在Help里" description="A simple IDEA plugin.">
    <add-to-group group-id="HelpMenu" anchor="last"/>
    </action>
    </actions>

    </idea-plugin>

    我这里注册了两个插件,第一个插件在一个新建的选项<group>下面,新建选项显示的文字是显示对话框

    class:插件入口类

    id:唯一标识插件

    text:选项显示的文字

    description:状态栏显示的文字

    icon:显示的图标(这里我没加)

    add-to-group:表示要添加到哪个选项下面

    anchor:插入选项的位置

  2. 实现Action

    src目录下创建与之前定义的相应的入口类,入口类要继承于com.intellij.openapi.actionSystem.AnAction

    com.ph0en1x.MenuAction 实现点击后显示一个对话框

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    package com.ph0en1x;

    import com.intellij.openapi.actionSystem.AnAction;
    import com.intellij.openapi.actionSystem.AnActionEvent;
    import com.intellij.openapi.ui.Messages;

    public class MenuAction extends AnAction{
    @Override
    public void actionPerformed(AnActionEvent anActionEvent) {
    Messages.showInfoMessage("Show some message", "This is title");
    }
    }

    com.ph0en1x.MenuAction2实现点击后显示一个对话框询问姓名,输入姓名后弹出欢迎界面

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    package com.ph0en1x;

    import com.intellij.openapi.actionSystem.AnAction;
    import com.intellij.openapi.actionSystem.AnActionEvent;
    import com.intellij.openapi.project.Project;
    import com.intellij.openapi.ui.Messages;

    public class MenuAction2 extends AnAction{
    @Override
    public void actionPerformed(AnActionEvent anActionEvent) {
    Project project = anActionEvent.getProject();
    String name = Messages.showInputDialog(project, "What's your name?", "This is title", Messages.getQuestionIcon());
    Messages.showMessageDialog(project, "Hello " + name + "!", "This is title", Messages.getInformationIcon());
    }
    }

  3. 完成后点击run,会打开一个新的IDEA,打开一个项目后,新的IDEA里就会有我们刚刚编写的插件

    action1

    result1


    action2

    result2_1

    result2_2