Skip to content

Latest commit

 

History

History
47 lines (35 loc) · 1.74 KB

diagnosticMarkupStyle.md

File metadata and controls

47 lines (35 loc) · 1.74 KB

Diagnostic Markup Style Handler

The DiagnosticMarkupCodeHandler type provides the ability to handle diagnostic markup syntax in code strings used in test fixtures.

Features

  • Get diagnostic spans
  • Remove all markup indicators from code
  • Add markup indicators at specified locations
  • Convert between styles

Provided Supported Styles

Supporting Custom Markup Style

The handler type is extensible so that it supports defining custom markup styles, much like how the supported ones are implemented.

Let's consider an example markup style that encloses the node within $, meaning that a node marked with a diagnostic appears as $<node>$:

public sealed class CustomDiagnosticMarkupCodeHandler : DiagnosticMarkupCodeHandler
{
    /// <inheritdoc/>
    protected override DiagnosticIndicatorInfo GetIndicatorInfo()
    {
        return DiagnosticIndicatorInfo.UnboundDiagnostic("$", "$");
    }

    /// <inheritdoc/>
    public override string RemoveMarkup(string code)
    {
        return code.Remove("$");
    }

    /// <inheritdoc/>
    [ExcludeFromCodeCoverage]
    public override string MarkupDiagnostic(string rawStringNode)
    {
        return $"${rawStringNode}$";
    }
}

Normally, the RemoveMarkup and the MarkupDiagnostic methods do not have to be overridden, however, they can trivially improve performance slightly. As general advice, it is preferred to only implement the GetIndicatorInfo method (for the above example), which resorts to using the base methods that already account for the declared diagnostic indicators of all types.