elio 1.5.0

Snappy, batteries-included terminal file manager with rich previews, inline images, bulk actions, and trash support.
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
mod bundle;
mod render;
mod semantics;
mod shell;

#[cfg(test)]
use crate::preview::code::syntax_manifest::CURATED_SYNTAXES;
#[cfg(test)]
use crate::preview::code::syntax_manifest::CuratedSyntax;
use crate::preview::code::syntax_manifest::curated_syntax;
use ratatui::text::Line;

pub(in crate::preview::code) fn is_enabled(code_syntax: &str) -> bool {
    curated_syntax(code_syntax).is_some()
}

#[cfg(test)]
pub(in crate::preview::code) fn supported_syntaxes() -> &'static [CuratedSyntax] {
    CURATED_SYNTAXES
}

pub(in crate::preview::code) fn render_syntect_code_preview<F>(
    code_syntax: &str,
    text: &str,
    line_numbers: bool,
    line_limit: usize,
    canceled: &F,
) -> Result<Vec<Line<'static>>, ()>
where
    F: Fn() -> bool,
{
    if shell::is_shell_like_syntax(code_syntax) {
        return Ok(shell::render_shell_code_preview(
            text,
            line_numbers,
            line_limit,
            canceled,
        ));
    }

    let syntax_set = bundle::syntax_set();
    let Some(syntax) = bundle::find_syntax(syntax_set, code_syntax) else {
        return Err(());
    };

    render::render_syntect_code_preview(
        text,
        syntax_set,
        syntax,
        line_numbers,
        line_limit,
        canceled,
    )
}

#[cfg(test)]
mod tests {
    use super::bundle::{find_syntax, syntax_set};
    use super::semantics::{SemanticRole, semantic_role_for_token};
    use super::*;
    use crate::preview::appearance as theme;
    use std::str::FromStr;
    use syntect::{
        easy::ScopeRangeIterator,
        parsing::{ParseState, ScopeStack},
    };

    fn span_color(line: &Line<'_>, token: &str) -> Option<ratatui::style::Color> {
        line.spans
            .iter()
            .find(|span| span.content.contains(token))
            .and_then(|span| span.style.fg)
    }

    fn palette_colors() -> Vec<ratatui::style::Color> {
        let palette = theme::code_preview_palette();
        vec![
            palette.fg,
            palette.bg,
            palette.selection_bg,
            palette.selection_fg,
            palette.caret,
            palette.line_highlight,
            palette.line_number,
            palette.comment,
            palette.string,
            palette.constant,
            palette.keyword,
            palette.function,
            palette.r#type,
            palette.parameter,
            palette.tag,
            palette.operator,
            palette.r#macro,
            palette.invalid,
        ]
    }

    fn token_scopes(code_syntax: &str, text: &str) -> Vec<(String, String)> {
        let syntax_set = syntax_set();
        let syntax = find_syntax(syntax_set, code_syntax).expect("syntax should exist");
        let mut parse_state = ParseState::new(syntax);
        let mut scope_stack = ScopeStack::new();
        let mut tokens = Vec::new();

        for line in text.lines() {
            // Mirror the render path: append \n so newlines-mode grammars
            // properly terminate line comments (same fix as in render.rs).
            let line_with_nl = format!("{line}\n");
            let ops = parse_state
                .parse_line(&line_with_nl, syntax_set)
                .expect("line should parse");
            for (range, op) in ScopeRangeIterator::new(&ops, &line_with_nl) {
                scope_stack.apply(op).expect("scope op should apply");
                let token = line_with_nl[range].trim_end_matches('\n');
                if !token.is_empty() {
                    tokens.push((token.to_string(), scope_stack.to_string()));
                }
            }
        }

        tokens
    }

    #[test]
    fn bundled_syntaxes_cover_initial_canaries() {
        let syntax_set = syntax_set();

        for syntax in supported_syntaxes() {
            assert!(
                find_syntax(syntax_set, syntax.canonical_id).is_some(),
                "missing syntect syntax for {}",
                syntax.canonical_id
            );
        }
    }

