.net 如何使用Visual Studio扩展创建绿色(或蓝色)波浪形装饰

yfjy0ee7  于 2023-11-20  发布在  .NET
关注(0)|答案(3)|浏览(195)

我有一个Visual Studio扩展,可以显示红色的错误曲线。我也喜欢提供其他颜色的曲线,例如黄色的警告。
创建红色曲线可以通过扩展ITagger类来完成,沿着行:

  1. internal sealed class MySquigglesTagger : ITagger<IErrorTag> {
  2. public IEnumerable<ITagSpan<IErrorTag>> GetTags(NormalizedSnapshotSpanCollection spans) {
  3. foreach (IMappingTagSpan<MyTokenTag> myTokenTag in this._aggregator.GetTags(spans))
  4. SnapshotSpan tagSpan = myTokenTag.Span.GetSpans(this._sourceBuffer)[0];
  5. yield return new TagSpan<IErrorTag>(tagSpan, new ErrorTag("Error", "some info about the error"));
  6. }
  7. }
  8. }

字符串
我所尝试的:
1.我的直觉(错误地)告诉我,返回一个带有不同errorType的ErrorTag可能会产生一个不同类型的标签,但是无论你传递什么字符串,波浪线都是红色的。例如,new ErrorTag(“Warning”)给出红色波浪线。MSDN文档几乎不存在。参见ErrorTag
1.在Tagging namespace中没有提到实现ITag的不同Tag类。我希望存在WarningTag或InfoTag。
1.在MSDN论坛上提问。

问题:如何创建绿色(或蓝色或黄色)波浪形装饰?可悲的是,即使是《双城之战》或错综复杂的解决方案也很受欢迎.

我的目标是VS2015和VS 2017。
编辑:在输入这个问题时,有人在MSDN论坛上回答说,这不能用当前的API来完成。在Visual Studio中真的不能制作黄色的波浪线吗?!

bweufnob

bweufnob1#

PredefinedErrorTypeNames包含ErrorTagErrorType属性的支持值。
您已经接近“Warning”,但PredefinedErrorTypeNames.Warning的值似乎是“compiler warning”。

uqdfh47h

uqdfh47h2#

只是为了记录我自己的问题和答案。
创建一个包含以下内容的文件SquigglesTaggerProvider.cs

  1. [Export(typeof(IViewTaggerProvider))]
  2. [ContentType("Your Content Type")]
  3. [TagType(typeof(ErrorTag))]
  4. internal sealed class SquigglesTaggerProvider : IViewTaggerProvider {
  5. [Import]
  6. private IBufferTagAggregatorFactoryService _aggregatorFactory = null;
  7. public ITagger<T> CreateTagger<T>(ITextView textView, ITextBuffer buffer) where T : ITag {
  8. ITagger<T> sc() {
  9. return new SquigglesTagger(buffer, this._aggregatorFactory) as ITagger<T>;
  10. }
  11. return buffer.Properties.GetOrCreateSingletonProperty(sc);
  12. }
  13. }

字符串
创建一个名为SquigglesTagger.cs的文件,包含以下内容:

  1. internal sealed class SquigglesTagger : ITagger<IErrorTag> {
  2. private readonly ITextBuffer _sourceBuffer;
  3. private readonly ITagAggregator<AsmTokenTag> _aggregator;
  4. internal SquigglesTagger(ITextBuffer buffer, IBufferTagAggregatorFactoryService aggregatorFactory) {
  5. this._sourceBuffer = buffer;
  6. ITagAggregator<AsmTokenTag> sc() {
  7. return aggregatorFactory.CreateTagAggregator<AsmTokenTag>(buffer);
  8. }
  9. this._aggregator = buffer.Properties.GetOrCreateSingletonProperty(sc);
  10. }
  11. public IEnumerable<ITagSpan<IErrorTag>> GetTags(NormalizedSnapshotSpanCollection spans) {
  12. foreach (IMappingTagSpan<MyTokenTag> myTokenTag in this._aggregator.GetTags(spans))
  13. SnapshotSpan tagSpan = myTokenTag.Span.GetSpans(this._sourceBuffer)[0];
  14. yield return new TagSpan<IErrorTag>(tagSpan, new ErrorTag(PredefinedErrorTypeNames.SyntaxError, "some info about the error"));
  15. }
  16. }
  17. }


PredefinedErrorTypeNames具有不同的预定义错误。

  1. public const string SyntaxError = "syntax error";
  2. public const string CompilerError = "compiler error";
  3. public const string OtherError = "other error";
  4. public const string Warning = "compiler warning";
  5. public const string Suggestion = "suggestion";


代码取自我的仓库here

展开查看全部
wfsdck30

wfsdck303#

