Introducing T4 Text Template Support in Rider 2019.3

T4 Text TemplatesBrace yourself! Another highly requested feature request was finally resolved – Support for T4 Text Templates. For those folks who don’t know what it is: T4 stands for Text Template Transformation Toolkit – a mixture of code blocks and text that can generate new text files or even code (similar to Razor). T4 templates are very handy to generate output based on our project model or external databases. For instance, using linq2db we can easily generate POCOs based on a database schema to allow for fast and type-safe database accessing.

Many ReSharper users might be familiar with the ForTea plugin that was developed and maintained by Julien Lebosquain. A huge round of applause for his amazing work and permission to integrate it directly in Rider! Besides some minor adjustments, the most significant missing part was the generation itself. Did we mention it’s still open-source?!

How does Rider help with T4 Text Templates?

Let’s start small by adding a new T4 text template file. There are regular .tt template files on which the generator is executed, and .ttinclude files, which allow for code sharing:

Creating new T4 text templates

Also a good reminder, that we can just type in the New File dialog and search for the file type we want.

As mentioned, the regular .tt template files are meant for generation. So here we are with some actions – Execute Template and Debug Template for design-time templates, and Preprocess Template for our run-time templates:

Executing and debugging T4 text templates

As you may have guessed, debugging also works fluently. Just place a breakpoint, hit Debug Template, and we can use all the debugger goodies, like seeing stack traces, inspecting variables, modifying the execution pointer, and many more:

Debugging T4 text templates

For standard control blocks, the code completion integration will let us choose from a list of available keywords:

Using block completion in T4 text templates

For navigating around, we can either use the normal symbol navigation (a.k.a. GoToFileMember) or make use of the Structure tool window:

Structure of T4 text templates

Good old code inspections & quick-fixes can help us to see errors at a glance and fix them right away. For instance here, when a feature block was misplaced before a statement block:

Reordering blocks in T4 text templates

And of course, it wouldn’t be Rider, if there wasn’t integration with all the other C# features that we’re familiar with, like code completion, namespace imports, live and postfix templates, formatting for control blocks, and last but not least, symbol navigation:

Common C# features in T4 text templates

Pro tip on the side: as shown in the GIF above, regular strings will turn into interpolated strings if we try to use them as such. Thanks to code completion!

We’re still working on shipping additional features around T4 templates, until then, download Rider 2019.3, try it out and tell us what you think!

About Matthias Koch

Matthias is a passionate C# developer and likes to talk about clean code, testing and tooling in general. Much of his spare time in the last years was devoted to his very own open source projects, including NUKE. He is working at JetBrains as developer advocate for the .NET department. Follow him on Twitter.
This entry was posted in How-To's and tagged , , , . Bookmark the permalink.

Leave a Reply

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