Advanced Postfix Templates
While simple templates can be handled by extending PostfixTemplate class directly, more advanced templates require additional functionalities like selecting the expression that a template should be applied to or editing a template content. The IntelliJ Platform provides the base classes simplifying advanced template's features implementation.
Postfix Templates With Expression Selector
In some contexts, it is not obvious what expression a template should be applied to. Consider the following Java code with the var postfix template at the end:
In the above code, a postfix template could be applied to the getMaxWeight() method invocation and the entire comparison expression depending on the user's intention. The postfix template implementation could automatically apply the template on the topmost or the closest applicable expression, but it is reasonable to leave the expression selection for the user.
Postfix templates with expression selector can be achieved by implementing PostfixTemplateWithExpressionSelector and PostfixTemplateExpressionSelector classes.
Example: IntroduceFieldPostfixTemplate introduces a Java class field, allowing to select one of the non-void expressions at the current offset.
Live Template Syntax-Based Postfix Templates
The IntelliJ Platform-based IDEs provide the Live Templates feature. It allows defining a template text with dynamic expressions, replaced with actual values depending on the context. If the implemented postfix template's expanding behavior can be achieved with the live template syntax, it is much easier to extend the StringBasedPostfixTemplate rather than implementing the expansion behavior programmatically.
Example: StreamPostfixTemplate wraps array expression within the Arrays.stream() method.
Editable Postfix Templates
All postfix templates that return true from the PostfixTemplate.isEditable() method and have a key starting with . (dot) can be edited. In the simplest case, only the template's name can be edited. To provide more advanced editing possibilities, like creating new templates in the settings dialog, editing the template's content, variables, expression conditions, etc., a plugin must implement the related PostfixTemplateProvider's methods:
PostfixTemplateProvider.createEditor()- returns an instance of thePostfixTemplateEditorclass which is responsible for building the UI settings for a particular template and creating a template from the settings provided in this UI. The editor UI may contain settings controls other than the textual editor, like lists, checkboxes, etc.PostfixTemplateProvider.writeExternalTemplate()andreadExternalTemplate()- serializes/deserializes a given template to/from XML elements. Serialized template data is stored in thePostfixTemplateStoragepersistent component.
Implementing template, editor, and serialization methods from scratch is a tedious task, so the IntelliJ Platform provides classes that help implement editable templates:
EditablePostfixTemplateWithMultipleExpressionsfor editable templates with expression selector and conditions limiting the applicable contexts. Like Live Template Syntax-Based Postfix Templates, it is text-based and uses the same live template syntax.PostfixTemplateEditorBasefor editors implementing the most common features like editing content, expression conditions, or using the topmost expression for template expandingUtility class
PostfixTemplatesUtilsproviding methods related to editable template data serialization
Examples:
JavaPostfixTemplateProviderproviding Java editable templatesJavaPostfixTemplateEditorimplementing editor for Java editable templatesObjectsRequireNonNullPostfixTemplateimplementing template wrapping the selected expression inObjects.requireNonNull()method
Surround Postfix Templates
Existing Surrounder implementations of the Surround With feature required to invoke the action can be reused for postfix completion by extending the SurroundPostfixTemplateBase class and returning the surrounder object from the getSurrounder() method.
Example: NotNullCheckPostfixTemplate surrounding the selected expression with an if statement checking if the expression is null.