确切地说,波浪形装饰具有一个Brush,该Brush要么是Red SolidColorBrush,要么是由具有与IErrorTag.ErrorType匹配的NameAttribute名称的导出的EditorType定义确定的Brush,该EditorType定义具有与IErrorTag. ErrorType匹配的NameAttribute的对应导出的ErrorTypeDefinition。
它是来自编辑器定义.编辑器ResourceDictionaryFromDefinition的ResourceDictionary,用于确定画笔。如果您没有覆盖此方法,则导出的编辑器定义可以为SolidColorBrush设置ForegroundColor,也可以将ForegroundBrush设置为任何画笔实现。
这些导出可以在ITagger<T>实现上进行。

  1. class ErrorEditorFormatDefinition : EditorFormatDefinition
  2. {
  3. public ErrorEditorFormatDefinition(Color errorColor, string displayName = null) : this(displayName)
  4. {
  5. this.ForegroundColor = errorColor;
  6. }
  7. public ErrorEditorFormatDefinition(Brush brush, string displayName = null) : this(displayName)
  8. {
  9. this.ForegroundBrush = brush;
  10. }
  11. private ErrorEditorFormatDefinition(string displayName)
  12. {
  13. if (displayName != null)
  14. {
  15. this.DisplayName = displayName;
  16. }
  17. this.BackgroundCustomizable = false;
  18. }
  19. }
  20. internal class ErrorTagger : ITagger<IErrorTag>
  21. {
  22. public const string MethodNotCoveredErrorType = "MethodNotCovered";
  23. public const string MethodPartiallyCoveredErrorType = "MethodPartiallyCovered";
  24. public const string LineNotCoveredErrorType = "LineNotCovered";
  25. public const string LinePartiallyCoveredErrorType = "LinePartiallyCovered";
  26. public event EventHandler<SnapshotSpanEventArgs> TagsChanged;
  27. [Export(typeof(ErrorTypeDefinition))]
  28. [Name(MethodNotCoveredErrorType)]
  29. public ErrorTypeDefinition MethodNotCoveredErrorTypeDefinition { get; }
  30. [Export(typeof(ErrorTypeDefinition))]
  31. [Name(MethodPartiallyCoveredErrorType)]
  32. public ErrorTypeDefinition MethodPartiallyErrorTypeDefinition { get; }
  33. [Export(typeof(ErrorTypeDefinition))]
  34. [Name(LineNotCoveredErrorType)]
  35. public ErrorTypeDefinition LineNotCoveredErrorTypeDefinition { get; }
  36. [Export(typeof(ErrorTypeDefinition))]
  37. [Name(LinePartiallyCoveredErrorType)]
  38. public ErrorTypeDefinition LinePartiallyErrorTypeDefinition { get; }
  39. [Export(typeof(EditorFormatDefinition))]
  40. [Name(MethodNotCoveredErrorType)]
  41. [UserVisible(true)]
  42. public EditorFormatDefinition MethodNotCoveredErrorFormatDefinition { get; } = new ErrorEditorFormatDefinition(new SolidColorBrush(Colors.Pink));
  43. [Export(typeof(EditorFormatDefinition))]
  44. [Name(MethodPartiallyCoveredErrorType)]
  45. public EditorFormatDefinition MethodPartiallyCoveredErrorFormatDefinition { get; } = new ErrorEditorFormatDefinition(new LinearGradientBrush()
  46. {
  47. StartPoint = new System.Windows.Point(0, 0),
  48. EndPoint = new System.Windows.Point(1, 0),
  49. GradientStops = new GradientStopCollection
  50. {
  51. new GradientStop(Colors.Yellow, 0.0),
  52. new GradientStop(Colors.Red, 0.25),
  53. new GradientStop(Colors.Blue, 0.75),
  54. new GradientStop(Colors.LimeGreen, 1.0)
  55. }
  56. },"Call me what you want");
  57. [Name(LineNotCoveredErrorType)]
  58. [Export(typeof(EditorFormatDefinition))]
  59. public EditorFormatDefinition LineNotCoveredErrorFormatDefinition { get; } = new ErrorEditorFormatDefinition(Colors.Brown);
  60. [Name(LinePartiallyCoveredErrorType)]
  61. public EditorFormatDefinition LinePartiallyCoveredErrorFormatDefinition { get; } = new ErrorEditorFormatDefinition(Colors.Cyan);
  62. public IEnumerable<ITagSpan<IErrorTag>> GetTags(NormalizedSnapshotSpanCollection spans)
  63. {
  64. return new List<ITagSpan<IErrorTag>>
  65. {
  66. // determine as appropriate
  67. new TagSpan<IErrorTag>(new SnapshotSpan(spans[0].Snapshot, new Span(0, spans[0].Snapshot.Length)), new ErrorTag(MethodPartiallyCoveredErrorType, "Method partially covered")),
  68. };
  69. }
  70. }

