How to create green (or blue) squiggle adornments with a Visual Studio extension

喜夏-厌秋 提交于 2019-12-03 12:22:08

The PredefinedErrorTypeNames contains the supported values for the ErrorType property of the ErrorTag.

You got close with "Warning", but the value of PredefinedErrorTypeNames.Warning appears to be "compiler warning".

Just to document my own question and answer.

Create a file SquigglesTaggerProvider.cs with the following content:

[Export(typeof(IViewTaggerProvider))]
[ContentType("Your Content Type")]
[TagType(typeof(ErrorTag))]
internal sealed class SquigglesTaggerProvider : IViewTaggerProvider {
    [Import]
    private IBufferTagAggregatorFactoryService _aggregatorFactory = null;

    public ITagger<T> CreateTagger<T>(ITextView textView, ITextBuffer buffer) where T : ITag {
        ITagger<T> sc() {
            return new SquigglesTagger(buffer, this._aggregatorFactory) as ITagger<T>;
        }
        return buffer.Properties.GetOrCreateSingletonProperty(sc);
    }
}

Create a file named SquigglesTagger.cs with the following content:

internal sealed class SquigglesTagger : ITagger<IErrorTag> {
    private readonly ITextBuffer _sourceBuffer;
    private readonly ITagAggregator<AsmTokenTag> _aggregator;

    internal SquigglesTagger(ITextBuffer buffer, IBufferTagAggregatorFactoryService aggregatorFactory) {
        this._sourceBuffer = buffer;            
        ITagAggregator<AsmTokenTag> sc() {   
            return aggregatorFactory.CreateTagAggregator<AsmTokenTag>(buffer);
        }
        this._aggregator = buffer.Properties.GetOrCreateSingletonProperty(sc);
    }

    public IEnumerable<ITagSpan<IErrorTag>> GetTags(NormalizedSnapshotSpanCollection spans) {
        foreach (IMappingTagSpan<MyTokenTag> myTokenTag in this._aggregator.GetTags(spans))        
            SnapshotSpan tagSpan = myTokenTag.Span.GetSpans(this._sourceBuffer)[0];
            yield return new TagSpan<IErrorTag>(tagSpan, new ErrorTag(PredefinedErrorTypeNames.SyntaxError, "some info about the error"));
        }
    }
}

The PredefinedErrorTypeNames has different errors predefined.

public const string SyntaxError = "syntax error";
public const string CompilerError = "compiler error";
public const string OtherError = "other error";
public const string Warning = "compiler warning";
public const string Suggestion = "suggestion";

The code is taken from my repository here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!