This topic provides an overview of the xamSyntaxEditor™ control’s error reporting functionality and shows how to configure and work with the control.
The following topics are prerequisites to understanding this topic:
This topic contains the following sections:
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.
You can set whether the error squigglies are displayed by setting the xamSyntaxEditor’s ErrorDisplayMode property.
The following table maps the desired property settings configuration.
You can change the color of the squiggly underlines using the ClassificationAppearanceMap property of the xamSyntaxEditor .
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
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.
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
The following topics provide additional information related to this topic.