Live template variables
When you expand a live template abbreviation, its variables either appear as input fields where you can type values or are replaced with values. These may be default values that you can modify or values calculated using functions.
To declare variables within templates, use the following format: $VAR$
.
In expressions, use variable names without opening and closing dollar characters $
, for example, lowercaseAndDash(ComponentName)
.
Define each variable using an expression and provide a default value for cases when the expression fails to evaluate.
This expression may contain the following constructs:
String constants in double quotes
Names of other variables defined in a live template
Predefined functions with possible arguments
Configure template variables
In the Settings dialog (Ctrl+Alt+S) , go to .
Select a template where you want to configure variables.
Specify variables in the template text and click Edit Variables.
In the Edit Template Variables dialog, you can do the following for each variable:
Change the variable name.
Define an expression using predefined functions.
Specify the default value for cases when the expression fails to evaluate. The default value should be enclosed in double quotation marks.
Specify whether you want to skip the variable when prompting the user for input if the expression evaluated successfully.
Predefined template variables
Aqua supports the following predefined live template variables that cannot be modified:
$END$
indicates the position of the caret when the code snippet is complete, and you can no longer press Tab to jump to the next variable.$SELECTION$
is used in surround templates and denotes the code fragment to be wrapped. After the template expands, it wraps the selected text as specified in the template. For example, if you selectEXAMPLE
in your code and invoke the"$SELECTION$"
template via the assigned abbreviation or by pressing Ctrl+Alt+T and selecting the desired template from the list, Aqua will wrap the selection in double quotes as follows:"EXAMPLE"
.
Functions used in live template variables
The following functions can be used to define live template variables:
Function | Description |
---|---|
| Returns a class, method, or field annotated with the specified qualified name. If there is more than one program element with the specified annotation in your project, it shows a list from which you can choose. For example, Some predefined live templates in the RESTful Web Services and Web Services groups use the |
| Returns the most recently declared array in the current scope (as a method variable or class field). If there is more than one array declared in the current scope, it also shows a list from which you can choose. For example, if you have the following method and expand a template with a variable that uses the
public String getCode() {
int a[] = new int[5];
int b[] = new int[10];
return code;
}
Some predefined live templates in the iterations group ( |
| Returns the characters that indicate the end of a block comment in the current language context. |
| Returns the characters that indicate the start of a block comment in the current language context. |
| Converts a string into camelCase. For example, |
| Capitalizes the first letter of a string. For example, |
| Capitalizes all the letters of a string, and inserts an underscore between the parts. For example, |
| Casts the right-side expression to the left-side expression type if necessary. Some predefined templates in the iterations group use the Compare to rightSideType(). |
| Returns the name of the current class (where you expand the template). Can be useful for constructor templates. Available in the Java context only. |
| Returns the contents of the system clipboard. |
| Returns the characters that indicate the end of a comment in the current language context. For languages with line comments, the return value is empty. |
| Returns the characters that indicate the start of a comment in the current language context. For languages with line comments, the return value is the start of a line comment, same as lineCommentStart(). |
| Invokes code completion at the position of the variable. |
| Invokes smart type completion at the position of the variable. |
| Returns the component type of an array. For example, you can pass the return value of arrayVariable() to the Available in the Java context only. |
| Returns a concatenation of all the strings passed to the function as parameters. For example, |
| Returns the name of the current package (with the class where you expanded the template). Available in the Java context only. |
| Returns the current system date. By default, without a parameter, it returns the date in the current system format. To use a different format, provide a parameter according to the SimpleDateFormat specification. For example, the |
| Returns a list of columns for a table or a view. The |
| Returns a name of a table or a view. The |
| Replaces the first letter of a string with the corresponding lowercase letter. For example, |
| Returns the default value if the expression is used in the return statement. Uses the |
| Returns the children of the specified class. If there is more than one child, it shows a list of subclasses from which you can choose. For example, Available in the Java context only. |
| Returns a list of strings suggested for completion when the template expands. For example, |
| Escapes special characters so that the result can be used in a Java string. For example, it replaces the tab character with |
| Returns the expected type of the expression where the template expands (in the right part of an assignment, after Available in the Java context only. |
| Returns the name of the current file with its extension. |
| Returns the name of the current file without its extension. |
| Returns the absolute path to the current file. |
| Returns the current file path relative to the current project. To check what the relative path is for a given file, right-click it and select Copy Reference, or press Ctrl+Alt+Shift+C. |
| Returns the first word of the string passed as the parameter. For example, |
| Executes the Groovy script passed as a string. The first argument is a string with either the text of the script or the path to the file that contains the script. The function passes other optional arguments to the script as values for The following example shows a
groovyScript("_1.toUpperCase()", MyVar)
The following example shows a
groovyScript("def result = ''; _1.split().eachWithIndex { item, index -> result = result + index.next() + '. ' + item + System.lineSeparator() }; return result;", SELECTION)
The last example uses the |
| Returns the likely type of elements stored in a |
| Returns the type of an iterable object, such as an array or a collection. Available in the Java context only. |
| Returns the most recently declared iterable object in the current scope (like an array or a collection). If there is more than one iterable object in the current scope, it also shows a list from which you can choose. The predefined live template Available in the Java context only. |
| Returns the characters that indicate the start of a line comment in the current language context. |
| Returns the current line number. |
| Converts a string into lower case and inserts n-dashes as separators. For example, |
| Returns the name of the method in which the template expands. Available in the Java context only. |
| Returns the list of parameter types of the method in which the template expands. Available in the Java context only. |
| Returns the list of parameter names of the method in which the template expands. Available in the Java context only. |
| Returns the type of the value returned by the current method in which the template expands. Available in the Java context only. |
| Returns the fully qualified name of the current class in which the template expands. Available in the Java context only. |
| Finds all occurrences of For example, the |
| Declares the left-side variable with a type of the right-side expression. Some predefined templates in the iterations group use the Compare to castToLeftSideType(). |
| Converts a string into snake_case. For example, |
| Returns the specified string with spaces as separators. For example, |
| Replaces spaces with underscores in the string passed as the parameter. For example, |
| Returns the substring up to the specified delimiter. This is helpful for removing the extensions in test file names. For example, |
| Returns the subtypes of the type passed as a string in the current scope. The predefined Available in the Java context only. |
| Returns all variables that may be assigned to the type passed as the parameter in the given context. This is similar to variableOfType() but it does not suggest |
| Returns a suggested name for the index variable in an iteration from most commonly used ones: Some of the predefined templates in the iterations group ( |
| Suggests the name for a variable based on the variable type and its initializer expression, according to your code style settings that refer to the variable naming rules. For example, if it is a variable that holds an element within an iteration, Aqua makes a guess on the most reasonable name taking into account the name of the iterated container. Some of the predefined templates in the iterations group ( |
| Returns the current system time. By default, without a parameter, it returns the time in the current system format. To use a different format, provide a parameter according to the SimpleDateFormat specification. For example, the |
| Returns the type of the variable passed as the parameter. Available in the Java context only. |
| Returns the type of an object parameter (for example, of a collection element). It accepts two parameters:
For example, Available in the Java context only. See also the Java |
| Transforms a string with underscores (like snake_case) into camelCase. For example, |
| Transforms underscores in a string to spaces. For example, |
| Returns the name of the current user. |
| Returns all variables that may be assigned to the type passed as the parameter. For example, If you pass an empty string Unlike suggestFirstVariableName(), this function also returns standard expressions, such as |
Example
Let's make a simple live template using variables and functions. It will create a Java class with a predefined attribute and a method. The template will contain three variables:
$ClassName$
: a name for a new class$Food$
: a list of three possible values: "meat", "grass", and "honey"$AnimalName$
: the class name starting with a lowercase letter so that it can be used in a sentence
Create a template with variables
Press Ctrl+Alt+S to open settings and then select
.Select the group, click , and select Live Template.
In the Abbreviation field, specify the characters that will be used to expand the template. For example,
animal
.In the Template text field, paste the following template:
class $ClassName$ extends Animal { String food = "$Food$"; public void animalFood() { System.out.println("The $AnimalName$ eats " + food); } }Click Edit Variables and, in the Edit Template Variables dialog, configure the variables:
$ClassName$
remains untouched meaning we expect users to type a value when they apply this template.$Food$
: in the Expression field, enterenum("meat","grass","honey")
. This function allows selecting one of the predefined values when users apply this template.$AnimalName$
: in the Expression field, enterdecapitalize(ClassName)
. This function takes the value of theClassName
variable and converts its first letter to the lower case. Select Skip if defined because we don't need to prompt a user for input.
Use live template
Start typing the name of a live template (
animal
in our example).Type a value of the variable (a class name in our example) and press Tab to jump to the next variable.
Using the keyboard arrows, select one of the values for the
food
string.