turbo-vision 2.4.0

A Rust implementation of the classic Borland Turbo Vision text-mode UI framework
Documentation
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// (C) 2025 - Enzo Lombardi

//! Syntax highlighting system - extensible token-based syntax coloring for editors.
// Syntax Highlighting System
//
// Provides extensible syntax highlighting for the EditorWindow component.
// Supports multiple programming languages with token-based coloring.
//
// Architecture:
// - SyntaxHighlighter trait - Define highlighting rules for a language
// - TokenType enum - Classification of syntax elements
// - Token struct - Represents a highlighted span (start, end, type)
// - Built-in highlighters for common languages

use crate::core::palette::Attr;

#[cfg(test)]
use crate::core::palette::TvColor;

/// Token types for syntax highlighting
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TokenType {
    /// Normal text (default)
    Normal,
    /// Keywords (if, for, while, etc.)
    Keyword,
    /// String literals ("text", 'c')
    String,
    /// Comments (// line, /* block */)
    Comment,
    /// Numeric literals (123, 0xFF, 3.14)
    Number,
    /// Operators (+, -, *, ==, etc.)
    Operator,
    /// Identifiers (variable names, function names)
    Identifier,
    /// Type names (struct, enum, class)
    Type,
    /// Preprocessor directives (#include, #define)
    Preprocessor,
    /// Function names
    Function,
    /// Special characters
    Special,
}

impl TokenType {
    /// Get the default color attribute for a token type
    pub fn default_color(&self) -> Attr {
        use crate::core::palette::colors::*;
        match self {
            TokenType::Normal => SYNTAX_NORMAL,
            TokenType::Keyword => SYNTAX_KEYWORD,
            TokenType::String => SYNTAX_STRING,
            TokenType::Comment => SYNTAX_COMMENT,
            TokenType::Number => SYNTAX_NUMBER,
            TokenType::Operator => SYNTAX_OPERATOR,
            TokenType::Identifier => SYNTAX_IDENTIFIER,
            TokenType::Type => SYNTAX_TYPE,
            TokenType::Preprocessor => SYNTAX_PREPROCESSOR,
            TokenType::Function => SYNTAX_FUNCTION,
            TokenType::Special => SYNTAX_SPECIAL,
        }
    }

    /// Get the palette index for this token type (editor-relative).
    /// Used by the editor's draw method to resolve colors through the palette chain.
    /// Maps through CP_EDITOR → window palette → app palette.
    pub fn palette_index(&self) -> u8 {
        use crate::core::palette::*;
        match self {
            TokenType::Normal => SYNTAX_NORMAL_IDX,
            TokenType::Keyword => SYNTAX_KEYWORD_IDX,
            TokenType::String => SYNTAX_STRING_IDX,
            TokenType::Comment => SYNTAX_COMMENT_IDX,
            TokenType::Number => SYNTAX_NUMBER_IDX,
            TokenType::Operator => SYNTAX_OPERATOR_IDX,
            TokenType::Identifier => SYNTAX_IDENTIFIER_IDX,
            TokenType::Type => SYNTAX_TYPE_IDX,
            TokenType::Preprocessor => SYNTAX_PREPROCESSOR_IDX,
            TokenType::Function => SYNTAX_FUNCTION_IDX,
            TokenType::Special => SYNTAX_SPECIAL_IDX,
        }
    }
}

/// Represents a highlighted token (span of text with a type)
#[derive(Debug, Clone)]
pub struct Token {
    /// Start column (character index)
    pub start: usize,
    /// End column (exclusive)
    pub end: usize,
    /// Token type
    pub token_type: TokenType,
}

impl Token {
    pub fn new(start: usize, end: usize, token_type: TokenType) -> Self {
        Token {
            start,
            end,
            token_type,
        }
    }
}

/// Trait for syntax highlighters
///
/// Implement this trait to add syntax highlighting for a language.
pub trait SyntaxHighlighter: Send + Sync {
    /// Get the language name
    fn language(&self) -> &str;

    /// Highlight a single line of text, returning tokens
    fn highlight_line(&self, line: &str, line_number: usize) -> Vec<Token>;

    /// Optional: Returns true if this is a multi-line context (e.g., inside a block comment)
    /// Default implementation returns false (no multi-line state)
    fn is_multiline_context(&self, _line_number: usize) -> bool {
        false
    }

    /// Optional: Update multi-line state after processing a line
    /// Default implementation does nothing
    fn update_multiline_state(&mut self, _line: &str, _line_number: usize) {}
}

/// Plain text highlighter (no highlighting)
pub struct PlainTextHighlighter;

impl SyntaxHighlighter for PlainTextHighlighter {
    fn language(&self) -> &str {
        "text"
    }

    fn highlight_line(&self, line: &str, _line_number: usize) -> Vec<Token> {
        if line.is_empty() {
            vec![]
        } else {
            vec![Token::new(0, line.len(), TokenType::Normal)]
        }
    }
}

