Here’s a basic example of how to create an IntelliJ plugin that adds a custom view.
Step 1: Create a new IntelliJ Plugin project
First, you need to create a new IntelliJ plugin project in IntelliJ IDEA Community. To do this:
- Open IntelliJ IDEA
- Go to
File
>New Project
- In the “New Project” dialog box, select “IDE Plugin” under the “Generators” section.
- Fill in the required information (e.g., plugin name, description, etc.)
- Click on “Create”
Step 2: Register the plugin
We need to register our plugin with IntelliJ IDEA. To do this:
- Open the
plugin.xml
file in the src/resources/META-INF - Add a new
<toolWindow>
element to the<extensions>
section:
<extensions defaultExtensionNs="com.intellij">
<toolWindow id="GPT Chat"
factoryClass="com.programtom.intellijgpt.GptWindowFactory"
anchor="right"/>
</extensions>
Step 2: Create a new UI component
IntelliJ has moved to kotlin programming language – so live with it. Create new Kotlin class:
class GptWindowFactory : ToolWindowFactory { override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) { val myToolWindow = GPTChatWindow() val contentFactory = ContentFactory.getInstance() val content = contentFactory.createContent(myToolWindow.content, "", false) toolWindow.contentManager.addContent(content) } }
and create a awt/swing UI for your view:
@Service(Service.Level.PROJECT) class GPTChatWindow { val content: JPanel init { this.content = JPanel().apply { //... your UI code here }}}
Caveats
There are probably million and one items that you need to address. The IntelliJ SDK has virtual wrappers around all kinds of APIs
- File API
- Background Work API
- UI Access/change API
- etc
As minimal – I want to share how to execute some work and “push” it to the UI. You need to
- Start a new thread like this:
ApplicationManager.getApplication().executeOnPooledThread { ... }
- Publish back to the UI with
ApplicationManager.getApplication().runReadAction { ... } //runWriteAction
Step 3: Run the plugin
The project comes with run configuration of “gradle runIde”, that allows you to hit Run Plugin – and test your code.
Step 4: Deploy
There are some more steps in the way of trully publishing it to the world and getting it approved in the marketplace, but – they are for another article. To get an actual build – just add “gradle assemble” Run Configuration. It will create a zip in build/distribution that you could share for beta testing.
If you are interested, I’ve done some plugins for:
- previous company that I’ve worked for – that bootstraps tests initial code – based on the existing business logic code
- I’ve coded a Plugin for my App Builder https://programtom.com/dev/product/app-builder-code-generation-spring-micro-service-vaadin-app/. The code is on github https://github.com/tomavelev/app_builder_plugins. To be honest – I’ve paused the work on the plugin – as there is no interest – and the last time I’ve tried it – it didn’t work
- The code in this article I’ve used in GPT IntelliJ: https://github.com/tomavelev/intellij-gpt as I’ve got interested in the AI space with several items build around it: