Creating File Templates on Plugin install

Answered

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"/>
</extensions>

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 https://intellij-support.jetbrains.com/hc/en-us/community/posts/207280245-File-Templates-in-plugin ... 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..

1
5 comments

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

1

Johan, check for some of the existing com.intellij.fileTemplateGroup EP implementations with the IntelliJ Platform Explorer:

https://plugins.jetbrains.com/intellij-platform-explorer?extensions=com.intellij.fileTemplateGroup

1

I don't really understand how the FileTemplateGroupDescriptorFactory can help with adding a group to the 'File > New' menu. How can I actually add this? any examples of how it is done? 

0

Please sign in to leave a comment.