On what parameter `XXX` refers in `groupDescriptor.addTemplate(FileTemplateDescriptor(XXX))` of IntelliJ IDEA SDK?

Answered

I want to provide some files templates in my IntelliJ IDEA plugin, however I can't understand how to specify the path to the template file.

I have analyzed this example. There is 

<fileTemplateGroup implementation="com.dengzii.plugin.template.template.FileTemplateFactory" order="first"/>

the plugin.xml, where FileTemplateFactory refers to:

class FileTemplateFactory : FileTemplateGroupDescriptorFactory {

    override fun getFileTemplatesDescriptor(): FileTemplateGroupDescriptor {

        val descriptor = FileTemplateGroupDescriptor("Module Template Plugin Descriptor", AllIcons.Nodes.Plugin)

        descriptor.addTemplate(getDescriptor("MainActivity.java", getFileIconByExt("java")))
        descriptor.addTemplate(getDescriptor("Manifest.xml", getFileIconByExt("xml")))
        descriptor.addTemplate(getDescriptor("Application.java", getFileIconByExt("java")))
        descriptor.addTemplate(getDescriptor("build.gradle", getFileIconByExt("gradle")))
        return descriptor
    }

    private fun getFileIconByExt(ext: String): Icon {
        return FileTypeManager.getInstance().getFileTypeByExtension(ext).icon ?: AllIcons.FileTypes.Unknown
    }

    private fun getDescriptor(templateName: String, icon: Icon?): FileTemplateDescriptor {
        return FileTemplateDescriptor(templateName, icon)
    }
}

My question is, what the

  • MainActivity.java
  • Manifest.xml
  • Application.java
  • build.gradle

are? There are no such files in resources/fileTemplates:

Same question by other words: how mentioned in the above image files are being detected by IntelliJ IDEA when the plugin is active? 

In my case, the FileTemplateGroupDescriptorFactory is:

class YDF_GUI_ComponentsTemplatesGroupFactory : FileTemplateGroupDescriptorFactory {
  override fun getFileTemplatesDescriptor(): FileTemplateGroupDescriptor {

    val groupDescriptor = FileTemplateGroupDescriptor("Yamato Daiwa Frontend GUI Components Templates Group", AllIcons.Nodes.Folder)

    /* [ Theory ] No ".ft" must be here. */
    groupDescriptor.addTemplate(FileTemplateDescriptor("Badge.styl"))

    return groupDescriptor

  }
}

The Badge.styl refers to fileTemplates/Styles/Badge.styl.ft.

Try to run the plugin and go to Settings -> File and Code Templates.
The group "Yamato Daiwa Frontend GUI Components Templates Group" presents, but the file **Badge.styl** is empty. Looks like this template has not been detected.

Seems to be obvious solution - replace

groupDescriptor.addTemplate(FileTemplateDescriptor("Badge.styl"))

with

groupDescriptor.addTemplate(FileTemplateDescriptor("Styles/Badge.styl"))

will change only the displaying name of file:

 

0
1 comment

Hi Takeshi,

Templates grouping is documented: https://plugins.jetbrains.com/docs/intellij/providing-file-templates.html.

You should put your templates to fileTemplates/j2ee. I recommend getting familiar with all the docs under https://plugins.jetbrains.com/docs/intellij/file-and-code-templates.html

1

Please sign in to leave a comment.