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.
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.
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.
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.
Of course, renaming works, too:
As does Find Usages, in files:
And across the solution, even providing syntax highlighted previews:
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.
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!