字符串
x1c 0d1x的数据
PredefinedErrorTypeNames只是那些保证具有匹配的ErrorTypeDefinition/EditorTypeDefinition的名称。

  1. public static class PredefinedErrorTypeNames {
  2. /// <summary>Represents syntax errors.</summary>
  3. public const string SyntaxError = "syntax error";
  4. /// <summary>Represents compiler errors.</summary>
  5. public const string CompilerError = "compiler error";
  6. /// <summary>Represents other errors.</summary>
  7. public const string OtherError = "other error";
  8. /// <summary>Represents compiler warnings.</summary>
  9. public const string Warning = "compiler warning";
  10. /// <summary>Represents a suggestion with no visual treatment.</summary>
  11. public const string Suggestion = "suggestion";
  12. ///<summary>Represents a suggestion with subtle visual treatment.</summary>
  13. public const string HintedSuggestion = "hinted suggestion";
  14. }


请注意,建议在字体和颜色中不可见-还请检查下面的DisplayName更改,以了解如何在字体和颜色中找到这些。

  1. [Export(typeof(EditorFormatDefinition))]
  2. [Name("suggestion")]
  3. [UserVisible(false)]
  4. internal class SuggestionClassificationFormatDefinition : EditorFormatDefinition
  5. {
  6. public SuggestionClassificationFormatDefinition()
  7. {
  8. this.ForegroundBrush = (Brush)Brushes.Transparent;
  9. this.BackgroundCustomizable = new bool?(false);
  10. this.DisplayName = "Suggestion";
  11. }
  12. }
  13. [Export(typeof(EditorFormatDefinition))]
  14. [Name("compiler warning")]
  15. [UserVisible(true)]
  16. internal class WarningClassificationFormatDefinition : EditorFormatDefinition
  17. {
  18. public WarningClassificationFormatDefinition()
  19. {
  20. this.ForegroundBrush = (Brush)new SolidColorBrush(Color.FromRgb((byte)0, (byte)128, (byte)0));
  21. this.BackgroundCustomizable = new bool?(false);
  22. this.DisplayName = "Warning";
  23. }
  24. }
  25. [Export(typeof(EditorFormatDefinition))]
  26. [Name("compiler error")]
  27. [UserVisible(true)]
  28. internal class CompilerErrorClassificationFormatDefinition : EditorFormatDefinition
  29. {
  30. public CompilerErrorClassificationFormatDefinition()
  31. {
  32. this.ForegroundBrush = (Brush)Brushes.Blue;
  33. this.BackgroundCustomizable = new bool?(false);
  34. this.DisplayName = "Compiler Error";
  35. }
  36. }
  37. [Export(typeof(EditorFormatDefinition))]
  38. [Name("other error")]
  39. [UserVisible(true)]
  40. internal class OtherErrorClassificationFormatDefinition : EditorFormatDefinition
  41. {
  42. public OtherErrorClassificationFormatDefinition()
  43. {
  44. this.ForegroundBrush = (Brush)new SolidColorBrush(Color.FromRgb((byte)149, (byte)23, (byte)149));
  45. this.BackgroundCustomizable = new bool?(false);
  46. this.DisplayName = "Other Error";
  47. }
  48. }
  49. [Export(typeof(EditorFormatDefinition))]
  50. [Name("syntax error")]
  51. [UserVisible(true)]
  52. internal class SyntaxErrorFormatDefinition : EditorFormatDefinition
  53. {
  54. public SyntaxErrorFormatDefinition()
  55. {
  56. this.ForegroundBrush = (Brush)Brushes.Red;
  57. this.BackgroundCustomizable = new bool?(false);
  58. this.DisplayName = "Syntax Error";
  59. }
  60. }
  61. [Export(typeof(EditorFormatDefinition))]
  62. [Name("hinted suggestion")]
  63. [UserVisible(true)]
  64. internal class HintedSuggestionClassificationFormatDefinition : EditorFormatDefinition
  65. {
  66. public HintedSuggestionClassificationFormatDefinition()
  67. {
  68. this.ForegroundBrush = (Brush)new SolidColorBrush(Color.FromRgb((byte)165, (byte)165, (byte)165));
  69. this.BackgroundCustomizable = new bool?(false);
  70. this.DisplayName = "Suggestion ellipses (...)";
  71. }
  72. }


在Vs 2022中有一个不包括在此列表中。ErrorType“编辑并继续”默认提供紫色画笔,它是字体和颜色中的“粗鲁编辑”。
如果你想在visual studio中编辑squiggle颜色,那么在编辑器定义上设置[UserVisible(true)],如果你这样做了this.BackgroundCustomizable = false;,就像我对ErrorEditorColor定义类所做的那样,假设背景没有被使用。如果你提供的是非SolidColorBrush,不要让用户可见。
最后,ErrorType = PredefinedErrorTypeNames.HintedSuggestion有一个不同的装饰,其他的都是波浪形。

展开查看全部

相关问题