r3bl_tui 0.7.2

TUI library to build modern apps inspired by React, Elm, with Flexbox, CSS, editor component, emoji support, and more
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
/*
 *   Copyright (c) 2022-2025 R3BL LLC
 *   All rights reserved.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

//! This module contains code for converting between syntect styled texts and tui styled
//! texts.
//!
//! A [Vec] or [`crate::List`] of styled text represents a single line of text in an
//! editor component, which is the output of a syntax highlighter (that takes plain text
//! and returns the styled text).
//!
//! There is a major difference in doing this conversion which is:
//! - tui styled texts are styled unicode strings,
//! - while syntect styled texts are styled plain text strings.
//!
//! This requires the conversion code to perform the following steps:
//! 1. Convert the syntect [`SyntectStyleStrSpanLine`] into a [`StyleUSSpanLine`].
//! 2. Then convert [`StyleUSSpanLine`] into a [`TuiStyledTexts`].

use syntect::parsing::SyntaxSet;

use super::{StyleUSSpan, StyleUSSpanLine};
use crate::{List, TuiColor, TuiStyle, TuiStyledTexts, tui_color, tui_style_attrib,
            tui_styled_text};

// Type aliases for syntect types.

type SyntectStyle = syntect::highlighting::Style;
type SyntectFontStyle = syntect::highlighting::FontStyle;
type SyntectColor = syntect::highlighting::Color;

/// Span are chunks of a text that have an associated style. There are usually multiple
/// spans in a line of text.
pub type SyntectStyleStrSpan<'a> = (SyntectStyle, &'a str);

/// A line of text is made up of multiple [`SyntectStyleStrSpan`]s.
pub type SyntectStyleStrSpanLine<'a> = Vec<SyntectStyleStrSpan<'a>>;

/// Maps common language names to their file extensions for syntax highlighting.
/// This allows markdown code blocks to use either language names (e.g., "rust")
/// or file extensions (e.g., "rs").
///
/// TypeScript, TOML, SCSS, Kotlin, Swift, and Dockerfile are not supported by `syntect`.
/// In order to add these languages we need to add custom `.sublime-syntax` files later
/// for better support of TypeScript, TOML, etc., `syntect` makes that easy:
/// ```no_run
/// # use syntect::parsing::SyntaxSet;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Example of loading additional syntaxes
/// let mut builder = SyntaxSet::load_defaults_newlines().into_builder();
/// builder.add_from_folder("path/to/extra/syntaxes", true)?;
/// let syntax_set = builder.build();
/// # Ok(())
/// # }
/// ```
fn map_language_to_extension(lang: &str) -> &str {
    match lang {
        // Direct mappings
        "python" => "py",
        "golang" | "go" => "go",
        "csharp" | "c#" => "cs",
        "cpp" | "c++" => "cpp",
        "objective-c" | "objc" => "m",
        "yaml" | "yml" => "yaml",
        "json" => "json",
        "html" => "html",
        "xml" => "xml",
        "markdown" | "md" => "md",
        "ruby" | "rb" => "rb",
        "r" => "r",
        "sql" => "sql",
        "makefile" => "makefile",

        // Languages that fall back to JavaScript
        "javascript" | "typescript" | "ts" => "js",

        // Languages that fall back to CSS
        "css" | "scss" | "sass" => "css",

        // Languages that fall back to Java
        "java" | "kotlin" | "kt" => "java",

        // Languages that fall back to Rust
        "rust" | "toml" | "swift" => "rs",

        // Languages that fall back to shell
        "shell" | "bash" | "sh" | "dockerfile" => "sh",

        // Default: assume it's already a file extension
        _ => lang,
    }
}

pub fn try_get_syntax_ref<'a>(
    syntax_set: &'a SyntaxSet,
    file_extension: &'a str,
) -> Option<&'a syntect::parsing::SyntaxReference> {
    let mapped_extension = map_language_to_extension(file_extension);
    syntax_set.find_syntax_by_extension(mapped_extension)
}

#[must_use]
pub fn convert_style_from_syntect_to_tui(st_style: SyntectStyle) -> TuiStyle {
    TuiStyle {
        color_fg: Some(convert_color_from_syntect_to_tui(st_style.foreground)),
        color_bg: Some(convert_color_from_syntect_to_tui(st_style.background)),
        bold: st_style
            .font_style
            .contains(SyntectFontStyle::BOLD)
            .then_some(tui_style_attrib::Bold),
        italic: st_style
            .font_style
            .contains(SyntectFontStyle::ITALIC)
            .then_some(tui_style_attrib::Italic),
        underline: st_style
            .font_style
            .contains(SyntectFontStyle::UNDERLINE)
            .then_some(tui_style_attrib::Underline),
        ..Default::default()
    }
}

#[must_use]
pub fn convert_color_from_syntect_to_tui(st_color: SyntectColor) -> TuiColor {
    tui_color!(st_color.r, st_color.g, st_color.b)
}

#[must_use]
pub fn convert_highlighted_line_from_syntect_to_tui(
    syntect_highlighted_line: &SyntectStyleStrSpanLine<'_>,
) -> StyleUSSpanLine {
    fn convert(vec_styled_str: &SyntectStyleStrSpanLine<'_>) -> StyleUSSpanLine {
        let mut it: StyleUSSpanLine = List::default();

        for (style, text) in vec_styled_str {
            let my_style = convert_style_from_syntect_to_tui(*style);
            it.push(StyleUSSpan::new(my_style, text));
        }

        it
    }

    let mut it = convert(syntect_highlighted_line);

    // Remove the background color from each style in the theme.
    for span in it.iter_mut() {
        span.style.remove_bg_color();
    }

    it
}

#[must_use]
pub fn convert_span_line_from_syntect_to_tui_styled_texts(
    syntect_styles: &SyntectStyleStrSpanLine<'_>,
) -> TuiStyledTexts {
    let mut acc = TuiStyledTexts::default();
    for (syntect_style, text) in syntect_styles {
        let my_style = convert_style_from_syntect_to_tui(*syntect_style);
        acc += tui_styled_text!(@style: my_style, @text: (*text).to_string());
    }
    acc
}

#[cfg(test)]
mod tests_simple_md_highlight {
    use syntect::{easy::HighlightLines, highlighting::Style, util::LinesWithEndings};

    use crate::{ConvertToPlainText, TuiStyledTexts, assert_eq2,
                convert_span_line_from_syntect_to_tui_styled_texts,
                get_cached_syntax_set, get_cached_theme, tui_color};

    #[test]
    fn simple_md_highlight() {
        // Generate MD content.
        let md_content = {
            #[cfg(target_os = "windows")]
            {
                let mut it = include_str!("test_assets/valid-content.md").to_string();
                it = it.replace("\r\n", "\n");
                it
            }
            #[cfg(not(target_os = "windows"))]
            {
                include_str!("test_assets/valid-content.md").to_string()
            }
        };

        // Load these once at the start of your program.
        let syntax_set = get_cached_syntax_set();
        let theme = get_cached_theme();

        // Prepare Markdown syntax highlighting.q
        let md_syntax = syntax_set.find_syntax_by_extension("md").unwrap();
        let mut highlight_lines = HighlightLines::new(md_syntax, theme);

        let mut line_idx = 0;
        let mut vec_styled_texts = vec![];

        for line in /* LinesWithEndings enables use of newlines mode. */
            LinesWithEndings::from(md_content.as_str())
        {
            let vec_styled_str: Vec<(Style, &str)> =
                highlight_lines.highlight_line(line, syntax_set).unwrap();

            // // To pretty print the output, use the following:
            // let escaped = as_24_bit_terminal_escaped(&vec_styled_str[..], false);
            // print!("{}", escaped);

            let styled_texts: TuiStyledTexts =
                convert_span_line_from_syntect_to_tui_styled_texts(&vec_styled_str);

            line_idx += 1;
            for (col_idx, styled_text) in styled_texts.inner.iter().enumerate() {
                println!("[L#:{line_idx} => C#:{col_idx}] {styled_text:#?}");
            }
            vec_styled_texts.push(styled_texts);
        }

        // 42 lines.
        assert_eq2!(vec_styled_texts.len(), 42);

        // Interrogate first line.
        {
            let line = &vec_styled_texts[0];
            assert_eq2!(line.len(), 4);
            assert_eq2!(line.to_plain_text(), "# My Heading\n");
            let col1 = &line[0];
            assert!(col1.get_style().bold.is_some());
            let col3 = &line[2];
            assert_eq2!(col3.get_style().color_fg.unwrap(), tui_color!(46, 206, 43));
        }

        // Interrogate last line.
        {
            let line = &vec_styled_texts[41];
            assert_eq2!(line.len(), 1);
            assert_eq2!(line.to_plain_text(), "--- END ---\n");
            let col1 = &line[0];
            assert_eq2!(
                col1.get_style().color_fg.unwrap(),
                tui_color!(193, 179, 208)
            );
        }
    }
}

