My favourite tips&tricks for the Best, Kick-Arse java IDE "IntelliJ IDEA".

Tuesday, February 01, 2005

Tip 5: Live Templates

One of the objective in my LIFE is "to achieve automation of all repeated tasks",
As I find it very disturbing to do (type) same stuff mechanically over and over again , so I always try to find ways that I could do use to automate stuff.

I found my self typing following lines over and over while coding any new "method" ,(foo in this case).

log.info("Entering method foo()");
log.info("Exiting method foo()");

and I stumbled upon "Live Templates"..................

So today's topic is Live templates:

Basically there are three types of live templates:
1 simple templates
2 parameterized templates
3 surround templates

Defining your own template can be very complex if one hasn't spent lots of time tweaking one.
Nevertheless this is very powerful feature of IDEA.

I wont be dwelling into the details of writing one.
Actually I will, we will be writing an extremely useful and easy to write live template, which is "1 simple template" (one listed above).

I also want to talk about "2 parameterized templates" too because they are very cool but it will have to wait until my next post.

So lets automate.

press 'ctrl+alt+s' then press 'm'

Enter following data in Dialog fields:

abbreviation: en
Group: user
Description: log entering method
Template text: log.info("Entering method $METHOD_NAME$");
context: Java Code

now click on edit variables:
here u'll see a variable named $METHOD_NAME$ because template text contains it. In the expression column select "methodName()", and also Check "Skip if defined" column.

voila , we are done.

now in any method type "en" followed by tab and enjoy the magic.

repeat the same trick for logging "exiting method".

there's lot more we can do with live templates, I hope I have encouraged u to look into the IDEA help for further tweaking live templates.

I'll be back with "2 parameterized templates" very soon.

till then,
chow.