Tuesday, November 12, 2013

How to use Intellij idea Live templates to make your life easy

Hi,
As to begin with this is my very first blog post. Here I'm going to explain how to use the concept of Live Templates in intellij idea IDE to make your coding life easy. (in this example I'm working with intellij idea version 12.1.4)

This is a feature which is in intellij idea IDE although people doesn't take full use of it.

Lets begin with an example. Intellij idea already have some live templates embedded with it. For example open one of your java classes in the editor and type "itar" as shown in below image

Image 1 - Example of using Live Template

Then press tab key and you'll see a iteration block generated as shown below.

Image 2 - After Live Template get expanded

This is an existing live template in intellij idea. Don't be amazed you can do lot more things by creating these kinds of customized Live templates.
So lets begin creating our own live template. I will keep to a step by step approach so it will be easy for you to grab.

1. First go to intellij idea settings window by File --> Settings (or by pressing ctrl+alt+s  keys together) using either way you will be directed to the settings window,

2. In the left pane there, find the list item called "Live Templates", which resides under "IDE settings", Once you click on that you'll get a window as shown below.

Image 3 - Settings window

Here you will see the existing live templates are grouped in to several groups in the right side pane. Examine them by expanding those nodes and clicking on those live templates. You will be able to get an idea of how they are formed.

3. There you will see a plus sign as marked in the above image in black. By clicking it you can create your own Live template. Resultant window is shown below.

Image 4 - Newly created Live Template

As you can see intellij idea will create a new group called "user" and create your new template inside it. Don't worry about the group though.
In this example I'm going to create a live template which can be used in the beginning of a method to check whether the given parameter is null or empty and if so throw and Exception after logging what happened there(this can be used for validation purposes).

4. Put the abbreviation you would like to use for invoking this live template in the abbreviation text box. Keeping this simple and short will be helpful since this will be the text you have to type to invoke the template using the tab key. For this example I'll put "cifne" (my way of shorting "Custom IF Null or Empty",  :-) )

5. Then put a description of what you are going to accomplish using the live template in the Description text box

6. We can define our own variables in these templates by enclosing them between two dollar marks
("$TEMP$" would be a variable), so that you can assign their values dynamically according to the context where they are going to be used rather than hard coding them. For example we can get the class name of the class we are going to use this template and use it as a template value.
Enough talking let's do some real coding

if ($VAR$ == null || $VAR$.equalsIgnoreCase("")) {
    logger.error("$CLASS_NAME$::$METHOD_NAME$ --> Error given $VAR$ is null or empty");
    throw new $EXEPTION$("$CLASS_NAME$::$METHOD_NAME$ --> Error given $VAR$ is null or empty");
}

7. Put the above code segment in to the template text field, you will be able to recognize the variable fields I have defined in the above code segment. Now lets see how to edit those variable values so that they will be automatically filled up when you use the template. You will see a "Edit variables" button is disabled in the above image (Image 4). After you put the code segment in template text, that button gets enabled. That's where you can edit those variables. Clicking that button will pop up a window as below, though it wont be filled up as below.

Image 5 -Edit variables

8. In the "Name" column it is the names of the variables which resides in the template text code segment, in the "Expression" column we can put expressions which will execute automatically and assign appropriate values to those variables according to the context. In the default column has the default values for those variables, those can be either generated values as "Expression" column or hard coded values, next column name is self explanatory.

9. There are so many methods we can use in the above "Expression" column, methods to retrieve class name, methods to suggest variable names, methods to retrieve current method name etc
you can find a list of usable methods in the jetbrains official web site - http://www.jetbrains.com/idea/webhelp/edit-template-variables-dialog.html
in this example I have used methods to retrieve class name, current method name, exception type and object type.

10. You can also change the way we intend to expand the live template, (default key is "Tab" key) in the "Options" section "Expand with" drop down list

11. Then its better to define applicable context. click the "Define" link in the window as marked in the image (Image 5), there you can mark the context where you want this template to apply. As you can see in the below image, in this example I have selected the "java-statement" context and "groovy-statement" context. you can define the context where you wish to use the template accordingly.

Image 6 - Selecting Context

12. Almost forgot. how are we going to use this template? Lets see. Assume we have a method which has a "String" parameter as an input. Then we have to validate that parameter and give a fair warning, not to mention that we have to log the incident too. So as you can see in the below image type "cifne" at the start of the method and press "Tab" key then intellij idea will expand the template you created and let you fill the empty slots that it cannot generate by itself and even it will suggestions to you for filling them.

Image 7 - Final outcome

Amazing isn't it? how the technology can ease our life. have fun with Live Templates




2 comments :

Ruchira Gayan Ranaweera said...

Nice post. Good luck and wish you for a next post.

Asa said...

thanks to share this!