5

IntelliJ Live Template that makes my life little easier

 3 years ago
source link: https://kotlintesting.com/intellij-live-template-that-makes-my-life-little-easier/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
IDE

IntelliJ Live Template that makes my life little easier

IntelliJ is great tool for developer productivity. Check how can we adjust IDE to our preferences. Tutorial: how to create custom live template?

  • Jarosław Michalik

    Jarosław Michalik

    GDG Kraków member

    📱Android Developer

    🎹 Piano & Guitar

    🏐 Volleyball

    More posts by Jarosław Michalik.

Jarosław Michalik

6 Apr 2021 • 3 min read
IntelliJ Live Template that makes my life little easier
live-template-test.gif

Creating unit test usually requires some boilerplate – you have to create separate class in proper directory, add annotations and provide test method name. While it's not much, I started to think: how can this boilerplate could be reduced?

To make our test discoverable by test framework and IDE, we usually need to annotate test method with @Test annotation.

I'm a big fan of backtick test method notation:

class RandomTestClass {
	@Test
	fun `it should do something`(){
		//
	}
}

I'm not so efficient in fast typing and "`" is quite far from "@" on keyboard. Is there a place for optimization?

IntelliJ live templates and where to find them

IDE of my choice for Kotlin and Android projects - IntelliJ Idea has already built-in live templates and completions for several languages.

On of the template I use from time-to-time is fun1 template, which simplifies creating function with just one argument:

fun1.giffun1 in action

Let's see how this template is defined in IDE.

To browse live templates, first open preferences:

templates_15.47.52.png

Then search for Live Templates in Editor section

templates_15.48.12.png

And find fun1 in Kotlin group templates:

templates_15.48.52.png
fun $NAME$($PARAM1$ : $PARAM1TYPE$) : $RETURN$ {
$END$
}

fun1 live template has several variables, that IDE user will be prompted to provide.

We have $NAME$ - function name, $PARAM1$ and $PARAM1TYPE$ which will be parameter name and its type, $RETURN$ - function return type and finally $END$ - anything that will go to the function body.

We have example of something very similar to what we are trying to accomplish, so let's just use copy that live template and start wondering what we would like to do:

How can we adjust it to our needs?

I'd like to have template which will give me following things:

  • test name prompt with backtick notation already present
  • @Test annotation (preferably with import)
  • prompt to write test body

Based on fun1 template I came up with test template:

@org.junit.jupiter.api.Test
fun `$NAME$`(){
    $END$
}

To create template for your JetBrains IDE, do the following steps:

  1. Add live template in Preferences:
Zrzut-ekranu-2021-03-29-o-10.18.52-1.png

2. Provide abbreviation (how you invoke live template), description and content of live template:

Zrzut-ekranu-2021-03-29-o-10.19.19-1.png

3. Define applicable context for template (in our case that will be Kotlin class)

Zrzut-ekranu-2021-03-29-o-10.19.38-1.png

4. Click Ok / Apply and enjoy your customized live template!

test-template.gif
Full working example

Summary

This template has some issues:

  • it is applicable outside test scope (can be inserted in production code)
  • it uses Junit5 annotation by default (maybe I should have two templates - test4 with Junit4 annotation and test5 with Junit5 import?)

I already got used to this template. It works for me. Will it also work for you?

Even though that template is only micro-optimization and probably won't affect my productivity as much as I initially hoped, I'm glad that I finally decided to play with IntelliJ advanced features. Give custom live templates a try.

Get the Kotlin Testing newsletter

Subscribe to newsletter and receive a copy of Test Doubles Cheatsheet!

You can of course unsubscribe anytime


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK