写 Java 代码的时候,经常会涉及到重复性的操作,这个时候就会想要是有这样一个插件就好了,如果是大家都会遇到的场景,IDE 或许已经提供了,再不然也有可能有人编写了相关的插件。要是这个操作是你们的编码环境特有的,那就只能自己写工具了。所以这里来学学如何编写 IDEA 插件,让自己的编程环境更加强大,更好的进行装逼。
开发环境
开发 IDEA 插件有以下这些依赖:
- IntelliJ IDEA Community Edition
- IntelliJ IDEA Community Edition 源码
- Plugin DevKit 插件
- IntelliJ Platform SDK
你可能已经安装了 Ultimate 版本,但是你还是需要安装 IDEA 的社区版本。因为商业版是闭源的,所以在调试时无法调试核心代码。
社区版的安装包里是不包含源码的,所以我们需要手动从 github 上 clone 一份:
git clone --depth 1 git://git.jetbrains.org/idea/community.git idea
关于从源码运行 IDEA 的方法参考:
http://www.jetbrains.org/intellij/sdk/docs/basics/checkout_and_build_community.html
添加 IDEA jdk
虽然不知道原因,但是根据
http://www.jetbrains.org/intellij/sdk/docs/basics/checkout_and_build_community.html
,我们需要建立一个IDEA jdk
来运行插件:

除非你在 Mac 上使用官方 JDK,否则你需要手动添加/lib/tools.jar
到 classpath 中。
配置 IntelliJ Platform SDK
打开File | Project Structure
新建一个IntelliJ Platform SDK
:

Java SDK 选择我们刚刚建立的
IDEA jdk
:

然后我们可以把下载的 IDEA 社区版源码添加到源码路径中,这样在调试时,就可以调试 IDEA 自身的代码了:


第一个插件
我们来编写一个最简单的插件来学习编写一个插件的完整步骤。
新建工程
选择IntellJ Platform Plugin
,然后 Project SDK 指定刚刚新建的 plugin sdk:

新建的插件项目:

插件根目录下有两个目录src
和resources
。src
是插件代码目录,resource
是插件资源目录,其中META-INF/plugin.xml
是插件的描述文件,就像 Java web 项目的web.xml
一样。
plugin.xml 默认的内容如下:
<idea-plugin>
<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[
Enter short description for your plugin here.<br>
<em>most HTML tags may be used</em>
]]></description>
<change-notes><![CDATA[
Add change notes here.<br>
<em>most HTML tags may be used</em>
]]>
</change-notes>
<idea-version since-build="145.0"/>
<extensions defaultExtensionNs="com.intellij">
</extensions>
<actions>
</actions>
</idea-plugin>
新建一个 Action
插件扩展 IDEA 最常见的方式就是在菜单栏或者工具栏中添加菜单项,用户通过点击菜单项来触发插件功能。IDEA 提供了AnAction
类,这个类有一个虚方法actionPerformed
,这个方法会在每次菜单被点击时调用。
新建一个自定义的 Action 有两个步骤:
- 继承
AnAction
类,在actionPerformed
方法中实现插件逻辑 - 注册 action,有两种方式,通过代码注册和通过
plugin.xml
注册
我们先写一个简单的 Action 类:
public class TextBoxes extends AnAction {
public TextBoxes() {
super("Text _Boxes");
}
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:
<actions>
<group text="_Sample Menu" description="Sample menu">
<add-to-group group-id="MainMenu" anchor="last" />
<action text="Text _Boxes" description="A test menu item" />
</group>
</actions>
这里我们新建了一个菜单组,其中 text 字符串的下划线表示这个字母作为快捷键。这个菜单显示的效果如下:

除了手动新建 Action,IDEA 还提供了快速新建的方法,在代码目录上点击新建,可以看到 Action:

可以在这个面板中填写你要新建的 Action 信息,IDEA 会帮你新建类,还有在 plugin.xml 中帮你注册:

运行插件
运行插件特别简单,和运行普通 Java 代码一样,点击运行或者调试的按钮,就会启动一个新的 IDEA 实例,这个实例中插件是生效的。
点击 Text Boxes 就可以看到插件的效果了。
- END -
最后,祝大家技术在沉淀中升华。
最新热文
15 个优秀开源的 Spring Boot 学习项目,一网打尽!
总有小惊喜
关注公众号:「Java 知己」,每天更新Java知识哦,期待你的到来!
-
发送「Idea」,获取最新Idea激活工具。
-
发送「Group」,加入无广告微信Java技术群。
-
发送「面试」,领取BATJ面试资料、面试视频攻略。
-
发送「玩转算法」,领取《玩转算法》系列视频教程。
-
千万不要发送「1024」…

网友评论