Getting IClassifier to work with custom content type
Asked Answered
O

1

0

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 :)

Oregon answered 23/8, 2013 at 10:7 Comment(0)
O
2

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.

Overawe answered 23/8, 2013 at 12:16 Comment(6)
Yes, I am looking at this implementation right now. Seems rather complicated. So I guess I will also have to implement an instance of this, but probably without the elision buffer. What do you mean by default implementation btw ? The template from Visual Studio ? I am not planning on having multiple content types, so maybe this default implementation could be helpful.Oregon
If you do not associate your file extension with a custom editor factory, then Visual Studio will use its own internal implementation of the editor factory. That implementation assigns content types through a number of different ways, one of which is the attribute you mentioned.Overawe
But of course, using the default implementation doesn't allow man to create navigation bars etc. or adding new file extensions to the editor..Oregon
@Oregon You are correct about user-defined file extensions, but not about the navigation bars. However, the navigation bars cannot be added by MEF so you'll need to use a VSPackage for it.Overawe
I did manage to add navigation bars, but then there were conflicts between my plugin and power productivity tools. Ctrl+Clicking on a token of my extension give the error "No symbol references are found". All this happened after I added the LanguageService - so now I am trying to go the "right" way.. :) I mean I have a tokenizer,parser and everything. I just need to get them triggered..Oregon
I found why my plugin was not working with power productivy tools : visualstudiogallery.msdn.microsoft.com/… Everything work perfect now :)Oregon

© 2022 - 2024 — McMap. All rights reserved.