Search code examples
mefvisual-studio-extensionsvsxvspackagevisual-studio-sdk

GetClassifier() from IClassifierProvider called twice?


I implemented a VS2013 extension in a form of VSPackage that also exports a classifier for a particular file extension. Everything is working fine, and the only thing that bothers me is that i get GetClassifier() called twice in my implementation of IClassifierProvider. That leads to creation of two classifiers both processing same changes. The implementation of IClassifierProvider is as simple as it is shown below.

[Export(typeof(IClassifierProvider))]
[ContentType(MyConstants.MyContentType)]
public sealed class MyClassifierProvider : IClassifierProvider
{
    public IClassifier GetClassifier(ITextBuffer textBuffer)
    {
        return new MyClassifier(textBuffer);
    }
}

I tried to minimize my package by removing everything not related to classification to no avail. Would really appreciate an advice on this one.

UPDATE: I was wrong about text buffers being different in GetClassifier calls. Updated this post accordingly.


Solution

  • One thing you should describe is what gesture resulted in multiple calls with different buffers. You'll most definitely get a call for GetClassifier for each file that is open, and you could possibly get multiple calls for the same text buffer as well. For the same text buffer, the common pattern is to have some other component be watching for file changes, process it once, and then report the changes via all classifiers.

    There's also "fancy" cases where text buffers can contain the contents of other text buffers, which get used for various features. That might also explain what you're seeing too.