/// Rust syntax highlighter
pub struct RustHighlighter {
    in_block_comment: bool,
}

impl RustHighlighter {
    pub fn new() -> Self {
        RustHighlighter {
            in_block_comment: false,
        }
    }

    fn is_rust_keyword(word: &str) -> bool {
        matches!(
            word,
            "as" | "async"
                | "await"
                | "break"
                | "const"
                | "continue"
                | "crate"
                | "dyn"
                | "else"
                | "enum"
                | "extern"
                | "false"
                | "fn"
                | "for"
                | "if"
                | "impl"
                | "in"
                | "let"
                | "loop"
                | "match"
                | "mod"
                | "move"
                | "mut"
                | "pub"
                | "ref"
                | "return"
                | "self"
                | "Self"
                | "static"
                | "struct"
                | "super"
                | "trait"
                | "true"
                | "type"
                | "unsafe"
                | "use"
                | "where"
                | "while"
        )
    }

    fn is_rust_type(word: &str) -> bool {
        // Common Rust types
        matches!(
            word,
            "i8" | "i16"
                | "i32"
                | "i64"
                | "i128"
                | "isize"
                | "u8"
                | "u16"
                | "u32"
                | "u64"
                | "u128"
                | "usize"
                | "f32"
                | "f64"
                | "bool"
                | "char"
                | "str"
                | "String"
                | "Vec"
                | "Option"
                | "Result"
                | "Box"
                | "Rc"
                | "Arc"
        ) || word.chars().next().map_or(false, |c| c.is_uppercase())
    }
}

impl SyntaxHighlighter for RustHighlighter {
    fn language(&self) -> &str {
        "rust"
    }

    fn highlight_line(&self, line: &str, _line_number: usize) -> Vec<Token> {
        let mut tokens = Vec::new();
        let chars: Vec<char> = line.chars().collect();
        let mut i = 0;

        // Check for block comment continuation
        if self.in_block_comment {
            // Find end of block comment
            if let Some(end_pos) = line.find("*/") {
                tokens.push(Token::new(0, end_pos + 2, TokenType::Comment));
                i = end_pos + 2;
            } else {
                // Entire line is comment
                tokens.push(Token::new(0, line.len(), TokenType::Comment));
                return tokens;
            }
        }

        while i < chars.len() {
            let ch = chars[i];

            // Line comment
            if i + 1 < chars.len() && ch == '/' && chars[i + 1] == '/' {
                tokens.push(Token::new(i, chars.len(), TokenType::Comment));
                break;
            }

            // Block comment start
            if i + 1 < chars.len() && ch == '/' && chars[i + 1] == '*' {
                let start = i;
                i += 2;
                // Find end of block comment
                let mut found_end = false;
                while i + 1 < chars.len() {
                    if chars[i] == '*' && chars[i + 1] == '/' {
                        i += 2;
                        found_end = true;
                        break;
                    }
                    i += 1;
                }
                if !found_end {
                    i = chars.len();
                }
                tokens.push(Token::new(start, i, TokenType::Comment));
                continue;
            }

            // String literal
            if ch == '"' {
                let start = i;
                i += 1;
                while i < chars.len() {
                    if chars[i] == '\\' && i + 1 < chars.len() {
                        i += 2; // Skip escaped character
                    } else if chars[i] == '"' {
                        i += 1;
                        break;
                    } else {
                        i += 1;
                    }
                }
                tokens.push(Token::new(start, i, TokenType::String));
                continue;
            }

            // Character literal
            if ch == '\'' {
                let start = i;
                i += 1;
                while i < chars.len() {
                    if chars[i] == '\\' && i + 1 < chars.len() {
                        i += 2; // Skip escaped character
                    } else if chars[i] == '\'' {
                        i += 1;
                        break;
                    } else {
                        i += 1;
                    }
                }
                tokens.push(Token::new(start, i, TokenType::String));
                continue;
            }

            // Number literal
            if ch.is_ascii_digit() {
                let start = i;
                while i < chars.len()
                    && (chars[i].is_ascii_alphanumeric() || chars[i] == '_' || chars[i] == '.')
                {
                    i += 1;
                }
                tokens.push(Token::new(start, i, TokenType::Number));
                continue;
            }

            // Operator
            if matches!(
                ch,
                '+' | '-' | '*' | '/' | '%' | '=' | '<' | '>' | '!' | '&' | '|' | '^' | '~'
            ) {
                let start = i;
                i += 1;
                // Handle multi-character operators
                while i < chars.len() && matches!(chars[i], '=' | '&' | '|' | '<' | '>') {
                    i += 1;
                }
                tokens.push(Token::new(start, i, TokenType::Operator));
                continue;
            }

            // Identifier or keyword
            if ch.is_alphabetic() || ch == '_' {
                let start = i;
                while i < chars.len() && (chars[i].is_alphanumeric() || chars[i] == '_') {
                    i += 1;
                }
                let word: String = chars[start..i].iter().collect();

                let token_type = if Self::is_rust_keyword(&word) {
                    TokenType::Keyword
                } else if Self::is_rust_type(&word) {
                    TokenType::Type
                } else {
                    TokenType::Identifier
                };

                tokens.push(Token::new(start, i, token_type));
                continue;
            }

            // Special characters (braces, parentheses, brackets, semicolons, etc.)
            // Create tokens for these so they're visible with proper color
            if matches!(
                ch,
                '{' | '}' | '(' | ')' | '[' | ']' | ';' | ',' | ':' | '.' | '?' | '@' | '#'
            ) {
                tokens.push(Token::new(i, i + 1, TokenType::Special));
                i += 1;
                continue;
            }

            // Skip whitespace (don't create tokens for it)
            i += 1;
        }

        tokens
    }