    #[test]
    fn direct_syntect_rendering_supports_registry_canonical_ids() {
        let rendered = render_syntect_code_preview("rust", "fn main() {}\n", true, 20, &|| false)
            .expect("rust syntax should render through syntect");

        assert!(
            rendered[0]
                .spans
                .iter()
                .any(|span| span.content.contains("fn"))
        );
        assert_eq!(
            span_color(&rendered[0], "fn"),
            Some(theme::code_preview_palette().keyword)
        );
    }

    #[test]
    fn clojure_support_renders_through_curated_syntect_bundle() {
        let rendered = render_syntect_code_preview(
            "clojure",
            "(ns elio.core)\n(defn greet [name] (str \"hi \" name))\n",
            true,
            20,
            &|| false,
        )
        .expect("clojure syntax should render through syntect");

        assert_eq!(
            span_color(&rendered[1], "defn"),
            Some(theme::code_preview_palette().keyword)
        );
    }

    #[test]
    fn fortran_support_renders_through_curated_syntect_bundle() {
        let rendered = render_syntect_code_preview(
            "fortran",
            "program elio\n  implicit none\n  print *, \"hello\"\nend program elio\n",
            true,
            20,
            &|| false,
        )
        .expect("fortran syntax should render through syntect");

        assert_eq!(
            span_color(&rendered[0], "program"),
            Some(theme::code_preview_palette().keyword)
        );
    }

    #[test]
    fn cobol_support_renders_through_curated_syntect_bundle() {
        let rendered = render_syntect_code_preview(
            "cobol",
            "       IDENTIFICATION DIVISION.\n       PROGRAM-ID. ELIOTEST.\n       PROCEDURE DIVISION.\n           DISPLAY \"HELLO\".\n           STOP RUN.\n",
            true,
            20,
            &|| false,
        )
        .expect("cobol syntax should render through syntect");

        assert_eq!(
            span_color(&rendered[0], "IDENTIFICATION"),
            Some(theme::code_preview_palette().keyword)
        );
    }

    #[test]
    fn unsupported_syntaxes_return_errors_for_safe_fallback() {
        for code_syntax in ["ziggy", "brainfuck", "totally-unknown-syntax"] {
            assert!(
                render_syntect_code_preview(code_syntax, "sample\n", true, 20, &|| false).is_err(),
                "expected {code_syntax} to fall back safely"
            );
        }
    }

    #[test]
    fn enabled_syntaxes_are_routed_to_syntect() {
        for syntax in supported_syntaxes() {
            assert!(
                is_enabled(syntax.canonical_id),
                "expected {} to be enabled",
                syntax.canonical_id
            );
        }
    }

