hikari-core 0.1.10

hikari (光) — the pluggable syntax-highlighting spine: a coverage-complete span model, a line-stateful lexer trait, an object-safe highlighter, and a batteries-included language registry (Rust/Python/Lisp/JSON/TOML/Markdown).
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
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
//! hikari (光) — the pluggable syntax-highlighting spine.
//!
//! The fleet-shared foundation for syntax highlighting across every pleme-io
//! editor (AsterIDE · escriba) and tool. It owns the narrow interface every
//! language backend lowers to; heavy backends (tree-sitter grammars, the
//! tatara-lisp macro-generated `(deflexer …)` output) ship as separate
//! `hikari-*` crates in this workspace so `hikari-core` stays zero-dependency
//! and any consumer vendors it with no transitive weight.
//!
//! The design (a stable trait spine so backends are additive, never rewrites):
//!
//!   * ONE narrow-waist output — a coverage-complete, non-overlapping,
//!     forward-only `Vec<HighlightSpan>` partition produced ONLY through
//!     [`SpanSink`], which makes gaps / overlaps / reversals structurally
//!     unrepresentable (the caller cannot fabricate the `Vec`).
//!   * ONE authored backend trait [`LanguageLexer`] whose associated
//!     `LineState: Copy + Eq` makes incremental line-restart re-lex a *type
//!     property* (a consumer may cache per-line and re-lex only until the
//!     entry state stops changing).
//!   * ONE object-safe [`Highlighter`] the render layer holds as `Box<dyn>`,
//!     bridged from any `LanguageLexer` by the blanket [`LineDriven`] adapter.
//!   * ONE [`Ecosystem`] registry with total, panic-free resolution — an
//!     unknown extension resolves to [`PLAIN_TEXT`], never a panic, never
//!     "everything is one language".
//!   * palette-independent [`HlClass`] + a [`Theme`] mapping class → [`Rgb`]
//!     so classification and color never entangle (Nord default).

#![forbid(unsafe_code)]
// The workspace sets `clippy::pedantic = warn`. These arms are intentional in
// a hand-rolled byte scanner + a fixed palette, so they are allowed with a
// reason rather than contorted: single-char cursor vars (`i`/`c`/`n`/`s`/`e`)
// are the idiom for a lexer; byte offsets provably fit `u32` (documents cap at
// 4 GiB); proper nouns (AsterIDE, pleme-io) aren't code; the resume-match and
// same-color palette arms read clearer as written.
#![allow(
    clippy::many_single_char_names,
    clippy::cast_possible_truncation,
    clippy::doc_markdown,
    clippy::single_match_else,
    clippy::collapsible_if,
    clippy::match_same_arms
)]

// ───────────────────────────── span ─────────────────────────────

/// A byte-offset span into the document. `start <= end` by construction, and
/// (when produced through [`SpanSink`]) both ends land on UTF-8 char
/// boundaries.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct ByteSpan {
    pub start: u32,
    pub end: u32,
}

impl ByteSpan {
    /// The sole constructor.
    ///
    /// # Panics
    /// Panics only on a caller bug (`start > end`). The lexer driver never
    /// constructs a reversed span, so this is unreachable in normal use.
    #[must_use]
    pub fn new(start: u32, end: u32) -> Self {
        assert!(start <= end, "ByteSpan::new: start {start} > end {end}");
        Self { start, end }
    }

    #[must_use]
    pub fn len(&self) -> u32 {
        self.end - self.start
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.start == self.end
    }

    /// The span as a `usize` range, for slicing the source text.
    #[must_use]
    pub fn range(&self) -> std::ops::Range<usize> {
        self.start as usize..self.end as usize
    }
}

// ───────────────────────────── class ────────────────────────────

/// A palette-independent semantic highlight class. A superset of the classes
/// egui / tree-sitter / the fleet's `Semantic` enums produce, so every backend
/// lowers to this one waist and the theme layer maps it to color exactly once.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum HlClass {
    Comment {
        multiline: bool,
    },
    Keyword,
    /// A `:keyword`-style argument / symbol (lisp `:kw`, etc.).
    KeywordArg,
    Type,
    Function,
    Namespace,
    Variable,
    Constant,
    Str,
    Escape,
    Numeric {
        float: bool,
    },
    Boolean,
    Punctuation,
    Operator,
    Attribute,
    Special,
    Hyperlink,
    Whitespace,
    Error,
    /// A diagnostic/severity class (LSP + the fleet `Semantic` collapse).
    Warning,
    Info,
    Hint,
    /// A diff class (git signs, review UIs).
    Added,
    Removed,
    /// Diff context / normal-fg text in a diff view.
    Unchanged,
    /// The default / unclassified class. Coverage gaps are filled with this.
    Plain,
}

/// One classified region of the document.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct HighlightSpan {
    pub span: ByteSpan,
    pub class: HlClass,
}

// ───────────────────────────── sink ─────────────────────────────

/// The ONLY way to produce highlight spans. A forward-only cursor over a
/// single line: [`push`](SpanSink::push) fills any gap before the pushed
/// region with [`HlClass::Plain`] and clamps backwards writes, so the emitted
/// sequence is a coverage-complete, non-overlapping, monotonically-increasing
/// partition **by construction** — a lexer cannot emit a gap, an overlap, or a
/// reversal. The backing `Vec` is private; only the driver calls
/// [`finish`](SpanSink::finish).
pub struct SpanSink {
    cursor: u32,
    line_end: u32,
    out: Vec<HighlightSpan>,
}

