Saving Keystrokes with Live Templates in IntelliJ IDEA

2019/01/21

Typing Code Over and Over?

In one of the projects, we had to create many customized enumerations. Something like this:

public class Status {
    ONLINE("on"), OFFLINE("off");
}

Each of this enum was supposed to have a findByCode method that would resolve “on” to ONLINE and “off” to offline. Something like this:

public enum Status {
    ONLINE("on"), OFFLINE("off");

    private final String code;

    Status(String code) {
        this.code = code;
    }

    public static Status findByCode(String code) throws IllegalArgumentException {
        for (Status value : Status.values()) {
            if (value.code.equals(code)) {
                return value;
            }
        }
        throw new IllegalArgumentException("No Status: '" + code + "'");
    }
}

Instead of superboring typing it over and over, let’s create a Live Template in the IntelliJ IDEA!

Live Template

Live templates in the IntelliJ IDEA are easy: they work like a supercharged Tab-completion.

Let’s create a custom one!

All Live Templates are available in the Preferences > Editor > Live Templates.

First, let’s add a New Template Group to the table, with the Java name, to keep things organized. Then, let’s create a new Live Template.

The properties that need to be configured are as follow:

The template is defined by the Apache Velocity syntax. Essentially, any Java syntax is allowed, while variables are indicated via $VARIABLENAME$ declaration (with dollars both in prefix and in suffix).

public static $ENUM$ findByCode(String $CODE$) throws IllegalArgumentException {
	for ($ENUM$ $ENUM_ELEMENT$ : $ENUM$.values()) {
		if ($ENUM_ELEMENT$.$CODE$.$EQUALS$($CODE$)) {
			return $ENUM_ELEMENT$;
		}
	}
	throw new IllegalArgumentException("No $ENUM$: '" + $CODE$ + "'");
}

Variables

The template code uses multiple variables. We can customize the suggestions or the actual values for these variables via Edit variables button. In the dialog, we can assign the following semantics:

The semantics and available methods are specified in the JetBrains Live Template documentation.

Contexts

Next, we need to assign one of the contexts, i. e. places where the Live Template is applicable. For the sake of the simplicity, let’s use Java / Declarations.

Customizations

Finally, we can finetune the customizations: let’s setup Reformat According to Style to make the expanded method honor the Java code formatting preferences.

Using the Live Template

Now, let’s use the freshly created Live Template. We can place a cursor on a reasonable place in the Status source, type enumfind and expand with TAB!

public class Status {
    ONLINE("on"), OFFLINE("off");

    enumfind<TAB>
}

IntelliJ will automagically complete the whole source code.

>> Home