I like to recommend some good IDEA plug-ins to everyone. These plug-ins have greatly improved our productivity and coding comfort.

Have you ever thought of developing an IDEA plug-in?

I thought about it, but I didn’t try it. A reader wanted me to write an introduction to IDEA development, so I spent a little time over the weekend looking into it.

However, this article is just a simple introduction to the development of IDEA plug-in. I have limited energy and will not discuss too much in depth for the time being. If you already have experience with IDEA plugin development, you can skip this article because it will waste 3 minutes of your time.

Good nonsense not to say! Let’s get right to it!

01 Create a new gradle-based plug-in project

Here we are developing plug-ins based on Gradle, which is IntelliJ’s official recommended solution for plug-in development.

First, select the Gradle project type and check the corresponding dependencies.

Second, fill in the project related attributes such as GroupId and ArtifactId.

Step 3: Wait for the project to download dependencies.

The first time you create an IDEA plug-in project, this step will be slow. Because we need to download the SDK for IDEA plug-in development.

02 Plugin project Structure Overview

The newly completed project structure is shown in the figure below.

The two configuration files that need extra attention here are.

plugin.xml: The core configuration file of the plug-in. It allows you to configure the plug-in name, plug-in introduction, plug-in author information, Action, and so on.

<idea-plugin>
    <id>github.javaguide.my-first-idea-plugin</id>
    <! -- Plugin name -->
    <name>Beauty</name>
    <! -- Plugin author information -->
    <vendor email="[email protected]" url="https://github.com/Snailclimb">JavaGuide</vendor>
    <! -- Introduction to the plugin -->
    <description><! </em>]> </em> </em></description>

    <! -- please see https://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html on how to target different products -->
    <depends>com.intellij.modules.platform</depends>

    <extensions defaultExtensionNs="com.intellij">
        <! -- Add your extensions here -->
    </extensions>

    <actions>
        <! -- Add your actions here -->
    </actions>
</idea-plugin>
Copy the code

build.gradle: Project dependency profile. It allows you to configure project third-party dependencies, plug-in versions, plug-in version update records, and more.

plugins {
    id 'java'
    id 'org.jetbrains.intellij' version '0.6.3'
}

group 'github.javaguide'
// The current plug-in version
version 1.0 the SNAPSHOT ' '

repositories {
    mavenCentral()
}

// Project dependencies
dependencies {
    testCompile group: 'junit'.name: 'junit'.version: '4.12'
}

// See https://github.com/JetBrains/gradle-intellij-plugin/
// The IDEA version of the plug-in is currently being developed
intellij {
    version '2020.1.2'
}
patchPluginXml {
    // Version update record
    changeNotes """ Add change notes here.

most HTML tags may be used"""
} Copy the code

For those of you who have not developed IDEA plug-ins, these two profiles may be confusing. Therefore, I specifically found a ready-made plugin provided by IDEA plugin market to illustrate. If you look at the following configuration file, it is very, very clear.

That’s very sweet! If that doesn’t get you to like it, why do I need this article?!

03 Manually Creating an Action

We can think of actions as idea-enhanced event response handlers where we can customize some event processing logic/actions. For example, when you click on a menu, we do a display dialog.

Step one, right clickjavaDirectory and select New an Action

Second, configure Action information such as the display name.

Once created, our plugin.xml < Actions > node will automatically generate the Action information we just created:

<actions>
    <! -- Add your actions here -->
    <action id="test.hello" class="HelloAction" text="Hello" description="IDEA plugin introduction">
      <add-to-group group-id="ToolsMenu" anchor="first"/>
    </action>
</actions>
Copy the code

And generate a class called HelloAction in the Java directory. Also, this class inherits AnAction and overrides the actionPerformed() method. The actionPerformed method is like the onClick method in JS, which triggers the corresponding action when you click.

I simply modified the actionPerformed method and added a line of code. This line of code is as simple as displaying a dialog box and displaying some information.

public class HelloAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {
        // Displays the dialog box and the corresponding information
        Messages.showInfoMessage("Material is not enough, plug-in to gather!"."Hello"); }}Copy the code

In addition, as we said above, each action is assigned to a Group, which can simply be regarded as the existing menu in IDEA.

Let me give you an example. The Action Group I created above is ToolsMenu(Tools). In this case, the Action we created is located under the Tools menu.

Here’s another example. If the Action Group I created above belongs to is FileMenu(File) under MainMenu (MainMenu bar at the top of IDEA).

<actions>
    <! -- Add your actions here -->
    <action id="test.hello" class="HelloAction" text="Hello" description="IDEA plugin introduction">
      <add-to-group group-id="FileMenu" anchor="first"/>
    </action>
</actions>
Copy the code

The location of the Action we created is under the File menu.

04 Acceptance Results

Clicking Gradle -> runIde will launch an IDEA that defaults to the plugin. Then, you can actually use the plugin on the IDEA.

The effect is as follows:

When we click on our custom Hello Action, a dialog box pops up showing our custom information.

05 Perfect it

For a bit of interface bells and whistles, we can also write an interface in Swing.

Here we simply implement a chatbot. In terms of code, I refer directly to a small project I wrote when I learned Java in my sophomore year (the code I wrote at that time was really bad! It’s so lame!) .

First, you need to apply for a robot on the Turing Robotics website. (Other robots, too, feel that this Turing robot is not as good as it used to be, and is not called many times for free)

Then, simply write a method to request the invocation of the robot. Because the code is relatively simple, I will not put out here, you simply look at the effect is good.

Code address: github.com/Snailclimb/… .

06 In-depth Study

If you want to further study the IDEA of the plugin, you can have a look at the website document: jetbrains.org/intellij/sd… .

Information on this subject is still relatively scarce. In addition to the official documents, you can also check out the following articles:

  • Easy to start IDEA plug-in development
  • IDEA plug-in development tutorial

07 afterword.

We develop IDEA plug-ins mainly to make IDEA easier to use. For example, some frameworks can reduce repetitive code writing after using, and some thematic plug-ins can make your IDEA look better.

This case of my article is just to tell the truth for a simple introduction to IDEA development, without any practical application significance. If you want to develop a good IDEA plug-in, use your imagination to take advantage of the IDEA plug-in platform.

Get up early, get out early! Think good, everybody three in a row to encourage this “goods”? (Purely for rhyming, not easy! What do young people talk about? Hahaha!)

The illustration based + personal computer original Java interview manual PDF version download address: link: pan.baidu.com/s/1S5VBDPzF… Password: 56 bs