Created
May 25, 2015 11:23
-
-
Save jonathanvdc/447a67b8381965dfaa9c to your computer and use it in GitHub Desktop.
Xml Highlighter for WinRT apps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using System.Linq; | |
using Windows.UI; | |
namespace Highlighter | |
{ | |
public struct HighlightedRange | |
{ | |
public HighlightedRange(int StartPosition, int Length, Color Color) | |
{ | |
this = new HighlightedRange(); | |
this.StartPosition = StartPosition; | |
this.Length = Length; | |
this.Color = Color; | |
} | |
public int StartPosition { get; private set; } | |
public int Length { get; private set; } | |
public int EndPosition { get { return StartPosition + Length; } } | |
public Color Color { get; private set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
var doc = this.CodeBox.Document; // this.CodeBox is a RichEditBox | |
string text; | |
doc.GetText(Windows.UI.Text.TextGetOptions.None, out text); | |
text = text.Replace('\r', '\n'); // Transform this so we don't have to deal with '\r' | |
var highlighter = new XmlHighlighter(text); // Highlight code | |
int textLength = text.Length; | |
doc.ApplyDisplayUpdates(); // Apply updates immediately | |
var fullRange = doc.GetRange(0, textLength); | |
fullRange.CharacterFormat.ForegroundColor = Colors.Black; // Apply foreground color before proceeding | |
foreach (var item in highlighter.GetHighlighting()) | |
{ | |
var range = doc.GetRange(item.StartPosition, item.EndPosition); | |
range.CharacterFormat.ForegroundColor = item.Color; // Apply specific colors | |
} | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using System.Linq; | |
using Windows.UI; | |
namespace Highlighter | |
{ | |
public class XmlHighlighter | |
{ | |
public XmlHighlighter(string Code) | |
{ | |
this.Code = Code; | |
} | |
public string Code { get; private set; } | |
private bool inNode; | |
private int index; | |
private List<HighlightedRange> highlighting; | |
private char Current | |
{ | |
get | |
{ | |
return Code[index]; | |
} | |
} | |
private void Next() | |
{ | |
char current = Current; | |
if (current == '<') | |
{ | |
inNode = true; | |
} | |
else if (current == '>') | |
{ | |
inNode = false; | |
} | |
index++; | |
} | |
#region Parsing | |
private void ParseString(char StartChar) | |
{ | |
int startIndex = index; | |
index++; | |
while (index < Code.Length) | |
{ | |
if (Current == StartChar) | |
{ | |
index++; | |
highlighting.Add(new HighlightedRange(startIndex, index - startIndex, StringColor)); | |
ParseAny(); | |
return; | |
} | |
else if (IsSpecialChar(Current)) | |
{ | |
ParseAny(); | |
return; | |
} | |
index++; | |
} | |
} | |
private void ParseNode() | |
{ | |
if (index + 3 < Code.Length && Code.Substring(index, 4) == "<!--") | |
{ | |
ParseComment(); | |
} | |
else | |
{ | |
Next(); | |
while (IsExtraNodeChar(Current)) | |
{ | |
index++; | |
} | |
ParseName(NodeColor); | |
} | |
} | |
private void ParseComment() | |
{ | |
inNode = false; | |
int startIndex = index; | |
index += 4; | |
while (index + 2 < Code.Length) | |
{ | |
if (Code.Substring(index, 3) == "-->") | |
{ | |
index += 3; | |
break; | |
} | |
else | |
{ | |
index++; | |
} | |
} | |
highlighting.Add(new HighlightedRange(startIndex, index - startIndex, CommentColor)); | |
} | |
private void ParseName(Color Color) | |
{ | |
int startIndex = index; | |
while (index < Code.Length) | |
{ | |
if (Current == ' ' || IsSpecialChar(Current)) | |
{ | |
highlighting.Add(new HighlightedRange(startIndex, index - startIndex, Color)); | |
ParseAny(); | |
return; | |
} | |
index++; | |
} | |
} | |
private void ParseName() | |
{ | |
ParseName(NameColor); | |
} | |
private void ParseAny() | |
{ | |
while (index < Code.Length) | |
{ | |
var current = Current; | |
if (current == '"' || current == '\'') | |
{ | |
ParseString(current); | |
} | |
else if (current == '<') | |
{ | |
ParseNode(); | |
} | |
else if (inNode && char.IsLetterOrDigit(current)) | |
{ | |
ParseName(); | |
} | |
else | |
{ | |
Next(); | |
} | |
} | |
} | |
#endregion | |
public List<HighlightedRange> GetHighlighting() | |
{ | |
index = 0; | |
highlighting = new List<HighlightedRange>(); | |
inNode = false; | |
ParseAny(); | |
return highlighting; | |
} | |
#region Static | |
private static char[] specialChars = new char[] { '<', '>', '=', '"', '\'', '/' }; | |
public static bool IsSpecialChar(char Value) | |
{ | |
return specialChars.Contains(Value); | |
} | |
public static bool IsExtraNodeChar(char Value) | |
{ | |
return Value == '/' || Value == '!' || Value == '?'; | |
} | |
public static Color StringColor | |
{ | |
get | |
{ | |
return Colors.DarkBlue; | |
} | |
} | |
public static Color NodeColor | |
{ | |
get | |
{ | |
return Colors.DarkRed; | |
} | |
} | |
public static Color NameColor | |
{ | |
get | |
{ | |
return Colors.Red; | |
} | |
} | |
public static Color CommentColor | |
{ | |
get | |
{ | |
return Colors.DarkGreen; | |
} | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment