[英]How to create green (or blue) squiggle adornments with a Visual Studio extension
我有一個Visual Studio擴展,顯示紅色錯誤信息。 我還喜歡提供其他顏色的花體,例如黃色警告。
可以通過以下方式擴展ITagger類來創建紅色花體:
internal sealed class MySquigglesTagger : ITagger<IErrorTag> {
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("Error", "some info about the error"));
}
}
}
我試過的
問題 :如何創建綠色(或藍色或黃色)花形裝飾物? 可悲的是,甚至奧秘或令人費解的解決方案也受到贊賞。
我的目標是VS2015和VS2017。
編輯:鍵入此問題時,在MSDN論壇上有人回答說,不能用當前的API完成。 在Visual Studio中真的不可能制作出黃色的花體嗎?
該PredefinedErrorTypeNames包含了支持的值ErrorType
的財產ErrorTag
。
您與“ Warning”接近,但是PredefinedErrorTypeNames.Warning
的值似乎是“ compiler warning”。
只是為了記錄我自己的問題和答案。
創建具有以下內容的文件SquigglesTaggerProvider.cs
:
[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);
}
}
創建一個名為SquigglesTagger.cs
的文件,其內容如下:
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"));
}
}
}
PredefinedErrorTypeNames
具有不同的PredefinedErrorTypeNames
錯誤。
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";
該代碼是從我的倉庫采取了這里 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.