IDEA Plug-in Development Tutorial (1)

I am participating in the "Nuggets · Sailing Program"

background

As developers using IDEA/Android Studio, everyone has used plug-ins more or less. With the version upgrade of development tools, many plug-ins will have compatibility problems, and the author may have stopped maintenance a few years ago.

If we can master some plug-in development knowledge and encounter plug-ins that have stopped maintenance, we can automatically maintain them. If we have some ideas, we can even develop plug-ins by ourselves.

At present, there are very few online tutorials about IDEA plug-ins, and some tutorials are relatively old, and many configurations have changed, so I plan to write a few tutorials on developing IDEA plug-ins, hoping to help everyone.

This article is the first part of the tutorial. Through this article, you can create a simple HelloWorld plug-in project, familiarize yourself with the IDEA plug-in development environment, and understand the IDEA plug-in project structure.

development tools

To develop IDEA plug-ins, you need to download IDEA, the download address: www.jetbrains.com/zh-cn/idea/…Choose the free Community version.

Create project

Open IDEA, click New Project, then select IDE Plugin, as shown below

idea_plugin_guide_1.png

Enter the relevant information of the plug-in project, and then click the Createbutton to create the project. The project structure is as follows:

DxyJsonToDart
├── .idea
│   └── .gitignore
│   └── gradle.xml
│   └── misc.xml
│   └── vcs.xml
├── .run
│   └── Run IDE with Plugin.run.xml
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── src
│   ├── main
│   │   ├── java
│   │   └── resources
│   │       └── META-INF
│   │           └── plugin.xml
│   │           └── pluginIcon.svg
├── .gitignore
├── build.gradle.kts
├── gradlew
├── gradlew.bat
└── settings.gradle.kts
复制代码

Those who have developed Android projects should be familiar with the project structure, which is basically similar to the Android project. Our code will be written src/main/java/包名in , which build.gradle.ktsis the configuration file of Gradle and the configuration file plugin.xmlof the plug-in. In plug-in development, we will often communicate with this Two configuration files to deal with.

Let's take a brief look at the build.gradle.kts file:

plugins {
    id("java")
    id("org.jetbrains.intellij") version "1.6.0"
}

group = "cn.dxy.app"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

intellij {
    version.set("2021.3")
    type.set("IC") 
    plugins.set(listOf(/* Plugin Dependencies */))
}

tasks {
    withType<JavaCompile> {
        sourceCompatibility = "11"
        targetCompatibility = "11"
    }

    patchPluginXml {
        sinceBuild.set("212")
        untilBuild.set("223.*")
    }

    signPlugin {
        certificateChain.set(System.getenv("CERTIFICATE_CHAIN"))
        privateKey.set(System.getenv("PRIVATE_KEY"))
        password.set(System.getenv("PRIVATE_KEY_PASSWORD"))
    }

    publishPlugin {
        token.set(System.getenv("PUBLISH_TOKEN"))
    }
}

复制代码

这个文件中的配置信息具体是什么意思,后续会讲到,暂时先不管。这时我们什么代码没有写,直接运行项目,因为在上面配置了 version.set("2021.3"),运行时会下载一个 2021.3 版本的 IDEA。这个 IDEA 大概有 1~2G 大小,下载成功后,这个 IDEA 会运行起来,运行结果如下图所示。

idea_plugin_guide_2.png

如果你切换到 Plugins 选项,你会发现我们开发的插件被自动安装到这个 IDEA 中了。

我第一次解除插件开发时有点蒙:一个 IDEA 运行启动了另外一个 IDEA。后来我才想通,因为我们的插件是要运行在 IDEA/Android Studio 中的,我们的插件适配不同的版本,还要进行代码调试,所以只能一个 IDEA 运行启动了另外一个 IDEA。

建议大家动手实践一下,会有更加直观的感受。

新建 Action

想一下我们平时是如何使用插件的,我们可能是右键点击了文件夹,或者是点击了某个菜单,这时会弹出一些选项,这些选项一般就是某个插件的入口,这些入口在 IDEA 插件中叫做 Action

下面我们写点代码,看看如何创建一个 Action,在项目的代码包中,点击右键,New => Plugin DevKit => Action,可以创建一个 Action

idea_plugin_guide_3.png

添加 Action 相关信息

idea_plugin_guide_4.png

在上图中,Add to Group 选择的是 Groups: NewGroup(New) Action: NewFile(File) Anchor: First,那么我们新建的这个 Action 就会出现在 New 这个选项中的第一个位置

点击确定按钮后,会新增一个继承 AnAction 的类 DxyJsonToDartAction

package cn.dxy.app.dxyjsontodart;

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

public class DxyJsonToDartAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {
        // TODO: insert action logic here
    }
}
复制代码

并且会在 plugin.xml 中新增这个 action

<actions>
    <action id="DxyJsonToDart" class="cn.dxy.app.dxyjsontodart.DxyJsonToDartAction" 
        text="我是 name" description="我是 description">
        <add-to-group group-id="NewGroup" anchor="first"/>
    </action>
</actions>
复制代码

这时我们再运行插件,看看我们刚刚编写的代码有什么效果:

After IDEA starts, we open a project at random, right-click on the folder, and we can see that the plugin option we just developed appears in the Newmenu .我是 name

idea_plugin_guide_5.png

Reference other plugins

The plug-ins we write can depend on other plug-ins, and after relying on other plug-ins, they can use their classes

Official documentation: plugins.jetbrains.com/docs/intell…

Open the project build.gradle.kts file and modify the code in plugins.set() as follows:

intellij {
    version.set("2021.3")
    type.set("IC") // Target IDE Platform

    plugins.set(
        listOf(
            "Dart:213.5744.122", //https://plugins.jetbrains.com/plugin/6351-dart/versions
            "io.flutter:63.2.4",//https://plugins.jetbrains.com/plugin/9212-flutter/versions/stable
            "GsonFormatPlus:1.6.1",//https://plugins.jetbrains.com/plugin/14949-gsonformatplus/versions
            "com.jack.plugin.autojson:1.5", //https://plugins.jetbrains.com/plugin/11600-autojson/versions
            "com.crzsc.FlutterAssetsGenerator:2.0.0",//https://plugins.jetbrains.com/plugin/15427-flutterassetsgenerator/versions
            "com.ruiyu.ruiyu:4.4.2", //https://plugins.jetbrains.com/plugin/11415-flutterjsonbeanfactory-only-null-safety-/versions
        )
    )
}
复制代码

Run the project again. After the operation is successful, switch to the Plugin option, as shown in the following figure:

idea_plugin_guide_6.png

We can see that the IDEA running this time will package and install all the plugins introduced above.

at last

That's all for this tutorial. If you practice, I believe you will have a general understanding of IDEA plug-in development.

More content will be introduced in subsequent tutorials.

In addition, if you are interested in those aspects, you can also leave a comment, and if I know of it, I will also share it.

Guess you like

Origin juejin.im/post/7145737911344824328