#[cfg(test)]
mod tests_convert_span_line_and_highlighted_line {
    use crate::{TuiStyledTexts, assert_eq2,
                convert_span_line_from_syntect_to_tui_styled_texts, tui_color};

    #[test]
    fn syntect_conversion() {
        let st_color_1 = syntect::highlighting::Color {
            r: 255,
            g: 255,
            b: 255,
            a: 0,
        };

        let st_color_2 = syntect::highlighting::Color {
            r: 0,
            g: 0,
            b: 0,
            a: 0,
        };

        let vec_styled_str: Vec<(syntect::highlighting::Style, &str)> = vec![
            // item 1.
            (
                syntect::highlighting::Style {
                    foreground: st_color_1,
                    background: st_color_1,
                    font_style: syntect::highlighting::FontStyle::empty(),
                },
                "st_color_1",
            ),
            // item 2.
            (
                syntect::highlighting::Style {
                    foreground: st_color_2,
                    background: st_color_2,
                    font_style: syntect::highlighting::FontStyle::BOLD,
                },
                "st_color_2",
            ),
            // item 3.
            (
                syntect::highlighting::Style {
                    foreground: st_color_1,
                    background: st_color_2,
                    font_style: syntect::highlighting::FontStyle::UNDERLINE
                        | syntect::highlighting::FontStyle::BOLD
                        | syntect::highlighting::FontStyle::ITALIC,
                },
                "st_color_1 and 2",
            ),
        ];

        let styled_texts: TuiStyledTexts =
            convert_span_line_from_syntect_to_tui_styled_texts(&vec_styled_str);

        // Should have 3 items.
        assert_eq2!(styled_texts.len(), 3);

        // item 1.
        {
            assert_eq2!(styled_texts[0].get_text(), "st_color_1");
            assert_eq2!(
                styled_texts[0].get_style().color_fg.unwrap(),
                tui_color!(255, 255, 255)
            );
            assert_eq2!(
                styled_texts[0].get_style().color_bg.unwrap(),
                tui_color!(255, 255, 255)
            );
        }

        // item 2.
        {
            assert_eq2!(styled_texts[1].get_text(), "st_color_2");
            assert_eq2!(
                styled_texts[1].get_style().color_fg.unwrap(),
                tui_color!(0, 0, 0)
            );
            assert_eq2!(
                styled_texts[1].get_style().color_bg.unwrap(),
                tui_color!(0, 0, 0)
            );
            assert!(styled_texts[1].get_style().bold.is_some());
        }

        // item 3.
        {
            assert_eq2!(styled_texts[2].get_text(), "st_color_1 and 2");
            assert_eq2!(
                styled_texts[2].get_style().color_fg.unwrap(),
                tui_color!(255, 255, 255)
            );
            assert_eq2!(
                styled_texts[2].get_style().color_bg.unwrap(),
                tui_color!(0, 0, 0)
            );
            assert!(styled_texts[2].get_style().bold.is_some());
            assert!(styled_texts[2].get_style().underline.is_some());
        }
    }
}

