mojiban 0.1.2

Mojiban (文字盤) — rich text rendering: markdown, syntax highlighting, styled spans
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
use crate::colors;
use crate::span::{RichLine, StyledSpan, TextStyle};

/// Rust language keywords.
const RUST_KEYWORDS: &[&str] = &[
    "fn", "let", "mut", "pub", "struct", "enum", "impl", "use", "mod", "if", "else", "match",
    "return", "for", "while", "loop", "break", "continue", "const", "static", "type", "trait",
    "where", "async", "await", "self", "super", "crate",
];

/// Nix language keywords.
const NIX_KEYWORDS: &[&str] = &[
    "let", "in", "rec", "with", "inherit", "if", "then", "else", "import", "builtins", "true",
    "false", "null",
];

/// Simple keyword-based syntax highlighter.
///
/// Applies word-boundary matching to color keywords, strings, comments,
/// and numbers. No tree-sitter or grammar files required.
pub struct SyntaxHighlighter;

impl SyntaxHighlighter {
    /// Create a new highlighter.
    #[must_use]
    pub fn new() -> Self {
        Self
    }

    /// Highlight a single line of source code.
    ///
    /// Applies simple keyword coloring for known languages. Unknown
    /// languages are returned as plain text.
    #[must_use]
    pub fn highlight_line(&self, line: &str, language: &str) -> RichLine {
        let (keywords, hash_comments): (&[&str], bool) = match language {
            "rust" | "rs" => (RUST_KEYWORDS, false),
            "nix" => (NIX_KEYWORDS, true),
            _ => return RichLine::from_spans(vec![StyledSpan::plain(line)]),
        };

        Tokenizer::new(line, keywords, hash_comments).tokenize()
    }
}

impl Default for SyntaxHighlighter {
    fn default() -> Self {
        Self::new()
    }
}

impl crate::TextProcessor for SyntaxHighlighter {
    /// Process each line of the input as plain text (no language-specific
    /// highlighting). Use [`SyntaxHighlighter::highlight_line`] directly
    /// for language-aware coloring.
    fn process(&self, input: &str) -> Vec<RichLine> {
        input.lines().map(|l| self.highlight_line(l, "")).collect()
    }
}

/// Internal tokenizer that walks a line character-by-character.
struct Tokenizer<'a> {
    chars: Vec<char>,
    keywords: &'a [&'a str],
    hash_comments: bool,
    spans: Vec<StyledSpan>,
    pos: usize,
    plain_start: usize,
}

