Skip to content
This repository was archived by the owner on Nov 10, 2018. It is now read-only.

Advanced Topics

Tim Seckinger edited this page Jan 16, 2017 · 8 revisions

Result vs LookupElements

The _goal variable (as explained here) can be used to control the inserted text (Result) and the completion suggestions (LookupElements) separately, inserting something into the editor but showing something completely distinct in the completion popup.

However, most of the time this will just make using the template clunkier, especially because pressing enter will still insert the first completion option.

Example (Groovy)

Insert default into the editor, but offer alt1 and alt2 in the completion popup:

if (_goal == 'RESULT') {
    return 'default'
} else {
    return ['alt1', 'alt2']
}

Using the Editor

IntelliJ IDEA’s EditorImpl object will be exposed to the script as _editor. This allows you to perform pretty much any action on the editor contents, including the parts outside of the template’s scope and more…​

Examples (Groovy)

Move the caret to the start of the file:

_editor.caretModel.moveToOffset 0
''

Color the editor’s background orange:

_editor.backgroundColor = java.awt.Color.ORANGE
''

"Stateful" scripts

The caching functionality (as explained here) can be "abused" for easily writing "stateful" scripts without using external resources such as temp files. The following example will initially offer 1 in the completion popup, increasing the number shown by 1 every time the live template is used:

_goal == 'LOOKUP_ELEMENTS' ?
        _cache.store(_cache.load('meta').orElse(0) + 1, 'meta') :
        ''

However, if you need to store data for longer than the cache expiry allows you to, or you require persistence across IDE restarts, you will still need to access the file system or other external resources from your script.