Search code examples
javaandroidkotlinandroid-testing

How can I create a code template in Android Studio to replicate the val template in Kotlin


Kotlin and Java code templates in Android Studio.

I tried to create a code template for the truth library that would work like the val template works i.e,

  • When you want to create a variable from the result of calling a function? you just type, functionName().val.
  • When you press enter a variable is created ie, val f = functionName().

How would I replicate this behaviour on the truth library such that when I type f.assert for instance, I get Truth.assertThat(f)?


Solution

  • This feature is named "Postfix completion"

    You can find details here https://www.jetbrains.com/help/idea/settings-postfix-completion.html

    I have tried now and it's allowed me to add and edit java and groovy templates but kotlin is not supported.

    The other perfect feature of Idea is Live templates you can use them to implement required templates.

    You can find details here https://www.jetbrains.com/help/idea/using-live-templates.html

    Update: I have checked the current version of Idea, kotlin is not supported yet.