Syntax Highlighting for .tt files?
Asked Answered
P

8

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.

Periodontal answered 4/4, 2011 at 22:29 Comment(1)
if you have Resharper there's a plugin for that: blog.jetbrains.com/dotnet/2013/01/11/…Yaya
T
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.

Telamon answered 4/4, 2011 at 22:46 Comment(4)
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?Nomadize
@JagodaSokół Tools > Options > tangible T4 Editor > Editor > set 'T4 Background color' to 30,30,30Nikianikita
Wonder if anyone here found a copy that works for Visual Studio Community 2017.Pancake
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.Cappadocia
S
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.

Sagamore answered 22/7, 2016 at 9:32 Comment(1)
Must vouch for this. Been using it all morning and it beats any of the fully free ones I found through VS's extensions.Waldo
A
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.

Adriaadriaens answered 5/4, 2011 at 0:6 Comment(2)
What are your experiences of both tools, pros/cons ? Would be valuable addition to your answer.Dissimilar
Actually, since I posted this, there is a third T4 editor, by DevArt.Adriaadriaens
N
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

Noctambulous answered 7/2, 2018 at 22:19 Comment(3)
Update: This supports VS 2008-2019 (VS 2017 and 2019 use the same installer file) but not yet available for VS 2022.Lebrun
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: paulhjlogan.com/posts/t4-template-languageservice-error/…. Devart does not appear to have a public bug reporting and status forum, only a bug submission/feedback form. I submitted a bug report -- and a comment about the dark colors being bad for DarkMode in VS. Hopefully they fix both.Clearly
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.Clearly
T
5

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.

Trangtranquada answered 24/9, 2021 at 16:35 Comment(0)
W
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.

Width answered 4/4, 2011 at 22:38 Comment(1)
Down-voted because although there is no way out of the box, there are ways to enable it.Dissimilar
C
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.

Clearly answered 2/1, 2023 at 19:7 Comment(0)
C
-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!

Cookgeneral answered 2/2, 2016 at 18:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.