IntelliJ Platform Plugin SDK Help

Providing File and Code Templates

The IntelliJ Platform allows plugins to provide custom file templates specific to the plugin's functionalities. In order to include custom templates in the plugin, a template file has to be created and placed in the specific place of plugin resources, depending on the template's purpose.

Creating File Templates

There are multiple ways of creating file templates:

Once the file templates are created and saved, they should be copied to the plugin project's resources directory. The created template can be found in the IDE configuration directory in the fileTemplates directory, or they can be exported via by selecting the File Templates (schemes) checkbox. The exported ZIP file will contain the fileTemplates directory with the created templates. The fileTemplates directory should be moved to the plugin project's resources folder, and the .ft extension must be added to the template files, e.g., My Class.java must be renamed to My Class.java.ft.

The template name and extension displayed in the IDE settings will automatically be extracted from the file name. The Enable Live Templates option will be enabled if the template's content includes a #[[$ fragment. Note that live templates expressions should be surrounded with #[[ and ]]# tokens to avoid being parsed by the Apache Velocity template engine.

File Templates Categories

File templates can be assigned to one of the predefined categories depending on their purpose and type.

Files

The Files category contains templates used for creating new files. This is the main category, and it includes all templates placed directly in the fileTemplates directory. It should contain only templates for given language core entities, e.g., a class, an interface, or other items that users very frequently create. If the language defines a lot of core entities, see Custom "Create File From Template" Actions for a more user-friendly solution.

Includes

The Includes category contains reusable fragments used for including in other file templates using the Apache Velocity #parse() directive, e.g., license header or documentation comment skeleton. It includes templates located in the fileTemplates/includes directory.

Code

The Code category contains templates used for inserting in existing files, e.g., a code fragment used in an intention action or quick fix. It includes templates located in the fileTemplates/code directory.

Internal

The Internal category contains templates that are not visible in the IDE settings by default and cannot be edited by users. It includes templates located in the fileTemplates/internal directory.

Other

The Other category contains other templates organized in groups. It includes templates located in the fileTemplates/j2ee directory and registered via the com.intellij.fileTemplateGroup extension point (EP). Note that the j2ee directory name is historical and unrelated to the J2EE technology. This category is intended for templates that are not used for creating core language entities or are used less frequently by users, e.g., a specific XML configuration file, a framework-specific class in Java language, etc. To include file templates in the Other section of the settings page, provide an implementation of the FileTemplateGroupDescriptorFactory and register it via the com.intellij.fileTemplateGroup EP.

Example: MavenFileTemplateGroupFactory

Note that FileTemplateGroupDescriptor is a subclass of FileTemplateDescriptor `, which allows creating nested groups.

Creating File Template Description

By default, the description contains generic text about the syntax and properties of the given template. It is highly recommended to provide a custom description explaining its purpose and any available properties. Overriding the default description is achieved by creating an HTML file with the name matching template's name but with the .html extension. Example:

  • Template file name: My Class.java.ft

  • Description file name: My Class.java.html

The description file must be located in the same directory as the template file. It is recommended to follow the convention from the default.html file.

Providing Default File Template Properties

A file template body can use a set of predefined properties exposed by the IntelliJ Platform out of the box. Some languages or frameworks may require additional properties exposed to existing templates. To provide custom properties, implement DefaultTemplatePropertiesProvider and register it via the com.intellij.defaultTemplatePropertiesProvider EP.

Example: Java Plugin's TemplatePackagePropertyProvider providing PACKAGE_NAME property based on the directory a file is created in.

Last modified: 29 九月 2022