88

Is there any way to use a default token value in Intellij Live templates? For example I have the following live template which declares a private variable that I would like to create nearly for every class:

private static final Logger logger = Logger.getLogger($CLASS$.class)

It seems unnecessary to type $CLASS$ every time when this live template is used, because it can be derived from the class in scope or filename. I was wondering if it is possible to use environment defined tokens in live templates as defaults?

Leonid
  • 22,360
  • 25
  • 67
  • 91

3 Answers3

121

Check some other templates that already use the current class name:

enter image description here

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
55

log4j:

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

slf4j:

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

Make sure that you set the applicable context to Java and that shorten FQ names and skip if defined are enabled.

Nowaker
  • 12,154
  • 4
  • 56
  • 62
  • 3
    I like the addition this article mentions for setting up en expression to populate the variable with a sensible default. https://coderwall.com/p/hzmhbw – Snekse Aug 08 '14 at 21:48
  • @Snekse That's a good addition. You are welcome to improve my answer by editing it. – Nowaker Aug 09 '14 at 02:31
  • This answer doesn't actually answer the questioner's question: to auto-fill the `$CLASS_NAME$` variable with the class name you need to set the "Expression" associated to the `$CLASS_NAME$` variable to be `className()`. See CrazyCoder's answer for a screenshot. – Adil Hussain Dec 09 '16 at 10:08
2

log4j 2:

private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger($CLASS_NAME$.class);
  • 2
    This answer doesn't actually answer the questioner's question: to auto-fill the `$CLASS_NAME$` variable with the class name you need to set the "Expression" associated to the `$CLASS_NAME$` variable to be `className()`. See CrazyCoder's answer for a screenshot. – Adil Hussain Dec 09 '16 at 10:09