About Anton Arhipov

Anton is JRebel Product Lead at ZeroTurnaround. Professional interests include programming laguages, middleware and tooling. Java enthusiast, vim fan, IntelliJ addict and JetBrains Academy member. He blogs at "Code Impossible" blog and speaks at Java conferences.

What’s Cool In IntelliJIDEA. Part II: Live Templates

In IntelliJ, I use Live Templates quite heavily and still think that these are underused. It is not only the standard number of templates that can make your life easier while typing yet another piece of code, but it is also the ability to define new templates with all the bells and whistles that are available in IntelliJ standard template list.

In the previous post I’ve covered the basics of Live Templates but I think that it would be cool to cover these in more details and to point out some less-known aspects of this functionality.

First of all, Ctrl+J is the shortcut to use in order to get a list of the available by default. But if you had time to practice, most likely you can remember the abbreviations by heart, like psvm, iter, psfs, soutv, etc.

One thing I do for sure before using Live Templates is that I change the default expansion key, which is Tab by default, to a Space key. It seems to me that it is more natural to use Space for this purpose as when you type you actually hit the Space key almost automatically.

Another cool feature of Live Templates is to surround a capable statement with a block of code – also covered in the previous post.

How do I define my own template?

IntelliJ provides a number of templates in its default distribution but obviously it doesn’t cover all cases you might need. So here’s the main purpose of this post – how to define a custom live template in IntelliJ. First, let’s make up a use case – a situation where we’d like to have some means for typing faster but which isn’t defined in the IDE by default.

For some reason, IntelliJ doesn’t provide try-catch template by default. It only suggests this option if you’d like to surround an existing statement with the try-catch block, but not when you just want to create an empty one. Here’s what we can do: open Settings window (Ctrl+Alt+S) and start typing ‘Live Templates’ – this will lead you to the templates’ settings – and hit the “Add..” button on the right.

It is quite easy to create a simple template which just has to generate a defined text for the given keyword. For the empty try-catch block we just need to fill in the abbreviation and the code itself, and not to forget to bind it to Java context using the corresponding checkbox.

This is quite a dumb template, nothing intelligent here. But what if we’d like to suggest the exception type in the catch block? What if we’d like to position the cursor in some particular place after the template is applied? Let’s make our brand new template a little smarter.

To ask IntelliJ for a type of the throwable in the catch block we can add a variable ($EXCEPTION$) and define its value using a special function provided by IntelliJ. Say, I’d like the type to be a subtype of java.lang.Exception class, hence I’m using the subtype(<type>) function:

You’ve probably noticed that I’ve used another variable, $END$, but there’s no value defined in the dialog window. This is a predefined variable, meaning where should the cursor be positioned after the last choice made within the templates.

So now once I type ‘try’ and hit the space key, the template is expanded to the following:

First the cursor will be positioned inside the catch block in order to choose the type of the exception to be handled. Right after I make my mind about the type and hit the Enter key, the cursor will be positioned back into the try block.

We can go further and add an option to choose the method call on the exception instance by emulating the Ctrl+Space behavior using a special function again. This is a fictional example but let’s assume we want that :)

For the purpose, I’ve defined a $METHOD$ variable which is assigned a complete() function in the template editor:

Now, once the type of the exception is selected, the next thing to happen is the automatic auto-completion action on the instance of the exception within the catch block – the list of available methods will popup:

There’s actually a fair number of functions that could be used in Live Templates for the variable assignment. The nice part is that these work not only for Java but also other languages – when writing JavaScript, HTML or Groovy code, for instance.

Surround with template

The other type of live templates available in IntelliJ is the surrounding templates. It means you can select a text block (or just position the cursor to a desired location) and hit the Ctrl+Alt+T shortcut – a popup with the templates list will be suggested.

Defining a custom surround template isn’t any different from the normal templates besides that the selected block of code (or text) is assigned to a predefined $SELECTION$ variable. Also, if a template contains this variable it will not appear in the list of normal templates provided via Ctrl+J.

Let’s make an example for defining a custon surrounding template. Assume we’d like to quote a selected text. This is a very simple template – just need to add the quotes around the $SELECTION$ variable in the template editor:

Now if I select a text and hit Ctrl+Alt+T shortcut and then “Q” key the selected text will be put into quotes:

Resume

If you use IntelliJ, Live Templates should be your bread and butter for tying the code faster. There’s plenty of the standard templates defined in IntelliJ, but don’t be limited only to the standard template list – define your own templates according to your code specifics – you will not regret that!

Reference: What’s Cool In IntelliJIDEA. Part II: Live Templates from our JCG partner Anton Arhipov at the Code Impossible blog.

Related Articles :

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


× three = 3



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close