xylo / intellij-postfix-templates

Custom Postfix Templates for Intellij IDEA
https://plugins.jetbrains.com/plugin/9862-custom-postfix-templates
Apache License 2.0
529 stars 94 forks source link
intellij java-template postfix-completion postfix-templates scala-template template-macros

Custom Postfix Templates for Intellij IDEA

Custom Postfix Templates is an Intellij IDEA plugin that allows you to define your own custom postfix completion templates. At the moment it supports the following programming languages with : Java, Scala, SQL, PHP, Go, Groovy, Python, LaTeX, Kotlin (untyped templates), Dart (untyped templates), JavaScript (untyped templates), and Rust (untyped templates).

So what is the difference to IDEA's postfix templates?

Since IDEA 2018 you are now able to define your own postfix templates in the settings UI (Editor → General → Postfix Templates). However, this is a pretty new feature and it's less functional than this plugin. Here are some of the advantages of this plugin:

Screencast

Screen Cast

Download

You can download the plugin Custom Postfix Templates via Settings → Plugins → Browse Repositories.

Usage

The plugin comes with a predefined set of templates for Java and Scala (see below) which can be immediatly applied in Java/Scala files. For instance, write

"1".toInt

in a Java file. If the completion popup does not automatically show up, press Ctrl+SPACE. Select the .toInt template and see how it is expanded.

And if you want to see the template definition, just press Alt+ENTER in the completiion popup and select Edit '.toInt' template.

Kinds of template files

There are three different types of template files:

Order of template files/rules

Template rules are applied in a first-come-first-serve manner, i.e., more specific rules/files should be placed above more general rules/files. Reorder files in the tree by selecting them and by using the up/down buttons.

Predefined web templates files

The plugin comes with a set of so-called "web template files" which provide in total more than 200 useful templates. While web template files are read-only and shall not be edited by the user because of automatic updates, you can still edit or deactivate templates of these files.

To change or deactivate a predefined template you just have to start the template name completion with Ctrl+Space and then press ALT+Enter and select the third item (Edit .TEMPLATE_NAME template). The corresponding web template file is opened and you see the definition of the template rule. Since you cannot this template file directly you have to override the template rule by pressing Alt+Enter and selecting Override template rule. This overriding works in a way that your template rule needs to be loaded before the predefined template gets loaded. This is done by adding your rule to a user template file which is placed above the predefined web template file in the plugin settings. In case that you don't have a user template file which is loaded before, you are offered to create one. After you selected an existing user template or created a new one the template rule to override is automatically added to this file and you can start adapting it. To deactivate a template rule, replace the rigth side of the rule with [SKIP].

Edit the templates

Press Shift+Alt+P (or go to menu Tools → Custom Postfix Templates → Edit Templates of Current Language) to open the custom postfix templates for the programming language in your current editor. Here you can easily change, remove, or add new templates matching your needs. Note that you have to save the template file explicitly (via Ctrl+S) in order to update the postfix templates in the IDE.

Template definitions

The file may contain multiple template definitions of the form:

.TEMPLATE_NAME : TEMPLATE_DESCRIPTION
    TEMPLATE_RULE1
    TEMPLATE_RULE2
    ...

Each template definition consists of a template name, a template description and an arbitrary number of template rules. The template name is used as key in the code completion and the template description is shown as hint in the code completion popup. The template rules define on which types the template can be applied and how the application is performed.

Simple template rules

A simple template rule has the form

    MATCHING_TYPE  →  TEMPLATE_CODE

whereas

MATCHING_TYPE

The options for MATCHING_TYPE may differ from programming language to programming language:

TEMPLATE_CODE

The TEMPLATE_CODE can be any text which may also contain template variables used as placeholder.

Template Examples

While writing the templates you can use the code completion for completing class names, variable names, template macros and arrows (→).

Advanced template rules

In the chapter above some options have been omitted for simplicity. If you need more functionality here is the full format of template rules including two optional parameters:

    MATCHING_TYPE [REQUIRED_CLASS]  →  TEMPLATE_CODE [FLAG]

Writing library specific template rules via REQUIRED_CLASS

Sometimes you may want to write library specific template rules, i.e. rules that shall be only applied when a certain library is included in the project. For instance, take a look at the .val template provided with this plugin:

.val : extract as value
    NON_VOID [lombok.val]    →  val $var:suggestVariableName()$ = $expr$;
    NON_VOID                 →  final $type*:expressionType(expr))$ $var:suggestVariableName()$ = $expr$;

It can be applied to any non-void expression and expands either to

val myVar = myExpression;

if lombok is available, or to

final MyType myVar = myExpression;

if you're using Java without lombok.

In this exmaple template the [lombok.val] part after the matching type is used to restrict the rule appliction to those cases where the class lombok.val is available in the class path.

In general you can use any class name between the square brackets you want to define a restriction on.

FLAGs

SKIP

You can use the [SKIP] flag for deactivating the template rule for a given matching type.

Example:

.sort : sort naturally
    de.endrullis.lazyseq.LazySeq  →  [SKIP]
    java.util.List                →  java.util.Collections.sort($expr$)

In this example a postfix template .sort is defined. The first rule tells the plugin that there shall be no completition for expressions of type LazySeq. The second rule defines how List expressions shall be completed.

USE_STATIC_IMPORTS

If you tag a template rule for Java with [USE_STATIC_IMPORTS] all static methods that are used will be automatically imported and your code gets more compact. For instance, lets take the following template rule:

.toList : convert to List
    ARRAY  →  java.util.Arrays.asList($expr$) [USE_STATIC_IMPORTS]

Since the rule is tagged with [USE_STATIC_IMPORTS] expanding of array.toList does not lead to Arrays.asList(array) but to asList(array) and the following line is added to your import statements:

import static java.util.Arrays.asList;
IMPORT

If you tag a template rule for Scala with [IMPORT FULLY_QUALIFIED_CLASSNAME] the given class (or method) import is automatically added to the file header when the template gets applied:

.printStream : get PrintStream
    java.io.File  →  new PrintStream($expr$)   [IMPORT java.io.PrintStream]

Note that you can use the IMPORT flag multiple times.

Update templates and open plugin settings

Go to Settings → Editor → Custom Postfix Templates or Tools → Custom Postfix Templates → Open Settings / Upgrade Templates. There you can chose between two different lambda styles and check/uncheck the template files you want to enable/disable.

Contribute

Any contributions are welcome. Just fork the project, make your changes and create a pull request.

Here are some guides:

See also