Search code examples
intellij-idealive-templates

How to complete variable names inside a string in IntelliJ Idea live templates?


Migrating from Eclipse where doing this was a breeze. One of the most common templates I used was logging the value of last used variable.

E.g.

String foo="bar";

log tab>

->

String foo="bar";
log.debug("foo:{}",foo);

I tried using:

log.debug("$var$:{}",$var);

with both complete() and completeSmart() as variable types, but they both failed miserably, i.e. did not provide any sane autocomplete suggestions.


Solution

  • See: How to use variable value in live templates in Intellij IDEA?

    In short: LOGGER.debug("$VAR_COPY$: {}", $VAR$); and define first VAR: variableOfType(Type), default "expr" and next $VAR_COPY$ escapeString(VAR) and skip if defined true. Tested with Idea 15.