44

Is there any way to tell Visual Studio 2010 to enable syntax highlighting for .tt files? I do have a directive to set the extension:

<#@ output extension=".ascx" #>

So I'd like Visual Studio to treat the file as an ascx file for the purpose of Syntax Highlighting.

Michael Stum
  • 177,530
  • 117
  • 400
  • 535
  • 2
    if you have Resharper there's a plugin for that: http://blog.jetbrains.com/dotnet/2013/01/11/introducing-fortea-a-t4-templating-plugin-for-resharper/ – CAD bloke Apr 15 '15 at 10:58

8 Answers8

30

Try the Tangible T4 Editor extension. There's a free version you can grab to try it out, but if you use them often, it's worth getting the pro license.

Mark H
  • 13,797
  • 4
  • 31
  • 45
  • 1
    But it is really ugly if you are using the dark theme of VS... The background of most lines is gray and the font color is white, so you are likely to see nothing. Or tell me if there is a way to change L4 syntax highlighting background to dark theme also? – Jagoda Gorus Jul 24 '17 at 07:53
  • 2
    @JagodaSokół Tools > Options > tangible T4 Editor > Editor > set 'T4 Background color' to 30,30,30 – jmdon Apr 11 '18 at 05:50
  • Wonder if anyone here found a copy that works for Visual Studio Community 2017. – JeeShen Lee Sep 08 '18 at 07:06
  • It seems that Tangible T4 Editor doesn't the HTML parts of a template with HTML output. This is quite disappointing - it seems that I have to choose between either C# or HTML syntax highlighting in T4 templates. – afarley Sep 14 '20 at 02:18
16

ForTea ReSharper plugin

https://github.com/jetbrains/fortea
https://plugins.jetbrains.com/plugin/13469-fortea/

  • Execution of classical T4 templates
  • Generation of C# code (aka template preprocessing)
  • Full intelligent support in T4 directives
  • Full intelligent support in C# in in-line blocks: find usages, refactorings, context actions, etc.
  • Extensive intelligent support for includes
  • Support for adding assembly and import directives through quick fixes
  • Lots of other IDE features: file structure, extend selection, refactorings, etc

To install ForTea, use Extension Manager from the ReSharper menu.

Otherwise Some old ones. Julie Lerman (EF book writer etc..) I recall her talking about one, years ago.

Alex
  • 14,104
  • 11
  • 54
  • 77
Tom Stickel
  • 19,633
  • 6
  • 111
  • 113
  • 2
    Must vouch for this. Been using it all morning and it beats any of the fully free ones I found through VS's extensions. – TyCobb Jan 11 '17 at 18:03
15

As well as the Tangible editor mentioned above there's also the Clarius one, Visual T4. You can find both on the Visual Studio Gallery and see which one you prefer.

GarethJ
  • 6,496
  • 32
  • 42
9

I just downloaded Devart T4 Editor. It seems to work okay, and allows you to customize the syntax colors, as well as provides intellisense. At this time, it's available for every version of Visual Studio from 2008 to 2017.

My only complaint so far is that it doesn't have an option for highlighting C# operators (e.g. <, >, +, etc), or numbers.

Devart T4 Editor

Drew Chapin
  • 7,779
  • 5
  • 58
  • 84
  • 1
    Update: This supports VS 2008-2019 (VS 2017 and 2019 use the same installer file) but not yet available for VS 2022. – Henry Kwon Feb 03 '22 at 01:30
  • Unfortunately, as of December 2022, there is a bug that makes it unusable in VS 2019, unless you roll-back to a prior update of VS -- which I am unwilling to do. See this description of the bug and roll-back workaround: https://www.paulhjlogan.com/posts/t4-template-languageservice-error/#debugging-thie-bug. Devart does not appear to have a public bug reporting and status forum, only a [bug submission/feedback form](https://www.devart.com/t4-editor/support.html). I submitted a bug report -- and a comment about the dark colors being bad for DarkMode in VS. Hopefully they fix both. – wopr_xl Dec 31 '22 at 17:06
  • Update: On January 2, 2023, I received an email back from DevArt confirming that they were able to replicate the VS 2019 bug and stated that they are working on a fix for the next release. – wopr_xl Jan 02 '23 at 18:59
4

If you don't want to install a plugin or change your file's extension, you can just change the "Editing Experience" for .tt files in the Visual Studio Options.

Go to Tools > Options... > Text Editor > File Extensions, and then add the tt file extension. You can bind it to whatever target language you want. The highlighting might be a little messed up around the template code, and you have to change the target language manually, but it works pretty well for not requiring any new software. Watch out though, because Intellisense's outlining and auto formatting can get really confused by the template code.

jpfx1342
  • 868
  • 8
  • 14
2

No there is not in the 2010 (or before) version of Visual Studio. The T4 templates won't invoke the language service of the template target for highlighting.

The only known editor which does this sub-language highlighting is the ASPX file editor. Doing it requires a very interesting dance between the language service and the ASPX Editor code base. It could certainly be extended to other frameworks like T4 templates but to my knowledge it hasn't been done.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
2

The free T4 Language extension from Brice Lambson available on the Visual Studio Marketplace works well for me in VS 2019, and has nice colors for dark mode while allowing you to easily customize the colors too. It is highly rated by other developers, and reported to work with VS 2022.

wopr_xl
  • 150
  • 1
  • 9
-1

(NB this relates to VS2013 not VS2010, but I'm hoping it works for both)

Change the extension of your template file!

Your template files don't need to have the .tt extension (they just need the Custom Tool to be "TextTemplatingFilePreprocessor" or "TextTemplatingFileGenerator"), so what you can do is to change the extension to whatever's most suitable.

I'm templating some javascript at the moment, so I've got some T4 templates with .js extensions.

Highlighting works nicely for the js bits, but obviously looks a bit rubbish for the C# bits and the declarations at the top (red squiggles everywhere). I can live with that!

Jon
  • 309
  • 3
  • 10