#[cfg(test)]
mod tests_convert_style_and_color {
    use smallvec::smallvec;

    use crate::{CommonResult, InlineVec, TuiStyle, TuiStylesheet, assert_eq2, ch,
                console_log, convert_style_from_syntect_to_tui, get_tui_style,
                get_tui_styles, new_style, throws, tui_color, tui_style_attrib,
                tui_stylesheet};

    #[test]
    fn syntect_style_conversion() {
        let st_style: syntect::highlighting::Style = syntect::highlighting::Style {
            foreground: syntect::highlighting::Color::WHITE,
            background: syntect::highlighting::Color::BLACK,
            font_style: syntect::highlighting::FontStyle::BOLD
                | syntect::highlighting::FontStyle::ITALIC
                | syntect::highlighting::FontStyle::UNDERLINE,
        };
        let style = convert_style_from_syntect_to_tui(st_style);
        assert_eq2!(style.color_fg.unwrap(), tui_color!(255, 255, 255));
        assert_eq2!(style.color_bg.unwrap(), tui_color!(0, 0, 0));
        assert!(style.bold.is_some());
        assert!(style.underline.is_some());
    }

    #[test]
    fn test_cascade_style() {
        let style_bold_green_fg = new_style!(
            id: {1} // "bold_green_fg"
            bold
            color_fg: {tui_color!(green)}
        );

        let style_dim = new_style!(
            id: {2} // "dim"
            dim
        );

        let style_yellow_bg = new_style!(
            id: {3} // "yellow_bg"
            color_bg: {tui_color!(yellow)}
        );

        let style_padding = new_style!(
            id: {4} // "padding"
            padding: {2}
        );

        let style_red_fg = new_style!(
            id: {5} // "red_fg"
            color_fg: {tui_color!(red)}
        );

        let style_padding_another = new_style!(
            id: {6} // "padding"
            padding: {1}
        );

        let my_style = style_bold_green_fg
            + style_dim
            + style_yellow_bg
            + style_padding
            + style_red_fg
            + style_padding_another;

        console_log!(my_style);

        assert_eq2!(my_style.padding.unwrap(), ch(3));
        assert_eq2!(my_style.color_bg.unwrap(), tui_color!(yellow));
        assert_eq2!(my_style.color_fg.unwrap(), tui_color!(red));
        assert!(my_style.bold.is_some());
        assert!(my_style.dim.is_some());
        assert!(my_style.computed.is_some());
        assert!(my_style.underline.is_none());
    }

