Support for alternative custom file templates for triggers, pages, components, etc.

Issue #949 resolved
Scott Wells repo owner created an issue

Comments (2)

  1. Scott Wells reporter

    Quick update on this. I tried to implement it a bit back and found that it's a shortcoming in the JetBrains plugin SDK. You can either have file templates like IC has them now...where there are multiple templates for each extension...or you can have ones that allow you to enter additional bind parameters. I logged a bug/enhancement request against the plugin SDK (I don't have the link handy right now) but haven't heard anything back from them. The only other way to do it would be for me to ignore the majority of what the plugin SDK gives in this area and implement it from (near-)scratch.

    The only semi-decent thing you can do is create a template and check Enable Live Templates. That allows you to have a pseudo-live template-style variable population after the file has been created. I know some users who have been able to achieve what they want using that.

    Here's an example of a file template that uses live templates to create a Sirono Common trigger handler:

    #parse("Apex File Header.cls")
    
    public with sharing class ${NAME} extends AbstractTriggerHandler
    {
        public with sharing class Factory implements TriggerHandlerFactory
        {
            public TriggerHandler create(List<SObject> objects, Map<Id, SObject> oldObjectsById)
            {
                return new ${NAME}(objects, oldObjectsById);
            }
        }
    
        private final List<#[[$SObject$]]#> #[[$newList$]]#;
        private final Map<Id, #[[$SObject$]]#> #[[$oldMap$]]#;
    
        private ${NAME}(List<SObject> #[[$newList$]]#, Map<Id, SObject> #[[$oldMap$]]#)
        {
            this.#[[$newList$]]# = #[[$newList$]]#;
            this.#[[$oldMap$]]# = (Map<Id, #[[$SObject$]]#>) #[[$oldMap$]]#;
        }
    }
    

    and here's what it looks like in action:

    LiveTemplateFileTemplate.gif

    It's not perfect, but as you can see in the image, you can use code completion for each subsequent variable in the live template so it make quick work of creating a rather complex file.

  2. Scott Wells reporter

    Resolving this because without changes in the base IDE, the approach documented/displayed here is about as good as it can get right now. I hope this is sufficient for folks to build more complex parameterized file templates.

  3. Log in to comment