imradv3 / src /WpfEditor /SyntaxHighlighter.cs
fasdfsa's picture
System.Text.Rune 用 4 个字节来表示一个字符
ebc2688
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()
{
// C++ 关键字
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;
// 将Rune转换为字符串进行处理
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];
}
}
}
}
}
}