    #[test]
    fn test_stylesheet_find_style_by_id() {
        let mut stylesheet = TuiStylesheet::new();

        let style1 = make_a_style(1);
        let result = stylesheet.add_style(style1);
        result.unwrap();
        assert_eq2!(stylesheet.styles.len(), 1);

        let style2 = make_a_style(2);
        let result = stylesheet.add_style(style2);
        result.unwrap();
        assert_eq2!(stylesheet.styles.len(), 2);

        // No macro.
        assert_eq2!(
            stylesheet.find_style_by_id(1).unwrap().id,
            tui_style_attrib::id(1)
        );
        assert_eq2!(
            stylesheet.find_style_by_id(2).unwrap().id,
            tui_style_attrib::id(2)
        );
        assert!(stylesheet.find_style_by_id(3).is_none());
        // Macro.
        assert_eq2!(
            get_tui_style!(@from: stylesheet, 1).unwrap().id,
            tui_style_attrib::id(1)
        );
        assert_eq2!(
            get_tui_style!(@from: stylesheet, 2).unwrap().id,
            tui_style_attrib::id(2)
        );
        assert!(get_tui_style!(@from: stylesheet, 3).is_none());
    }

    #[test]
    fn test_stylesheet_find_styles_by_ids() {
        fn assertions_for_find_styles_by_ids(result: &Option<InlineVec<TuiStyle>>) {
            assert_eq2!(result.as_ref().unwrap().len(), 2);
            assert_eq2!(result.as_ref().unwrap()[0].id, tui_style_attrib::id(1));
            assert_eq2!(result.as_ref().unwrap()[1].id, tui_style_attrib::id(2));
        }

        let mut stylesheet = TuiStylesheet::new();

        let style1 = make_a_style(1);
        let result = stylesheet.add_style(style1);
        result.unwrap();
        assert_eq2!(stylesheet.styles.len(), 1);

        let style2 = make_a_style(2);
        let result = stylesheet.add_style(style2);
        result.unwrap();
        assert_eq2!(stylesheet.styles.len(), 2);

        // Contains.
        assertions_for_find_styles_by_ids(&stylesheet.find_styles_by_ids(&[1, 2]));
        assertions_for_find_styles_by_ids(&get_tui_styles!(
            @from: &stylesheet,
            [1, 2]
        ));
        // Does not contain.
        assert_eq2!(stylesheet.find_styles_by_ids(&[3, 4]), None);
        assert_eq2!(get_tui_styles!(@from: stylesheet, [3, 4]), None);
    }

