Saturday, December 18, 2010

IntelliJ Plugin Development, [Personal Log]

 Log entry 1.0.0

I've been working with IntelliJ for a few years now, actually started using it back
in college, I think I was version 4.5.
But really working with IntelliJ only comes when you work more then a few hours a day with it,
so after years of close encountering all of IntelliJ little perks and corks I came across a requirement in my work place for a plugin.

The scenario :
We created an annotation that posses a value of an id, let's call it "Id"
This Id's value corresponds to a Xml element that has an attribute named Id with the same value as the annotation's one.
(The XML file will probably have multiple Id elements, in them is some content which isn't important for the plugin's purpose)

let's illustrate it for clearance :

The XML file :
<queries>

   <query id="exampleId">
                SELECT * FROM CLIENTS
   query>

   <query id="anotherExampleId">
                SELECT * FROM CLIENTS where CLIENT_NAME=@CLIENT_NAME
   query>

</queries>

The CLASS file :
@RefId(Id = "exampleId")
public interface MockForTesting {
}

The language behind the xml & annotation is not important for the plugin development, so will not be further elaborated.

The requirement was that on mouse-click or keyboard-shortcut on the Id, on either the class
file or xml file, the cursor will jump to the corresponding Id.

So I started digging a bit in jetbrains' plugin repository and try to find a plugin that does exactly that or at least close to it.
well I found the JumpToUsage plugin which is pretty cool one, all sources are included &
it pretty straight-forward, so understanding the code wasn't hard, the thing is the IntelliJ openApi
is filled with Factories & Utilities that can do practically everything, but! how the hell you suppose to find the right Factory or utility class the meets your needs?

Well it's hard, that's the answer :)

A few tips for starting a plugin development:
  1. Try to find an open source plugin that provides a similar activity for your needs, you may have to change a lot of the code, but it will give a great start getting familiar with the provided tools.
  2. Download Jetbrains OpenApi sources, and attach them to your project, you soon find out that most of the classes are not placed in the proper packages.
    So there's a lot of work to do here if you want all the classes arranged properly, but no need, just move the classes to their right packages when you need to see their sources.
  3. After understanding the basics of the plugin development from an example project,
    create a new plugin project, construct a small activity or component and debug it,
    the first time you do this - IntelliJ will open a new instance (like after a fresh installation)
    so configure and let it run your plugin, if you have a Breakpoint placed in a proper place, you get to see if it all worked, and you're set for starting some real coding.

End of Log entry 1.0.0