    #[test]
    fn curated_bundle_supports_newly_vendored_languages() {
        for (code_syntax, snippet) in [
            ("dockerfile", "FROM rust:1.87\nRUN cargo build --release\n"),
            ("hcl", "server { listen = \"127.0.0.1\" enabled = true }\n"),
            (
                "terraform",
                "terraform { required_version = \">= 1.7\" }\nresource \"null_resource\" \"example\" {}\n",
            ),
            (
                "typescript",
                "export type User = { name: string }\nconst greet = (user: User) => user.name;\n",
            ),
            (
                "tsx",
                "export function App() { return <button className=\"cta\">Hi</button>; }\n",
            ),
            (
                "jsx",
                "export function App() { return <button className=\"cta\">Hi</button>; }\n",
            ),
            (
                "nix",
                "{ description = \"elio\"; outputs = { self }: { packages.default = self; }; }\n",
            ),
            (
                "cmake",
                "cmake_minimum_required(VERSION 3.28)\nproject(elio)\nadd_executable(elio main.cpp)\n",
            ),
            (
                "scss",
                "$fg: #fff;\n.button { color: $fg; @include hover { color: red; } }\n",
            ),
            ("sass", "$fg: #fff\n.button\n  color: $fg\n"),
            ("less", "@fg: #fff;\n.button { color: @fg; }\n"),
            (
                "cs",
                "public class Greeter { public string Greet(string name) => name; }\n",
            ),
            (
                "dart",
                "class Greeter { String greet(String name) => name; }\n",
            ),
            (
                "zig",
                "const std = @import(\"std\");\npub fn main() void {}\n",
            ),
            (
                "kotlin",
                "class Greeter { fun greet(name: String): String = name }\n",
            ),
            (
                "swift",
                "struct Greeter { func greet(name: String) -> String { name } }\n",
            ),
            (
                "elixir",
                "defmodule Greeter do\n  def greet(name), do: \"hi #{name}\"\nend\n",
            ),
            (
                "fortran",
                "program elio\n  implicit none\n  print *, \"hello\"\nend program elio\n",
            ),
            (
                "cobol",
                "       IDENTIFICATION DIVISION.\n       PROGRAM-ID. ELIOTEST.\n       PROCEDURE DIVISION.\n           DISPLAY \"HELLO\".\n           STOP RUN.\n",
            ),
            ("julia", "function greet(name)\n  return name\nend\n"),
            ("just", "build:\n  cargo test\n"),
            (
                "powershell",
                "function Invoke-Greeting([string]$Name) {\n  Write-Host \"Hello $Name\"\n}\n",
            ),
            (
                "qml",
                "import QtQuick\nItem {\n  id: root\n  property bool active: true\n  onActiveChanged: console.log(\"changed\")\n}\n",
            ),
        ] {
            let rendered = render_syntect_code_preview(code_syntax, snippet, true, 20, &|| false)
                .expect("vendored syntax should render through syntect");
            assert!(
                rendered
                    .iter()
                    .flat_map(|line| line.spans.iter())
                    .any(|span| span.style.fg.is_some()),
                "expected {code_syntax} to produce styled output"
            );
        }
    }

    #[test]
    fn rendered_syntect_colors_only_use_elio_code_palette() {
        let allowed = palette_colors();
        let rendered = render_syntect_code_preview(
            "rust",
            "fn main() {\n    let answer = 42;\n    println!(\"hi\"); // note\n}\n",
            true,
            20,
            &|| false,
        )
        .expect("rust syntax should render through syntect");

        for line in &rendered {
            for span in &line.spans {
                if let Some(color) = span.style.fg {
                    assert!(
                        allowed.contains(&color),
                        "found non-Elio syntect color {color:?} in span {:?}",
                        span.content
                    );
                }
            }
        }
    }

    #[test]
    fn rendered_syntect_tokens_map_to_elio_semantic_roles() {
        let palette = theme::code_preview_palette();
        let rust = render_syntect_code_preview(
            "rust",
            "fn main() {\n    let answer = 42;\n    println!(\"hi\"); // note\n}\n",
            true,
            20,
            &|| false,
        )
        .expect("rust syntax should render through syntect");
        assert_eq!(span_color(&rust[0], "fn"), Some(palette.keyword));
        assert_eq!(span_color(&rust[1], "42"), Some(palette.constant));
        assert!(
            rust[2]
                .spans
                .iter()
                .any(|span| span.style.fg == Some(palette.string)),
            "expected a string-colored span in {:?}",
            rust[2]
        );
        assert!(
            rust[2]
                .spans
                .iter()
                .any(|span| span.style.fg == Some(palette.comment)),
            "expected a comment-colored span in {:?}",
            rust[2]
        );

        let html = render_syntect_code_preview(
            "html",
            "<div class=\"app\">elio</div>\n",
            true,
            20,
            &|| false,
        )
        .expect("html syntax should render through syntect");
        assert_eq!(span_color(&html[0], "div"), Some(palette.tag));
        assert_eq!(span_color(&html[0], "class"), Some(palette.parameter));
    }

