Search code examples
c#visual-studio-2010visual-studio-2012mef

Getting IClassifier to work with custom content type


I am trying to add syntax highlighting for a custom content type based on text e.g.

static class RTextContentType
{
    public const string ContentTypeName = Constants.CONTENT_TYPE;
    [Export, Name(ContentTypeName), BaseDefinition("text")]
    internal static ContentTypeDefinition ContentTypeDefinition = null; // set via MEF
}

Then the classifier provider is declared like this,

[Export(typeof(IClassifierProvider)), ContentType(Constants.CONTENT_TYPE)]
class RTextClassifierProvider : IClassifierProvider
{
   ...
}

The problem is, that the constructor of the provider is never get called for my specified extensions. Note here, that I provide the editor factories and the file extensions are associated with the factory. I can see the factory getting initialized through debugging for all associated files.

If I change the content type to "text" the constructor gets called.

So the question is, how can one map a custom content type to a file extension ?

Second note, using FileExtensionToContentTypeDefintion

is not an option, as this does not allow a lot of features..

Thanks for the help :)


Solution

  • Your implementation of IVsEditorFactory is responsible for setting the content type of the text displayed in the editor. The default implementation of this interface includes special support for FileExtensionToContentTypeDefinitionAttribute as a simple extension mechanism for users that don't require some of the more advanced features (e.g. projection buffers for multiple content types).

    The DjangoEditorFactory class includes a nested class named TextBufferEventListener, which shows one example for how an editor factory might assign the content types to the buffers displayed in an editor window.