impl SpanSink {
    /// A sink covering `[line_start, line_start + line_len)`. Public so a
    /// direct [`Highlighter`] impl (e.g. a tree-sitter backend) can construct
    /// one and keep coverage-by-construction, not only the [`LineDriven`]
    /// bridge.
    #[must_use]
    pub fn new(line_start: u32, line_len: u32) -> Self {
        Self {
            cursor: line_start,
            line_end: line_start + line_len,
            out: Vec::new(),
        }
    }

    /// A sink covering a whole document `[0, len)` — for a non-line backend
    /// that pushes absolute offsets and wants gap-fill for free.
    #[must_use]
    pub fn for_document(len: u32) -> Self {
        Self::new(0, len)
    }

    /// Classify `[start, end)` (absolute byte offsets) as `class`. A gap
    /// `[cursor, start)` is filled with [`HlClass::Plain`]; a backwards
    /// `start` is clamped to the cursor; an empty region is dropped.
    pub fn push(&mut self, start: u32, end: u32, class: HlClass) {
        let start = start.max(self.cursor);
        let end = end.min(self.line_end);
        if end <= start {
            return;
        }
        if start > self.cursor {
            self.out.push(HighlightSpan {
                span: ByteSpan::new(self.cursor, start),
                class: HlClass::Plain,
            });
        }
        self.out.push(HighlightSpan {
            span: ByteSpan::new(start, end),
            class,
        });
        self.cursor = end;
    }

    /// Finish: fill any trailing gap with [`HlClass::Plain`] and return the
    /// coverage-complete, non-overlapping, forward-only partition.
    #[must_use]
    pub fn finish(mut self) -> Vec<HighlightSpan> {
        if self.cursor < self.line_end {
            self.out.push(HighlightSpan {
                span: ByteSpan::new(self.cursor, self.line_end),
                class: HlClass::Plain,
            });
        }
        self.out
    }
}

// ─────────────────────── backend + render traits ────────────────

/// The AUTHORED backend trait — implement this to add a language. One total
/// method lexes a single line given the cross-line state carried out of the
/// previous line; returning the state at the line's end. `LineState: Eq` is
/// what makes incremental re-lex a type property (re-lex stops at the first
/// line whose entry state is unchanged).
pub trait LanguageLexer: Send + Sync {
    type LineState: Copy + Eq + Default + Send + Sync;

    fn lex_line(
        &self,
        line: &str,
        line_start: u32,
        entry: Self::LineState,
        sink: &mut SpanSink,
    ) -> Self::LineState;
}

/// The object-safe, render-facing trait the editor holds as `Box<dyn>`. The
/// default [`LineDriven`] bridge re-lexes the whole document; a consumer may
/// implement `Highlighter` directly for an incremental line cache.
pub trait Highlighter: Send + Sync {
    fn highlight(&self, text: &str) -> Vec<HighlightSpan>;
}

/// The blanket bridge: any [`LanguageLexer`] is a [`Highlighter`]. No backend
/// author ever hand-writes `Highlighter`.
pub struct LineDriven<L: LanguageLexer> {
    pub lexer: L,
}

impl<L: LanguageLexer> LineDriven<L> {
    #[must_use]
    pub fn new(lexer: L) -> Self {
        Self { lexer }
    }
}

impl<L: LanguageLexer> Highlighter for LineDriven<L> {
    fn highlight(&self, text: &str) -> Vec<HighlightSpan> {
        let mut out = Vec::new();
        let mut state = L::LineState::default();
        let mut offset: u32 = 0;
        // split_inclusive keeps the trailing '\n' on each line, so offsets are
        // contiguous and the partition covers every byte of `text`.
        for line in text.split_inclusive('\n') {
            let line_len = u32::try_from(line.len()).unwrap_or(u32::MAX);
            let mut sink = SpanSink::new(offset, line_len);
            state = self.lexer.lex_line(line, offset, state, &mut sink);
            out.extend(sink.finish());
            offset = offset.saturating_add(line_len);
        }
        out
    }
}

// ───────────────────── incremental line cache ───────────────────

/// The object-safe incremental face the render layer holds as `Box<dyn>`. A
/// stateful highlighter that reuses prior work: on each call it re-lexes only
/// from the first content-changed line until the carried entry state
/// re-converges (the `LineState` fixpoint), reusing every cached line before
/// and after. Its output is **byte-identical** to the equivalent one-shot
/// [`Highlighter::highlight`] — incrementality is an optimization, never a
/// semantic change (the differential-equivalence invariant, fuzz-tested).
pub trait IncrementalHighlighter: Send + Sync {
    /// Re-highlight `text`, reusing cached per-line spans where the document
    /// is unchanged. `&mut self` because the cache advances.
    fn highlight(&mut self, text: &str) -> Vec<HighlightSpan>;

    /// How many lines the most recent [`highlight`](Self::highlight) call
    /// actually re-lexed — `0` on a fully-cached (idle re-render) call. The
    /// seal's idle-work witness: an unchanged document re-lexes nothing.
    fn last_relexed(&self) -> usize;
}

/// One cached line: the `LineState` carried *into* it, the state carried *out*
/// of it, the exact line bytes (incl. trailing `\n`) for the content compare,
/// and the line's spans stored **line-relative** (0-based) so a length change
/// above never invalidates them — they are re-based to absolute on emit.
struct CachedLine<S> {
    entry: S,
    exit: S,
    text: Box<str>,
    rel_spans: Vec<HighlightSpan>,
}