    #[test]
    fn powershell_tokens_map_to_semantic_roles() {
        let palette = theme::code_preview_palette();
        let sample = "function Invoke-Greeting([string]$Name) {\n  if ($Name) { Write-Host \"Hello $Name\" }\n}\n";
        let rendered = render_syntect_code_preview("powershell", sample, true, 20, &|| false)
            .expect("powershell syntax should render through syntect");

        assert_eq!(span_color(&rendered[0], "function"), Some(palette.keyword));
        assert_eq!(
            span_color(&rendered[0], "Invoke-Greeting"),
            Some(palette.function)
        );
        assert_eq!(span_color(&rendered[0], "[string]"), Some(palette.r#type));
        assert_eq!(span_color(&rendered[0], "$Name"), Some(palette.parameter));
        assert_ne!(span_color(&rendered[1], "Write-Host"), Some(palette.fg));
        assert_eq!(span_color(&rendered[1], "\"Hello "), Some(palette.string));
        assert_eq!(span_color(&rendered[1], "$Name"), Some(palette.parameter));
    }

    #[test]
    fn qml_tokens_map_to_semantic_roles() {
        let palette = theme::code_preview_palette();
        let sample = "import QtQuick\nItem {\n  id: root\n  required property bool active: true\n  Component.onCompleted: {\n    if (active) {\n      console.log(\"hello\")\n    }\n  }\n}\n";
        let rendered = render_syntect_code_preview("qml", sample, true, 20, &|| false)
            .expect("qml syntax should render through syntect");

        assert_eq!(span_color(&rendered[1], "Item"), Some(palette.r#type));
        assert_eq!(span_color(&rendered[3], "required"), Some(palette.keyword));
        assert_eq!(span_color(&rendered[3], "property"), Some(palette.keyword));
        assert_eq!(span_color(&rendered[3], "active"), Some(palette.parameter));
        assert_eq!(
            span_color(&rendered[4], "Component.onCompleted"),
            Some(palette.parameter)
        );
        assert_eq!(span_color(&rendered[5], "if"), Some(palette.keyword));
        assert_eq!(span_color(&rendered[6], "log"), Some(palette.function));
        assert_eq!(span_color(&rendered[6], "\"hello\""), Some(palette.string));
    }

    #[test]
    fn sh_tokens_map_to_semantic_roles() {
        let palette = theme::code_preview_palette();
        let sample = "NAME=elio\nif [ -n \"$HOME\" ]; then\n  echo \"$NAME\"\nfi # done\n";
        let rendered = render_syntect_code_preview("sh", sample, true, 20, &|| false)
            .expect("sh syntax should render through syntect");
        let scopes = token_scopes("sh", sample);

        assert_ne!(
            span_color(&rendered[0], "NAME"),
            Some(palette.fg),
            "sh assignment fell back to fg; scopes: {scopes:#?}"
        );
        assert_ne!(
            span_color(&rendered[1], "if"),
            Some(palette.fg),
            "sh keyword fell back to fg; scopes: {scopes:#?}"
        );
        assert_ne!(
            span_color(&rendered[1], "$"),
            Some(palette.fg),
            "sh variable marker fell back to fg; scopes: {scopes:#?}"
        );
        assert_ne!(
            span_color(&rendered[1], "HOME"),
            Some(palette.fg),
            "sh variable fell back to fg; scopes: {scopes:#?}"
        );
        assert_ne!(
            span_color(&rendered[2], "echo"),
            Some(palette.fg),
            "sh builtin fell back to fg; scopes: {scopes:#?}"
        );
        assert_eq!(
            span_color(&rendered[3], "#"),
            Some(palette.comment),
            "sh comment marker did not map to comment color; scopes: {scopes:#?}"
        );
        assert_eq!(
            span_color(&rendered[3], " done"),
            Some(palette.comment),
            "sh comment did not map to comment color; scopes: {scopes:#?}"
        );
    }

    #[test]
    fn bash_tokens_map_to_semantic_roles() {
        let palette = theme::code_preview_palette();
        let sample = "NAME=elio\nif [ -n \"$HOME\" ]; then\n  echo \"$NAME\"\nfi # done\n";
        let rendered = render_syntect_code_preview("bash", sample, true, 20, &|| false)
            .expect("bash syntax should render through syntect");
        let scopes = token_scopes("bash", sample);

        assert_ne!(
            span_color(&rendered[0], "NAME"),
            Some(palette.fg),
            "bash assignment fell back to fg; scopes: {scopes:#?}"
        );
        assert_ne!(
            span_color(&rendered[1], "if"),
            Some(palette.fg),
            "bash keyword fell back to fg; scopes: {scopes:#?}"
        );
        assert_ne!(
            span_color(&rendered[1], "$"),
            Some(palette.fg),
            "bash variable marker fell back to fg; scopes: {scopes:#?}"
        );
        assert_ne!(
            span_color(&rendered[1], "HOME"),
            Some(palette.fg),
            "bash variable fell back to fg; scopes: {scopes:#?}"
        );
        assert_ne!(
            span_color(&rendered[2], "echo"),
            Some(palette.fg),
            "bash builtin fell back to fg; scopes: {scopes:#?}"
        );
        assert_eq!(
            span_color(&rendered[3], "#"),
            Some(palette.comment),
            "bash comment marker did not map to comment color; scopes: {scopes:#?}"
        );
        assert_eq!(
            span_color(&rendered[3], " done"),
            Some(palette.comment),
            "bash comment did not map to comment color; scopes: {scopes:#?}"
        );
    }

    #[test]
    fn sh_plain_commands_and_functions_map_to_semantic_roles() {
        let palette = theme::code_preview_palette();
        let sample =
            "deploy() {\n  grep -q \"$HOME\" /etc/profile\n  my_tool --flag \"$NAME\"\n}\n";
        let rendered = render_syntect_code_preview("sh", sample, true, 20, &|| false)
            .expect("sh syntax should render through syntect");
        let scopes = token_scopes("sh", sample);

        assert_ne!(
            span_color(&rendered[0], "deploy"),
            Some(palette.fg),
            "sh function name fell back to fg; scopes: {scopes:#?}"
        );
        assert_ne!(
            span_color(&rendered[1], "grep"),
            Some(palette.fg),
            "sh command fell back to fg; scopes: {scopes:#?}"
        );
        assert_ne!(
            span_color(&rendered[1], "-q"),
            Some(palette.fg),
            "sh option fell back to fg; scopes: {scopes:#?}"
        );
        assert_ne!(
            span_color(&rendered[2], "my_tool"),
            Some(palette.fg),
            "sh custom command fell back to fg; scopes: {scopes:#?}"
        );
        assert_ne!(
            span_color(&rendered[2], "--flag"),
            Some(palette.fg),
            "sh long option fell back to fg; scopes: {scopes:#?}"
        );
    }

    #[test]
    fn sh_common_builtins_and_redirections_map_to_semantic_roles() {
        let palette = theme::code_preview_palette();
        let sample = "#!/bin/sh\nset -e\ncd /tmp\ntrap 'cleanup' EXIT\nexport PATH=\"$HOME/bin:$PATH\"\nsource ./env.sh\nread -r NAME\nexec \"$NAME\" > /tmp/out.log\n";
        let rendered = render_syntect_code_preview("sh", sample, true, 20, &|| false)
            .expect("sh syntax should render through shell-aware renderer");

        assert_eq!(span_color(&rendered[0], "#!"), Some(palette.r#macro));
        assert_ne!(span_color(&rendered[1], "set"), Some(palette.fg));
        assert_ne!(span_color(&rendered[2], "cd"), Some(palette.fg));
        assert_ne!(span_color(&rendered[3], "trap"), Some(palette.fg));
        assert_ne!(span_color(&rendered[4], "export"), Some(palette.fg));
        assert_ne!(span_color(&rendered[5], "source"), Some(palette.fg));
        assert_ne!(span_color(&rendered[6], "read"), Some(palette.fg));
        assert_ne!(span_color(&rendered[7], "exec"), Some(palette.fg));
        assert_ne!(span_color(&rendered[7], ">"), Some(palette.fg));
    }

    #[test]
    fn sh_heredoc_markers_and_pipeline_commands_map_to_semantic_roles() {
        let palette = theme::code_preview_palette();
        let sample = "cat <<EOF | grep -i \"$needle\"\n";
        let rendered = render_syntect_code_preview("sh", sample, true, 20, &|| false)
            .expect("sh syntax should render through shell-aware renderer");

        assert_eq!(span_color(&rendered[0], "cat"), Some(palette.function));
        assert_eq!(span_color(&rendered[0], "<<"), Some(palette.operator));
        assert_eq!(span_color(&rendered[0], "EOF"), Some(palette.parameter));
        assert_eq!(span_color(&rendered[0], "|"), Some(palette.operator));
        assert_eq!(span_color(&rendered[0], "grep"), Some(palette.function));
        assert_eq!(span_color(&rendered[0], "-i"), Some(palette.parameter));
        assert_eq!(span_color(&rendered[0], "$needle"), Some(palette.parameter));
    }

    #[test]
    fn sh_export_like_builtins_keep_assignment_context_for_following_names() {
        let palette = theme::code_preview_palette();
        let sample = "export PATH=\"$HOME/bin\"\nreadonly NAME=elio\nlocal count=1\n";
        let rendered = render_syntect_code_preview("sh", sample, true, 20, &|| false)
            .expect("sh syntax should render through shell-aware renderer");

        assert_eq!(span_color(&rendered[0], "export"), Some(palette.function));
        assert_eq!(span_color(&rendered[0], "PATH"), Some(palette.parameter));
        assert_eq!(span_color(&rendered[0], "="), Some(palette.operator));
        assert_eq!(span_color(&rendered[1], "readonly"), Some(palette.function));
        assert_eq!(span_color(&rendered[1], "NAME"), Some(palette.parameter));
        assert_eq!(span_color(&rendered[1], "="), Some(palette.operator));
        assert_eq!(span_color(&rendered[2], "local"), Some(palette.function));
        assert_eq!(span_color(&rendered[2], "count"), Some(palette.parameter));
        assert_eq!(span_color(&rendered[2], "="), Some(palette.operator));
    }

    #[test]
    fn semantic_role_classifier_covers_expected_scope_families() {
        let stack = ScopeStack::from_str("source.rust keyword.control.rust").unwrap();
        assert_eq!(
            semantic_role_for_token("if", stack.as_slice()),
            SemanticRole::Keyword
        );

        let stack =
            ScopeStack::from_str("text.html.basic meta.tag entity.other.attribute-name.html")
                .unwrap();
        assert_eq!(
            semantic_role_for_token("class", stack.as_slice()),
            SemanticRole::Parameter
        );

        let stack = ScopeStack::from_str(
            "source.c meta.preprocessor.include entity.name.function.preprocessor",
        )
        .unwrap();
        assert_eq!(
            semantic_role_for_token("include", stack.as_slice()),
            SemanticRole::Macro
        );

        let stack =
            ScopeStack::from_str("source.shell.bash variable.other.readwrite.assignment.shell")
                .unwrap();
        assert_eq!(
            semantic_role_for_token("MAKE", stack.as_slice()),
            SemanticRole::Parameter
        );

        let stack = ScopeStack::from_str("source.js variable.other.readwrite.js").unwrap();
        assert_eq!(
            semantic_role_for_token("Greeter", stack.as_slice()),
            SemanticRole::Type
        );

        let stack = ScopeStack::from_str("source.shell.bash").unwrap();
        assert_eq!(
            semantic_role_for_token("then", stack.as_slice()),
            SemanticRole::Keyword
        );

        let stack = ScopeStack::from_str("source.shell.bash").unwrap();
        assert_eq!(
            semantic_role_for_token("printf", stack.as_slice()),
            SemanticRole::Function
        );

        let stack = ScopeStack::from_str("source.shell.bash").unwrap();
        assert_eq!(
            semantic_role_for_token("$HOME", stack.as_slice()),
            SemanticRole::Parameter
        );

        let stack = ScopeStack::from_str("source.shell.bash meta.function-call.shell").unwrap();
        assert_eq!(
            semantic_role_for_token("grep", stack.as_slice()),
            SemanticRole::Function
        );

        let stack =
            ScopeStack::from_str("source.shell.bash meta.function-call.arguments.shell").unwrap();
        assert_eq!(
            semantic_role_for_token("--flag", stack.as_slice()),
            SemanticRole::Parameter
        );
    }

    #[test]
    fn sql_tokens_map_to_semantic_roles() {
        let palette = theme::code_preview_palette();
        let sample = concat!(
            "SELECT id, name FROM users WHERE id = 42; -- pick one\n",
            "CREATE TABLE items (id INTEGER PRIMARY KEY, label TEXT NOT NULL);\n",
            "INSERT INTO items VALUES (99, 'hello');\n",
        );
        let rendered = render_syntect_code_preview("sql", sample, true, 20, &|| false)
            .expect("sql syntax should render through syntect");
        let scopes = token_scopes("sql", sample);

        // DML keywords must be colored — not bleeding comment scope from line 1.
        assert_eq!(
            span_color(&rendered[0], "SELECT"),
            Some(palette.keyword),
            "SELECT should be keyword; scopes: {scopes:#?}"
        );
        assert_eq!(
            span_color(&rendered[0], "--"),
            Some(palette.comment),
            "-- should be comment color; scopes: {scopes:#?}"
        );
        // Line 2 must NOT be in comment scope despite the -- on line 1.
        assert_eq!(
            span_color(&rendered[1], "CREATE"),
            Some(palette.keyword),
            "CREATE on line 2 should be keyword, not comment; scopes: {scopes:#?}"
        );
        assert_eq!(
            span_color(&rendered[2], "INSERT INTO"),
            Some(palette.keyword),
            "INSERT INTO on line 3 should be keyword; scopes: {scopes:#?}"
        );
        // String literals must be string-colored.
        assert_eq!(
            span_color(&rendered[2], "hello"),
            Some(palette.string),
            "string literal should be string color; scopes: {scopes:#?}"
        );
        // Numeric constant (42 avoids collision with line numbers 1–3).
        assert_eq!(
            span_color(&rendered[0], "42"),
            Some(palette.constant),
            "numeric constant should be constant color; scopes: {scopes:#?}"
        );
        // Comparison operator must be operator color, not keyword (keyword.operator.*
        // prefix must not be swallowed by the broader keyword selector).
        assert_eq!(
            span_color(&rendered[0], "="),
            Some(palette.operator),
            "= should be operator color, not keyword; scopes: {scopes:#?}"
        );
    }

    #[test]
    fn semantic_role_classifier_keeps_shell_heuristics_scoped_to_shell_sources() {
        let rust_stack =
            ScopeStack::from_str("source.rust meta.function-call.arguments.rust").unwrap();
        assert_eq!(
            semantic_role_for_token("--flag", rust_stack.as_slice()),
            SemanticRole::Fg
        );

        let shell_stack =
            ScopeStack::from_str("source.shell.bash meta.function-call.arguments.shell").unwrap();
        assert_eq!(
            semantic_role_for_token("--flag", shell_stack.as_slice()),
            SemanticRole::Parameter
        );
    }
}