56

Here is the Eclipse template that I want to port:

${:import(org.apache.log4j.Logger)}
private static final Logger LOG = Logger.getLogger(${enclosing_type}.class);

My current version in IDEA is as follows:

private static final Logger LOG = Logger.getLogger($CLASS_NAME$.class);$END$

where $CLASS_NAME$ is configured to use className() as its expression.

Unfortunately, I don't find any documentation on adding the import statement. Is there somehing equivalent to Eclipse ${:import(...)}?

Philipp Claßen
  • 41,306
  • 31
  • 146
  • 239
  • import for typescript is not implemented yet: see https://youtrack.jetbrains.com/issue/WEB-37425/Importable-state-after-Live-Template-insertion – TmTron Oct 18 '22 at 09:39

4 Answers4

120

According to this post, it is intended to use only full-qualified expressions. I tried it out and this worked for me:

private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger($CLASS_NAME$.class);$END$

IDEA automatically shortens it and adds the necessary import statements:

import org.apache.log4j.Logger;
// ...
private static final Logger LOG = Logger.getLogger(MyClass.class);

If you want to try yourself, note that you first have to define CLASS_NAME as className() via Edit variables. Also make sure that you allowed your Live Template for Java declarations via Change (at the bottom). Here is a screenshot with the final setup:

enter image description here

Philipp Claßen
  • 41,306
  • 31
  • 146
  • 239
  • 3
    Here I was getting all upset because I couldn't figure out why IntelliJ wasn't _magically_ generating the appropriate imports. Not only did your suggestion accomplish what I was attempting, this is better than what I was hoping for. This way, it is more explicit and there is no room for ambiguity. Nice! – Lo-Tan Mar 04 '14 at 20:56
  • @KrishPrabakar In the question, CLASS_NAME was assumed to be already defined as className(). I added an example now, which should help to understand the answer on its own. – Philipp Claßen Apr 13 '20 at 13:38
  • @PhilippClaßen yes it surely helps now – KrishPrabakar Jun 29 '20 at 13:20
  • Is there a way to do something similar with javascript/typescript ? It would be really handy :) – Esben Andersen Mar 18 '22 at 21:13
5

Just to save a little time for new visitors here: the accepted answer now needs some changes.
Go to Settings -> Editor -> Live Templates, select others, add a template:

private static final org.apache.logging.log4j.Logger logger = org.apache.logging.log4j.LogManager.getLogger($CLASS_NAME$.class);$END$

Then, press Edit Variables on the left and set expression for CLASS_NAME to className().
After all, set context on the bottom to Java -> Declaration (and Groovy -> Declaration if desired). Imports will be magically generated on insert.

coffman21
  • 943
  • 11
  • 18
1

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
-2

For apache commons logging use:

private static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory.getLog($CLASS_NAME$.class);$END$
Isma
  • 14,604
  • 5
  • 37
  • 51
bhlowe
  • 418
  • 4
  • 8