    #[test]
    fn test_stylesheet_builder() -> CommonResult<()> {
        throws!({
            let id_2 = 2;
            let style1 = make_a_style(1);
            let mut stylesheet = tui_stylesheet! {
                style1,
                new_style!(
                    id: {id_2} /* using a variable instead of string literal */
                    padding: {1}
                    color_bg: {tui_color!(55, 55, 248)}
                ),
                make_a_style(3),
                smallvec![
                    new_style!(
                        id: {4}
                        padding: {1}
                        color_bg: {tui_color!(55, 55, 248)}
                    ),
                    new_style!(
                        id: {5}
                        padding: {1}
                        color_bg: {tui_color!(85, 85, 255)}
                    ),
                ],
                make_a_style(6)
            };

            assert_eq2!(stylesheet.styles.len(), 6);
            assert_eq2!(
                stylesheet.find_style_by_id(1).unwrap().id,
                tui_style_attrib::id(1)
            );
            assert_eq2!(
                stylesheet.find_style_by_id(2).unwrap().id,
                tui_style_attrib::id(2)
            );
            assert_eq2!(
                stylesheet.find_style_by_id(3).unwrap().id,
                tui_style_attrib::id(3)
            );
            assert_eq2!(
                stylesheet.find_style_by_id(4).unwrap().id,
                tui_style_attrib::id(4)
            );
            assert_eq2!(
                stylesheet.find_style_by_id(5).unwrap().id,
                tui_style_attrib::id(5)
            );
            assert_eq2!(
                stylesheet.find_style_by_id(6).unwrap().id,
                tui_style_attrib::id(6)
            );
            assert!(stylesheet.find_style_by_id(7).is_none());

            let result = stylesheet.find_styles_by_ids(&[1, 2]);
            assert_eq2!(result.as_ref().unwrap().len(), 2);
            assert_eq2!(result.as_ref().unwrap()[0].id, tui_style_attrib::id(1));
            assert_eq2!(result.as_ref().unwrap()[1].id, tui_style_attrib::id(2));
            assert_eq2!(stylesheet.find_styles_by_ids(&[13, 41]), None);
            let style7 = make_a_style(7);
            let result = stylesheet.add_style(style7);
            result.unwrap();
            assert_eq2!(stylesheet.styles.len(), 7);
            assert_eq2!(
                stylesheet.find_style_by_id(7).unwrap().id,
                tui_style_attrib::id(7)
            );
        });
    }

    /// Helper function.
    fn make_a_style(id: u8) -> TuiStyle {
        TuiStyle {
            id: Some(tui_style_attrib::Id(id)),
            dim: Some(tui_style_attrib::Dim),
            bold: Some(tui_style_attrib::Bold),
            color_fg: tui_color!(0, 0, 0).into(),
            color_bg: tui_color!(0, 0, 0).into(),
            ..TuiStyle::default()
        }
    }
}

#[cfg(test)]
mod tests_language_mapping {
    use super::*;
    use crate::get_cached_syntax_set;

