Concepts
- PostfixTemplate
- A single template with a UI label, a suffix text used for the completion and a short description which explains a transformation.
The abstract class
PostfixTemplate
is the base of all available postfix template implementations. - PostfixTemplateProvider
- A set of templates for a language. A
PostfixTemplateProvider
manages a list of custom postfix templates for a certain language.
Implementation
An implementation is done in a few steps:
- Implement your PostfixTemplates
- Implement a PostfixTemplateProvider
- Write a test case
- Setup your plugin.xml
Implement PostfixTemplate
A custom postfix template needs to pass a few properties to its parent constructor:
- name
- The presentable name to be displayed in the user interface. I couldn’t find a place where IntelliJ uses this, though.
- key
- The suffix which triggers your template. If you suffix is
.key
then your template’sisApplicable(...)
andexpand(...)
methods will be called after the user typed.key<TAB>
. The key has to start with a character supported byisTerminalSymbol(...)
of your template provider (see below). - example
- The description displayed in the code completion menu. Return a short explanation with a sample here.
Example of a PostfixTemplate implementation
This simple implementation for the JSON language wraps any JSON value in braces, e.g. 42.o
is turned into {42}
. Of course, the JSON is invalid after the expansion, but this is still helpful to quickly modify a JSON file.
|
|
Add documentation files
IntelliJ expects that each PostfixTemplate provides a few html files. They are used to display an example before and after a invocation along with a short description.
Create these files belong in a resource folder named postfixTemplates/yourClassName
:
description.html
before.language.html
after.language.html
yourClassName
is a placeholder for the name of your new template class. language
is probably a placeholder to define the content type of your sample, e.g. json
or java
. I couldn’t validate this, though.
Example of documentation files
postfixTemplates/WrapInObjectPostfixTemplate/description.html
- A short file which is rendered in the settings dialog for your new template.
|
|
postfixTemplates/WrapInObjectPostfixTemplate/before.json.html
- A freemarker template to show sample content before a postifx completion.
$key
is replaced with the key defined in your implementation:
|
|
postfixTemplates/WrapInObjectPostfixTemplate/after.json.html
- A freemarker template to display the result of a postfix completion:
|
|
Implement PostfixTemplateProvider
Create a new subclass of PostfixTemplateProvider
.
Return a set of your custom templates in the method getTemplates()
.
public boolean isTerminalSymbol(char currentChar)
is called by IntelliJ to find out whether a given separator is supported by your plugin. For example, .
is the separator used for Java postfix completion and most other templates. Different templates of your provider are allowed to use different separators, just make sure that your provider supports all of them.
The shortest, meaningful implementation is
|
|
preCheck(...)
, preExpand(...)
and afterExpand(...)
can be used to patch your file, for example.
The Java template provider adds a missing semicolon where necessary, for example.
Write a test case
Don’t forget to write a test case for your PostfixTemplates! You’ll be thankful in a few months.
Actually it’s pretty simple to do it. Here’s a short test of our implementation above:
|
|
Configuration in your plugin.xml
You need to add your new template provider in your plugin.xml
file.
Add one template provider for each language, each
provider offers one ore more templates for a given language.
Example
|
|
Gotchas
- An expansion key must be a valid Java identifier.
- A limitation of IntelliJ’s implementation is that the key used for expansion must be valid Java identifier. An expansion key consisting like
.[]
is not possible. Choose a valid key like.a
instead.
Links
- Definition of PostfixTemplateProvider
- PostfixTemplateProvider (github.com)
- Definition of PostfixTemplate
- PostfixTemplate (github.com)
- IntelliJ online help of the postfix completion feature
- Postfix completion in the IntelliJ handbook (jetbrains.com)