impl<'a> Tokenizer<'a> {
    fn new(line: &str, keywords: &'a [&'a str], hash_comments: bool) -> Self {
        Self {
            chars: line.chars().collect(),
            keywords,
            hash_comments,
            spans: Vec::new(),
            pos: 0,
            plain_start: 0,
        }
    }

    fn len(&self) -> usize {
        self.chars.len()
    }

    #[must_use]
    fn tokenize(mut self) -> RichLine {
        while self.pos < self.len() {
            let ch = self.chars[self.pos];
            if self.try_line_comment(ch)
                || self.try_string(ch)
                || self.try_number(ch)
                || self.try_keyword(ch)
            {
                continue;
            }
            self.pos += 1;
        }
        self.flush_plain(self.len());
        if self.spans.is_empty() {
            self.spans.push(StyledSpan::default());
        }
        RichLine::from_spans(self.spans)
    }

    /// Flush accumulated plain text up to `end`.
    fn flush_plain(&mut self, end: usize) {
        if self.plain_start < end {
            let text: String = self.chars[self.plain_start..end].iter().collect();
            self.spans.push(StyledSpan::plain(text));
        }
    }

    /// Try to consume a line comment (`//` or `#`). Returns true if consumed.
    fn try_line_comment(&mut self, ch: char) -> bool {
        let is_double_slash = ch == '/' && self.pos + 1 < self.len() && self.chars[self.pos + 1] == '/';
        let is_hash = self.hash_comments && ch == '#';
        if !is_double_slash && !is_hash {
            return false;
        }
        self.flush_plain(self.pos);
        let comment: String = self.chars[self.pos..].iter().collect();
        self.spans
            .push(StyledSpan::new(comment, TextStyle::colored(colors::COMMENT)));
        self.pos = self.len();
        self.plain_start = self.len();
        true
    }

    /// Try to consume a string literal (`"..."` or `'...'`). Returns true if consumed.
    fn try_string(&mut self, ch: char) -> bool {
        if ch != '"' && ch != '\'' {
            return false;
        }
        self.flush_plain(self.pos);
        let start = self.pos;
        let quote = ch;
        self.pos += 1;
        while self.pos < self.len() {
            if self.chars[self.pos] == '\\' && self.pos + 1 < self.len() {
                self.pos += 2;
            } else if self.chars[self.pos] == quote {
                self.pos += 1;
                break;
            } else {
                self.pos += 1;
            }
        }
        let text: String = self.chars[start..self.pos].iter().collect();
        self.spans
            .push(StyledSpan::new(text, TextStyle::colored(colors::STRING)));
        self.plain_start = self.pos;
        true
    }

    /// Try to consume a number literal. Returns true if consumed.
    fn try_number(&mut self, ch: char) -> bool {
        if !ch.is_ascii_digit() || self.preceded_by_word_char() {
            return false;
        }
        self.flush_plain(self.pos);
        let start = self.pos;
        while self.pos < self.len()
            && (self.chars[self.pos].is_ascii_digit()
                || self.chars[self.pos] == '.'
                || self.chars[self.pos] == '_')
        {
            self.pos += 1;
        }
        let text: String = self.chars[start..self.pos].iter().collect();
        self.spans
            .push(StyledSpan::new(text, TextStyle::colored(colors::NUMBER)));
        self.plain_start = self.pos;
        true
    }

    /// Try to consume a keyword (word that matches the keyword list). Returns true if consumed.
    fn try_keyword(&mut self, ch: char) -> bool {
        if !is_word_start(ch) || self.preceded_by_word_char() {
            return false;
        }
        let word_start = self.pos;
        while self.pos < self.len() && is_word_continue(self.chars[self.pos]) {
            self.pos += 1;
        }
        let word: String = self.chars[word_start..self.pos].iter().collect();
        if self.keywords.contains(&word.as_str()) && !self.followed_by_word_char() {
            self.flush_plain(word_start);
            self.spans
                .push(StyledSpan::new(word, TextStyle::colored(colors::KEYWORD)));
            self.plain_start = self.pos;
        }
        // Word consumed either way (keyword or not)
        true
    }

    /// Whether the character immediately before `self.pos` is a word character.
    fn preceded_by_word_char(&self) -> bool {
        self.pos > 0 && is_word_continue(self.chars[self.pos - 1])
    }

    /// Whether the character at `self.pos` is a word-continuation character.
    fn followed_by_word_char(&self) -> bool {
        self.pos < self.len() && is_word_continue(self.chars[self.pos])
    }
}

/// Characters that start a word (for keyword detection).
fn is_word_start(ch: char) -> bool {
    ch.is_ascii_alphabetic() || ch == '_'
}

