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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//! Syntax highlighter implementation
use super::language::Language;
use super::theme::SyntaxTheme;
use super::types::{Token, TokenType};
/// Syntax highlighter
pub struct SyntaxHighlighter {
theme: SyntaxTheme,
}
impl Default for SyntaxHighlighter {
fn default() -> Self {
Self::new()
}
}
impl SyntaxHighlighter {
/// Create a new syntax highlighter with default theme
pub fn new() -> Self {
Self {
theme: SyntaxTheme::default(),
}
}
/// Create with a specific theme
pub fn with_theme(theme: SyntaxTheme) -> Self {
Self { theme }
}
/// Get the theme
pub fn theme(&self) -> &SyntaxTheme {
&self.theme
}
/// Set the theme
pub fn set_theme(&mut self, theme: SyntaxTheme) {
self.theme = theme;
}
/// Highlight a line of code
pub fn highlight_line(&self, line: &str, lang: Language) -> Vec<Token> {
self.highlight_line_with_state(line, lang, false).0
}
/// Highlight a line of code with block comment state
/// Returns (tokens, in_block_comment_at_end)
pub fn highlight_line_with_state(
&self,
line: &str,
lang: Language,
in_block_comment: bool,
) -> (Vec<Token>, bool) {
let mut tokens = Vec::new();
let keywords = lang.keywords();
let types = lang.types();
let (line_comment, block_comment) = lang.comment_patterns();
let mut in_block = in_block_comment;
let chars: Vec<char> = line.chars().collect();
let mut i = 0;
// If we're continuing from a block comment
if in_block {
if let Some((_, end)) = block_comment {
// Look for end of block comment
if let Some(end_pos) = line.find(end) {
tokens.push(Token::new(&line[..end_pos + end.len()], TokenType::Comment));
i = end_pos + end.len();
in_block = false;
} else {
// Entire line is a comment
tokens.push(Token::new(line, TokenType::Comment));
return (tokens, true);
}
}
}
while i < chars.len() {
// Check for block comment start
if let Some((start, end)) = block_comment {
if line[i..].starts_with(start) {
let comment_start = i;
i += start.len();
// Look for end of block comment on same line
if let Some(rel_end) = line[i..].find(end) {
let comment_end = i + rel_end + end.len();
tokens.push(Token::new(
&line[comment_start..comment_end],
TokenType::Comment,
));
i = comment_end;
continue;
} else {
// Block comment continues to next line
tokens.push(Token::new(&line[comment_start..], TokenType::Comment));
return (tokens, true);
}
}
}
// Check for line comment
if !line_comment.is_empty() && line[i..].starts_with(line_comment) {
tokens.push(Token::new(&line[i..], TokenType::Comment));
break;
}
// Check for string (double quote)
if chars[i] == '"' {
let start = i;
i += 1;
while i < chars.len() {
if chars[i] == '\\' && i + 1 < chars.len() {
i += 2;
} else if chars[i] == '"' {
i += 1;
break;
} else {
i += 1;
}
}
let s: String = chars[start..i].iter().collect();
tokens.push(Token::new(s, TokenType::String));
continue;
}
// Check for string (single quote)
if chars[i] == '\'' && lang != Language::Rust {
let start = i;
i += 1;
while i < chars.len() {
if chars[i] == '\\' && i + 1 < chars.len() {
i += 2;
} else if chars[i] == '\'' {
i += 1;
break;
} else {
i += 1;
}
}
let s: String = chars[start..i].iter().collect();
tokens.push(Token::new(s, TokenType::String));
continue;
}
// Check for Rust char literal or lifetime
if chars[i] == '\'' && lang == Language::Rust {
let start = i;
i += 1;
// Lifetime or char
while i < chars.len() && (chars[i].is_alphanumeric() || chars[i] == '_') {
i += 1;
}
if i < chars.len() && chars[i] == '\'' {
i += 1; // char literal
let s: String = chars[start..i].iter().collect();
tokens.push(Token::new(s, TokenType::String));
} else {
// Lifetime
let s: String = chars[start..i].iter().collect();
tokens.push(Token::new(s, TokenType::Type));
}
continue;
}
// Check for number
if chars[i].is_ascii_digit()
|| (chars[i] == '.' && i + 1 < chars.len() && chars[i + 1].is_ascii_digit())
{
let start = i;
// Hex
if chars[i] == '0'
&& i + 1 < chars.len()
&& (chars[i + 1] == 'x' || chars[i + 1] == 'X')
{
i += 2;
while i < chars.len() && chars[i].is_ascii_hexdigit() {
i += 1;
}
} else {
// Decimal or float
while i < chars.len()
&& (chars[i].is_ascii_digit()
|| chars[i] == '.'
|| chars[i] == 'e'
|| chars[i] == 'E')
{
i += 1;
}
}
// Suffix (f32, u64, etc.)
while i < chars.len() && chars[i].is_alphabetic() {
i += 1;
}
let s: String = chars[start..i].iter().collect();
tokens.push(Token::new(s, TokenType::Number));
continue;
}
// Check for Rust macro
if lang == Language::Rust && chars[i].is_alphabetic() {
let start = i;
while i < chars.len() && (chars[i].is_alphanumeric() || chars[i] == '_') {
i += 1;
}
if i < chars.len() && chars[i] == '!' {
i += 1;
let s: String = chars[start..i].iter().collect();
tokens.push(Token::new(s, TokenType::Macro));
continue;
}
// Reset and handle as identifier
i = start;
}
// Check for identifier/keyword
if chars[i].is_alphabetic() || chars[i] == '_' {
let start = i;
while i < chars.len() && (chars[i].is_alphanumeric() || chars[i] == '_') {
i += 1;
}
let word: String = chars[start..i].iter().collect();
// Check if it's a keyword
if keywords.contains(&word.as_str()) {
tokens.push(Token::new(word, TokenType::Keyword));
} else if types.contains(&word.as_str()) {
tokens.push(Token::new(word, TokenType::Type));
} else if i < chars.len() && chars[i] == '(' {
// Function call
tokens.push(Token::new(word, TokenType::Function));
} else {
tokens.push(Token::new(word, TokenType::Normal));
}
continue;
}
// Check for Rust attribute
if lang == Language::Rust
&& chars[i] == '#'
&& i + 1 < chars.len()
&& chars[i + 1] == '['
{
let start = i;
let mut depth = 0;
while i < chars.len() {
if chars[i] == '[' {
depth += 1;
} else if chars[i] == ']' {
depth -= 1;
if depth == 0 {
i += 1;
break;
}
}
i += 1;
}
let s: String = chars[start..i].iter().collect();
tokens.push(Token::new(s, TokenType::Attribute));
continue;
}
// Check for Python/shell comment
if (lang == Language::Python
|| lang == Language::Shell
|| lang == Language::Ruby
|| lang == Language::Yaml
|| lang == Language::Toml)
&& chars[i] == '#'
{
tokens.push(Token::new(&line[i..], TokenType::Comment));
break;
}
// Python decorator
if lang == Language::Python && chars[i] == '@' {
let start = i;
i += 1;
while i < chars.len()
&& (chars[i].is_alphanumeric() || chars[i] == '_' || chars[i] == '.')
{
i += 1;
}
let s: String = chars[start..i].iter().collect();
tokens.push(Token::new(s, TokenType::Attribute));
continue;
}
// Operators and punctuation
if "+-*/%=<>!&|^~?:;,.()[]{}@".contains(chars[i]) {
tokens.push(Token::new(chars[i].to_string(), TokenType::Operator));
i += 1;
continue;
}
// Whitespace and other
tokens.push(Token::new(chars[i].to_string(), TokenType::Normal));
i += 1;
}
(tokens, in_block)
}
/// Highlight multiple lines of code with block comment tracking
pub fn highlight(&self, code: &str, lang: Language) -> Vec<Vec<Token>> {
let mut in_block_comment = false;
code.lines()
.map(|line| {
let (tokens, still_in_block) =
self.highlight_line_with_state(line, lang, in_block_comment);
in_block_comment = still_in_block;
tokens
})
.collect()
}
/// Get color for a token
pub fn token_color(&self, token_type: TokenType) -> crate::style::Color {
self.theme.color(token_type)
}
}