Sunday, 28 January 2018

This week 1/2018 - IntelliJ plugin

Hi Fellows,
this time I started year from creating a tool to automate my work. I always meet with problem of creating builder, mappers and some test data filling dto/vo for test purpose.
At the beginning I started from some simple examples of such plug-ins. Then I was looking for tutorial "how to start", what is concept and what are main interfaces, files, classes required to create plug-in. Unfortunately I didn't find any good for me tutorial with diagrams and main steps. Finally I had to experiment and iteratively find the solution. Now I know the main steps and I described them below in nutshell.


1. IntelliJ project - it is easy to create new plugin project, isn't it ?

2. Plug-in information stored in path META-INF/plugin.xml.



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<idea-plugin>
    <id>example</id>
    <name>Builder plugin</name>
    <version>1.0</version>
    <actions>
        <action id="builderCreator" class="intellij.BuilderAction" text="Builder creator">
            <add-to-group group-id="GenerateGroup" anchor="last"/>
        </action>
    </actions>
</idea-plugin>

The most important part is to define action, where class is extension of AnAction abstract class.


2. Dependencies - we need at least a IntelliJ IDEA SDK - define it in project structure.

3. Class hierarchy - as I wrote above, in my case the main class of plugin is AnAction. Because of error connected with startTransaction problem I found some example where was used other class BaseCodeInsightAction. As I checked its parent class overwrite startInTransaction method. Anyway this extension add a few other function. I'd like to have list of classes with sublists of class fields. This works fine with MemberChooser class.
Panels and other elements are good know from Swing and AWT.

4. Artefacts - to create module jar, you just need to execute Build -> Prepare All Plugins Modules For Deployment.

5. Import project  - when I'd like to import my sources to new IntelliJ project I had  to in a hacky way change my project iml file. Type of module from JAVA

<module type="JAVA_MODULE" version="4">

to PLUGIN

<module type="PLUGIN_MODULE" version="4">

and check path to my plugin.xml

Project Structure -> Modules -> Plugin Deployment -> Path to META-INF/plugin.xml
 
I didn't make this project as maven project. I had some problems with maven dependencies and I didn't spend much time to resolve it.