其他
开发属于自己的第一款 IDEA 插件!
往期热门文章:
1、《往期精选优秀博文都在这里了!》 2、又一个程序员跑路删库跑路被抓了,导致服务器瘫痪 36 个小时! 3、恕我直言,有了这款 IDEA 插件,你可能只需要写 30% 的代码。。。 4、Java8 的 Stream API 的确牛X,但性能究竟如何呢? 5、关于MySQL索引面试题的6连炮!招架的住吗? 6、真香!IDEA 最新版本,支持免打扰和轻量模式!
一、开发环境
IntelliJ IDEA Community Edition IntelliJ IDEA Community Edition 源码 Plugin DevKit 插件 IntelliJ Platform SDK
1.1、安装IntelliJ IDEA Community Edition
1.2、下载IntelliJ IDEA Community Edition源码
1.3、添加IDEA jdk
IDEA jdk
来运行插件:/lib/tools.jar
到classpath中。1.4、配置IntelliJ Platform SDK
File | Project Structure
新建一个IntelliJ Platform SDK
:IDEA jdk
:二、第一个插件
2.1、新建工程
IntellJ Platform Plugin
,然后Project SDK指定刚刚新建的plugin sdk:src
和resources
。src
是插件代码目录,resource
是插件资源目录,其中META-INF/plugin.xml
是插件的描述文件,就像Java web项目的web.xml
一样。<id>com.your.company.unique.plugin.id</id>
<name>Plugin display name here</name>
<version>1.0</version>
<vendor email="support@yourcompany.com" url="http://www.yourcompany.com">YourCompany</vendor>
[ <description><![CDATA[](https://mp.weixin.qq.com/s/PiAxqEhkR8g1AOYGGS5Yqw)
Enter short description for your plugin here.<br>
<em>most HTML tags may be used</em>
]]></description>
[ <change-notes><![CDATA[](https://mp.weixin.qq.com/s/PiAxqEhkR8g1AOYGGS5Yqw)
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="145.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>
-->
<extensions defaultExtensionNs="com.intellij">
<!-- Add your extensions here -->
</extensions>
<actions>
<!-- Add your actions here -->
</actions>
</idea-plugin>
2.2、新建一个Action
AnAction
类,这个类有一个虚方法actionPerformed
,这个方法会在每次菜单被点击时调用。继承 AnAction
类,在actionPerformed
方法中实现插件逻辑注册action,有两种方式,通过代码注册和通过 plugin.xml
注册
// 如果通过Java代码来注册,这个构造函数会被调用,传给父类的字符串会被作为菜单项的名称
// 如果你通过plugin.xml来注册,可以忽略这个构造函数
public TextBoxes() {
// 设置菜单项名称
super("Text _Boxes");
// 还可以设置菜单项名称,描述,图标
// super("Text _Boxes","Item description",IconLoader.getIcon("/Mypackage/icon.png"));
}
public void actionPerformed(AnActionEvent event) {
Project project = event.getData(PlatformDataKeys.PROJECT);
String txt= Messages.showInputDialog(project, "What is your name?", "Input your name", Messages.getQuestionIcon());
Messages.showMessageDialog(project, "Hello, " + txt + "!\n I am glad to see you.", "Information", Messages.getInformationIcon());
}
}
plugin.xml
中注册这个Action:<group id="MyPlugin.SampleMenu" text="_Sample Menu" description="Sample menu">
<add-to-group group-id="MainMenu" anchor="last" />
<action id="Myplugin.Textboxes"class="Mypackage.TextBoxes" text="Text _Boxes" description="A test menu item" />
</group>
</actions>
2.3、运行插件
三、参考资料
Setting Up a Development Environment How to make an IntelliJ IDEA plugin in less than 30 minutes