其他
开发属于自己的第一款 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
回复 【idea激活】即可获得idea的激活方式
回复 【Java】获取java相关的视频教程和资料
回复 【SpringCloud】获取SpringCloud相关多的学习资料
回复 【python】获取全套0基础Python知识手册
回复 【2020】获取2020java相关面试题教程
阅读更多
相信自己,没有做不到的,只有想不到的
在这里获得的不仅仅是技术!
喜欢就给个“在看”