4
头图

Author: Xiao Fu Ge
Blog: https://bugstack.cn

Precipitate, share, and grow, so that you and others can gain something! 😄

I. Introduction

Don't step on some pits, you are not a mature code farmer at all!

What do you think KFC family bucket is? Is it a bucket for the whole family to eat together, just a little bit? No, KFC's family bucket is talking about chicken family buckets!

Sometimes I just like to hear this story because of the need to address some of the issues to search, toss, verification, exclude technical solutions, because the wrong direction, so efforts will be wasted. You can only find and combine the technical solutions suitable for your own problem scenarios from the numerous materials, documents, and source code time and time.

But this process is sometimes a must go through. It is rare that sometimes the correct answer or person can be found at one time. Even if it is found at the beginning, I will check other information to see if there is any better. No, this is you, right?

Two, throw a problem

I want to develop IDEA plug-in again!

After studying the related technologies of bytecode instrumentation, 🤔 is considering whether it can also be used to extract system interfaces during the development phase in addition to the usual non-invasive monitoring after the code is online.

With this idea that came out of my head, I thought that if we want to deal with this matter, the core problem is to develop an IDEA plug-in + bytecode instrumentation ability, enhance the running method when the code is running, and extract relevant necessary information. . Not to mention the case is really made, as follows:

  • Case address: Based on IDEA plug-in development and bytecode instrumentation technology, automatic analysis of R&D delivery quality is realized
  • Follow-up question: In fact, the realization here can only be regarded as a case. I have not fully understood the IDEA plug-in development capabilities. For example, this IDEA plug-in requires some basic configuration, so where do you open it? Can the real-time monitoring and generated interface information be displayed on the right side of the IDEA interface or can it be exported? What if I introduce some other functions that integrate IDEA plug-in development capabilities? What technologies are used here? Wait, these problems need to be solved one by one in order to fully develop a usable IDEA plug-in. For this, more in-depth data collation and practical verification are needed.

Third, the problems involved in the development of plug-ins

problem summary : The basic problem process involved in developing an IDEA plug-in is as follows:

  • development method : In the description of the official website, there are two ways to create IDEA plug-in projects, namely, IntelliJ Platform Plugin template creation and Gradle construction.
  • frame entrance : After an IDEA plug-in is developed, consider where to embed it, such as entering the configuration from the Edit, Tools, etc. of the IDEA form, or embedding the form into the left and right toolbars or the dialogue under the IDEA form frame.
  • UI : Thinking about what language should be used to develop the form, yes, it uses the technical capabilities of Swing and Awt.
  • API : In IDEA plug-in development, it is generally carried out around the project, so basically it is necessary to obtain project information, class information, file information, etc. from the IDEA plug-in JDK development capabilities.
  • external function : This one is used to combine the plug-in capabilities with external systems, such as whether you need to upload the obtained interface to the server, or download files from remote, etc.

Four, two configurations for developing plug-ins

1. Basic configuration

  1. IntelliJ IDEA 2019.3.1 x64
  2. JDK needs to be configured with IntelliJ Platform Plugin JDK, set in Project Setting, so that IDEA plug-in can be developed normally
  3. id 'org.jetbrains.intellij' version '0.6.3'
  4. gradle-5.2.1 matches the plug-in development under the 2019 IDEA version
  5. Settings -> Build, Execution,Deloyment -> Build Tools, configure Gradle. Gradle user home = D:/Program Files (x86)/gradle/gradle-5.2.1/.gradle User Gradle from = gradle-wrapper.properties or Specified location as follows:

If you are using IDEA New Project's default IntelliJ Platform Plugin, you can only focus on steps 1 and 2, but if you need Gradle, you need to pay attention to the settings of steps 3, 4, and 5. Of course, it is usually more recommended to use Gradle to build the project, so when you need some additional Jar packages, you only need to build.gradle instead of copying the required Jar packages to the project's lib.

2. Encountered a problem

