IDEA Plugin Development

· 4 min read · 667 Words · -Views -Comments

Every weekday morning we conduct CodeReview sessions. The general process involves a group of people looking at a shared screen, opening each project, reviewing commits, and recording problematic code by noting line numbers, file names, project names, and issues. However, the recording process is time-consuming and error-prone. The need to record actually reduces the efficiency of CodeReview. We’ve made several attempts to improve CodeReview efficiency.

Initially, one person operated the IDE to browse code while another recorded notes, but the recorder often struggled to keep up, resulting in incomplete records such as missing code locations and issues. Later, we tried using IDE bookmarks for marking, but this approach had limitations: it didn’t support cross-project functionality (each project was independent) and lacked export capabilities. Our output typically needed to be recorded in Confluence.

As shown above, the recording and follow-up process was inefficient and time-consuming. So I thought, why not develop an enhanced version of bookmarks? I’ll implement those missing features myself!

Feasibility

If we’re going to do this, we need to consider feasibility and outline the required functionality:

  1. Record target line numbers, project names, and file paths
  2. Application-level [multi-project] sharing
  3. Persistence across IDE restarts (closely related to point 2, essentially persistent storage)
  4. Set system clipboard content
  5. Keyboard shortcut binding support

SDK and Third-party Research

After reviewing official documentation and mature plugin projects, I confirmed that file system operations, system clipboard access, persistent storage, and keyboard shortcut binding are all supported. This confirmed that the project was feasible.

Here I’d like to comment on two characteristics of Chinese blog articles: 1) Excessive reposting with lack of original content - if you’re going to repost, at least include the original link and properly format images (some layouts are completely messed up); 2) Lack of substantive content - good articles don’t necessarily need to be long, but they should focus on key points and explain things clearly. Well, we still have work to do.

Environment Setup

IDEA provides excellent support for plugin development (well, it’s their own platform after all). Click “New Project”:

Click “Next” and complete the setup.

Project Structure

The initialized project is quite basic. Here’s my project structure:

├── README.md
├── bookmarker4idea.iml
├── out
├── resources
│   └── META-INF
│       └── plugin.xml // Configuration file
└── src  // Source code
    ├── cn
    │   └── alanhe
    │       ├── BookMarkX.java
    │       ├── BookMarkXPersistentStateComponent.java
    │       ├── BookmarkXItemState.java
    │       └── CopyBookMarkX.java
    ├── icon
    │   └── bookmarker.png // Icons and static resources
    └── test // Tests

plugin.xml

Development mainly consists of two parts: Java and XML. We need to configure menus, icons, events, and persistent storage in this file. Here’s part of my plugin configuration:

<idea-plugin>
    <id>cn.alanhe.plugin.bookmarkx4idea</id>
    <name>bookmarkx4idea</name>
    <version>1.0</version>
    <vendor email="i@alanhe.me" url="https://github.com/alanhe421/bookmarkex4idea">personal</vendor>

    <!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description -->
    <idea-version since-build="173.0"/>

    <!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
         on how to target different products -->
    <!--     uncomment to enable plugin in all products-->
    <depends>com.intellij.modules.lang</depends>

    <extensions defaultExtensionNs="com.intellij">
        <applicationService serviceImplementation="cn.alanhe.BookMarkXPersistentStateComponent"/>
    </extensions>

    <actions>
        <!-- Add your actions here -->
        <action id="bookmark" class="cn.alanhe.BookMarkX" text="toggle BookMarkx" description="bookmark">
            <add-to-group group-id="Bookmarks"/>
            <keyboard-shortcut keymap="$default" first-keystroke="ctrl alt B"/>
        </action>
        <action id="ExportBookMark" class="cn.alanhe.CopyBookMarkX" text="Copy BookMark to Clipboard"
                icon="/icon/bookmarker.png"
                description="Copy BookMark to Clipboard">
            <add-to-group group-id="EditorPopupMenu" anchor="first"/>
        </action>
    </actions>
</idea-plugin>

Final Thoughts

Developing an IDEA plugin proved to be a rewarding experience that addressed a real pain point in our daily workflow. The process demonstrated how custom tooling can significantly improve team productivity by automating repetitive tasks and providing better integration with existing development environments.

The key takeaways from this project include the importance of thorough research before implementation, understanding the IDEA plugin architecture, and leveraging the extensive SDK documentation provided by JetBrains. While the initial setup and configuration might seem complex, the structured approach and well-documented APIs make plugin development quite manageable.

This bookmark enhancement plugin not only solved our specific CodeReview efficiency issues but also opened up possibilities for creating other custom tools tailored to our development workflow. The ability to extend IDE functionality is a powerful capability that every development team should consider exploring to optimize their processes.

Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover