Introducing Source Templates

ReSharper’s Live Templates are a very powerful way of generating code. Type the name of a template, hit Tab or select the template from the code completion list, and the text template is expanded and inserted into the editor. Any using statements are automatically added, the code is reformatted and you can tab between interactive, editable fields. To make them even more powerful, each field can be bound to a macro that will provide a code completion list of items that are generated dynamically, or taken from the surrounding context.

For example, the nguid template expands to a newly generated GUID, shown in several formats in a completion list. Other macros will invoke smart completion, suggest a variable name, return the name of the file and so on. Add to this support for multi-file templates, surround templates and the ability to save your own templates to a shared solution settings file, and it’s easy to see how Live Templates can help speed up writing code.

And now ReSharper 9.1 introduces Source Templates, to make it even easier to create your own templates – a Source Template is a Live Template that is defined in your own source code.

Example template definition

But why would we want to do this? Why create templates in source, when we can also create them in the Live Templates editor?

There are a couple of reasons. First, it’s very easy – you just define a Source Template as normal code, in a source file in your solution. This will automatically get picked up by ReSharper when checked in, and you don’t have to touch the .sln.dotSettings file. It doesn’t get any easier than that!

Second, the big difference from traditional Live Templates is that Source Templates are also extension methods. This means that your Source Template will be strongly typed – both ReSharper and the compiler will verify that the code is correct, doesn’t have typos, includes the right using statements, and of course you get all the usual ReSharper goodness while editing your template – code completion, inspections, navigation and best of all, the template is updated if code is refactored.

Making the Source Template an extension method has an interesting effect on how it’s invoked. Traditional Live Templates can only be invoked at certain places, usually at the start of a declaration, statement or expression. A Source Template is invoked as an extension method. That is, a Source Template will be available only after you press “ .” on a compatible expression – similar to the way the PostFix plugin’s functionality is invoked. For example, the traditional foreach Live Template is available only where you would normally enter a foreach statement (but not in an if statement, or inside a call to Console.Write). In contrast, the example foreach Source Template shown above is available as though it were an extension method, and appears in the completion list after typing “ .” following an expression or variable of type IEnumerable<T>. The expression used as the target of the extension method replaces all usages of the extension parameter in the template.

Example expanding source template

This opens up a very different use case for Source Templates as opposed to Live Templates. You can now create templates that are specific to a type – so the  foreach template will only apply to objects or expressions that implement IEnumerable<T>. Other Source Templates can be targeted at other types, simply by changing the type that the extension method extends. Think of templates that only operate on strings, or objects that implement INotifyPropertyChanged or  IObservable<T>, or make them available to all objects by extending System.Object.

So how do you create a Source Template? Once you’ve created an extension method, you’ll need to mark it with the [SourceTemplate] attribute. This is one of ReSharper’s annotation attributes, similar to [NotNull] and [CanBeNull] which ReSharper can use to to improve analysis inspections and suggestions.

The simplest way to get the annotations is to add a reference to the JetBrains.Annotations nuget package. This is the official JetBrains annotations package, and adds a .dll that contains all of the attributes that ReSharper understands. And don’t worry about adding extra binary dependencies! This package applies the [Conditional] attribute to the annotation attributes, which means that they don’t end up compiled into your assembly. You can deploy your code without copying JetBrains.Annotations.dll.

Once your extension method has been defined as a Source Template, you can also add fields and macros that you can edit and tab between. Fields can be defined as parameters on the extension method, and occurrences inside the method become editable when the template is invoked. You can bind a macro to the parameter with the [Macro] annotation attribute, using the Expression parameter to specify which macro to use (and of course ReSharper gives you code completion here). Alternatively, you can bind a macro to a variable in the extension method, which allows for a macro to suggest the name of a variable, for example, by specifying the Target parameter of the [Macro] attribute. And the Editable parameter allows control over which usage of the field in the method body is editable, even allowing the value to be marked as non-editable.

If you’re familiar with traditional Live Templates, you’ll be aware of the syntax of referring to fields (e.g. $value$) and the use of special predefined field names such as $END$ to specify the final location of the text caret after the template is fully expanded. Source Templates still support these special variables, and the field name syntax, as long as it remains valid C#. For example, you can set the final location of the text caret to be in a string literal: string.Format("$END$");. But to use the value of a field called foo in a variable name, you’d normally need to write var valueOf$foo$ = GetFoo();, which isn’t valid C#. In order to handle these cases, you can use an “expanding comment.” This is a special comment that begins with a dollar sign ( //$ … or /*$ … */). Any text that is inside one of these comments is inserted into the template when it’s invoked, and anything in the standard field syntax ( $foo$) is also expanded. For example, //$ var valueOf$foo$ = GetFoo();.

The help site contains more details on defining Source Templates, including how to specify which macro to bind to a field. And to help you get started writing your own templates, here are some samples:

 

This entry was posted in How-To's and tagged , , . Bookmark the permalink.

4 Responses to Introducing Source Templates

  1. Pingback: The Morning Brew - Chris Alcock » The Morning Brew #1879

  2. Steven says:

    Are source templates meant to be publicly visible? Or is that a technical constraint? Can I use source templates that are defined in another library? (binary reference, not project reference)

    • Matt Ellis says:

      Source Templates require source to work. ReSharper takes this source and rewrites it, in a manner similar to how it does refactoring, to fit the context in which it’s invoked. It also needs the comments for when the code wouldn’t compile due to macro/parameter usage. So, ReSharper can’t use templates declared in a binary reference, because it doesn’t have the source for them.

  3. bitbonk says:

    I am tempted to define source templates in my library and deploy it with my library. Will the user of my library be able to disable the templates of my library if he doesn’t like it?

Leave a Reply

Your email address will not be published. Required fields are marked *