After using Gradle to build the project, you will encounter several problems;

  1. Download Gradle 5.2.1 version in advance and configure it, otherwise the automatic download of the build project will be slower https://gradle.org/next-steps/?version=5.2.1&format=all
  2. It will be slower to pull related content when building a project, and it will be better if you have an agent.
  3. [Troublesome problem] During the build process of IDEA plug-in development based on Gradle, a matching version of IDEA software will be downloaded to start the test development plug-in, the zip package ideaIC-2019.3.1.zip several hundred megabytes. At this time, basically you will encounter a crash error Could not resolve all files for configuration ':detachedConfiguration1'. . What if you do not want to worry about the trouble, you can manually download and SHA1 encrypt the downloaded file and put the downloaded file in the cache folder .gradle\caches\modules-2\files-2.1 specific operations are as follows:

    • Open the current user's .gradle directory under the system disk, and enter the .gradle\caches\modules-2\files-2.1 directory, which is the directory for cache files. This directory is your error address during the build process, Could not get resource D:\Program Files (x86)\gradle\gradle-5.2.1\.gradle\caches\modules-2\files-2.1\com.jetbrains.intellij.idea\ideaIC\2019.3.1
    • The directory name of the encrypted folder 2dae8e50d4b0508cad2e680b53414f657954f390 (which may not be the case for yours), I go, this should be encrypted , but what encryption is it? After understanding know this is SHA1 encrypted, and the file is SHA1 encryption generated only string , but no such order on the windows, online SHA1 too much trouble, but also to upload files, so the thought of Java API, there is also through the git hash command line to achieve. Temporarily copy our file ideaIC-2019.3.1.zip to this directory. Run the sha1sum.exe ideaIC-2019.3.1.zip command to generate a unique and unique string (used to verify the integrity of the file), so you get the 2dae8e50d4b0508cad2e680b53414f657954f390 directory name
    • Next, create a new directory 2dae8e50d4b0508cad2e680b53414f657954f390 2019.3.1 directory, and move ideaIC-2019.3.1.zip into it.
  4. [Stack Overflow] During the Gradle build process, it consumes a Java heap space memory and may report an error 0612d94378ce01. So you can also create a new file gradle.properties in the IDEA project root directory, add the following content, change the gradle Jvm parameter org.gradle.jvmargs=-Xmx2024m -XX:MaxPermSize=512m not to mention it's pretty easy to use, It turned out to be a success.

Five, write a test case

1. Engineering structure

PluginGuide
├── .gradle
└── src
    ├── main
    │   └── java
    │       ├── HiClazz.java
    │       ├── MyDumbAwareAction.java
    │       ├── MySearchableConfigurable.java
    │       ├── MyToolWindowFactory.java    
    │       └── TestUI.java    
    └── resources
        ├── icons  
        └── META-INF
            └── plugin.xml 
  • HiClazz is an implementation class that inherits AnAction, used to attach to the form of IDEA, click to open the corresponding page
  • MyDumbAwareAction and MyToolWindowFactory are used together to set the window at the bottom of IDEA, which is the same as the position of the console output information you see.
  • MySearchableConfigurable can be used to configure the form in Settings.
  • TestUI is a form developed based on Swing, and the verification is opened in the AnAction implementation class.
  • plugin.xml is the configuration file of the entire IDEA coffee, all your windows will be reflected in this configuration file.

2. AnAction

public class HiClazz extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {
        Project project = e.getData(PlatformDataKeys.PROJECT);
        PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
        String classPath = psiFile.getVirtualFile().getPath();
        String title = "Hello World!";
        Messages.showMessageDialog(project, classPath, title, Messages.getInformationIcon());
    }

}
  • The test reads the information of the mouse hovering in the class file in IDEA. We can configure this AnAction to each IDEA menu.

3. MyToolWindowFactory

public class MyToolWindowFactory implements ToolWindowFactory {

    @Override
    public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {

        toolWindow.setToHideOnEmptyContent(true);

        class MyPanel extends SimpleToolWindowPanel {

            public MyPanel(boolean vertical) {
                super(vertical);

                DefaultActionGroup group = new DefaultActionGroup();
                group.add(new MyDumbAwareAction("Login1"));
                group.add(new MyDumbAwareAction("Login2"));
                group.add(new MyDumbAwareAction("Login3"));

                ActionToolbar toolbar = ActionManager.getInstance().createActionToolbar("ToolBar", group, false);
                setToolbar(toolbar.getComponent());
            }

        }

        // 添加一个页
        toolWindow.getContentManager().addContent(ContentFactory.SERVICE.getInstance().createContent(new MyPanel(false), "First", false), 0);

    }

}
  • In the bottom form of IDEA, if you want to display your own form, you need to develop the corresponding ToolWindowFactory implementation class so that you can display your content.
  • The idea here is basically the development method of Swing technology. If you are not familiar with Swing, the most part of the content will be unfamiliar.

4. plugin.xml