/// The incremental re-lex cache over any [`LanguageLexer`]. The `LineState`
/// fixpoint made operational: re-lexing halts at the first line whose entry
/// state *and* bytes both match the cache, so an edit costs
/// `O(changed lines + lines until the carried state re-converges)`, not
/// `O(document)`.
///
/// Zero-dependency by design (hikari-core's invariant): the content compare is
/// std `str` equality and the cache stores the line bytes. A `hikari-*` sibling
/// may swap the `Box<str>` for a BLAKE3 row hash to drop the O(document) memory
/// — that is a strictly-internal change behind this same object-safe trait.
pub struct LineCache<L: LanguageLexer> {
    lexer: L,
    lines: Vec<CachedLine<L::LineState>>,
    last_relexed: usize,
}

impl<L: LanguageLexer> LineCache<L> {
    #[must_use]
    pub fn new(lexer: L) -> Self {
        Self {
            lexer,
            lines: Vec::new(),
            last_relexed: 0,
        }
    }

    /// Lex one line at base offset 0 (line-relative spans) carrying `entry`.
    fn lex_relative(&self, line: &str, entry: L::LineState) -> (L::LineState, Vec<HighlightSpan>) {
        let line_len = u32::try_from(line.len()).unwrap_or(u32::MAX);
        let mut sink = SpanSink::new(0, line_len);
        let exit = self.lexer.lex_line(line, 0, entry, &mut sink);
        (exit, sink.finish())
    }
}

impl<L: LanguageLexer> IncrementalHighlighter for LineCache<L> {
    fn highlight(&mut self, text: &str) -> Vec<HighlightSpan> {
        let mut next: Vec<CachedLine<L::LineState>> = Vec::new();
        let mut out: Vec<HighlightSpan> = Vec::new();
        let mut entry = L::LineState::default();
        let mut offset: u32 = 0;
        let mut relexed = 0usize;

        for (i, line) in text.split_inclusive('\n').enumerate() {
            let line_len = u32::try_from(line.len()).unwrap_or(u32::MAX);
            // Reuse iff the same-index cached line carried the same entry state
            // AND holds the same bytes. Both must hold: same bytes with a
            // different entry state (e.g. a block comment opened above) lexes
            // differently, and the fixpoint is exactly "entry state matches".
            let (exit, rel_spans) = match self.lines.get(i) {
                Some(c) if c.entry == entry && &*c.text == line => (c.exit, c.rel_spans.clone()),
                _ => {
                    relexed += 1;
                    self.lex_relative(line, entry)
                }
            };

            // Re-base the line-relative spans to absolute document offsets.
            for s in &rel_spans {
                out.push(HighlightSpan {
                    span: ByteSpan::new(offset + s.span.start, offset + s.span.end),
                    class: s.class,
                });
            }
            next.push(CachedLine {
                entry,
                exit,
                text: line.into(),
                rel_spans,
            });
            entry = exit;
            offset = offset.saturating_add(line_len);
        }

        self.lines = next;
        self.last_relexed = relexed;
        out
    }

    fn last_relexed(&self) -> usize {
        self.last_relexed
    }
}

/// The total fallback: any [`Highlighter`] as an [`IncrementalHighlighter`] by
/// re-lexing the whole document every call. Correct but not incremental — the
/// default a backend gets when it does not carry a `LineState` cache (e.g. a
/// tree-sitter backend that reparses via its own `Tree::edit`).
pub struct WholeReHighlighter {
    inner: Box<dyn Highlighter>,
    last_relexed: usize,
}

impl WholeReHighlighter {
    #[must_use]
    pub fn new(inner: Box<dyn Highlighter>) -> Self {
        Self {
            inner,
            last_relexed: 0,
        }
    }
}

impl IncrementalHighlighter for WholeReHighlighter {
    fn highlight(&mut self, text: &str) -> Vec<HighlightSpan> {
        self.last_relexed = text.split_inclusive('\n').count();
        self.inner.highlight(text)
    }
    fn last_relexed(&self) -> usize {
        self.last_relexed
    }
}

// ───────────────────────── plugin + registry ────────────────────

