File size: 9,581 Bytes
ae59d0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebc2688
ae59d0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebc2688
 
 
ae59d0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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];
                    }
                }
            }
        }
    }
}