File Templates - custom variables

Issue #1474 new
Eric Kintzer created an issue

See also resolved issue: Alternative file templates

So, if I create a File Template (here called Apex Domain Class bis) for a .cls extension us...ng custom variables like ${SOBJ} such as shown below …

#parse("Apex File Header.cls")

public virtual class ${NAME} extends ApplicationSobjectDomain implements I${NAME} {

    public ${NAME}(${SOBJ}[] sObjectList) {
        super (sObjectList);
    }

    public static I${NAME} newInstance(List<${SOBJ}> sObjectList){
        return (I${NAME}) Application.Domain.newInstance(sObjectList);
    }

    public class Constructor implements fflib_SObjectDomain.IConstructable {
        public fflib_SObjectDomain construct(List<SObject> sObjectList) {
            return new ${NAME}(sObjectList);
        }
    }
}

Then if I invoke this on the File | New | Apex Domain Class bis I get prompted for the file name and SOBJ. All well and good. No need for the Live Template workaround and the ugly #[[$var$]]# syntax.

But if I invoke via File | New | Apex Class | Apex Domain Class bis, I do not get prompted for the $SOBJ variable. You only get an option to supply the file name and only the predefined ${NAME} field is applied to the template. The Live Template workaround also did not function.

Comments (6)

  1. Scott Wells repo owner

    Eric, it seems that something has changed in the live template-enabled file template stuff since I posted last to #949. As shown in the animated gif there, if you use File>New>Apex Class and then choose the desired template from the template drop-down, it would take you through the live template. It doesn't seem to do that anymore. Instead now you have to use File>New>Custom Template Name for it to activate the live template. That does work for me, though. I'll raise that seeming regression with JetBrains.

    Also note that I'm using the Velocity template placeholder syntax as shown in #949, e.g., #[[$SObject$]]#, and I don't think your template does use that. Take a look at the file template for HTML File for a built-in example of a template with post-creation params.

  2. Eric Kintzer reporter

    Scott --

    1 - I could also get Live Template syntax to work but only from File > New > Custom Template but of course, using the custom variable approach is a bit more convenient (and you mention it in the Description field for the various Apex templates which is how I got started in this whole “issue”).

    2 - My question was more about why File > New > Apex Class > choose custom template didn’t work for custom variables - that said, perhaps your remark above is the root cause for each file creation UI “path”. I could never get it to prompt for the custom variables.

    Eric

  3. Scott Wells repo owner

    Gotcha. The only way I've ever gotten it to work at all in a satisfactory manner is using Velocity live template placeholders. My big question is why that doesn't kick in when using FIle>New>Apex Class>Choose Custom Template. I'll follow up with JetBrains on that either this week or after I get back from Dreamforce.

  4. Eric Kintzer reporter

    my real goal was to get multi-files to be created but that seems to work only in ReSharper which I am not using (I wanted to build, for a given sobjectType, the fflib (Enterprise Architecture Pattern) Service, ServiceImpl, Selector, Domain, and various Interface classes in one click)

  5. Scott Wells repo owner

    Yeah, the standard file template-based file creation mechanism isn't going to help you there. I've implemented that in a custom manner such that meta.xml files are created when the corresponding source files are created, the correct set of bundle files are created when Aura and LWC components are created, etc., but I don't think there's a general-purpose way to do that in the JetBrains IDEs that IC supports.

  6. Xander Victory

    Just tested this myself (I’ve had very similar frustrations, funnily enough with trigger handler templates 😂)

    • using $FOO$ and new->ApexClass->Template subtype = blank spaces where the variables should be = code reformat breaks everything
    • using #[[$SObject$]]# and new->ApexClass->Template subtype = SObject in the variable places, no live template triggered (but hey, at least file is syntactically valid)
    • Using direct new->Template subtype = correct live template usage, but no meta file created (this seems to be the IDE’s expected way, native filetypes don’t display user templates in the ‘enter name’ list)

  7. Log in to comment