Template macros
When you apply code templates, JetBrains Rider initializes template parameters with some values. To calculate these values, it uses template macros, which can retrieve a lot of useful data based on the surrounding context, For instance, current filename, current solution name, containing namespace, clipboard content, and so on. Even more, macros can invoke code completion actions, guess and suggest types, variables, tags, and more.
As soon as you declare a new template parameter, the parameter appears in the Edit variables dialog, where you can also choose a macro for the parameter. When creating a source template, macros are specified using the [Macro]
attribute.
Some macros just bring you the necessary item. For example, Current solution name or Suggest tag name. Other macros have their own parameter, which you need to specify. For example, if you choose the Suggest variable of type macro for a template parameter, you will need to provide a type for the corresponding macro parameter.
If you do not define any macro for a template parameter, the parameter name is inserted when you apply the template. The user will be suggested to edit this name if the parameter editable.
The best way to grasp the usage of template macros is to study the rich set of JetBrains Rider's predefined templates.
List of template macros
The table below lists all macros that are available for variables in .NET-specific templates.
The Expression column lists macro expressions that can be used in source templates, for example, [Macro(Target = "myVariable", Expression = "suggestVariableName()")]
The Description column lists macros as they are displayed in the Choose Macro dialog when you create or edit template variables in the user interface.
Expression | Description | Details |
---|---|---|
| Guess element type of collection represented by variable | Analyzes code and guesses type of element of a collection. Macro parameters:
|
| Suggest name for an index variable | Suggests a unique name for an index variable at the evaluation point |
| Current file name without extension and with all non-alphanumeric characters replaced with underscores | Evaluates to the current file name without extension and with all non-alphanumeric characters replaced with underscores |
| Name of the primary file without extension with all non-alphanumeric characters replaced with underscores | Evaluates to the primary file name without extension and with all non-alphanumeric characters replaced with underscores |
| Suggest an array variable | Suggests a variable whose type is an array type |
| Insert reference to type | Evaluates to the selected type name Macro parameters:
|
| Value of another variable with the first letter in uppercase | Capitalizes string value (i.e. changes the case of the first letter to uppercase) Macro parameters:
|
| Clipboard content | Evaluates to the current textual clipboard content |
| Execute basic completion | Shows basic code completion list at the point where the variable is evaluated |
| Execute smart completion | Shows smart code completion list at the point where the variable is evaluated |
| Execute type completion | Shows type completion list at the point where the variable is evaluated |
| Constant value | Evaluates to the specified constant value Macro parameters:
|
| Containing type member name | Evaluates to the short name of the most inner containing type member (e.g. method or property) |
| Containing type name | Evaluates to the short name of the most inner containing type |
| Containing type reference | Evaluates to the reference of the most inner containing type |
| List of items describing current context | Provides a list of items describing current context. This includes file name, containing type name, namespace name, etc. |
| Date and time when the file was created in specified format | Evaluates to file creation date and time of the current file Macro parameters:
|
| Current date in specified format | Evaluates to the current date Macro parameters:
|
| Current date and time in specified format | Evaluates to the current date and time Macro parameters:
|
| Containing namespace | Evaluates to the name of the containing namespace |
| Value of another variable with the first character in lowercase | Decapitalizes string value (i.e. changes the case of the first letter to lowercase) Macro parameters:
|
| Default namespace | Evaluates to the default namespace for the current project |
| Default namespace for current file | Evaluates to the default namespace for the current file |
| Current file name | Evaluates to the current file name |
| Current file name without extension | Evaluates to the current file name without extension |
| Full user name of the current user | Evaluates to the full name of the current user |
| Guess element type for expected collection type | Guesses the element type if a collection type is expected at this point |
| Guess type expected at this point | Guesses the type expected at this point |
| New GUID | Generates a new Globally Unique Identifier (GUID) |
| Current line number | Evaluates to number of the line where the macro is evaluated |
| Comma-delimited list of values | Displays the specified list of values in the completion list Macro parameters:
To add the comma ( |
| Current project output assembly name | Evaluates to the output assembly name for the current project |
| Suggest parameter of type | Suggests parameters of the specified type Macro parameters:
|
| Name of the current project | Evaluates to the current project name |
| Current solution name | Evaluates to the current solution name |
| Value of another variable, where spaces will be replaced with '_' | Changes spaces to underscores (i.e. 'do something useful' becomes 'do_something_useful') Macro parameters:
|
| Suggest enumerable variable | Suggests a visible variable that can be enumerated (that is, used in a foreach loop as collection) |
| Suggest name for a variable | When executed in a variable declaration (where variable name should stand), suggests a name for the variable |
| Suggest variable of type | Suggests variables of the specified type Macro parameters:
|
| Suggest type for a new variable | Suggests a type for a new variable declared in the template |
| Current file name in uppercase with all non-alphanumeric characters replaced with underscores | Evaluates to the current file name in uppercase with all non-alphanumeric characters replaced with underscores |
| Short name of the current user | Evaluates to the current username |
| Name of entity | Evaluates to 'nameof(entity)' if possible or 'entity' otherwise |
| DependencyProperty type | Evaluates to dependency property type specific to the current framework |
| Full tag name | Inserts the full name of the containing tag |
| Suggest XML attribute name by tag | Suggests XML attribute name used in the same tags in the current document |
| Suggest XML attribute name | Suggests XML attribute name used in the current document |
| Suggest XML tag name | Suggests XML tag name used in the current document |
| Tag name | Inserts the name of the containing tag without namespace |
| Tag namespace | Inserts the namespace of the containing tag |
| Cast to the required type (if the cast is necessary) | Inserts (if required) a cast to the type which is expected at the left side of the assignment expression |
| File header | Inserts the file header specified in settings |
| Test Subject | Inserts the type of the class for which tests are generated |
| Suggest attribute name by tag | Suggests an attribute name used in the same tags in the current document |
| Suggest attribute value | Suggests an attribute value for the current HTML tag attribute |
| Suggest tag name | Suggests a tag name used in the current document |
| ASP.NET Masterpage content generator | Generates content for masterpage content placeholders at the point where the variable is evaluated |
| ASP.NET MVC Action | Shows completion list with available ASP.NET MVC Actions at the point where the variable is evaluated |
| ASP.NET MVC Controller | Shows completion list with available ASP.NET MVC Controllers at the point where the variable is evaluated |
| Insert runat="server" if server-side tag selected | Inserts runat="server" if a server-side tag selected |
| Substitute Unreal template macros | Substitutes Unreal template macros |
| Containing type name with const | Evaluates to the short name of the most inner containing type with a const specifier |
| Containing function parameter list | Evaluates to a comma-separated list of the parameter names of the containing function |
| Documentation: function parameter type with name | Duplicates the containing template line for each function parameter and evaluates to the parameter type with name |
| Documentation: function parameter name | Duplicates the containing template line for each function parameter and evaluates to the parameter name |
| Documentation: function parameter type | Duplicates the containing template line for each function parameter and evaluates to the parameter type |
| Documentation: function return value | Keeps the containing template line only if the function return type is non-void |
| Documentation: template parameter name | Duplicates the containing template line for each template parameter and evaluates to the template parameter name |
| Documentation: macro parameter name | Duplicates the containing template line for each macro parameter and evaluates to the macro parameter name |
| Documentation: function return type | Evaluates to the function return type |
| Documentation: entity short name | Evaluates to the short name of the entity that is being documented |
| Documentation: entity qualified name | Evaluates to the qualified name of the entity that is being documented |
| Enum to string: enumerator name | Duplicates the containing template line for each enumerator and evaluates to the name of the enumerator |
| Enum to string: enumerator name as a string | Duplicates the containing template line for each enumerator and evaluates to a string with the name of the enumerator |
| Enum to string: enum name | The name of the enum that the action is invoked on |
| Newline in the documentation and enum-to-string templates | Evaluates to a newline in the documentation and enum-to-string templates |
| File header (C++) | Inserts the file header specified in settings |
| Precompiled header file include directive | Evaluates to an include directive for the precompiled header file or to an empty string if the project does not use precompiled headers |
| Current file name without whitespace characters | Evaluates current file name without whitespace characters |
| Current parent Unity type name | Current parent Unity type name |
| Generate Baker name based on parent Unity type name | Generate Baker name based on parent Unity type name |