我正在使用Visual Studio 2015扩展来添加对新语言的支持,并且我有一个错误标记器。我知道它有点工作,因为我在错误所在的行上滚动条旁边有红色标记。另外,我在其他类(例如我的分类器)中使用相同的解析器,我知道它可以正常工作。这是我的Tagger代码。有什么想法吗?
public IEnumerable<ITagSpan<ErrorTag>> GetTags(NormalizedSnapshotSpanCollection spans)
{
var parser = new NodeParser(true);
var tags = new List<ITagSpan<ErrorTag>>();
foreach (var span in spans)
{
//Reparse the whole thing. It's just must simpler
var spanToParse = new SnapshotSpan(span.Snapshot, new Span(0, span.Snapshot.Length));
IList<LexicalElement> lexicalElements;
parser.Parse(spanToParse.GetText(), "Editor", out lexicalElements);
try
{
tags.AddRange(GetTagsFromElements(lexicalElements, span));
}
catch (FormatException)
{
}
}
return tags;
}
private IEnumerable<ITagSpan<ErrorTag>> GetTagsFromElements(IEnumerable<LexicalElement> elements, SnapshotSpan span)
{
foreach (var element in elements.Where(element => element.Name.Contains("_")))
{
var startIndex = span.Start.Position + element.StartCursor.Location;
var endIndex = span.Start.Position + element.EndCursor.Location;
if (endIndex > span.End.Position)
continue;
var errorSpan = new Span(startIndex, endIndex - startIndex);
if(errorSpan.IntersectsWith(span))
yield return new TagSpan<ErrorTag>(new SnapshotSpan(span.Snapshot,
new Span(span.Start.Position + element.StartCursor.Location,
element.EndCursor.Location - element.StartCursor.Location)), new ErrorTag(PredefinedErrorTypeNames.SyntaxError));
}
}
这是我的代码提供商:
[Export(typeof(ITaggerProvider))]
[ContentType("ConfigNode")]
[TagType(typeof(ErrorTag))]
class SyntaxErrorTaggerProvider : ITaggerProvider
{
public ITagger<T> CreateTagger<T>(ITextBuffer buffer) where T : ITag
{
//create a single tagger for each buffer.
Func<ITagger<T>> sc = delegate () { return new SyntaxErrorTagger(buffer) as ITagger<T>; };
return buffer.Properties.GetOrCreateSingletonProperty(sc);
}
}