While browsing StackOverflow for questions about VS2010, I came upon a question about how to make .less files open in the CSS editor. Coincidentally, I had written up an extension to do this in response to a tweet from about a month ago. Since the popularity of this has doubled (from one to two people), I went ahead and stuck this up on the Visual Studio Gallery.
There was a similar question to this posted on the editor forums on msdn a week or so ago. Since three is a crowd, I figured I'd throw together a blog article about how this extension works, in case anyone else finds themselves in need of something similar.
At it's core, this extension is extremely simple; it's a pkgdef file that looks like this (for the Less case):
pkgdef
Less
[$RootKey$\Languages\File Extensions\.less] @="{A764E898-518D-11d2-9A89-00C04F79EFC3}" [$RootKey$\Editors\{A764E89A-518D-11d2-9A89-00C04F79EFC3}\Extensions] "less"=dword:00000028
This creates two registry entries:
File Extensions
ContentType
MEF
IVsTextBuffer
The pkgdef file lets us do this as an extension and without worrying about the registry root, courtesy of the $RootKey$ variable.
$RootKey$
The only other piece of information there is the dword value, which is just the priority of that file extension to editor factory mapping. It doesn't really matter for a unique file extension, and I'm not sure why I picked the 00000028 value; it may have just been the value of a different file extension mapping for the CSS language service.
00000028
Making the extension was also pretty simple:
Visual C#->Extensibility
<PropertyGroup>
.csproj
<IncludeAssemblyInVSIXContainer>false</IncludeAssemblyInVSIXContainer> <IncludeDebugSymbolsInVSIXContainer>false</IncludeDebugSymbolsInVSIXContainer> <IncludeDebugSymbolsInLocalVSIXDeployment>false</IncludeDebugSymbolsInLocalVSIXDeployment>
...and that's it. If you build and run or debug that project, it'll open an instance of the experimental hive, which should have that extension loaded. Because the pkgdef file automatically figures out the correct registry root to go with the registry hive that instance of Visual Studio is using, only the experimental hive will have the new file extension mapping. If you want your regular VS to have it as well, just find the VSIX in the bin\Debug or bin\Release folder the project lives in and double-click it from explorer (that will install it in VS).
VSIX
bin\Debug
bin\Release
If you want to apply this solution to a different language service or file extension, you need to:
less
HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\10.0_Config\Languages\Language Services
GUID
HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\10.0_Config\Editors
You can also add this to any existing extension, by just repeating steps #3-#5 above. If you already have a pkgdef file, you just need to add those two declarations to the existing file.