-
Notifications
You must be signed in to change notification settings - Fork 2
Using return values
You can return various types of values from your script, and the result will be intelligently converted for substitution / completion.
Often, simply calculating some text to be inserted is all a script needs to do. Therefore, "normal" objects that have no special handling, such as Strings and Integers will be toStringed and inserted in the place of the template variable.
Readers and InputStreams will be fully read using the default charset to insert their text.
A Supplier will be queried for a return value using Supplier.get().
An Optional will be unwrapped to get its value.
Returning a collection (or similar type, see below) triggers a completion popup offering all its values.
Each value is itself converted according as described on this page.
Nested collections are flattened.
The following collection-ish types are supported:
-
Maps (using their values only)
['a', 4] // Simple example => a, 4
[a: 'b', c: 'd'] // Map values => b, d
['a', new StringReader('b')] // Recursive conversion => a, b
['a', ['b', 'c']] // Flattening => a, b, c
['a', [abc: ['b', 'c'].stream(), xyz: new StringReader("d")]] // Recursive flattening and conversion => a, b, c, d
[] // Empty collection => null
If you want to print the text to be inserted instead of returning a string,
you can simply print as normal and then return _out
at the end.
The script gets its own StringWriter
bound to _out
and the standard output System.out
.
The StringWriter buffers everything written to it and when it is converted as the script’s result as per
<<"Normal" objects>>, its toString()
returns the buffered text.
-
You can check the "Skip if defined" option in the "Edit template variables" dialog to skip the single-element completion popup.
If you return a collection of size greater than 1 from your script, the popup will still be shown. -
Also consider the template options available below the "Edit variables" button.