准确来说,这个波浪装饰有一个刷子,它要么是红色的实心刷子,要么是由导出的EditorFormatDefinition决定的刷子,其NameAttribute名称与IErrorTag.ErrorType的名称匹配,并且对应的导出的ErrorTypeDefinition也具有与IErrorTag.ErrorType匹配的NameAttribute。
是EditorFormatDefinition.CreateResourceDictionaryFromDefinition中的ResourceDictionary确定了刷子。如果您没有重写此方法,则导出的EditorFormatDefinition可以设置ForegroundColor为SolidColorBrush,或者将ForegroundBrush设置为任何刷子实现。
这些导出可以在ITagger实现上进行。例如
class ErrorEditorFormatDefinition : EditorFormatDefinition
{
public ErrorEditorFormatDefinition(Color errorColor, string displayName = null) : this(displayName)
{
this.ForegroundColor = errorColor;
}
public ErrorEditorFormatDefinition(Brush brush, string displayName = null) : this(displayName)
{
this.ForegroundBrush = brush;
}
private ErrorEditorFormatDefinition(string displayName)
{
if (displayName != null)
{
this.DisplayName = displayName;
}
this.BackgroundCustomizable = false;
}
}
internal class ErrorTagger : ITagger<IErrorTag>
{
public const string MethodNotCoveredErrorType = "MethodNotCovered";
public const string MethodPartiallyCoveredErrorType = "MethodPartiallyCovered";
public const string LineNotCoveredErrorType = "LineNotCovered";
public const string LinePartiallyCoveredErrorType = "LinePartiallyCovered";
public event EventHandler<SnapshotSpanEventArgs> TagsChanged;
[Export(typeof(ErrorTypeDefinition))]
[Name(MethodNotCoveredErrorType)]
public ErrorTypeDefinition MethodNotCoveredErrorTypeDefinition { get; }
[Export(typeof(ErrorTypeDefinition))]
[Name(MethodPartiallyCoveredErrorType)]
public ErrorTypeDefinition MethodPartiallyErrorTypeDefinition { get; }
[Export(typeof(ErrorTypeDefinition))]
[Name(LineNotCoveredErrorType)]
public ErrorTypeDefinition LineNotCoveredErrorTypeDefinition { get; }
[Export(typeof(ErrorTypeDefinition))]
[Name(LinePartiallyCoveredErrorType)]
public ErrorTypeDefinition LinePartiallyErrorTypeDefinition { get; }
[Export(typeof(EditorFormatDefinition))]
[Name(MethodNotCoveredErrorType)]
[UserVisible(true)]
public EditorFormatDefinition MethodNotCoveredErrorFormatDefinition { get; } = new ErrorEditorFormatDefinition(new SolidColorBrush(Colors.Pink));
[Export(typeof(EditorFormatDefinition))]
[Name(MethodPartiallyCoveredErrorType)]
public EditorFormatDefinition MethodPartiallyCoveredErrorFormatDefinition { get; } = new ErrorEditorFormatDefinition(new LinearGradientBrush()
{
StartPoint = new System.Windows.Point(0, 0),
EndPoint = new System.Windows.Point(1, 0),
GradientStops = new GradientStopCollection
{
new GradientStop(Colors.Yellow, 0.0),
new GradientStop(Colors.Red, 0.25),
new GradientStop(Colors.Blue, 0.75),
new GradientStop(Colors.LimeGreen, 1.0)
}
},"Call me what you want");
[Name(LineNotCoveredErrorType)]
[Export(typeof(EditorFormatDefinition))]
public EditorFormatDefinition LineNotCoveredErrorFormatDefinition { get; } = new ErrorEditorFormatDefinition(Colors.Brown);
[Name(LinePartiallyCoveredErrorType)]
public EditorFormatDefinition LinePartiallyCoveredErrorFormatDefinition { get; } = new ErrorEditorFormatDefinition(Colors.Cyan);
public IEnumerable<ITagSpan<IErrorTag>> GetTags(NormalizedSnapshotSpanCollection spans)
{
return new List<ITagSpan<IErrorTag>>
{
new TagSpan<IErrorTag>(new SnapshotSpan(spans[0].Snapshot, new Span(0, spans[0].Snapshot.Length)), new ErrorTag(MethodPartiallyCoveredErrorType, "Method partially covered")),
};
}
}
![LinearGradientBrush squiggles](https://istack.dev59.com/r0TvT.webp)
预定义的错误类型名称只是那些保证具有相匹配的错误类型定义/编辑器格式定义的名称。
public static class PredefinedErrorTypeNames {
/// <summary>Represents syntax errors.</summary>
public const string SyntaxError = "syntax error";
/// <summary>Represents compiler errors.</summary>
public const string CompilerError = "compiler error";
/// <summary>Represents other errors.</summary>
public const string OtherError = "other error";
/// <summary>Represents compiler warnings.</summary>
public const string Warning = "compiler warning";
/// <summary>Represents a suggestion with no visual treatment.</summary>
public const string Suggestion = "suggestion";
///<summary>Represents a suggestion with subtle visual treatment.</summary>
public const string HintedSuggestion = "hinted suggestion";
}
请注意,在“字体和颜色”中看不到“建议”选项 - 还要检查下面的“显示名称”更改,以了解如何在“字体和颜色”中找到它们。
[Export(typeof(EditorFormatDefinition))]
[Name("suggestion")]
[UserVisible(false)]
internal class SuggestionClassificationFormatDefinition : EditorFormatDefinition
{
public SuggestionClassificationFormatDefinition()
{
this.ForegroundBrush = (Brush)Brushes.Transparent;
this.BackgroundCustomizable = new bool?(false);
this.DisplayName = "Suggestion";
}
}
[Export(typeof(EditorFormatDefinition))]
[Name("compiler warning")]
[UserVisible(true)]
internal class WarningClassificationFormatDefinition : EditorFormatDefinition
{
public WarningClassificationFormatDefinition()
{
this.ForegroundBrush = (Brush)new SolidColorBrush(Color.FromRgb((byte)0, (byte)128, (byte)0));
this.BackgroundCustomizable = new bool?(false);
this.DisplayName = "Warning";
}
}
[Export(typeof(EditorFormatDefinition))]
[Name("compiler error")]
[UserVisible(true)]
internal class CompilerErrorClassificationFormatDefinition : EditorFormatDefinition
{
public CompilerErrorClassificationFormatDefinition()
{
this.ForegroundBrush = (Brush)Brushes.Blue;
this.BackgroundCustomizable = new bool?(false);
this.DisplayName = "Compiler Error";
}
}
[Export(typeof(EditorFormatDefinition))]
[Name("other error")]
[UserVisible(true)]
internal class OtherErrorClassificationFormatDefinition : EditorFormatDefinition
{
public OtherErrorClassificationFormatDefinition()
{
this.ForegroundBrush = (Brush)new SolidColorBrush(Color.FromRgb((byte)149, (byte)23, (byte)149));
this.BackgroundCustomizable = new bool?(false);
this.DisplayName = "Other Error";
}
}
[Export(typeof(EditorFormatDefinition))]
[Name("syntax error")]
[UserVisible(true)]
internal class SyntaxErrorFormatDefinition : EditorFormatDefinition
{
public SyntaxErrorFormatDefinition()
{
this.ForegroundBrush = (Brush)Brushes.Red;
this.BackgroundCustomizable = new bool?(false);
this.DisplayName = "Syntax Error";
}
}
[Export(typeof(EditorFormatDefinition))]
[Name("hinted suggestion")]
[UserVisible(true)]
internal class HintedSuggestionClassificationFormatDefinition : EditorFormatDefinition
{
public HintedSuggestionClassificationFormatDefinition()
{
this.ForegroundBrush = (Brush)new SolidColorBrush(Color.FromRgb((byte)165, (byte)165, (byte)165));
this.BackgroundCustomizable = new bool?(false);
this.DisplayName = "Suggestion ellipses (...)";
}
}
在Vs 2022中有一个未包含在此列表中的错误类型“Edit and Continue”,默认情况下提供了一个紫色的刷子,在“Fonts And Colors”中是“Rude Edit”。
如果您想要在Visual Studio中编辑波浪线的颜色,那么请在EditorFormatDefinition上设置[UserVisible(true)],如果您这样做,请将this.BackgroundCustomizable = false;设置为false,就像我在ErrorEditorFormatDefinition类中所做的那样,因为背景不会被使用。如果您提供的不是SolidColorBrush,请不要将其设置为用户可见。
最后,对于ErrorType = PredefinedErrorTypeNames.HintedSuggestion,有一个不同的装饰,其他都是波浪线。
Tagging
命名空间还有其他实现了ITag
接口的标记,其中包括TextMarkerTag
。你读过 https://msdn.microsoft.com/en-us/library/dd885121.aspx 吗? - C.Evenhuis