3

I have defined the following live template in IntelliJ:

private static final Logger log = LoggerFactory.getLogger($CLASS_NAME$.class);

I use it to insert logger variable to a class.

Is it possible to define so that template also adds

import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 

to the file if these definitions are still absent?

Dims
  • 47,675
  • 117
  • 331
  • 600
  • Live template inserts some code in one place. I think what you should use is a file template - to always include the imports and static declaration in every file (you may filter out files with `Test` in their names). Alternatively, you can always add this import in every file and let the `optimize imports` task remove them if not needed. – Jaroslaw Pawlak Mar 14 '16 at 11:02
  • Possible duplicate of [Creating new live-templates with import statements in IntelliJ IDEA](https://stackoverflow.com/questions/17190489/creating-new-live-templates-with-import-statements-in-intellij-idea) – findusl Nov 08 '17 at 10:28

2 Answers2

6

Define it fully in the Live template:

private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger($CLASS_NAME$.class);

and IntelliJ should auto reformat the expression to an import. (Assuming you already have the lib JAR downloaded and configured with IntelliJ).

Edit: As comment says: the Shorten FQ Names check-box should be checked (which it is by default)

Tested with IntelliJ IDEA 15.0.4

Harmelodic
  • 5,682
  • 1
  • 28
  • 31
0

Now its possible to add live templates with static imports:

You have to check static import in Options

@org.junit.Test
public void should$EXPR$when$CONDITION$() {
    org.junit.Assert.assertThat(null, org.hamcrest.CoreMatchers.is(org.hamcrest.CoreMatchers.nullValue())); 
}

enter image description here

Jorge Tovar
  • 1,374
  • 12
  • 17