0

Мне нужно получить теги классификаций для строк ITextSnapshotLine в текущем текстовом представлении.Visual Studio SDK - IViewTagAggregatorFactoryService.CreateTagAggregator вызывает исключение

Во-первых, я получаю активный вид текста:

public static IWpfTextView GetTextView() 
{ 
    var textManager = (IVsTextManager)ServiceProvider.GlobalProvider.GetService(typeof(SVsTextManager)); 
    IVsTextView vTextView = null; 
    var mustHaveFocus = 1; 
    textManager.GetActiveView(mustHaveFocus, null, out vTextView); 
    var userData = vTextView as IVsUserData; 
    if (userData != null) 
    { 
     IWpfTextViewHost viewHost; 
     object holder; 
     var guidViewHost = DefGuidList.guidIWpfTextViewHost; 
     userData.GetData(ref guidViewHost, out holder); 
     viewHost = (IWpfTextViewHost)holder; 
     var textView = viewHost.TextView; 
     return textView; 
    } 

    return null; 
} 

Затем я получаю коллекцию ITextViewLine линий с точки зрения и вызвать GetClassificationTags метод на каждом:

GetClassificationTags(new SnapshotSpan(line.Start, line.Length), textView) 

Метод выглядит это:

public IEnumerable<IMappingTagSpan<IClassificationTag>> GetClassificationTags(SnapshotSpan span, ITextView textView) 
{ 
    var snapshot = textView.TextSnapshot; 

    var componentModel = (IComponentModel)ServiceProvider.GlobalProvider.GetService(typeof(SComponentModel)); 
    var exportProvider = componentModel.DefaultExportProvider; 

    var viewTagAggregatorFactoryService = exportProvider.GetExportedValue<IViewTagAggregatorFactoryService>(); 

    var tagAggregator = viewTagAggregatorFactoryService.CreateTagAggregator<IClassificationTag>(textView); 

    return tagAggregator.GetTags(span); 
} 

В результате у меня все классифицировано правильно. Однако Visual Studio выдает исключение и записывает его в файл ActivityLog.xml. Это происходит только после классификации всех строк в первый раз. Информация, содержащаяся в файле журнала говорит:

System.InvalidOperationException: 
Unexpected false&#x000D;&#x000A; 
at Roslyn.Utilities.Contract.ThrowIfFalse(Boolean condition, String message)&#x000D;&#x000A; 
at Microsoft.CodeAnalysis.Editor.Implementation.Diagnostics.AbstractDiagnosticsTaggerProvider`1.CreateTagger[T](ITextBuffer buffer)&#x000D;&#x000A; 
at Microsoft.CodeAnalysis.Editor.Implementation.Diagnostics.AbstractDiagnosticsTaggerProvider`1.Microsoft.VisualStudio.Text.Tagging.ITaggerProvider.CreateTagger[T](ITextBuffer buffer)&#x000D;&#x000A; 
at Microsoft.VisualStudio.Text.Tagging.Implementation.TagAggregator`1.GatherTaggers(ITextBuffer textBuffer) 

я заметил, что исключение не выдается после того, как закомментировать строку ниже:

var tagAggregator = viewTagAggregatorFactoryService.CreateTagAggregator<IClassificationTag>(textView); 

Иногда, также это исключение в файле журнала:

System.InvalidOperationException: Unexpected false&#x000D;&#x000A; 
at Roslyn.Utilities.Contract.ThrowIfFalse(Boolean condition, String message)&#x000D;&#x000A; 
at Microsoft.CodeAnalysis.Editor.Tagging.AbstractAsynchronousTaggerProvider`1.TagSource.GetTagIntervalTreeForBuffer(ITextBuffer buffer)&#x000D;&#x000A; 
at Microsoft.CodeAnalysis.Editor.Tagging.AbstractAsynchronousTaggerProvider`1.Tagger.GetTagsWorker(NormalizedSnapshotSpanCollection requestedSpans, Boolean accurate, CancellationToken cancellationToken)&#x000D;&#x000A; 
at Microsoft.CodeAnalysis.Editor.Tagging.AbstractAsynchronousTaggerProvider`1.Tagger.GetTags(NormalizedSnapshotSpanCollection requestedSpans)&#x000D;&#x000A; 
at Microsoft.VisualStudio.Text.Tagging.Implementation.TagAggregator`1.&lt;GetTagsForBuffer&gt;d__38.MoveNext() 

Мой вопрос: что вызывает это исключение и как я могу избавиться от него?

ответ

1

The code is open source, вы можете просто взглянуть. Я предполагаю, что вы пытаетесь это на фоновом потоке.

+0

спасибо. Вы правы - этот код был выполнен в фоновом потоке. –