Search code examples
visual-studio-extensionsvsixvs-extensibilityvisual-studio-sdk

Visual Studio SDK - GetClassificationSpans doesn't get called


In my classifier class I have a method containing the code below:

var handler = ClassificationChanged;
if (handler != null)
{
        IVsTextManager textManager = (IVsTextManager)ServiceProvider.GlobalProvider.GetService(typeof(SVsTextManager));
        IVsTextView vTextView = null;
        int mustHaveFocus = 1;
        textManager.GetActiveView(mustHaveFocus, null, out vTextView);
        IVsUserData userData = vTextView as IVsUserData;
        if (userData != null)
        {
            IWpfTextViewHost viewHost;
            object holder;
            Guid guidViewHost = DefGuidList.guidIWpfTextViewHost;
            userData.GetData(ref guidViewHost, out holder);
            viewHost = (IWpfTextViewHost)holder;
            IWpfTextView textView = viewHost.TextView;
            ITextSnapshot textSnapshot = textView.TextSnapshot;
            SnapshotSpan span = new SnapshotSpan(textSnapshot, 0, textSnapshot.Length);

            var eventArgs = new ClassificationChangedEventArgs(span);
            handler(this, eventArgs);
        }
}

When I call my method, GetClassificationSpans doesn't get called. I don't understand why. Is there anything I do wrong? How can I force Visual Studio to call GetClassificationSpans and reapply classification formats to the code when no edit occurred?


Solution

  • I looked at the TextBuffer as SLaks suggested and it led me to the solution. I have a reference to my classifier somewhere else in the code to call my method mentioned in the question. However, it turned out to be a reference to another classifier which had different TextBuffer. I made sure the reference was right and now everything works correctly.