|
|
using System; |
|
|
using System.Collections.Generic; |
|
|
using System.Text.RegularExpressions; |
|
|
using System.Windows.Media; |
|
|
|
|
|
namespace WpfEditor |
|
|
{ |
|
|
public class SyntaxHighlighter |
|
|
{ |
|
|
private Dictionary<string, Brush> _keywords = new Dictionary<string, Brush>(); |
|
|
private List<Regex> _patterns = new List<Regex>(); |
|
|
private Dictionary<Regex, Brush> _patternColors = new Dictionary<Regex, Brush>(); |
|
|
private string _commentStart = "/*"; |
|
|
private string _commentEnd = "*/"; |
|
|
private string _lineComment = "//"; |
|
|
|
|
|
public SyntaxHighlighter() |
|
|
{ |
|
|
|
|
|
SetupCppHighlighting(); |
|
|
} |
|
|
|
|
|
public void SetupCppHighlighting() |
|
|
{ |
|
|
|
|
|
string[] keywords = new string[] { |
|
|
"auto", "break", "case", "catch", "class", "const", "continue", "default", "delete", "do", "else", |
|
|
"enum", "explicit", "export", "extern", "for", "friend", "goto", "if", "inline", "mutable", "namespace", |
|
|
"new", "operator", "private", "protected", "public", "register", "return", "sizeof", "static", "struct", |
|
|
"switch", "template", "this", "throw", "try", "typedef", "typeid", "typename", "union", "using", |
|
|
"virtual", "volatile", "while" |
|
|
}; |
|
|
|
|
|
foreach (var keyword in keywords) |
|
|
{ |
|
|
_keywords[keyword] = Brushes.Blue; |
|
|
} |
|
|
|
|
|
|
|
|
_patterns.Add(new Regex(@"\b\d+(\.\d+)?([eE][+-]?\d+)?\b")); |
|
|
_patternColors[_patterns[_patterns.Count - 1]] = Brushes.DarkOrange; |
|
|
|
|
|
|
|
|
_patterns.Add(new Regex(@"""\.\*?""")); |
|
|
_patternColors[_patterns[_patterns.Count - 1]] = Brushes.Brown; |
|
|
|
|
|
_patterns.Add(new Regex(@"'.'|'\\.'|'\\\\'")); |
|
|
_patternColors[_patterns[_patterns.Count - 1]] = Brushes.Brown; |
|
|
|
|
|
|
|
|
_commentStart = "/*"; |
|
|
_commentEnd = "*/"; |
|
|
_lineComment = "//"; |
|
|
} |
|
|
|
|
|
public void Colorize(List<TextLine> lines) |
|
|
{ |
|
|
bool inMultiLineComment = false; |
|
|
|
|
|
for (int i = 0; i < lines.Count; i++) |
|
|
{ |
|
|
var line = lines[i]; |
|
|
string lineText = string.Concat(line.Characters.Select(c => c.Character.ToString())); |
|
|
|
|
|
|
|
|
foreach (var c in line.Characters) |
|
|
{ |
|
|
c.IsComment = false; |
|
|
c.IsKeyword = false; |
|
|
c.IsString = false; |
|
|
c.IsNumber = false; |
|
|
c.Foreground = Brushes.Black; |
|
|
} |
|
|
|
|
|
|
|
|
if (inMultiLineComment) |
|
|
{ |
|
|
int endIndex = lineText.IndexOf(_commentEnd); |
|
|
if (endIndex >= 0) |
|
|
{ |
|
|
|
|
|
for (int j = 0; j <= endIndex + _commentEnd.Length - 1 && j < line.Characters.Count; j++) |
|
|
{ |
|
|
line.Characters[j].IsComment = true; |
|
|
line.Characters[j].Foreground = Brushes.Green; |
|
|
} |
|
|
inMultiLineComment = false; |
|
|
|
|
|
|
|
|
if (endIndex + _commentEnd.Length < lineText.Length) |
|
|
{ |
|
|
ColorizeLineSegment(line, endIndex + _commentEnd.Length, lineText.Length - (endIndex + _commentEnd.Length)); |
|
|
} |
|
|
} |
|
|
else |
|
|
{ |
|
|
|
|
|
for (int j = 0; j < line.Characters.Count; j++) |
|
|
{ |
|
|
line.Characters[j].IsComment = true; |
|
|
line.Characters[j].Foreground = Brushes.Green; |
|
|
} |
|
|
} |
|
|
} |
|
|
else |
|
|
{ |
|
|
|
|
|
int lineCommentIndex = lineText.IndexOf(_lineComment); |
|
|
int multiLineCommentIndex = lineText.IndexOf(_commentStart); |
|
|
|
|
|
if (lineCommentIndex >= 0 && (multiLineCommentIndex < 0 || lineCommentIndex < multiLineCommentIndex)) |
|
|
{ |
|
|
|
|
|
if (lineCommentIndex > 0) |
|
|
{ |
|
|
ColorizeLineSegment(line, 0, lineCommentIndex); |
|
|
} |
|
|
|
|
|
|
|
|
for (int j = lineCommentIndex; j < line.Characters.Count; j++) |
|
|
{ |
|
|
line.Characters[j].IsComment = true; |
|
|
line.Characters[j].Foreground = Brushes.Green; |
|
|
} |
|
|
} |
|
|
else if (multiLineCommentIndex >= 0) |
|
|
{ |
|
|
|
|
|
if (multiLineCommentIndex > 0) |
|
|
{ |
|
|
ColorizeLineSegment(line, 0, multiLineCommentIndex); |
|
|
} |
|
|
|
|
|
int endIndex = lineText.IndexOf(_commentEnd, multiLineCommentIndex + _commentStart.Length); |
|
|
if (endIndex >= 0) |
|
|
{ |
|
|
|
|
|
for (int j = multiLineCommentIndex; j <= endIndex + _commentEnd.Length - 1 && j < line.Characters.Count; j++) |
|
|
{ |
|
|
line.Characters[j].IsComment = true; |
|
|
line.Characters[j].Foreground = Brushes.Green; |
|
|
} |
|
|
|
|
|
|
|
|
if (endIndex + _commentEnd.Length < lineText.Length) |
|
|
{ |
|
|
ColorizeLineSegment(line, endIndex + _commentEnd.Length, lineText.Length - (endIndex + _commentEnd.Length)); |
|
|
} |
|
|
} |
|
|
else |
|
|
{ |
|
|
|
|
|
for (int j = multiLineCommentIndex; j < line.Characters.Count; j++) |
|
|
{ |
|
|
line.Characters[j].IsComment = true; |
|
|
line.Characters[j].Foreground = Brushes.Green; |
|
|
} |
|
|
inMultiLineComment = true; |
|
|
} |
|
|
} |
|
|
else |
|
|
{ |
|
|
|
|
|
ColorizeLineSegment(line, 0, lineText.Length); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
private void ColorizeLineSegment(TextLine line, int startIndex, int length) |
|
|
{ |
|
|
if (length <= 0 || startIndex >= line.Characters.Count) |
|
|
return; |
|
|
|
|
|
|
|
|
string text = string.Concat(line.Characters.GetRange(startIndex, Math.Min(length, line.Characters.Count - startIndex)).Select(c => c.Character.ToString())); |
|
|
|
|
|
|
|
|
foreach (var keyword in _keywords.Keys) |
|
|
{ |
|
|
int index = 0; |
|
|
while ((index = text.IndexOf(keyword, index)) >= 0) |
|
|
{ |
|
|
|
|
|
bool isWordStart = index == 0 || !char.IsLetterOrDigit(text[index - 1]); |
|
|
bool isWordEnd = index + keyword.Length == text.Length || !char.IsLetterOrDigit(text[index + keyword.Length]); |
|
|
|
|
|
if (isWordStart && isWordEnd) |
|
|
{ |
|
|
for (int i = 0; i < keyword.Length && i + index + startIndex < line.Characters.Count; i++) |
|
|
{ |
|
|
line.Characters[i + index + startIndex].IsKeyword = true; |
|
|
line.Characters[i + index + startIndex].Foreground = _keywords[keyword]; |
|
|
} |
|
|
} |
|
|
index += keyword.Length; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
for (int i = 0; i < _patterns.Count; i++) |
|
|
{ |
|
|
var regex = _patterns[i]; |
|
|
var matches = regex.Matches(text); |
|
|
|
|
|
foreach (Match match in matches) |
|
|
{ |
|
|
for (int j = 0; j < match.Length && j + match.Index + startIndex < line.Characters.Count; j++) |
|
|
{ |
|
|
if (regex.ToString().Contains("\\d")) |
|
|
{ |
|
|
line.Characters[j + match.Index + startIndex].IsNumber = true; |
|
|
} |
|
|
else if (regex.ToString().Contains("\\\"")) |
|
|
{ |
|
|
line.Characters[j + match.Index + startIndex].IsString = true; |
|
|
} |
|
|
line.Characters[j + match.Index + startIndex].Foreground = _patternColors[regex]; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |