1. 初始化项目
- 新建一个Idea 插件项目,当前选择的是gradle的 项目
-
修改gradle内的相关依赖仓库的镜像为国内仓库
image-20211221194353541- settings.gradle
pluginManagement {
repositories {
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
gradlePluginPortal()
}
}
rootProject.name = 'ideaPluginTest'
- build.gradle 添加仓库镜像并设置当前项目为UTF8格式
// Copyright 2000-2021 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
plugins {
id 'java'
id 'org.jetbrains.intellij' version '1.3.0'
}
group 'org.intellij.sdk'
version '2.0.0'
sourceCompatibility = 11
allprojects {
repositories {
maven { url 'https://maven.aliyun.com/repository/public/' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
mavenLocal()
mavenCentral()
}
}
repositories {
mavenCentral()
}
// See https://github.com/JetBrains/gradle-intellij-plugin/
intellij {
version = '2021.1.3'
plugins = ['com.intellij.java']
}
buildSearchableOptions {
enabled = false
}
patchPluginXml {
version = project.version
sinceBuild = '211'
untilBuild = '213.*'
}
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
-
plugin.xml idea插件的配置文件,用于定义插件的描述信息以及 插件注册信息
<idea-plugin> <id>org.example.ideaPluginTest</id> <!-- 插件名称--> <name>Plugin display name here</name> <!-- 作者相关信息--> <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> <!-- 该插件依赖的组件和插件信息,当前依赖idea平台基础库 --> <!-- please see https://plugins.jetbrains.com/docs/intellij/plugin-compatibility.html on how to target different products --> <depends>com.intellij.modules.platform</depends> <!-- 注册 idea 的扩展点 --> <extensions defaultExtensionNs="com.intellij"> <!-- Add your extensions here --> </extensions> <!-- 注册 idea 的菜单相关操作项 --> <actions> <!-- Add your actions here --> </actions> </idea-plugin>
2. 添加一个菜单选项
-
通过DevKit 向菜单添加一个自己的操作项
image-20211221200702480 -
向编辑器popup上添加一个操作项
image-20211221200946886确认添加后,会自动向plugin.xml 文件中添加一条action记录并创建相关的操作类
<idea-plugin> <id>org.example.ideaPluginTest</id> <!-- 插件名称--> <name>Plugin display name here</name> <!-- 作者相关信息--> <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> <!-- 该插件依赖的组件和插件信息,当前依赖idea平台基础库 --> <!-- please see https://plugins.jetbrains.com/docs/intellij/plugin-compatibility.html on how to target different products --> <depends>com.intellij.modules.platform</depends> <!-- 注册 idea 的扩展点 --> <extensions defaultExtensionNs="com.intellij"> <!-- Add your extensions here --> </extensions> <!-- 注册 idea 的菜单相关操作项 --> <actions> <!-- Add your actions here --> <action id="myAction" class="com.test.MyAction" text="自定义操作" description="我的自定义操作"> <add-to-group group-id="EditorPopupMenu" anchor="first"/> </action> </actions> </idea-plugin>
public class MyAction extends AnAction { @Override public void actionPerformed(AnActionEvent e) { // TODO: insert action logic here } }
-
添加该操作的处理事件,中文部分一定要通过Alt+Enter 选择转为拉丁语 ,变成Unicode码格式,要不会显示乱码
public class MyAction extends AnAction { @Override public void actionPerformed(AnActionEvent e) { //显示消息提示框 Messages.showInfoMessage("\u6211\u7684\u81EA\u5B9A\u4E49\u64CD\u4F5C", "\u63D0\u793A\u6807\u9898"); } }
-
调试和验证插件功能
image-20211221201302050双击执行gradle的 runide 任务就会自动启动一个自动安装好当前开发的插件的idea 环境
我们可以在这个idea 进行相关的运行测试和调试相关功能
-
在新启动的IDEA 中创建一个空的项目,并新建一个文件,在文件中右击编辑区域,就可以看到我们自己注册上的菜单功能
image-20211221201855031
3. 打包IDEA 插件并生成插件包
在gradle中执行buildPlugin操作,gradle编译生成zip包,只要按照对应的zip包插件就行
image-20211221202405339 image-20211221203012273
网友评论