/// A language identity — an interned static name (closed by linkage, no open
/// enum to keep exhaustive).
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct Language(pub &'static str);

/// The unclassified fallback language — its highlighter emits everything as
/// [`HlClass::Plain`].
pub const PLAIN_TEXT: Language = Language("plaintext");

/// How a plugin claims a document.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Selector {
    /// A file extension without the dot, lowercase (e.g. `"rs"`).
    Extension(&'static str),
    /// An exact file name (e.g. `"Cargo.toml"`).
    Filename(&'static str),
}

/// A pluggable language backend: identity + how it's selected + its factory.
pub trait LanguagePlugin: Send + Sync {
    fn language(&self) -> Language;
    fn selectors(&self) -> &'static [Selector];
    fn make_highlighter(&self) -> Box<dyn Highlighter>;

    /// The incremental (line-cached) highlighter for this language. The default
    /// wraps [`make_highlighter`](Self::make_highlighter) in a
    /// [`WholeReHighlighter`] (correct, re-lexes the whole document); a
    /// `LineState`-carrying backend overrides this to return a real
    /// [`LineCache`] and gains the fixpoint re-lex for free.
    fn make_incremental(&self) -> Box<dyn IncrementalHighlighter> {
        Box::new(WholeReHighlighter::new(self.make_highlighter()))
    }
}

/// The registry. Total resolution: an unmatched path resolves to
/// [`PLAIN_TEXT`], never a panic.
pub struct Ecosystem {
    plugins: Vec<Box<dyn LanguagePlugin>>,
}

impl Default for Ecosystem {
    fn default() -> Self {
        Self::with_builtins()
    }
}

impl Ecosystem {
    /// An empty registry.
    #[must_use]
    pub fn new() -> Self {
        Self {
            plugins: Vec::new(),
        }
    }

    /// The batteries-included default: every built-in language plugin.
    #[must_use]
    pub fn with_builtins() -> Self {
        let mut eco = Self::new();
        for p in langs::builtins() {
            eco.plugins.push(p);
        }
        eco
    }

    pub fn register(&mut self, plugin: Box<dyn LanguagePlugin>) {
        self.plugins.push(plugin);
    }

    /// All languages the registry can highlight.
    #[must_use]
    pub fn languages(&self) -> Vec<Language> {
        self.plugins.iter().map(|p| p.language()).collect()
    }

    /// Resolve a file path to a language. Filename match wins over extension;
    /// no match resolves to [`PLAIN_TEXT`].
    #[must_use]
    pub fn resolve(&self, path: &str) -> Language {
        let name = path.rsplit(['/', '\\']).next().unwrap_or(path);
        for p in &self.plugins {
            for sel in p.selectors() {
                if let Selector::Filename(f) = sel {
                    if name.eq_ignore_ascii_case(f) {
                        return p.language();
                    }
                }
            }
        }
        if let Some(ext) = name.rsplit_once('.').map(|(_, e)| e) {
            for p in &self.plugins {
                for sel in p.selectors() {
                    if let Selector::Extension(e) = sel {
                        if ext.eq_ignore_ascii_case(e) {
                            return p.language();
                        }
                    }
                }
            }
        }
        PLAIN_TEXT
    }

    /// A highlighter for a language, or the plain-text highlighter if none is
    /// registered for it.
    #[must_use]
    pub fn highlighter_for(&self, lang: Language) -> Box<dyn Highlighter> {
        for p in &self.plugins {
            if p.language() == lang {
                return p.make_highlighter();
            }
        }
        Box::new(PlainHighlighter)
    }

    /// The one call an editor needs: path → highlighter.
    #[must_use]
    pub fn highlighter_for_path(&self, path: &str) -> Box<dyn Highlighter> {
        self.highlighter_for(self.resolve(path))
    }

    /// An **incremental** highlighter for a language, or the plain-text
    /// fallback. This is the call an editor's render loop holds across frames
    /// — it re-lexes only what changed.
    #[must_use]
    pub fn incremental_highlighter_for(&self, lang: Language) -> Box<dyn IncrementalHighlighter> {
        for p in &self.plugins {
            if p.language() == lang {
                return p.make_incremental();
            }
        }
        Box::new(WholeReHighlighter::new(Box::new(PlainHighlighter)))
    }

    /// Path → incremental highlighter. The render-loop entry point.
    #[must_use]
    pub fn incremental_highlighter_for_path(&self, path: &str) -> Box<dyn IncrementalHighlighter> {
        self.incremental_highlighter_for(self.resolve(path))
    }
}

/// The plain-text highlighter: one `Plain` span over the whole text.
pub struct PlainHighlighter;

impl Highlighter for PlainHighlighter {
    fn highlight(&self, text: &str) -> Vec<HighlightSpan> {
        if text.is_empty() {
            return Vec::new();
        }
        vec![HighlightSpan {
            span: ByteSpan::new(0, u32::try_from(text.len()).unwrap_or(u32::MAX)),
            class: HlClass::Plain,
        }]
    }
}

// ───────────────────────────── theme ────────────────────────────

/// An 8-bit sRGB color. Palette-independent; the consumer maps it to its own
/// color type (egui `Color32`, ratatui `Color`, …).
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct Rgb {
    pub r: u8,
    pub g: u8,
    pub b: u8,
}

impl Rgb {
    #[must_use]
    pub const fn new(r: u8, g: u8, b: u8) -> Self {
        Self { r, g, b }
    }
}

/// Maps a [`HlClass`] to a color. The default is Nord (the fleet look). A
/// future `hikari-theme` crate will source this from `ishou_tokens`.
pub trait Theme: Send + Sync {
    fn color(&self, class: HlClass) -> Rgb;
}

/// The Nord Polar Night / Snow Storm / Aurora / Frost palette.
pub struct NordTheme;

impl Theme for NordTheme {
    fn color(&self, class: HlClass) -> Rgb {
        // Nord anchors: fg snow #D8DEE9, comment polar #616E88, keyword frost
        // #81A1C1, string aurora-green #A3BE8C, number aurora-purple #B48EAD,
        // type frost #8FBCBB, function frost #88C0D0, constant/bool orange
        // #D08770, punctuation snow #ECEFF4, error red #BF616A.
        match class {
            HlClass::Comment { .. } => Rgb::new(0x61, 0x6E, 0x88),
            HlClass::Keyword => Rgb::new(0x81, 0xA1, 0xC1),
            HlClass::KeywordArg | HlClass::Attribute => Rgb::new(0xB4, 0x8E, 0xAD),
            HlClass::Type | HlClass::Namespace => Rgb::new(0x8F, 0xBC, 0xBB),
            HlClass::Function => Rgb::new(0x88, 0xC0, 0xD0),
            HlClass::Str => Rgb::new(0xA3, 0xBE, 0x8C),
            HlClass::Escape | HlClass::Special => Rgb::new(0xEB, 0xCB, 0x8B),
            HlClass::Numeric { .. } => Rgb::new(0xB4, 0x8E, 0xAD),
            HlClass::Boolean | HlClass::Constant => Rgb::new(0xD0, 0x87, 0x70),
            HlClass::Operator => Rgb::new(0x81, 0xA1, 0xC1),
            HlClass::Punctuation => Rgb::new(0xEC, 0xEF, 0xF4),
            HlClass::Hyperlink => Rgb::new(0x5E, 0x81, 0xAC),
            HlClass::Error | HlClass::Removed => Rgb::new(0xBF, 0x61, 0x6A),
            HlClass::Warning => Rgb::new(0xEB, 0xCB, 0x8B),
            HlClass::Info => Rgb::new(0x81, 0xA1, 0xC1),
            HlClass::Hint => Rgb::new(0x5E, 0x81, 0xAC),
            HlClass::Added => Rgb::new(0xA3, 0xBE, 0x8C),
            HlClass::Variable | HlClass::Whitespace | HlClass::Unchanged | HlClass::Plain => {
                Rgb::new(0xD8, 0xDE, 0xE9)
            }
        }
    }
}

// ─────────────────────── the table-driven backend ───────────────

/// The hand-rolled backend: one char-class scanner parameterized by a
/// per-language [`LangTable`], covering the common shape of the built-in
/// languages. Heavier backends (tree-sitter, macro-generated) live in sibling
/// `hikari-*` crates and satisfy the same [`LanguageLexer`] / [`LanguagePlugin`]
/// traits.
pub mod langs {
    use super::{HlClass, Language, LanguageLexer, LanguagePlugin, LineDriven, Selector, SpanSink};

    /// Per-language lexing table.
    pub struct LangTable {
        pub keywords: &'static [&'static str],
        pub line_comments: &'static [&'static str],
        pub block_comment: Option<(&'static str, &'static str)>,
        pub string_delims: &'static [char],
        /// Treat `:name` tokens as [`HlClass::KeywordArg`] (lisp keywords).
        pub colon_keywords: bool,
    }

    /// Cross-line state: continuing a block comment or a string literal.
    #[derive(Clone, Copy, PartialEq, Eq, Default)]
    pub enum LineMode {
        #[default]
        Normal,
        InBlockComment,
        /// Inside a string opened on a previous line; carries the delimiter.
        InString(char),
    }

    /// The table lexer.
    pub struct TableLexer {
        pub table: &'static LangTable,
    }

    #[inline]
    fn is_ident_start(c: char) -> bool {
        c == '_' || c.is_alphabetic()
    }
    #[inline]
    fn is_ident_continue(c: char) -> bool {
        c == '_' || c.is_alphanumeric()
    }

    impl LanguageLexer for TableLexer {
        type LineState = LineMode;

        #[allow(clippy::too_many_lines)]
        fn lex_line(
            &self,
            line: &str,
            line_start: u32,
            entry: LineMode,
            sink: &mut SpanSink,
        ) -> LineMode {
            let t = self.table;
            let n = line.len();
            let base = line_start;
            let push = |sink: &mut SpanSink, s: usize, e: usize, class: HlClass| {
                sink.push(base + s as u32, base + e as u32, class);
            };
            let mut i = 0usize;
            let mut mode = entry;

            // Resume a continuation from the previous line.
            match mode {
                LineMode::InBlockComment => {
                    if let Some((_, close)) = t.block_comment {
                        if let Some(rel) = line.find(close) {
                            let e = rel + close.len();
                            push(sink, 0, e, HlClass::Comment { multiline: true });
                            i = e;
                            mode = LineMode::Normal;
                        } else {
                            push(sink, 0, n, HlClass::Comment { multiline: true });
                            return LineMode::InBlockComment;
                        }
                    } else {
                        mode = LineMode::Normal;
                    }
                }
                LineMode::InString(delim) => {
                    let e = scan_string_body(line, 0, delim);
                    match e {
                        Some(end) => {
                            push(sink, 0, end, HlClass::Str);
                            i = end;
                            mode = LineMode::Normal;
                        }
                        None => {
                            push(sink, 0, n, HlClass::Str);
                            return LineMode::InString(delim);
                        }
                    }
                }
                LineMode::Normal => {}
            }

            let _ = mode;
            'scan: while i < n {
                let c = line[i..].chars().next().unwrap();
                let cl = c.len_utf8();

                // whitespace run
                if c.is_whitespace() {
                    let s = i;
                    while i < n {
                        let d = line[i..].chars().next().unwrap();
                        if !d.is_whitespace() {
                            break;
                        }
                        i += d.len_utf8();
                    }
                    push(sink, s, i, HlClass::Whitespace);
                    continue 'scan;
                }

                // line comments
                for lc in t.line_comments {
                    if line[i..].starts_with(lc) {
                        push(sink, i, n, HlClass::Comment { multiline: false });
                        i = n;
                        continue 'scan;
                    }
                }

                // block comment open
                if let Some((open, close)) = t.block_comment {
                    if line[i..].starts_with(open) {
                        if let Some(rel) = line[i + open.len()..].find(close) {
                            let e = i + open.len() + rel + close.len();
                            push(sink, i, e, HlClass::Comment { multiline: true });
                            i = e;
                            continue 'scan;
                        }
                        push(sink, i, n, HlClass::Comment { multiline: true });
                        return LineMode::InBlockComment;
                    }
                }

                // string literal
                if t.string_delims.contains(&c) {
                    match scan_string_body(line, i + cl, c) {
                        Some(end) => {
                            push(sink, i, end, HlClass::Str);
                            i = end;
                            continue 'scan;
                        }
                        None => {
                            push(sink, i, n, HlClass::Str);
                            return LineMode::InString(c);
                        }
                    }
                }

                // number
                if c.is_ascii_digit() {
                    let s = i;
                    let mut is_float = false;
                    i += cl;
                    while i < n {
                        let d = line[i..].chars().next().unwrap();
                        if d.is_ascii_alphanumeric() || d == '_' {
                            i += d.len_utf8();
                        } else if d == '.' {
                            is_float = true;
                            i += 1;
                        } else {
                            break;
                        }
                    }
                    push(sink, s, i, HlClass::Numeric { float: is_float });
                    continue 'scan;
                }

                // colon keyword (lisp `:name`)
                if t.colon_keywords && c == ':' && i + 1 < n {
                    let next = line[i + 1..].chars().next().unwrap();
                    if is_ident_start(next) {
                        let s = i;
                        i += 1;
                        while i < n {
                            let d = line[i..].chars().next().unwrap();
                            if !is_ident_continue(d) {
                                break;
                            }
                            i += d.len_utf8();
                        }
                        push(sink, s, i, HlClass::KeywordArg);
                        continue 'scan;
                    }
                }

                // identifier / keyword
                if is_ident_start(c) {
                    let s = i;
                    i += cl;
                    while i < n {
                        let d = line[i..].chars().next().unwrap();
                        if !is_ident_continue(d) {
                            break;
                        }
                        i += d.len_utf8();
                    }
                    let word = &line[s..i];
                    let class = if t.keywords.contains(&word) {
                        HlClass::Keyword
                    } else if matches!(
                        word,
                        "true" | "false" | "True" | "False" | "None" | "nil" | "null"
                    ) {
                        HlClass::Boolean
                    } else if word.chars().next().is_some_and(char::is_uppercase) {
                        HlClass::Type
                    } else {
                        HlClass::Variable
                    };
                    push(sink, s, i, class);
                    continue 'scan;
                }

                // punctuation / operator (single char)
                let class = if "+-*/%=<>!&|^~".contains(c) {
                    HlClass::Operator
                } else {
                    HlClass::Punctuation
                };
                push(sink, i, i + cl, class);
                i += cl;
            }

            LineMode::Normal
        }
    }

    /// Scan a string body starting at `from` (just after the opening delim),
    /// honoring `\` escapes. Returns the byte index just past the closing
    /// delim, or `None` if the line ends first (string continues).
    fn scan_string_body(line: &str, from: usize, delim: char) -> Option<usize> {
        let n = line.len();
        let mut i = from;
        while i < n {
            let c = line[i..].chars().next().unwrap();
            let cl = c.len_utf8();
            if c == '\\' && i + cl < n {
                let e = line[i + cl..].chars().next().unwrap();
                i += cl + e.len_utf8();
                continue;
            }
            i += cl;
            if c == delim {
                return Some(i);
            }
        }
        None
    }

    /// A [`LanguagePlugin`] backed by a static [`LangTable`].
    pub struct TablePlugin {
        pub language: Language,
        pub selectors: &'static [Selector],
        pub table: &'static LangTable,
    }

    impl LanguagePlugin for TablePlugin {
        fn language(&self) -> Language {
            self.language
        }
        fn selectors(&self) -> &'static [Selector] {
            self.selectors
        }
        fn make_highlighter(&self) -> Box<dyn super::Highlighter> {
            Box::new(LineDriven::new(TableLexer { table: self.table }))
        }
        fn make_incremental(&self) -> Box<dyn super::IncrementalHighlighter> {
            // A real LineState-carrying cache — the table backend gains the
            // fixpoint re-lex, not the whole-document fallback.
            Box::new(super::LineCache::new(TableLexer { table: self.table }))
        }
    }

    // ── the built-in language tables ──

    static RUST_KW: &[&str] = &[
        "as", "async", "await", "break", "const", "continue", "crate", "dyn", "else", "enum",
        "extern", "fn", "for", "if", "impl", "in", "let", "loop", "match", "mod", "move", "mut",
        "pub", "ref", "return", "self", "Self", "static", "struct", "super", "trait", "type",
        "unsafe", "use", "where", "while",
    ];
    static RUST_TABLE: LangTable = LangTable {
        keywords: RUST_KW,
        line_comments: &["//"],
        block_comment: Some(("/*", "*/")),
        string_delims: &['"'],
        colon_keywords: false,
    };
    static RUST_SEL: &[Selector] = &[Selector::Extension("rs")];

    static PY_KW: &[&str] = &[
        "and", "as", "assert", "async", "await", "break", "class", "continue", "def", "del",
        "elif", "else", "except", "finally", "for", "from", "global", "if", "import", "in", "is",
        "lambda", "nonlocal", "not", "or", "pass", "raise", "return", "try", "while", "with",
        "yield",
    ];
    static PY_TABLE: LangTable = LangTable {
        keywords: PY_KW,
        line_comments: &["#"],
        block_comment: None,
        string_delims: &['"', '\''],
        colon_keywords: false,
    };
    static PY_SEL: &[Selector] = &[Selector::Extension("py")];

    // tatara-lisp is the fleet's own dialect, so its high-frequency heads earn
    // their place here alongside the generic Lisp ones. Counts are corpus
    // occurrences over the 1121 .tlisp/.lisp files under ~/code/github/pleme-io
    // (measured 2026-07-29): `define` 2395, `defsubcmd` 671, `defalias` 583,
    // `defcaixa` 274. The evaluator's special forms (`define`, `lambda`, `let`,
    // `if`, `cond`, `quote`, `set!`, `begin`) can never come from the domain
    // registry — they are a closed hardcoded match in
    // tatara-lisp-eval/src/special.rs — so a table backend is exactly the right
    // home for them.
    static LISP_KW: &[&str] = &[
        "def",
        "defn",
        "defmacro",
        "defcaixa",
        "deflexer",
        "define",
        "defun",
        "defmodule",
        "defsuite",
        "deftest",
        "let",
        "let*",
        "letrec",
        "lambda",
        "fn",
        "if",
        "cond",
        "case",
        "when",
        "unless",
        "do",
        "begin",
        "quote",
        "quasiquote",
        "unquote",
        "set!",
        "and",
        "or",
        "not",
        "import",
        "importar",
    ];
    static LISP_TABLE: LangTable = LangTable {
        keywords: LISP_KW,
        line_comments: &[";"],
        block_comment: Some(("#|", "|#")),
        string_delims: &['"'],
        colon_keywords: true,
    };
    static LISP_SEL: &[Selector] = &[
        Selector::Extension("lisp"),
        // `.tlisp` is tatara-lisp's script extension — 571 files in the fleet,
        // every one of which resolved to NO language before this line, so the
        // editor painted them as plain text. The omission was invisible
        // because `.lisp` (550 files) did resolve.
        Selector::Extension("tlisp"),
        Selector::Extension("lsp"),
        Selector::Extension("el"),
        Selector::Extension("scm"),
    ];

    static JSON_TABLE: LangTable = LangTable {
        keywords: &["true", "false", "null"],
        line_comments: &[],
        block_comment: None,
        string_delims: &['"'],
        colon_keywords: false,
    };
    static JSON_SEL: &[Selector] = &[Selector::Extension("json")];

    static TOML_TABLE: LangTable = LangTable {
        keywords: &["true", "false"],
        line_comments: &["#"],
        block_comment: None,
        string_delims: &['"', '\''],
        colon_keywords: false,
    };
    static TOML_SEL: &[Selector] = &[
        Selector::Extension("toml"),
        Selector::Filename("Cargo.lock"),
    ];

    static MD_TABLE: LangTable = LangTable {
        keywords: &[],
        line_comments: &[],
        block_comment: None,
        string_delims: &['`'],
        colon_keywords: false,
    };
    static MD_SEL: &[Selector] = &[Selector::Extension("md"), Selector::Extension("markdown")];

    /// Every built-in language plugin (the batteries-included default set).
    #[must_use]
    pub fn builtins() -> Vec<Box<dyn LanguagePlugin>> {
        vec![
            Box::new(TablePlugin {
                language: Language("rust"),
                selectors: RUST_SEL,
                table: &RUST_TABLE,
            }),
            Box::new(TablePlugin {
                language: Language("python"),
                selectors: PY_SEL,
                table: &PY_TABLE,
            }),
            Box::new(TablePlugin {
                language: Language("lisp"),
                selectors: LISP_SEL,
                table: &LISP_TABLE,
            }),
            Box::new(TablePlugin {
                language: Language("json"),
                selectors: JSON_SEL,
                table: &JSON_TABLE,
            }),
            Box::new(TablePlugin {
                language: Language("toml"),
                selectors: TOML_SEL,
                table: &TOML_TABLE,
            }),
            Box::new(TablePlugin {
                language: Language("markdown"),
                selectors: MD_SEL,
                table: &MD_TABLE,
            }),
        ]
    }
}