    #[test]
    fn test_map_language_to_extension() {
        // Test Rust mapping
        assert_eq!(map_language_to_extension("rust"), "rs");
        assert_eq!(map_language_to_extension("rs"), "rs");

        // Test JavaScript mapping
        assert_eq!(map_language_to_extension("javascript"), "js");
        assert_eq!(map_language_to_extension("js"), "js");

        // Test TypeScript mapping (falls back to JS)
        assert_eq!(map_language_to_extension("typescript"), "js");
        assert_eq!(map_language_to_extension("ts"), "js");

        // Test Python mapping
        assert_eq!(map_language_to_extension("python"), "py");
        assert_eq!(map_language_to_extension("py"), "py");

        // Test Go mapping
        assert_eq!(map_language_to_extension("golang"), "go");
        assert_eq!(map_language_to_extension("go"), "go");

        // Test shell/bash mapping
        assert_eq!(map_language_to_extension("shell"), "sh");
        assert_eq!(map_language_to_extension("bash"), "sh");
        assert_eq!(map_language_to_extension("sh"), "sh");

        // Test C# mapping
        assert_eq!(map_language_to_extension("csharp"), "cs");
        assert_eq!(map_language_to_extension("c#"), "cs");
        assert_eq!(map_language_to_extension("cs"), "cs");

        // Test fallback mappings
        assert_eq!(map_language_to_extension("toml"), "rs"); // Falls back to Rust
        assert_eq!(map_language_to_extension("scss"), "css"); // Falls back to CSS
        assert_eq!(map_language_to_extension("sass"), "css"); // Falls back to CSS
        assert_eq!(map_language_to_extension("kotlin"), "java"); // Falls back to Java
        assert_eq!(map_language_to_extension("swift"), "rs"); // Falls back to Rust
        assert_eq!(map_language_to_extension("dockerfile"), "sh"); // Falls back to shell

        // Test unknown language (should return as-is)
        assert_eq!(map_language_to_extension("unknown"), "unknown");
        assert_eq!(map_language_to_extension("xyz"), "xyz");
    }

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

        // Test that both "rust" and "rs" resolve to the same syntax
        let rust_syntax = try_get_syntax_ref(syntax_set, "rust");
        let rs_syntax = try_get_syntax_ref(syntax_set, "rs");

        assert!(rust_syntax.is_some());
        assert!(rs_syntax.is_some());
        assert_eq!(rust_syntax.unwrap().name, rs_syntax.unwrap().name);

        // Test other common language mappings
        assert!(try_get_syntax_ref(syntax_set, "javascript").is_some());
        assert!(try_get_syntax_ref(syntax_set, "python").is_some());
        assert!(try_get_syntax_ref(syntax_set, "golang").is_some());

        // Test that unknown languages return None
        assert!(try_get_syntax_ref(syntax_set, "unknown_language").is_none());
    }

    #[test]
    fn test_available_syntaxes_and_mappings() {
        let syntax_set = syntect::parsing::SyntaxSet::load_defaults_newlines();

        println!("Available syntaxes and their extensions:");
        for syntax in syntax_set.syntaxes().iter() {
            println!(
                "Syntax: {} -> Extensions: {:?}",
                syntax.name, syntax.file_extensions
            );
        }

        // Test our mappings
        let test_mappings = [
            ("rust", "rs"),
            ("javascript", "js"),
            ("ts", "js"),         // Falls back to JS
            ("typescript", "js"), // Falls back to JS
            ("python", "py"),
            ("golang", "go"),
            ("go", "go"),
            ("csharp", "cs"),
            ("c#", "cs"),
            ("cpp", "cpp"),
            ("c++", "cpp"),
            ("objective-c", "m"),
            ("objc", "m"),
            ("shell", "sh"),
            ("bash", "sh"),
            ("sh", "sh"),
            ("yaml", "yaml"),
            ("yml", "yaml"),
            ("toml", "rs"), // Falls back to Rust
            ("json", "json"),
            ("html", "html"),
            ("css", "css"),
            ("scss", "css"), // Falls back to CSS
            ("sass", "css"), // Falls back to CSS
            ("xml", "xml"),
            ("markdown", "md"),
            ("md", "md"),
            ("ruby", "rb"),
            ("rb", "rb"),
            ("java", "java"),
            ("kotlin", "java"), // Falls back to Java
            ("kt", "java"),     // Falls back to Java
            ("swift", "rs"),    // Falls back to Rust
            ("r", "r"),
            ("sql", "sql"),
            ("dockerfile", "sh"), // Falls back to shell
            ("makefile", "makefile"),
        ];

        println!("\nTesting our mappings:");
        for (lang, expected_ext) in test_mappings.iter() {
            let mapped_ext = map_language_to_extension(lang);
            assert_eq!(mapped_ext, *expected_ext, "Mapping failed for {}", lang);

            let syntax_ref = syntax_set.find_syntax_by_extension(expected_ext);
            println!(
                "{} -> {} : {}",
                lang,
                expected_ext,
                if syntax_ref.is_some() { "" } else { "" }
            );
        }
    }
}