javaintellij-idealive-templates

Live Template for Java in intellij IDEA does not behave correctly in block comments?


I am defining my own live template and it seems that the behaviour is faulty when using (dependent) variables inside block comments (not tested for line comments).

My template:

$commentStart$*
* Repository to handle {@link  $ClassName$} entities.
$commentEnd$
@DDD.Repository
public interface $EntityName$Repository {
    $commentStart$*
     * Persists the given instance
     * @param $entityName$
     * to persist
     * @return the given {@code $entityName$}
    $commentEnd$
    $ClassName$ save($ClassName$ $entityName$);
    
    $END$
}

With the following variables:

Defined Variables

The template works as expected if I leave all block comments free of any variables. Also, I'm pretty sure that the variable ClassName shouldn't be necessary.

I tried several variations, such as removing blockCommentStart() and simply using /** (and with end respectively). I tried using the same variable in all code places but nothing makes the template work if the variables are embedded in the comments. I exepected the template to evaluate variables according to their expressions and insert them correctly.


Solution

  • Ended up solving it with a file template:

    #if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
    #parse("File Header.java")
    /**
     * Repository to handle {@link ${NAME}} entities.
     *
     * @see ${NAME}
     */
     @DDD.Repository
    public interface ${NAME}Repository {
    
        #set($arg = $NAME.substring(0,1).toLowerCase() + $NAME.substring(1))
        /**
         * Persists the given instance.
         *
         * @param ${arg} 
         *            the entity to persist
         *
         * @return the given {@code ${arg}}
         */
        ${NAME} save(${NAME} ${arg});
    }