Version

Error Reporting (xamSyntaxEditor)

Topic Overview

Purpose

This topic provides an overview of the xamSyntaxEditor™ control’s error reporting functionality and shows how to configure and work with the control.

Required background

The following topics are prerequisites to understanding this topic:

Topic Purpose

In this topic, you will find information to help you better understand the xamSyntaxEditor’s functions.

This topic covers the text editing capabilities of the xamSyntaxEditor control from both the developer and user’s perspective.

Introduction

Error reporting summary

The xamSyntaxEditor highlights the flagged text as syntactically incorrect, based on the current language’s grammar rules. You can Identify the incorrect text by its being underlined using squiggly underlines, which by default are red. The user can hover the mouse over a squiggly underline to see details about the error, displayed in a tooltip.

The screenshot below is of a xamSyntaxEditor , showing some errors and a tooltip with error description of the hovered error.

xamSyntaxEditor Errors.png

Configure Whether the xamSyntaxEditor Displays Error Squigglies

Overview

You can set whether the error squigglies are displayed by setting the xamSyntaxEditor’s ErrorDisplayMode property.

Property settings

The following table maps the desired property settings configuration.

In order to: Use this property: And set it to:

Change error squigglies display mode

ErrorDisplayMode

An instance of SyntaxErrorDisplayMode

Configure Color of the Squiggly Underlines

Overview

You can change the color of the squiggly underlines using the ClassificationAppearanceMap property of the xamSyntaxEditor .

Example

The following code changes the color of the squiggly underlines to Magenta:

In C#:

// create a new classification appearance map
ClassificationAppearanceMap cam = new ClassificationAppearanceMap();
// add a solid color brush with magenta color as "SyntaxError" classification type
cam.AddMapEntry(
    ClassificationType.SyntaxError,
    new TextDocumentAppearance()
    {
        Foreground = new SolidColorBrush(Colors.Magenta)
    }
);
// set the map to the xamSyntaxEditor
this.xamSyntaxEditor1.ClassificationAppearanceMap = cam;

In Visual Basic:

' create a new classification appearance map
Dim cam As New ClassificationAppearanceMap()
' add a solid color brush with magenta color as "SyntaxError" classification type
cam.AddMapEntry(ClassificationType.SyntaxError, New TextDocumentAppearance() With { _
      .Foreground = New SolidColorBrush(Colors.Magenta) _
})
' set the map to the xamSyntaxEditor
Me.xamSyntaxEditor1.ClassificationAppearanceMap = cam

Obtain the Document’s Errors

Overview

After initially loading a document each change made to the document in the editor the xamSyntaxEditor re-parses the document’s content. This is an asynchronous process, which you can monitor by attaching a handler to the PropertyChanged event of TextDocument and checking for the SyntaxTree property. After a change to the SyntaxTree’s property, you can check for errors by querying the ContainsDiagnostics property of each node of the syntax tree.

Example

The following code example shows how to obtain the list of syntax errors and how to find each error description and the line where the error occurs:

In C#:

// obtain document
TextDocument doc = this.xamSyntaxEditor1.Document;
if (doc.SyntaxTree.RootNode.ContainsDiagnostics)
{
    // create a text span of the whole document
    TextSpan ts = new TextSpan(0, doc.CurrentSnapshot.Length);
    // obtain errors from the whole document
    IEnumerable<NodeDiagnostic> diagnostics =
        doc.SyntaxTree.RootNode.GetDiagnostics(ts);
    foreach (NodeDiagnostic nd in diagnostics)
    {
 // obtain the location of the error
 SnapshotSpan span = nd.SnapshotSpan;
 TextLocation location = span.Snapshot.LocationFromOffset(span.Offset);
 int errorLineNumber = location.Line + 1;
 int errorCharacter = location.Character + 1;
        // obtain the error message
        string errorMessage = nd.Message;
 }
}

In Visual Basic:

' obtain document
Dim doc As TextDocument = Me.xamSyntaxEditor1.Document
If doc.SyntaxTree.RootNode.ContainsDiagnostics Then
    ' create a text span of the whole document
    Dim ts As New TextSpan(0, doc.CurrentSnapshot.Length)
    ' obtain errors from the whole document
    Dim diagnostics As IEnumerable(Of NodeDiagnostic) = _
        doc.SyntaxTree.RootNode.GetDiagnostics(ts)
    For Each nd As NodeDiagnostic In diagnostics
 ' obtain the location of the error
        Dim span As SnapshotSpan = nd.Span
        Dim location As TextLocation = span.Snapshot.LocationFromOffset(span.Offset)
        Dim errorLineNumber As Integer = location.Line + 1
        Dim errorCharacter As Integer = location.Character + 1
        ' obtain the error message
        Dim errorMessage As String = nd.Message
    Next
End If

Related Topics

The following topics provide additional information related to this topic.

Topic Purpose

This topic lists the languages supported by the xamSyntaxEditor and shows you how to use each of them.

This topic provides information on how to change the colors and other appearance attributes assigned to language elements by the xamSyntaxEditor .

This topic provides information on how to change the presention of the document’s content inside the xamSyntaxEditor .