netbeansfreemarkernetbeans-8code-templates

How can I use curly braces in Netbeans code templates for example for an slf4j template?


Creating a Netbeans code template for creating an slf logger is described here:

http://wiki.netbeans.org/SLF4JCodeTemplate

However creating code templates for log statements, e.g.

logger.debug("Something: {}", var);

is harder than expected because the template language doesn't balance curly braces. This means it will end the capture at the first ending curly brace.

There exist some examples, like for example How to get current class name in Netbeans code template? but they do not touch into the curly brace issue.

I have tried to escape them in every way I could think of so farm including:

${LOGGER default="logger" editable=false}.debug("${logMessage}${: '{}'}", ${EXP instanceof="<any>" default="exp"});

and

${LOGGER default="logger" editable=false}.debug("${logMessage}${: \{\}}", ${EXP instanceof="<any>" default="exp"});

but no luck. Also my google skills have been failing me so far.


Solution

  • Turns out there is a simple solution. I didn't find it anywhere near anything about netbeans code templates, but under a question about freemarker:

    How to output ${expression} in Freemarker without it being interpreted?

    Basically the answer is to use r"..." around the code, like this:

    ${LOGGER default="logger" editable=false}.debug("${logMessage}${:r"{}"}", ${EXP instanceof="<any>" default="exp"});
    

    Now this can be assigned to sld, so I can type slt, expand it to:

    logger.debug("logMessage: {}", <last variable>);
    

    Where "logMessage" is selected (so I can overwrite it with something useful, one tab selects ": {}" so I can delete it if I want to log without parameters and a last tab selects which is the last assigned value (in case I want to replace or remove it).