    fn is_multiline_context(&self, _line_number: usize) -> bool {
        self.in_block_comment
    }

    fn update_multiline_state(&mut self, line: &str, _line_number: usize) {
        // Check if we enter or exit a block comment
        let mut chars = line.chars().peekable();
        while let Some(ch) = chars.next() {
            if ch == '/' && chars.peek() == Some(&'*') {
                self.in_block_comment = true;
                chars.next();
            } else if ch == '*' && chars.peek() == Some(&'/') {
                self.in_block_comment = false;
                chars.next();
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_token_type_default_colors() {
        // Verify each token type has a color
        assert_ne!(
            TokenType::Keyword.default_color(),
            Attr::new(TvColor::Black, TvColor::Black)
        );
        assert_ne!(
            TokenType::String.default_color(),
            Attr::new(TvColor::Black, TvColor::Black)
        );
    }

    #[test]
    fn test_token_type_palette_index() {
        use crate::core::palette::*;
        assert_eq!(TokenType::Normal.palette_index(), SYNTAX_NORMAL_IDX);
        assert_eq!(TokenType::Keyword.palette_index(), SYNTAX_KEYWORD_IDX);
        assert_eq!(TokenType::String.palette_index(), SYNTAX_STRING_IDX);
        assert_eq!(TokenType::Comment.palette_index(), SYNTAX_COMMENT_IDX);
        assert_eq!(TokenType::Number.palette_index(), SYNTAX_NUMBER_IDX);
        assert_eq!(TokenType::Operator.palette_index(), SYNTAX_OPERATOR_IDX);
        assert_eq!(TokenType::Identifier.palette_index(), SYNTAX_IDENTIFIER_IDX);
        assert_eq!(TokenType::Type.palette_index(), SYNTAX_TYPE_IDX);
        assert_eq!(
            TokenType::Preprocessor.palette_index(),
            SYNTAX_PREPROCESSOR_IDX
        );
        assert_eq!(TokenType::Function.palette_index(), SYNTAX_FUNCTION_IDX);
        assert_eq!(TokenType::Special.palette_index(), SYNTAX_SPECIAL_IDX);
    }

    #[test]
    fn test_plain_text_highlighter() {
        let highlighter = PlainTextHighlighter;
        let tokens = highlighter.highlight_line("Hello, world!", 0);
        assert_eq!(tokens.len(), 1);
        assert_eq!(tokens[0].token_type, TokenType::Normal);
    }

    #[test]
    fn test_rust_highlighter_keywords() {
        let highlighter = RustHighlighter::new();
        let tokens = highlighter.highlight_line("fn main() {", 0);

        // Should have tokens for "fn", "main", and possibly others
        assert!(!tokens.is_empty());

        // Find "fn" token
        let fn_token = tokens.iter().find(|t| t.token_type == TokenType::Keyword);
        assert!(fn_token.is_some(), "Should find 'fn' keyword");
    }

    #[test]
    fn test_rust_highlighter_strings() {
        let highlighter = RustHighlighter::new();
        let tokens = highlighter.highlight_line(r#"let s = "hello";"#, 0);

        // Find string token
        let string_token = tokens.iter().find(|t| t.token_type == TokenType::String);
        assert!(string_token.is_some(), "Should find string literal");
    }

    #[test]
    fn test_rust_highlighter_comments() {
        let highlighter = RustHighlighter::new();
        let tokens = highlighter.highlight_line("// This is a comment", 0);

        assert_eq!(tokens.len(), 1);
        assert_eq!(tokens[0].token_type, TokenType::Comment);
    }

    #[test]
    fn test_rust_highlighter_numbers() {
        let highlighter = RustHighlighter::new();
        let tokens = highlighter.highlight_line("let x = 42;", 0);

        // Find number token
        let number_token = tokens.iter().find(|t| t.token_type == TokenType::Number);
        assert!(number_token.is_some(), "Should find number literal");
    }

    #[test]
    fn test_rust_highlighter_types() {
        let highlighter = RustHighlighter::new();
        let tokens = highlighter.highlight_line("let x: i32 = 0;", 0);

        // Find type token
        let type_token = tokens.iter().find(|t| t.token_type == TokenType::Type);
        assert!(type_token.is_some(), "Should find type name");
    }
}