-
Notifications
You must be signed in to change notification settings - Fork 1
/
XmlSyntaxData.cs
58 lines (53 loc) · 2.42 KB
/
XmlSyntaxData.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Microsoft.Language.Xml;
using System;
using System.Collections.Generic;
using System.Linq;
namespace XmlSyntaxVisualizerUwp
{
/// <summary>
/// Data class used to expose interesting Xml <see cref="SyntaxNode"/> properties to the UI.
/// </summary>
public class XmlSyntaxData
{
public int HashId { get; set; }
public string Type { get; set; }
public string TypeClass { get; set; }
public string Text { get; set; }
public List<XmlSyntaxError> Errors { get; set; }
public int SpanStart { get; set; }
public int SpanEnd { get; set; }
public int Length => SpanEnd - SpanStart;
public bool IsError => Errors != null && Errors.Count() > 0;
public string ErrorText => IsError ? string.Join(' ', Errors.Select(e => e.Id.ToString().Substring(4) + ": " + e.Description)) : string.Empty;
// Hierarchical reference for TreeView templates
public List<XmlSyntaxData> Children { get; set; }
/// <summary>
/// Reads in a <see cref="SyntaxNode"/> and returns a <see cref="XmlSyntaxData"/>
/// </summary>
/// <param name="node">Parsed Xml Node</param>
/// <param name="withChildren">Include children in this new node.</param>
/// <returns>UI ready data.</returns>
public static XmlSyntaxData FromNode(SyntaxNode node, bool withChildren = true)
{
return new XmlSyntaxData()
{
HashId = node.GetHashCode(),
Type = node.IsList ? "SyntaxList" : node.GetType().Name,
TypeClass = node.IsList ? "list" : (node.IsToken ? "token" : "syntax"),
Text = node.IsToken ? (node as SyntaxToken).Text : string.Empty,
Errors = node.ContainsDiagnostics ?
node.GetDiagnostics().Select(d => new XmlSyntaxError()
{
Id = d.ErrorID,
Description = d.GetDescription()
}).ToList()
: Array.Empty<XmlSyntaxError>().ToList(),
SpanStart = node.FullSpan.Start,
SpanEnd = node.FullSpan.End,
Children = withChildren ? node.ChildNodes.Select(child => XmlSyntaxData.FromNode(child)).ToList() : Array.Empty<XmlSyntaxData>().ToList()
};
}
}
}