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/Preferences 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
Specify whether you want to skip the variable when prompting the user for input if the expression evaluated successfully
Predefined template variables
JetBrains Rider supports the following predefined live template variables that cannot be modified:
$END$
indicates the position of the cursor 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, JetBrains Rider 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 |
---|---|
camelCase(<String>) | Converts a string into camelCase. For example, |
capitalize(<String>) | Capitalizes the first letter of a string. For example, |
capitalizeAndUnderscore(<String>) | Capitalizes all the letters of a string, and inserts an underscore between the parts. For example, |
complete() | Invokes code completion at the position of the variable. |
completeSmart() | Invokes smart type completion at the position of the variable. |
concat(<String>, ...) | Returns a concatenation of all the strings passed to the function as parameters. For example, |
date([format]) | 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 |
dbColumns() | Returns a list of columns for a table or a view. The |
dbObjectName() | Returns a name of a table or a view. The |
decapitalize(<String>) | Replaces the first letter of a string with the corresponding lowercase letter. For example, |
defaultReturnValues | Returns the default value if the expression is used in the return statement. Uses the errorVariableName parameter if the expression is of the error type. |
enum(<String>, ...) | Returns a list of strings suggested for completion when the template expands. For example, |
escapeString(<String>) | Escapes special characters so that the result can be used in a Java string. For example, it replaces the tab character with |
expectedType() | Returns the expected type of the expression where the template expands (in the right part of an assignment, after return , as a method parameter, and so on). |
fileName() | Returns the name of the current file with its extension. |
fileNameWithoutExtension() | Returns the name of the current file without its extension. |
filePath() | Returns the absolute path to the current file. |
fileRelativePath() | 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. |
firstWord(<String>) | Returns the first word of the string passed as the parameter. For example, |
lineNumber() | Returns the current line number. |
lowercaseAndDash(<String>) | Converts a string into lower case and inserts n-dashes as separators. For example, lowercaseAndDash("MyExampleName") and lowercaseAndDash("my example name") both return my-example-name . |
regularExpression(<String>, <Pattern>, <Replacement>) | Finds all occurrences of Pattern in a String and replaces them with Replacement . You can specify the pattern as a regular expression to find everything that matches it in the string. |
snakeCase(<String>) | Converts a string into snake_case. For example, snakeCase("fooBar") and snakeCase("foo bar") both return foo_bar . |
spaceSeparated(<String>) | Returns the specified string with spaces as separators. For example, spaceSeparated("fooBar") returns foo Bar and spaceSeparated("Foo_BAR") returns Foo BAR . |
spacesToUnderscores(<String>) | Replaces spaces with underscores in the string passed as the parameter. For example, spacesToUnderscores("foo bar BAZ") returns foo_bar_BAZ . |
substringBefore(<String>, <Delimeter>) | Returns the substring up to the specified delimiter. This is helpful for removing the extensions in test file names. For example, substringBefore(fileName(),".") returns component-test if used in a file named component-test.js. |
time([format]) | 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 |
underscoresToCamelCase(<String>) | Transforms a string with underscores (like snake_case) into camelCase. For example, underscoresToCamelCase(foo_bar_baz) and underscoresToCamelCase(FOO_BaR_baZ) both return fooBarBaz . |
underscoresToSpaces(<String>) | Transforms underscores in a string to spaces. For example, underscoresToSpaces(foo_bar_baz) returns foo bar baz and underscoresToSpaces(FOO_BaR_baZ) returns FOO BaR baZ . |
user() | Returns the name of the current user. |