Writing IntelliJ Plugins
Jul 17, 2016
3 minute read

Now having a stable version of SmartSmells - my code smell detection tool for java - I wanted to write a plugin for Intellij to detect code smells during the process of coding. Here is a quick guide how to prepare for the IntelliJ-Plugin-Development:

1. Setup

Install jdk 8 to work with Intellij 16, being the newest version by the time of writing. Download the community edition of IntelliJ. This is neccessary to have a sdk for plugin development but also to view source files of intellij.

Use following git command:

git clone --depth 1 https://github.com/JetBrains/intellij-community.git

Depth 1 is used to make just a shallow clone as the repository is very big and would take some hours to fully download.

2. Create Project

Once you’ve opened IntelliJ go to File -> New -> Project… Then choose IntelliJ Platform Plugin. To specify the SDK click on New… and choose the path to your downloaded Intellij Community Edition. As you see in the screenshot, I also picked groovy as additional library. Now click on next, choose a project name and path and you’re done!

Sorry no project screenshot..

3. Hello World - Actions and Windows

Now that we successfully setup a project, we can build a simple Hello World example. Write down following groovy code in a class called HelloWorld.groovy (Java is nearly identical, just add some public's and change the ifPresent-Part to .ifPresent(this::sayHello)).

class HelloWorld extends AnAction {

    @Override
    void actionPerformed(AnActionEvent e) {
        Optional.ofNullable(e.getProject()).ifPresent { sayHello(it) }
    }

    private static void sayHello(Project project) {
        Messages.showMessageDialog(project, "Hello World!", "I'm a Title!", Messages.getInformationIcon())
    }
}

We simply extend the AnAction class and override the actionPerformed method, which acts as a ClickListener. The last step of including it into IntelliJ aims to add following lines into your plugin.xml. This will create your ‘Greet’-Action and add it to the tools menu.

        <action id="test.HelloWorld" class="HelloWorld" text="Greet"
                description="blaaa">
            <add-to-group group-id="ToolsMenu" anchor="first"/>
        </action>

Next, as we want to create something more useful, we create a side window (like the project side window which appears and disappears through a click.) which can hold Swing components. For this we extend the ToolWindowFactory class and override the createToolWindowContent()-method. We create a canvas where we draw some figures. As a canvas is a JPanel we can pass it to the content factory which creates a content object that we add to the tool window.

public class MyWindow implements ToolWindowFactory {

    private MyCanvas canvas = new MyCanvas();

    @Override
    public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
        ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
        Content content = contentFactory.createContent(canvas, "MyWindow", false);
        toolWindow.getContentManager().addContent(content);
    }
}

Dont’t forget to add the tool window to your plugin.xml like this:

        <toolWindow id="Simple Canvas" secondary="true" anchor="right"
                    factoryClass="MyWindow"/>

Now we successfully created an IntelliJ-Plugin with some basic elements like actions and tool windows. The result is shown in following screenshot.

Sorry no hello world screenshot..