Creating New Functions for Live Templates
This tutorial illustrates how to add custom functions to an IntelliJ Platform plugin and make them available for use by Live Templates. As an example, a function is created to convert a selection to Title Case. Refer to the SDK code sample
Implementing a New Function
Under the hood, the predefined functions for Live Templates are called macros. A new custom function for Live Templates is implemented in
TitleCaseMacro, which extends
TitleCaseMacro methods are of particular interest:
TitleCaseMacro()constructor passes the name and description of the macro to the parent constructor.
isAcceptableInContext()method tests whether the macro is available in the current context. The test relies on the
MarkdownContextobject previously defined in the
calculateResult()method gets invoked when the titleCase function is used in a Live Template. The text to be capitalized is retrieved from the Live Template and converted to Title Case.
Adding a Live Template
Using the procedures previously discussed for Template Creation and Export the Live Template, add a Live Template to the Markdown.xml file for the plugin. The XML representation of an example Live Template using the new
titleCase function is listed below.
There is only one variable,
TITLE. The expression for
TITLE evaluates to the
titleCase function provided by the plugin. The argument to the
titleCase function is
SELECTION, which tells the IntelliJ Platform to operate on the current selection.
Register Extension Point
com.intellij.liveTemplateMacro extension point, register the implementation with the IntelliJ Platform.
Now verify the plugin is working correctly.
Run the plugin in a Development Instance.
Create a new file
testing.mdand enter several words in lower case.
Highlight the text and enter Ctrl/Cmd+J to open the Select Template popup. Confirm that the SDK: Convert to title case is available in the popup, and select it.
Test that the Live Template works by entering m or Return. The text will change to have each word capitalized: