IntelliJ LiveTemplate getter snippet with unnecessary space

76 Views Asked by At

Im trying to create null-safety template for getter (returning Optional) with such code:

#if($field.modifierStatic)
static ##
#end
Optional<$field.type> ##
#if($field.recordComponent)
    ${field.name}##
#else
    #set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
    #if ($field.boolean && $field.primitive)
    is##
    #else
    get##
    #end
    ${name}##
#end
 () {
return Optional.ofNullable($field.name);
}

but while trying to create getter i get error:

Incorrect method 'Optional get Product () { return Optional.ofNullable(product); }'

It looks like the templeate doesn't neither resolve field type nor join correctly 'get' with fieldName (space between).

Could someone help? My IntelliJ version: 2020.03.04

1

There are 1 best solutions below

0
Bas Leijdekkers On

Any whitespace in the template is included in the generated code. Most is removed again on the reformat that automatically happens after the generation. But whitespace inside a method name will break the code and reformatting will not fix it. So you will need to remove the indentation of the #if statement to avoid that.

This template should work:

#if($field.modifierStatic)
static ##
#end
Optional<$field.type> ##
#if($field.recordComponent)
    ${field.name}##
#else
    #set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
#if ($field.boolean && $field.primitive)
is##
#else
get##
#end
${name}##
#end
() {
return Optional.ofNullable($field.name);
}