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"/>
</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..
Please sign in to leave a comment.
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
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
You can check and add stuff when your custom Application component is initializing.
Example of creating FileTemplate:
https://github.com/JetBrains/intellij-community/blob/734eb7add6c4c472e1e342de8153d14f1a66a446/platform/lang-impl/src/com/intellij/ide/actions/SaveFileAsTemplateAction.java#L41-L43
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?
Search · org.mule.tooling.lang.dw.templates.WeaveFilesTemplateManager (github.com)
Was a good repo to look at.
Thanks Jakub!