Intentions versus live templates versus actions

Hi.  I have been looking through the forums and the source code to learn more about each of these but I'm not sure which one to use: intentions, live templates, or actions. Honestly, I'm not even sure what the proper definitions are. When I am writing Java code, I use the meta-J pop up all the time to generate iterations and so on. It's incredibly great! I also use the generate ctor, toString(), etc. stuff. I also sometimes used the tiny little pop-up/hint thing that suggests I can replace an if-return with a simple return expression etc... I think the itar is called a live template and the pop-up hint thing is an intention. How is that different than an action I created my plug-in with "New Action"?

My goal is simple to start with: for the ANTLR plug-in, I would like to allow programmers to insert predefined rules for common lexical items like identifiers, comments, white space and so on. I envision providing multiple versions of floating-point numbers and identifiers but in general there will the perhaps less than 10 kinds of text blobs I want to inject. In the future there might be more. I would like the user to be able to specify the actual name of the rule as part of the injection just like I specify the iteration variables with itar template. In the future I might also want to select a string reference like 'begin' in the grammar and have it automatically generate an associated lexical rule like BEGIN : 'begin' or something like that.  I'm not sure whether that would be done with the same mechanism/concept.

My question: should I be trying to build a live template set? an intention? a set of actions? Are they all really the same thing just invoked in different ways? Sorry if this is obvious. I just want to make sure I don't go down the wrong path only to find out I'm using the wrong mechanism. I can figure out the API for myself from the examples and code if I could just get the proper starting point.




Live templates are code snippets, possibly with some placeholders, inserted into editor using Meta+J or (since IDEA 13.1) just code completion. They are invoked by entering their prefix.

Intentions are actions that can be invoked from editor by pressing Alt+Enter when a light bulb appears, indicating that the IDE can do something automatically for you at this position. Common cases are simple code transformations (De Morgan laws, condition invesion etc).

Actions are... well, actions. Things that can be invoked, most often from a menu or a toolbar. Or via a shortcut. They all can be found in Meta+Shift+A (Go To Action).

When you generate a constructor, you're using Generate action (probably invoked with a shortcut). In fact it's an ActionGroup (composite action) containing other actions: for constructor, getters/setters, equals/hashCode. A plugin can also contribute its own actions to that list.

What should you use? I don't know. It's mostly a question of UX. All these things can be used to achieve the same. Live templates are most declarative of all, so the possibilities are restricted there. But they might well be sufficient for your task. And they can be modified by user, which it harder with other things. Actions are as discoverable as you place them in UI. Live templates pop up in autocompletion, so the users will probably notice them. Intentions or Generate actions are more hidden, but are more powerful than live templates. And you should also consider what the best way to invoke your functionality should be. A string in the editor? Alt+Enter? Generate? A shortcut? It's for you to decide :)


Peter gave a really great answer, one additional API I can mention is the file template extension points.
You can register with the fileTemplateGroup extension point, and then your users will be able to create a new file from a given set of templates

An example is -

The file templates are pretty powerful too, as they allow you to use velocity directives, so you can generate most things in a file too :)
This might be useful if there's a default ANTLR file you want users to be able to start from, and also provide a bunch of different base files to choose from

Edit: The file upload didn't work too well; The final screenshot is similiar to that of a new java file creation, showing Class, Interface, Abstract class, etc


Thanks so much guys!  woot! I'll add to my plugin dev notes and consider your suggestions.


Check this out. :)  From your help and reading the intellij source code, it was clear how to get this working.


Please sign in to leave a comment.