Introducing ForTea – a T4 templating plugin for ReSharper

T4 files have sometimes suffered from a lack of love. It’s brilliant that Visual Studio provides an ASP.NET-like template language in the box with Visual Studio, but there is not much support – for example, there is no syntax colouring in Visual Studio, and ReSharper knows nothing about the contents of the file. We have an item on our issue tracker to include support, but I’m very pleased to announce that one of our users has beaten us to it and created a very impressive plugin to do just that – the first plugin we have that provides support for a new file type.

The ForTea plugin by Julien Lebosquain (who also created the GammaXaml plugin) was released as version 1.0 just before Christmas. You can find it on GitHub – which means it’s Open Source, if you’re interested in contributing – and from the readme on the home page, here is a direct link to an msi download.

So what does it give you? Well, obviously it provides syntax highlighting for .tt and .ttinclude files, but that’s to be expected.

Where this plugin really shines is that it integrates with ReSharper’s PSI (Program Structure Interface). In other words, once the plugin is installed, ReSharper gets access to the abstract syntax tree of the file, and uses this to provide familiar code completion, analyses and refactorings.

Intellisense displayed in T4 template

As you can see from the screenshot, it supports code completion for T4 directives, and even in this little snippet from a brand new file, is greying out namespaces that aren’t used. And as expected, Alt+Enter on the unused import statements invokes the standard ReSharper context action to remove unused directives.

Remove unused directives context action in T4 file

The plugin also recognises C# code blocks, and provides the expected analysis and code completion – notice the light bulb context action icon, the greyed out unused variable name, and the code completion drop-down.

Intellisense and analyses in T4 file

It can understand multiple code blocks – this example provides code completion in one code block for a method defined in a separate code block at the end of the file, and also marks the duration variable defined in the first code block as in use. It even suggests that the public method can be made private, and provides a hint that the FormatTimeSpan method could be static.

Multiple code block support in T4 file

Of course, renaming works, too:

Renaming variable across T4 code block

As does Find Usages, in files:

Find usages in single T4 file

And across the solution, even providing syntax highlighted previews:

Find usages results including T4 file

One of the nicest features is importing references. ReSharper handles this for C# files – if a type isn’t found, it can suggest to import a namespace or add an assembly reference to the project. But T4 files don’t use the project’s references. Instead, they use namespaces and assemblies declared at the top of the file. Once the ForTea plugin is installed, ReSharper will suggest to add namespaces and references for missing types, but will correctly add them to the file.

Importing references and namespaces into T4 file

As impressive as it is for a 1.0 release, there are a couple of limitations. It should hopefully be obvious that there will be conflicts with other Visual Studio extensions that provide T4 support, so any similar extensions will need to be uninstalled. There are also a few things not yet supported. For example, it’s C# only, and there’s no support for outlining (this is a feature of the Visual Studio editor, and not handled by ReSharper). There is no code formatter yet, so ReSharper won’t help with indenting and code cleanup. See Julien’s notes for more details.

This is a fantastic effort from a member of the ReSharper community, and has produced a very impressive plugin. Please, download it (direct link), use it, and give Julien some feedback. And, of course, it’s open source, hosted on GitHub, so if you’d like to see new features, or find any issues, report them, or grab a copy of the source and submit some pull requests!

This entry was posted in News and Events, ReSharper Tips&Tricks and tagged , , . Bookmark the permalink.

5 Responses to Introducing ForTea – a T4 templating plugin for ReSharper

  1. Peter Šulek says:

    This is greate plugin, i use it everyday in my job (working on DSL designer with tt files).

  2. Alex says:

    Great news, thanks for getting it out! Sometimes macroing up your code is not such a bad thing.

  3. Daniel Alves says:

    Has any compilation compatible with Resharper 8 for testing? :P

  4. Matt Ellis says:

    It’s in the works. There’s been some initial work, as you can see from this pull request: https://github.com/MrJul/ForTea/pull/17. And it’s open source, so any help is gratefully accepted!

  5. Michal says:

    This is really great! Thank you very much for this contribution :)

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>