How to create a Template Mapping for “Fast New File (FNF)” extension

A “pattern”, in Fast New File extension, is basically a regular expression that  maps the file name you entered, to a Visual Studio Template. The extension has a few common patterns built in. These patterns,

  • Map MyType.cs to the C# Class template and creates a class named ​MyType.
  • Map IMyType.cs to the C# Interface template and creates an interface named MyType. etc.

A full list of built in mappings can be found in the source file in Fast new File git repo.

If you need custom mappings though, the good news is that this list of patterns are entirely overridable. All you need to do is drop a Patterns.json file containing the list of patterns into a folder named VisualStudio.FastNewFile in your Application Data folder. If you have already used the extension, this file should already exist with the default patterns in the above folder. If not create this folder by entering the following commands in to the Command Prompt:

c:\..> cd %appdata%
c:\..> mkdir VisualStudio.FastNewFile
c:\..> notepad Patterns.json

The the Patterns.json file holds an array of TemplateMappings; which is to say it holds an array of JSON objects each having three string properties: Pattern, Language and TemplateName. You can add your custom patterns by adding to this list. You can also remove patterns you don’t like, by simply removing them from the list and saving the file.

Adding a your own Mapping

In this example, we’ll be adding a pattern that will create a basic MSTest Unit Test using the standard template. This pattern will enable you to type in Test<ClassName>.cs in to the FNF File Name prompt and have it create a basic Unit Test applying the template.

The Language property is easy. It’ll be "CSharp". The Pattern property requires a named capture group called name. The text that matches this group, is what will be used as the file name created by FNF. So, in this case, our pattern would be:

"Pattern": "^(?<name>Test.*)\\.cs$"

Next, we have to figure out the name of the Razor Page template. VisualStudio standard templates are typically located in

\VisualStudioInstallationDirectory\Common7\IDE\ItemTemplates\Language\Locale\

More details about Item Template location can be found here on MSDN.

The right template on my system seemed to be the one at:

C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ItemTemplates\CSharp\Test\1033\BasicUnitTest\

So, now the Pattern.js file will contain…

[{
     "Language": "CSharp", 
     "Pattern": "^(?<name>Test.*)\\.cs$", 
     "TemplateName": "C:/Program Files (x86)/Microsoft Visual Studio 14.0/Common7/IDE/ItemTemplates/CSharp/Test/1033/BasicUnitTest/BasicUnitTest.vstemplate"
   },
...
]

(Note: Full path to .vstemplate files are supported only from version 1.6 of FNF)

You’ll note that I’ve added this pattern to the top of the file. This is because FNF scans for the patterns from the beginning of the array and stops when it finds a match. Since we have the mapping for a C# Class at the start (which will match *.cs), this, more specific pattern, must be placed before that. Also, I’ve had to use / instead of \ as path separators, to ensure that the JSON serializer didn’t mistake it for an escape character.

The Pattern is now all set. Test it out in a MSTest project. If you use this on a non MSTest project though, you are likely to see an error due to the MS Test template not being supported in that project type.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.