Using File Templates Programmatically
File templates provided by a plugin can be used during new file creation, in code intention actions, or other plugin features. They can be accessed with the FileTemplateManager
service providing methods returning all or single file templates from a given category. For example, to obtain a template from the Code category, pass its name to the getCodeTemplate()
method (notice the lack of the .ft extension):
To render a template content, prepare and pass Properties
object to the getText()
method:
Creating New Files from Template
The common use case for file templates is creating new files with the initial content specific to a language or framework supported by the plugin. File templates assigned to the Files category are automatically available in the action group. Sometimes, creating a file from a given template in a specific project place doesn't make sense, or a template requires some additional properties for its content. It is possible to control a file template's visibility and its available properties using CreateFromTemplateHandler
implementation registered in the com.intellij.createFromTemplateHandler
EP.
Example: JavaCreateFromTemplateHandler
Exposing File Templates from the Other Category
File templates from the Other category are not exposed by default. To make them available in the UI, a plugin has to implement and register an action. The easiest way to do it is by extending CreateFileFromTemplateAction
base class which contains standard implementations of necessary methods. It allows customizing the action by providing an action name, icon, overriding methods creating dialog, and others. Example action:
The new action should be registered under the NewGroup
group, e.g:
Action presentation texts should be added to the resource bundle defined in plugin.xml according to the rules described in Localizing Actions and Groups:
Custom "Create File From Template" Actions
In some cases, the default mechanism for creating files from templates is insufficient. Consider a language that defines multiple types of core entities, e.g., in the Java language, the following entities can be created: Class, Interface, Record, Enum, and Annotation.
Having all of those items in the
action group may overwhelm users with the number of options to choose. It is more user-friendly to provide a single action and let users choose a specific entity type in the creation dialog:This can be achieved by placing templates in the Internal category, so they are not picked up by the default mechanism, and then registering a custom CreateFileFromTemplateAction
implementation as described in the Exposing File Templates from the Other Category section. To provide a list of selectable entity types, the action should provide a custom dialog with multiple file kinds for every template type, e.g.:
As file templates are placed in the fileTemplates/internal directory, they are not listed in the settings page, and users can't adjust them to their needs. Internal templates can be exposed in the Files category by additionally registering them via the com.intellij.internalFileTemplate
EP, e.g.:
Example: NewKotlinFileAction
for Kotlin files creation action.
Improving "Save File as Template…" Action
Some languages or frameworks may require creating many custom file templates directly in the IDE with SaveFileAsTemplateHandler
and registering it via the com.intellij.saveFileAsTemplateHandler
EP.
Example: SaveJavaAsTemplateHandler
replacing existing class and package names with ${NAME}
and ${PACKAGE_NAME}
properties placeholders respectively.