// ───────────────────────────── tests ────────────────────────────

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

    fn covers(text: &str, spans: &[HighlightSpan]) {
        // Coverage-complete + non-overlapping + forward-only by construction.
        let mut cursor = 0u32;
        for s in spans {
            assert_eq!(s.span.start, cursor, "gap/overlap at {cursor}");
            assert!(s.span.end > s.span.start);
            cursor = s.span.end;
        }
        assert_eq!(cursor as usize, text.len(), "partition does not cover text");
    }

    #[test]
    fn partition_is_coverage_complete() {
        let eco = Ecosystem::with_builtins();
        for (path, src) in [
            ("a.rs", "fn main() {\n    let x = 42; // hi\n}\n"),
            ("b.py", "def f(x):\n    return \"s\"  # c\n"),
            ("c.lisp", "(defcaixa :name \"x\" 42) ; c\n"),
            ("c.tlisp", "(define f (lambda (x) \"s\")) ; c\n"),
            ("d.txt", "no language here\n"),
        ] {
            let h = eco.highlighter_for_path(path);
            let spans = h.highlight(src);
            covers(src, &spans);
        }
    }

    #[test]
    fn resolves_by_extension_not_always_rust() {
        let eco = Ecosystem::with_builtins();
        assert_eq!(eco.resolve("src/main.rs"), Language("rust"));
        assert_eq!(eco.resolve("app.py"), Language("python"));
        assert_eq!(eco.resolve("x.lisp"), Language("lisp"));
        // tatara-lisp's script extension. 571 fleet files resolved to no
        // language at all before this was added; pin it so it cannot regress.
        assert_eq!(eco.resolve("x.tlisp"), Language("lisp"));
        assert_eq!(eco.resolve("tools/check.tlisp"), Language("lisp"));
        assert_eq!(eco.resolve("Cargo.lock"), Language("toml"));
        // The bug: a non-Rust file must NOT resolve to rust.
        assert_eq!(eco.resolve("notes.txt"), PLAIN_TEXT);
        assert_ne!(eco.resolve("app.py"), Language("rust"));
    }

    #[test]
    fn rust_keyword_is_classified() {
        let eco = Ecosystem::with_builtins();
        let spans = eco.highlighter_for_path("a.rs").highlight("fn x");
        assert_eq!(spans[0].class, HlClass::Keyword); // `fn`
    }

    #[test]
    fn multiline_string_and_block_comment_thread_state() {
        let eco = Ecosystem::with_builtins();
        let spans = eco.highlighter_for_path("a.rs").highlight("/* a\nb */ x\n");
        covers("/* a\nb */ x\n", &spans);
        assert!(matches!(
            spans[0].class,
            HlClass::Comment { multiline: true }
        ));
    }

    #[test]
    fn plain_text_is_one_plain_span() {
        let h = PlainHighlighter;
        let spans = h.highlight("hello");
        assert_eq!(spans.len(), 1);
        assert_eq!(spans[0].class, HlClass::Plain);
    }

    // ── incremental line cache (the LineState-fixpoint seal) ──

    /// A tiny deterministic LCG so the differential fuzz is reproducible
    /// without a `rand` dependency (hikari-core is zero-dep).
    fn lcg(state: &mut u64) -> u64 {
        *state = state
            .wrapping_mul(6_364_136_223_846_793_005)
            .wrapping_add(1_442_695_040_888_963_407);
        *state >> 33
    }

    /// S5 seal — the load-bearing invariant: an incremental re-lex is
    /// byte-identical to a one-shot re-lex, for every edit. Differential fuzz
    /// over random insert/delete edits against a Rust-ish corpus.
    #[test]
    fn incremental_is_byte_identical_to_one_shot() {
        let eco = Ecosystem::with_builtins();
        let one_shot = eco.highlighter_for_path("f.rs");
        let mut cache = eco.incremental_highlighter_for_path("f.rs");

        let alphabet: Vec<char> = "fn xy=42;{}\n/*/ \"ab\"//c".chars().collect();
        let mut text = String::from("fn main() {\n    let x = 1;\n}\n");
        let mut seed = 0x1234_5678_9abc_def0u64;

        for _ in 0..400 {
            // Random edit: insert a char or delete one, at a random boundary.
            let len = text.chars().count();
            let at = if len == 0 {
                0
            } else {
                (lcg(&mut seed) as usize) % (len + 1)
            };
            let byte_at = text.char_indices().nth(at).map_or(text.len(), |(b, _)| b);
            if len > 4 && lcg(&mut seed) % 2 == 0 {
                // delete one char
                if let Some((b, c)) = text[byte_at..].char_indices().next() {
                    let start = byte_at + b;
                    text.replace_range(start..start + c.len_utf8(), "");
                }
            } else {
                let c = alphabet[(lcg(&mut seed) as usize) % alphabet.len()];
                text.insert(byte_at, c);
            }

            let inc = cache.highlight(&text);
            let full = one_shot.highlight(&text);
            assert_eq!(inc, full, "incremental != one-shot for {text:?}");
            covers(&text, &inc);
        }
    }

    /// S6 seal — an unchanged document re-lexes NOTHING on the second call.
    #[test]
    fn idle_rehighlight_relexes_zero_lines() {
        let eco = Ecosystem::with_builtins();
        let mut cache = eco.incremental_highlighter_for_path("f.rs");
        let text = "fn a() {}\nfn b() {}\nfn c() {}\n";
        let _ = cache.highlight(text);
        let _ = cache.highlight(text); // idle re-render
        assert_eq!(
            cache.last_relexed(),
            0,
            "idle re-render must re-lex nothing"
        );
    }

    /// The fixpoint: a one-line edit re-lexes only the lines up to where the
    /// carried `LineState` re-converges — here, a leaf edit re-lexes just its
    /// own line, not the whole 60-line document.
    #[test]
    fn single_line_edit_relexes_locally() {
        let eco = Ecosystem::with_builtins();
        let mut cache = eco.incremental_highlighter_for_path("f.rs");
        let mut text = String::new();
        for i in 0..60 {
            text.push_str(&format!("let v{i} = {i};\n"));
        }
        let _ = cache.highlight(&text); // prime: 60 lines lexed
        // Edit line 30's value only (no block-comment/string state crosses).
        let edited = text.replacen("let v30 = 30;", "let v30 = 999;", 1);
        let _ = cache.highlight(&edited);
        assert_eq!(
            cache.last_relexed(),
            1,
            "a local edit must re-lex exactly its own line (state re-converges immediately)"
        );
    }

    /// A block comment opened mid-document propagates: re-lex continues past
    /// the edited line until the carried state re-converges (here, the `*/`).
    #[test]
    fn cross_line_state_change_propagates_then_converges() {
        let eco = Ecosystem::with_builtins();
        let mut cache = eco.incremental_highlighter_for_path("f.rs");
        let text = "let a = 1;\nlet b = 2;\nlet c = 3;\nlet d = 4;\n";
        let _ = cache.highlight(text);
        // Open a block comment on line 0 that closes on line 2.
        let edited = "let a = 1; /*\nstill comment\n*/ let c = 3;\nlet d = 4;\n";
        let inc = cache.highlight(edited);
        assert_eq!(inc, eco.highlighter_for_path("f.rs").highlight(edited));
        // Lines 0..=2 re-lexed (state in flight); line 3 reused (state reconverged).
        assert!(
            cache.last_relexed() <= 3,
            "re-lex must stop once the block comment closes and state reconverges"
        );
    }
}