Sam's Blog

19 Apr

File types and extensions in Visual Studio 2012

The Visual Studio 2010 SDK introduced the new class FileExtensionToContentTypeDefinition. This post is all about why I no longer use/export this class, and what I now do instead. The steps in this post only associate specific, known extension(s) with a content type. Further integration into the user-customizable File Types settings and Open With… features requires additional work described in my answer to this Stack Overflow question: Supporting user-specified file extensions in custom Visual Studio language service.

The following requirements led me away from this attribute.

  1. The Type & Member Dropdown Bars feature is not exposed through an MEF editor extension point. Supporting this feature requires providing an implementation of IVsCodeWindowManager (beyond the scope of this article).
  2. To provide an implementation of IVsCodeWindowManager, you must provide an implementation of IVsLanguageInfo.
  3. If you provide an implementation of IVsLanguageInfo, the GetFileExtensions and GetLanguageName methods of that interface will be used instead of the values given to FileExtensionToContentTypeDefinition.

To maximize your ability to support the full range of Visual Studio features in your extension, you can take the following steps to register your new language or file type.

1. In your VSPackage.resx file, add the following values.

  • 100 – Language name
  • 110 – Package name
  • 111 – Package description

2. Derive a package class from Package.

[PackageRegistration(UseManagedResourcesOnly = true)]
[InstalledProductRegistration("#110", "#111", "1.0")]
[Guid("your guid here")]
public class ExamplePackage : Package
{
}

3. Implement IVsLanguageInfo.

This is actually pretty simple.

4. Register the IVsLanguageInfo implementation

Register the IVsLanguageInfo implementation using the ProvideLanguageServiceAttribute and ProvideLanguageExtensionAttribute registration attributes. Override the Package.Initialize method to provide the implementation. Here is the updated ExamplePackage class.

5. Create and export a ContentTypeDefinition.

NOTE: Since we are not using a custom editor factory, the content type name used here must match the language name used as the 2nd argument to ProvideLanguageExtensionAttribute above.

One Response to “File types and extensions in Visual Studio 2012”

  1. 1
    Stefanos Says:

    Hallo Sam,

    i am also a plugin author.. And I am also fighting through the poor msdn documentation.. After following your example and with the help of python tools, I managed to add navigation bars for my custom language.. The problem is however, that now PowerProductivity tools try to go to the references of the tokens from my custom language.

    I believe the problem is, that my MEF extensions use ContentType of “text” because I didn’t know how to map different file extensions, or user defined extensions for my plugin.. So what I ended up doing was , enabling my plugin for all text files and disabling the functionality by reading the associated extensions through the registry..

    Do you have any idea how to properly implement this ?

    Thanks

Leave a Reply

© 2024 Sam's Blog | Entries (RSS) and Comments (RSS)

Your Index Web Directorywordpress logo