intellij-ideajetbrains-idelive-templates

IntelliJ live template from class type


I was playing around with IntelliJ live templates and I work every day with Instancio library https://www.instancio.org/

One of the things that are a bit verbose is writing for instance:

List<String> varName = Instancio.createList(String.class);

But at the end I also create Lists for any kind of type...

My objective was to create a live template so that when I write for instance Animal.ilist it replaces that by

List<Animal> varName = Instancio.createList(Animal.class);

The only thing that I have achieved so far is

List<> varname = Instancio.createList(.class);

but I could not make it work for type.ilist

enter image description here


Solution

  • I am adding the answer here so it has more visibility:

    I checked the postfix completion which can be found on IntelliJ 2023.2 version under

    Settings -> Editor -> General -> PostFix Completion

    I thought I could create a new group but I ended up adding it under Java:

    Created template

    1. Add as a key the "Keyword" that would trigger this postfix live template. In my case "ilist"
    2. Select the minimum language level. I left it as default.
    3. Add the expression under the editable section. The $EXPR$ contains the text that was written before the "keyword".

    Since $EXPR$ contains my class type I added the $EXPR$ to the result parts of my template where it was needed.

    Note: the $END$ which sets the cursor to that position after the live template is triggered this lets you set a variable name without needing to position yourself.

    Here you can see how to use it:

    How it works