/// Characters that continue a word.
fn is_word_continue(ch: char) -> bool {
    ch.is_ascii_alphanumeric() || ch == '_'
}

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

    fn highlighter() -> SyntaxHighlighter {
        SyntaxHighlighter::new()
    }

    #[test]
    fn unknown_language_returns_plain() {
        let line = highlighter().highlight_line("some code here", "brainfuck");
        assert_eq!(line.plain_text(), "some code here");
        assert_eq!(line.len(), 1);
        assert_eq!(line.spans[0].style, TextStyle::default());
    }

    #[test]
    fn empty_input() {
        let line = highlighter().highlight_line("", "rust");
        assert_eq!(line.plain_text(), "");
    }

    #[test]
    fn rust_keyword_fn() {
        let line = highlighter().highlight_line("fn main() {", "rust");
        let kw = line.spans.iter().find(|s| s.text == "fn");
        assert!(kw.is_some(), "should find 'fn' keyword span");
        assert_eq!(kw.unwrap().style.color, colors::KEYWORD);
    }

    #[test]
    fn rust_keyword_let() {
        let line = highlighter().highlight_line("let x = 5;", "rust");
        let kw = line.spans.iter().find(|s| s.text == "let");
        assert!(kw.is_some(), "should find 'let' keyword span");
        assert_eq!(kw.unwrap().style.color, colors::KEYWORD);
    }

    #[test]
    fn rust_keyword_not_in_identifier() {
        // "letter" contains "let" but should NOT color it as a keyword
        let line = highlighter().highlight_line("letter", "rust");
        let kw = line.spans.iter().find(|s| s.text == "let");
        assert!(kw.is_none(), "'let' should not be extracted from 'letter'");
    }

    #[test]
    fn rust_string_colored() {
        let line = highlighter().highlight_line("let s = \"hello\";", "rust");
        let string_span = line.spans.iter().find(|s| s.text.contains("hello"));
        assert!(string_span.is_some(), "should find string span");
        assert_eq!(string_span.unwrap().style.color, colors::STRING);
    }

    #[test]
    fn rust_comment_colored() {
        let line = highlighter().highlight_line("// a comment", "rust");
        let comment_span = line.spans.iter().find(|s| s.text.contains("comment"));
        assert!(comment_span.is_some(), "should find comment span");
        assert_eq!(comment_span.unwrap().style.color, colors::COMMENT);
    }

    #[test]
    fn rust_number_colored() {
        let line = highlighter().highlight_line("let x = 42;", "rust");
        let num_span = line.spans.iter().find(|s| s.text == "42");
        assert!(num_span.is_some(), "should find number span");
        assert_eq!(num_span.unwrap().style.color, colors::NUMBER);
    }

    #[test]
    fn nix_keyword_let() {
        let line = highlighter().highlight_line("let x = 1;", "nix");
        let kw = line.spans.iter().find(|s| s.text == "let");
        assert!(kw.is_some(), "should find 'let' keyword in nix");
        assert_eq!(kw.unwrap().style.color, colors::KEYWORD);
    }

    #[test]
    fn nix_keyword_inherit() {
        let line = highlighter().highlight_line("inherit pkgs;", "nix");
        let kw = line.spans.iter().find(|s| s.text == "inherit");
        assert!(kw.is_some());
        assert_eq!(kw.unwrap().style.color, colors::KEYWORD);
    }

    #[test]
    fn nix_hash_comment() {
        let line = highlighter().highlight_line("# nix comment", "nix");
        let comment_span = line.spans.iter().find(|s| s.text.contains("nix comment"));
        assert!(comment_span.is_some());
        assert_eq!(comment_span.unwrap().style.color, colors::COMMENT);
    }

    #[test]
    fn rust_multiple_keywords() {
        let line = highlighter().highlight_line("pub fn foo() {}", "rust");
        let pub_kw = line.spans.iter().find(|s| s.text == "pub");
        let fn_kw = line.spans.iter().find(|s| s.text == "fn");
        assert!(pub_kw.is_some());
        assert!(fn_kw.is_some());
        assert_eq!(pub_kw.unwrap().style.color, colors::KEYWORD);
        assert_eq!(fn_kw.unwrap().style.color, colors::KEYWORD);
    }

    #[test]
    fn plain_text_reconstructed() {
        let line = highlighter().highlight_line("fn main() { let x = 42; }", "rust");
        assert_eq!(line.plain_text(), "fn main() { let x = 42; }");
    }

    #[test]
    fn default_trait() {
        let h = SyntaxHighlighter::default();
        let line = h.highlight_line("test", "rust");
        assert_eq!(line.plain_text(), "test");
    }

    #[test]
    fn rs_alias_works() {
        let line = highlighter().highlight_line("fn test()", "rs");
        let kw = line.spans.iter().find(|s| s.text == "fn");
        assert!(kw.is_some());
    }

    #[test]
    fn escaped_string() {
        let line = highlighter().highlight_line(r#"let s = "he\"llo";"#, "rust");
        assert_eq!(line.plain_text(), r#"let s = "he\"llo";"#);
        let string_span = line.spans.iter().find(|s| s.text.contains("he\\\"llo"));
        assert!(string_span.is_some());
        assert_eq!(string_span.unwrap().style.color, colors::STRING);
    }

    // ---- Number edge cases ----

    #[test]
    fn number_with_decimal_point() {
        let line = highlighter().highlight_line("let x = 3.14;", "rust");
        let num = line.spans.iter().find(|s| s.text.contains("3.14"));
        assert!(num.is_some(), "should find decimal number");
        assert_eq!(num.unwrap().style.color, colors::NUMBER);
    }

    #[test]
    fn number_with_underscores() {
        let line = highlighter().highlight_line("let x = 1_000_000;", "rust");
        let num = line.spans.iter().find(|s| s.text.contains("1_000_000"));
        assert!(num.is_some(), "should find number with underscores");
        assert_eq!(num.unwrap().style.color, colors::NUMBER);
    }

    #[test]
    fn number_not_in_identifier() {
        // "x42" should NOT extract "42" as a separate number
        let line = highlighter().highlight_line("x42", "rust");
        let num = line.spans.iter().find(|s| s.text == "42" && s.style.color == colors::NUMBER);
        assert!(num.is_none(), "42 inside identifier should not be a separate number span");
    }

    #[test]
    fn number_at_start_of_line() {
        let line = highlighter().highlight_line("42", "rust");
        let num = line.spans.iter().find(|s| s.text == "42");
        assert!(num.is_some());
        assert_eq!(num.unwrap().style.color, colors::NUMBER);
    }

    #[test]
    fn number_zero() {
        let line = highlighter().highlight_line("let x = 0;", "rust");
        let num = line.spans.iter().find(|s| s.text == "0");
        assert!(num.is_some());
        assert_eq!(num.unwrap().style.color, colors::NUMBER);
    }

    // ---- String edge cases ----

    #[test]
    fn single_quoted_string() {
        let line = highlighter().highlight_line("let c = 'a';", "rust");
        let string_span = line.spans.iter().find(|s| s.text.contains('a') && s.style.color == colors::STRING);
        assert!(string_span.is_some(), "single-quoted string should be highlighted");
    }

    #[test]
    fn empty_string() {
        let line = highlighter().highlight_line(r#"let s = "";"#, "rust");
        let string_span = line.spans.iter().find(|s| s.text == "\"\"");
        assert!(string_span.is_some(), "empty string literal should be highlighted");
        assert_eq!(string_span.unwrap().style.color, colors::STRING);
    }

    #[test]
    fn multiple_strings_on_line() {
        let line = highlighter().highlight_line(r#"let a = "hello"; let b = "world";"#, "rust");
        let string_spans: Vec<_> = line.spans.iter()
            .filter(|s| s.style.color == colors::STRING)
            .collect();
        assert_eq!(string_spans.len(), 2, "should find two string spans");
        assert!(string_spans[0].text.contains("hello"));
        assert!(string_spans[1].text.contains("world"));
    }

    #[test]
    fn string_with_escaped_backslash() {
        let line = highlighter().highlight_line(r#"let s = "path\\to";"#, "rust");
        let string_span = line.spans.iter().find(|s| s.style.color == colors::STRING);
        assert!(string_span.is_some());
        assert!(string_span.unwrap().text.contains("path\\\\to"));
    }

    // ---- Comment edge cases ----

    #[test]
    fn comment_consumes_rest_of_line() {
        let line = highlighter().highlight_line("let x = 1; // comment with code fn", "rust");
        let comment = line.spans.iter().find(|s| s.style.color == colors::COMMENT);
        assert!(comment.is_some());
        assert!(comment.unwrap().text.contains("comment with code fn"));
        // "fn" after // should NOT be a keyword
        let fn_kw = line.spans.iter().find(|s| s.text == "fn" && s.style.color == colors::KEYWORD);
        assert!(fn_kw.is_none(), "keyword in comment should not be highlighted separately");
    }

    #[test]
    fn comment_only_line() {
        let line = highlighter().highlight_line("// entire line is comment", "rust");
        assert_eq!(line.spans.len(), 1);
        assert_eq!(line.spans[0].style.color, colors::COMMENT);
        assert_eq!(line.spans[0].text, "// entire line is comment");
    }

    #[test]
    fn hash_comment_only_line_nix() {
        let line = highlighter().highlight_line("# nix comment line", "nix");
        assert_eq!(line.spans.len(), 1);
        assert_eq!(line.spans[0].style.color, colors::COMMENT);
    }

    // ---- Keyword boundary detection ----

    #[test]
    fn keyword_not_prefix_of_identifier() {
        // "letting" contains "let" as prefix
        let line = highlighter().highlight_line("letting", "rust");
        let kw = line.spans.iter().find(|s| s.text == "let" && s.style.color == colors::KEYWORD);
        assert!(kw.is_none(), "'let' should not be extracted from 'letting'");
    }

    #[test]
    fn keyword_not_suffix_of_identifier() {
        // "outlet" contains "let" as suffix
        let line = highlighter().highlight_line("outlet", "rust");
        let kw = line.spans.iter().find(|s| s.text == "let" && s.style.color == colors::KEYWORD);
        assert!(kw.is_none(), "'let' should not be extracted from 'outlet'");
    }

    #[test]
    fn keyword_at_end_of_line() {
        let line = highlighter().highlight_line("x = fn", "rust");
        let kw = line.spans.iter().find(|s| s.text == "fn");
        assert!(kw.is_some(), "'fn' at end of line should be detected");
        assert_eq!(kw.unwrap().style.color, colors::KEYWORD);
    }

    #[test]
    fn keyword_after_punctuation() {
        let line = highlighter().highlight_line("(let x)", "rust");
        let kw = line.spans.iter().find(|s| s.text == "let");
        assert!(kw.is_some(), "'let' after parenthesis should be detected");
        assert_eq!(kw.unwrap().style.color, colors::KEYWORD);
    }

    #[test]
    fn keyword_before_punctuation() {
        let line = highlighter().highlight_line("fn()", "rust");
        let kw = line.spans.iter().find(|s| s.text == "fn");
        assert!(kw.is_some(), "'fn' before parenthesis should be detected");
    }

    // ---- All Rust keywords recognized ----

    #[test]
    fn all_rust_keywords_recognized() {
        for &kw_str in RUST_KEYWORDS {
            let input = format!(" {kw_str} ");
            let line = highlighter().highlight_line(&input, "rust");
            let found = line.spans.iter().find(|s| s.text == kw_str && s.style.color == colors::KEYWORD);
            assert!(found.is_some(), "Rust keyword '{kw_str}' should be highlighted");
        }
    }

    // ---- All Nix keywords recognized ----

    #[test]
    fn all_nix_keywords_recognized() {
        for &kw_str in NIX_KEYWORDS {
            let input = format!(" {kw_str} ");
            let line = highlighter().highlight_line(&input, "nix");
            let found = line.spans.iter().find(|s| s.text == kw_str && s.style.color == colors::KEYWORD);
            assert!(found.is_some(), "Nix keyword '{kw_str}' should be highlighted");
        }
    }

    // ---- Mixed content preservation ----

    #[test]
    fn mixed_keywords_strings_numbers_comments() {
        let input = r#"pub fn add(a: 42, b: "hello") // sum"#;
        let line = highlighter().highlight_line(input, "rust");
        assert_eq!(line.plain_text(), input, "plain text must match original");

        let pub_kw = line.spans.iter().find(|s| s.text == "pub");
        assert!(pub_kw.is_some());
        assert_eq!(pub_kw.unwrap().style.color, colors::KEYWORD);

        let fn_kw = line.spans.iter().find(|s| s.text == "fn");
        assert!(fn_kw.is_some());
        assert_eq!(fn_kw.unwrap().style.color, colors::KEYWORD);

        let num = line.spans.iter().find(|s| s.text == "42");
        assert!(num.is_some());
        assert_eq!(num.unwrap().style.color, colors::NUMBER);

        let string_span = line.spans.iter().find(|s| s.text.contains("hello"));
        assert!(string_span.is_some());
        assert_eq!(string_span.unwrap().style.color, colors::STRING);

        let comment = line.spans.iter().find(|s| s.text.contains("sum"));
        assert!(comment.is_some());
        assert_eq!(comment.unwrap().style.color, colors::COMMENT);
    }

    // ---- Plain text reconstruction for complex lines ----

    #[test]
    fn plain_text_preserved_nix() {
        let input = "let x = import ./foo.nix; # import config";
        let line = highlighter().highlight_line(input, "nix");
        assert_eq!(line.plain_text(), input);
    }

    #[test]
    fn plain_text_preserved_with_strings_and_escapes() {
        let input = r#"let msg = "say \"hi\"";"#;
        let line = highlighter().highlight_line(input, "rust");
        assert_eq!(line.plain_text(), input);
    }

    // ---- Whitespace-only and special inputs ----

    #[test]
    fn whitespace_only_input() {
        let line = highlighter().highlight_line("   ", "rust");
        assert_eq!(line.plain_text(), "   ");
    }

    #[test]
    fn tab_characters_preserved() {
        let line = highlighter().highlight_line("\tlet x = 1;", "rust");
        assert_eq!(line.plain_text(), "\tlet x = 1;");
        let kw = line.spans.iter().find(|s| s.text == "let");
        assert!(kw.is_some());
    }

    #[test]
    fn unicode_identifiers_not_keywords() {
        // Unicode chars should not be treated as keywords
        let line = highlighter().highlight_line("\u{6587}\u{5B57}", "rust");
        assert_eq!(line.plain_text(), "\u{6587}\u{5B57}");
        // Should be plain text, no keyword coloring
        for span in &line.spans {
            assert_ne!(span.style.color, colors::KEYWORD);
        }
    }

    // ---- Nix-specific: keywords shared with Rust ----

    #[test]
    fn nix_let_in_combination() {
        let line = highlighter().highlight_line("let x = 1; in x", "nix");
        let let_kw = line.spans.iter().find(|s| s.text == "let");
        let in_kw = line.spans.iter().find(|s| s.text == "in");
        assert!(let_kw.is_some());
        assert!(in_kw.is_some());
        assert_eq!(let_kw.unwrap().style.color, colors::KEYWORD);
        assert_eq!(in_kw.unwrap().style.color, colors::KEYWORD);
    }

    #[test]
    fn nix_true_false_null() {
        for &kw in &["true", "false", "null"] {
            let input = format!(" {kw} ");
            let line = highlighter().highlight_line(&input, "nix");
            let found = line.spans.iter().find(|s| s.text == kw);
            assert!(found.is_some(), "Nix keyword '{kw}' should be found");
            assert_eq!(found.unwrap().style.color, colors::KEYWORD);
        }
    }

    // ---- Multiple comments/hashes ----

    #[test]
    fn double_slash_in_string_not_comment() {
        let line = highlighter().highlight_line(r#"let url = "http://example.com";"#, "rust");
        // The "//" inside the string should be part of the string, not a comment
        let comment_spans: Vec<_> = line.spans.iter()
            .filter(|s| s.style.color == colors::COMMENT)
            .collect();
        assert!(comment_spans.is_empty(), "// inside string should not create comment span");
    }

    // ---- Consecutive keywords ----

    #[test]
    fn consecutive_keywords_separated_by_space() {
        let line = highlighter().highlight_line("pub async fn", "rust");
        let keywords: Vec<_> = line.spans.iter()
            .filter(|s| s.style.color == colors::KEYWORD)
            .collect();
        assert_eq!(keywords.len(), 3);
        assert_eq!(keywords[0].text, "pub");
        assert_eq!(keywords[1].text, "async");
        assert_eq!(keywords[2].text, "fn");
    }

    // ---- Edge: keyword followed immediately by number ----

    #[test]
    fn keyword_followed_by_number_no_space() {
        // "let42" is not a keyword — it's an identifier
        let line = highlighter().highlight_line("let42", "rust");
        let kw = line.spans.iter().find(|s| s.text == "let" && s.style.color == colors::KEYWORD);
        assert!(kw.is_none(), "'let' should not be extracted from 'let42'");
    }

    // ---- Struct/enum/impl coverage ----

    #[test]
    fn rust_struct_enum_impl() {
        let line = highlighter().highlight_line("pub struct Foo { impl Bar for Foo {} enum Baz {}", "rust");
        for kw in &["struct", "impl", "enum"] {
            let found = line.spans.iter().find(|s| s.text == *kw);
            assert!(found.is_some(), "keyword '{kw}' should be found");
            assert_eq!(found.unwrap().style.color, colors::KEYWORD);
        }
    }

    // ---- Empty line returns a span ----

    #[test]
    fn empty_line_returns_single_empty_span() {
        let line = highlighter().highlight_line("", "rust");
        assert_eq!(line.len(), 1);
        assert!(line.spans[0].is_empty());
    }

    // ---- Rust attribute (regression: hash must not start comment in Rust) ----

    #[test]
    fn rust_attribute_not_treated_as_comment() {
        let line = highlighter().highlight_line("#[derive(Debug)]", "rust");
        let comment_spans: Vec<_> = line.spans.iter()
            .filter(|s| s.style.color == colors::COMMENT)
            .collect();
        assert!(comment_spans.is_empty(), "#[derive] should not be a comment in Rust");
        assert_eq!(line.plain_text(), "#[derive(Debug)]");
    }

    #[test]
    fn rust_hash_in_macro_not_comment() {
        let line = highlighter().highlight_line("#![allow(unused)]", "rust");
        let comment_spans: Vec<_> = line.spans.iter()
            .filter(|s| s.style.color == colors::COMMENT)
            .collect();
        assert!(comment_spans.is_empty());
    }

    #[test]
    fn nix_hash_still_is_comment() {
        let line = highlighter().highlight_line("# nix comment", "nix");
        assert_eq!(line.spans.len(), 1);
        assert_eq!(line.spans[0].style.color, colors::COMMENT);
    }

    // ---- Unterminated string ----

    #[test]
    fn unterminated_string_no_panic() {
        let line = highlighter().highlight_line(r#"let s = "unterminated"#, "rust");
        assert_eq!(line.plain_text(), r#"let s = "unterminated"#);
    }

    #[test]
    fn unterminated_single_quote_no_panic() {
        let line = highlighter().highlight_line("let c = 'x", "rust");
        assert_eq!(line.plain_text(), "let c = 'x");
    }

    // ---- Only punctuation ----

    #[test]
    fn punctuation_only() {
        let line = highlighter().highlight_line("(){};,.", "rust");
        assert_eq!(line.plain_text(), "(){};,.");
        for span in &line.spans {
            assert_eq!(span.style, TextStyle::default());
        }
    }

    // ---- Code with trailing whitespace ----

    #[test]
    fn trailing_whitespace_preserved() {
        let line = highlighter().highlight_line("fn main()   ", "rust");
        assert_eq!(line.plain_text(), "fn main()   ");
    }

    // ---- Leading whitespace before keyword ----

    #[test]
    fn leading_whitespace_before_keyword() {
        let line = highlighter().highlight_line("    fn main()", "rust");
        let kw = line.spans.iter().find(|s| s.text == "fn");
        assert!(kw.is_some());
        assert_eq!(kw.unwrap().style.color, colors::KEYWORD);
        assert_eq!(line.plain_text(), "    fn main()");
    }

    // ---- String containing keyword ----

    #[test]
    fn keyword_inside_string_not_highlighted_separately() {
        let line = highlighter().highlight_line(r#"let s = "fn let pub";"#, "rust");
        let fn_kw = line.spans.iter().find(|s| s.text == "fn" && s.style.color == colors::KEYWORD);
        assert!(fn_kw.is_none(), "'fn' inside string should not be a keyword");
    }

    // ---- Number followed by keyword ----

    #[test]
    fn number_then_keyword() {
        let line = highlighter().highlight_line("42 fn", "rust");
        let num = line.spans.iter().find(|s| s.text == "42");
        assert!(num.is_some());
        assert_eq!(num.unwrap().style.color, colors::NUMBER);
        let kw = line.spans.iter().find(|s| s.text == "fn");
        assert!(kw.is_some());
        assert_eq!(kw.unwrap().style.color, colors::KEYWORD);
    }

    // ---- Multiple lines independently ----

    #[test]
    fn highlight_line_is_stateless() {
        let h = highlighter();
        let l1 = h.highlight_line("fn foo()", "rust");
        let l2 = h.highlight_line("let x = 1;", "rust");
        assert!(l1.spans.iter().any(|s| s.text == "fn"));
        assert!(l2.spans.iter().any(|s| s.text == "let"));
    }

    // ---- Nix if-then-else ----

    #[test]
    fn nix_if_then_else() {
        let line = highlighter().highlight_line("if x then y else z", "nix");
        for kw in &["if", "then", "else"] {
            let found = line.spans.iter().find(|s| s.text == *kw);
            assert!(found.is_some(), "Nix keyword '{kw}' should be found");
            assert_eq!(found.unwrap().style.color, colors::KEYWORD);
        }
    }

    // ---- Nix import with path ----

    #[test]
    fn nix_import_path() {
        let line = highlighter().highlight_line("import ./default.nix", "nix");
        let kw = line.spans.iter().find(|s| s.text == "import");
        assert!(kw.is_some());
        assert_eq!(kw.unwrap().style.color, colors::KEYWORD);
        assert_eq!(line.plain_text(), "import ./default.nix");
    }

    // ---- Complex Rust line ----

    #[test]
    fn complex_rust_line() {
        let input = "pub async fn process(data: &str) -> Result<String, Error> {";
        let line = highlighter().highlight_line(input, "rust");
        assert_eq!(line.plain_text(), input);
        for kw in &["pub", "async", "fn"] {
            let found = line.spans.iter().find(|s| s.text == *kw);
            assert!(found.is_some(), "keyword '{kw}' should be found");
            assert_eq!(found.unwrap().style.color, colors::KEYWORD);
        }
    }

    // ---- Highlight the SyntaxHighlighter::default() returns usable instance ----

    #[test]
    fn default_instance_works_for_all_languages() {
        let h = SyntaxHighlighter::default();
        let _ = h.highlight_line("test", "rust");
        let _ = h.highlight_line("test", "rs");
        let _ = h.highlight_line("test", "nix");
        let _ = h.highlight_line("test", "unknown");
    }

    // ---- TextProcessor trait ----

    #[test]
    fn text_processor_trait_returns_plain_lines() {
        use crate::TextProcessor;
        let h = highlighter();
        let lines = h.process("line one\nline two");
        assert_eq!(lines.len(), 2);
        assert_eq!(lines[0].plain_text(), "line one");
        assert_eq!(lines[1].plain_text(), "line two");
    }
}