<extensions defaultExtensionNs="com.intellij">
    <!-- Add your extensions here -->
    <toolWindow canCloseContents="true" anchor="bottom"
                id="SmartIM"
                factoryClass="MyToolWindowFactory">
    </toolWindow>
    
    <!-- 在Setting中添加自定义配置模版 -->
    <projectConfigurable groupId="Other Settings" displayName="My Config" id="thief.id"
                         instance="MySearchableConfigurable"/>
</extensions>

<actions>
    <!-- Add your actions here -->
    <action id="HiId_FileMenu" class="HiClazz" text="HiName">
        <add-to-group group-id="FileMenu" anchor="first"/>
        <add-to-group group-id="MainMenu" anchor="first"/>
        <add-to-group group-id="EditMenu" anchor="first"/>
        <add-to-group group-id="ViewMenu" anchor="first"/>
        <add-to-group group-id="CodeMenu" anchor="first"/>
        <add-to-group group-id="AnalyzeMenu" anchor="first"/>
        <add-to-group group-id="RefactoringMenu" anchor="first"/>
        <add-to-group group-id="BuildMenu" anchor="first"/>
        <add-to-group group-id="RunMenu" anchor="first"/>
        <add-to-group group-id="ToolsMenu" anchor="first"/>
        <add-to-group group-id="WindowMenu" anchor="first"/>
        <add-to-group group-id="HelpMenu" anchor="first"/>
    </action>
    <action id="HiId_EditorPopupMenu" class="HiClazz" text="HiName">
        <add-to-group group-id="EditorPopupMenu" anchor="first"/>
    </action>
    <action id="HiId_MainToolBar" class="HiClazz" text="HiName">
        <add-to-group group-id="MainToolBar" anchor="first"/>
    </action>
</actions>
  • In the configuration of plugin.xml, it is mainly to configure each function realization window under the corresponding menu, such as under Tools, in toolWindow, etc.

5. Test results

Start and run

  • IDEA plug-in development and operation will be based on ::runIde

Run interface

  • You can see the newly added HiName plug-in in each menu of IDEA. You can select the content you need to configure when you actually develop it.

Running effect

  • When the mouse clicks on the class, click HiName to see the corresponding project class information.

Six, plug-in development can do everything

Search IDEA plug-in development on GitHub, a total of 44 pages of content, https://github.com/search?p=41&q=idea%E6%8F%92%E4%BB%B6&type=Repositories involves automated testing, Engineering scaffolding, API generation, DAO classes for database generation, some common tools, and of course some more interesting ones, such as: catching fish and reading books, listening to Guo Degang's cross talk, WeChat chat, LAN chat, English translation, etc. Here I will give you a few to broaden your mind.

1. Quickly generate CRUD project code

  • address : https://github.com/mars05/crud-intellij-plugin
  • Description : an idea plug-in for adding, deleting, modifying and checking, which can help you quickly generate model, dao, service, controller and other related codes according to the database table structure. Support MyBatis, JPA, MybatisPlus at the same time.

2. Fish and chat in IDEA

  • address : https://github.com/Jamling/SmartIM4IntelliJ
  • Description : SmartIM (formerly SmartQQ) plug-in on ntelliJ IDEA, you can use QQ or WeChat chat in IDEA. After the installation is successful, a SmartIM tab will appear in the bottom bar (if there is no bottom bar, check ToolButtons in the menu View)

3. Visual process orchestration

  • address : https://github.com/alibaba/compileflow
  • description : compileflow Process engine is one of Taobao workflow TBBPM engines, it is a stateless process engine that focuses on pure memory execution, by converting process files to generate java code to compile and execute, which is concise and efficient. Currently, it is the process engine of multiple core systems such as mid-stage transactions in Alibaba's business. It is widely used in Alibaba's mid-office solutions to support multiple business scenarios such as shopping guide, transaction, contract performance, and funding.

Seven, summary

  • IDEA development technology involves familiarity with IDEA plug-in development API and UI interface development, so if you want to develop an IDEA plug-in, you basically cannot do without writing Swing, but you don’t need too complicated pages. All this part of the skills is still good.
  • The IDEA official website documents only provide two methods to build IDEA plug-in projects, but Gradle is more recommended, which can satisfy you for the convenient introduction of other functional components and extensions of other content.
  • IDEA plug-in development can develop many technical plug-ins for efficiency-enhancing R&D programming, such as some monitoring, scaffolding, interface API and debugging, flow-based low-code layout, etc., so the value of this part of the content is still quite large.

8. Series recommendation


小傅哥
4.7k 声望28.4k 粉丝

CodeGuide | 程序员编码指南 - 原创文章、案例源码、资料书籍、简历模版等下载。