Creating File Templates on Plugin install


I am creating a plugin, and as part of plugin installation, I would like to install several pre-defined File Templates (File > New > My Template Group > My Template 1).

How would I go about doing this? Is there an install/uninstall plugin hook? What is the API for creating/exposing new File Templates?


Update:  It appears the primary hooks are via extensions defined in the plugin.xml, for fileTypeFactory and fileTemplateGroup.

<extensions defaultExtensionNs="com.intellij">
<fileTypeFactory implementation="com.github.davidjgonzalez.intellij.scriptconsole.templates.JspFileTypeFactory"/>
<fileTemplateGroup implementation="com.github.davidjgonzalez.intellij.scriptconsole.templates.FileTemplateGroup"/>

I'm not sure how the mapping between the actual template file (fileTemplates/j2ee/my-template.ext.ft) which seems to happen in my FileTemplateGroup implementation, and the custom fileTypeFactory (and underlying custom LanguageFileType) is made.


Update 2: I found this post ... which seems to work. This means that I do not need to have any extensions (fileTemplateGroup or fileTypeFactory).
In fact, I don't understand what fileTemplateGroup is supposed to do.. It appears something under New > Edit Templates > Other tab..

Comment actions Permalink

You can use com.intellij.ide.fileTemplates.FileTemplateGroupDescriptorFactory to provide bundled, pre-defined templates that user can edit. You can then access these templates in your plugin (e.g. "Create" action) via com.intellij.ide.fileTemplates.FileTemplateManager#getJ2eeTemplate, see org.jetbrains.idea.maven.utils.MavenUtil#runOrApplyFileTemplate as sample


Please sign in to leave a comment.