tuicr 0.10.0

Review AI-generated diffs like a GitHub pull request, right from your terminal.
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
use ratatui::style::{Color, Modifier, Style};
use std::path::Path;
use two_face::theme::EmbeddedThemeName;

use crate::model::diff_types::LineOrigin;

/// A single line of highlighted spans (style + text pairs).
type HighlightedSpans = Vec<(Style, String)>;

/// Per-line highlight results for a file: `Some` if the line was highlighted, `None` on failure.
type HighlightedLines = Vec<Option<HighlightedSpans>>;

/// Helper to highlight lines of code from a diff
pub struct SyntaxHighlighter {
    pub syntax_set: syntect::parsing::SyntaxSet,
    pub theme: syntect::highlighting::Theme,
    /// Background color for added lines
    pub add_bg: Color,
    /// Background color for deleted lines
    pub del_bg: Color,
}

pub(crate) struct DiffHighlightSequences {
    pub old_lines: Vec<String>,
    pub new_lines: Vec<String>,
    pub old_line_indices: Vec<Option<usize>>,
    pub new_line_indices: Vec<Option<usize>>,
}

impl Default for SyntaxHighlighter {
    fn default() -> Self {
        Self::new(
            EmbeddedThemeName::Base16EightiesDark,
            Color::Rgb(0, 35, 12),
            Color::Rgb(45, 0, 0),
        )
    }
}

impl SyntaxHighlighter {
    /// Create a new syntax highlighter with the given theme and diff background colors
    pub fn new(theme_name: EmbeddedThemeName, add_bg: Color, del_bg: Color) -> Self {
        let syntax_set = two_face::syntax::extra_newlines();
        let theme_set = two_face::theme::extra();
        let theme = theme_set[theme_name].clone();

        Self {
            syntax_set,
            theme,
            add_bg,
            del_bg,
        }
    }

    /// Highlight all lines in a file's content.
    ///
    /// Returns `None` when no syntax can be resolved for the file (by path or shebang).
    /// Otherwise returns one entry per input line:
    /// - `Some(spans)` if that line was highlighted successfully (including empty spans)
    /// - `None` if highlighting failed for that specific line
    pub fn highlight_file_lines(
        &self,
        file_path: &Path,
        lines: &[String],
    ) -> Option<HighlightedLines> {
        use syntect::easy::HighlightLines;

        // Get syntax definition
        let syntax = self.get_syntax(file_path).or_else(|| {
            lines
                .first()
                .and_then(|line| self.syntax_set.find_syntax_by_first_line(line))
        })?;

        // Create highlighter
        let mut highlighter = HighlightLines::new(syntax, &self.theme);

        Some(Self::collect_line_highlights(lines, |line| {
            // Highlight failures are scoped to the single line; other lines still keep highlighting.
            highlighter
                .highlight_line(&format!("{}\n", line), &self.syntax_set)
                .ok()
                .map(|ranges| {
                    let mut spans: Vec<(Style, String)> = ranges
                        .into_iter()
                        .map(|(style, text)| {
                            (Self::syntect_to_ratatui_style(style), text.to_string())
                        })
                        .collect();
                    // Strip trailing \n that syntect includes from the input.
                    // Leaving it causes ratatui to allocate an extra buffer cell,
                    // misaligning side-by-side diff columns on short (padded) lines.
                    if let Some(last) = spans.last_mut()
                        && last.1.ends_with('\n')
                    {
                        last.1.truncate(last.1.len() - 1);
                        if last.1.is_empty() {
                            spans.pop();
                        }
                    }
                    spans
                })
        }))
    }

    fn collect_line_highlights<F>(lines: &[String], mut highlight_line: F) -> HighlightedLines
    where
        F: FnMut(&str) -> Option<HighlightedSpans>,
    {
        let mut result = Vec::with_capacity(lines.len());
        for line in lines {
            result.push(highlight_line(line));
        }
        result
    }

    fn highlighted_line_at(
        highlighted_lines: Option<&[Option<HighlightedSpans>]>,
        line_idx: Option<usize>,
    ) -> Option<HighlightedSpans> {
        line_idx
            .and_then(|idx| highlighted_lines.and_then(|all| all.get(idx)))
            .and_then(|line_highlight| line_highlight.as_ref().cloned())
    }

    pub(crate) fn split_diff_lines_for_highlighting(
        line_contents: &[String],
        line_origins: &[LineOrigin],
    ) -> DiffHighlightSequences {
        debug_assert_eq!(line_contents.len(), line_origins.len());

        let mut old_lines = Vec::new();
        let mut new_lines = Vec::new();
        let mut old_line_indices = Vec::with_capacity(line_origins.len());
        let mut new_line_indices = Vec::with_capacity(line_origins.len());

        for (content, origin) in line_contents.iter().zip(line_origins.iter()) {
            match origin {
                LineOrigin::Context => {
                    let old_idx = old_lines.len();
                    old_lines.push(content.clone());
                    old_line_indices.push(Some(old_idx));

                    let new_idx = new_lines.len();
                    new_lines.push(content.clone());
                    new_line_indices.push(Some(new_idx));
                }
                LineOrigin::Addition => {
                    let new_idx = new_lines.len();
                    new_lines.push(content.clone());
                    old_line_indices.push(None);
                    new_line_indices.push(Some(new_idx));
                }
                LineOrigin::Deletion => {
                    let old_idx = old_lines.len();
                    old_lines.push(content.clone());
                    old_line_indices.push(Some(old_idx));
                    new_line_indices.push(None);
                }
            }
        }

        DiffHighlightSequences {
            old_lines,
            new_lines,
            old_line_indices,
            new_line_indices,
        }
    }

    pub(crate) fn highlighted_line_for_diff_with_background(
        &self,
        old_highlighted_lines: Option<&[Option<HighlightedSpans>]>,
        new_highlighted_lines: Option<&[Option<HighlightedSpans>]>,
        old_line_idx: Option<usize>,
        new_line_idx: Option<usize>,
        origin: LineOrigin,
    ) -> Option<HighlightedSpans> {
        let spans = match origin {
            LineOrigin::Addition => Self::highlighted_line_at(new_highlighted_lines, new_line_idx),
            LineOrigin::Deletion => Self::highlighted_line_at(old_highlighted_lines, old_line_idx),
            LineOrigin::Context => Self::highlighted_line_at(new_highlighted_lines, new_line_idx),
        }?;

        Some(self.apply_diff_background(spans, origin))
    }

    fn syntect_to_ratatui_style(style: syntect::highlighting::Style) -> Style {
        let fg_color = Color::Rgb(style.foreground.r, style.foreground.g, style.foreground.b);
        let mut ratatui_style = Style::default().fg(fg_color);

        if style
            .font_style
            .contains(syntect::highlighting::FontStyle::BOLD)
        {
            ratatui_style = ratatui_style.add_modifier(Modifier::BOLD);
        }
        if style
            .font_style
            .contains(syntect::highlighting::FontStyle::ITALIC)
        {
            ratatui_style = ratatui_style.add_modifier(Modifier::ITALIC);
        }
        if style
            .font_style
            .contains(syntect::highlighting::FontStyle::UNDERLINE)
        {
            ratatui_style = ratatui_style.add_modifier(Modifier::UNDERLINED);
        }

        ratatui_style
    }

    /// Map extensions not in two-face's syntax set to a known equivalent.
    fn fallback_extension(ext: &str) -> Option<&'static str> {
        match ext {
            "jsx" | "mjs" | "cjs" => Some("js"),
            "hbs" | "handlebars" | "mustache" | "ejs" | "pug" | "jade" | "njk" => Some("html"),
            "mdx" => Some("md"),
            "jsonc" | "json5" | "prisma" => Some("json"),
            "heex" => Some("rb"),
            _ => None,
        }
    }

    /// Map extension-less filenames to a known syntax extension.
    fn fallback_filename(name: &str) -> Option<&'static str> {
        match name {
            "Containerfile" => Some("sh"),
            "Justfile" | "justfile" => Some("sh"),
            _ => None,
        }
    }

    /// Resolve syntax from a file path using this lookup order:
    /// extension -> lowercase extension (when different) -> fallback extension ->
    /// filename token -> filename name -> fallback filename.
    fn get_syntax(&self, file_path: &Path) -> Option<&syntect::parsing::SyntaxReference> {
        // Try by extension first
        if let Some(ext) = file_path.extension().and_then(|e| e.to_str()) {
            if let Some(syntax) = self.syntax_set.find_syntax_by_extension(ext) {
                return Some(syntax);
            }

            let normalized = ext.to_ascii_lowercase();
            if normalized != ext
                && let Some(syntax) = self.syntax_set.find_syntax_by_extension(&normalized)
            {
                return Some(syntax);
            }

            // Try fallback mapping for extensions not in syntect's defaults
            if let Some(fallback) = Self::fallback_extension(&normalized)
                && let Some(syntax) = self.syntax_set.find_syntax_by_extension(fallback)
            {
                return Some(syntax);
            }
        }

        // Try token/name matches for extension-less files (e.g. Makefile, BUILD).
        if let Some(filename) = file_path.file_name().and_then(|f| f.to_str()) {
            if let Some(syntax) = self.syntax_set.find_syntax_by_token(filename) {
                return Some(syntax);
            }

            if let Some(syntax) = self.syntax_set.find_syntax_by_name(filename) {
                return Some(syntax);
            }

            if let Some(fallback) = Self::fallback_filename(filename)
                && let Some(syntax) = self.syntax_set.find_syntax_by_extension(fallback)
            {
                return Some(syntax);
            }
        }

        None
    }

    /// Apply diff background colors to highlighted spans based on line origin
    pub fn apply_diff_background(
        &self,
        spans: Vec<(Style, String)>,
        origin: LineOrigin,
    ) -> Vec<(Style, String)> {
        let bg_color = match origin {
            LineOrigin::Addition => self.add_bg,
            LineOrigin::Deletion => self.del_bg,
            LineOrigin::Context => return spans, // No background for context
        };

        spans
            .into_iter()
            .map(|(style, text)| (style.bg(bg_color), text))
            .collect()
    }
}

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

    #[test]
    fn should_find_syntax_for_uppercase_extension() {
        let highlighter = SyntaxHighlighter::default();
        let syntax = highlighter.get_syntax(Path::new("SRC/MAIN.RS"));
        assert!(syntax.is_some());
    }

    #[test]
    fn should_find_syntax_for_build_filename_token() {
        let highlighter = SyntaxHighlighter::default();
        let syntax = highlighter.get_syntax(Path::new("BUILD"));
        assert!(syntax.is_some());
    }

    #[test]
    fn should_highlight_each_line_independently() {
        let highlighter = SyntaxHighlighter::default();
        let lines = vec![
            "fn main() {".to_string(),
            "    let x = 42;".to_string(),
            "}".to_string(),
        ];
        let highlighted = highlighter.highlight_file_lines(Path::new("main.rs"), &lines);

        assert!(highlighted.is_some());
        let highlighted = highlighted.unwrap();
        assert_eq!(highlighted.len(), lines.len());
        assert!(highlighted.iter().all(|line| line.is_some()));
    }

    #[test]
    fn should_keep_file_highlighting_when_one_line_fails() {
        let lines = vec!["first".to_string(), "bad".to_string(), "third".to_string()];
        let highlighted = SyntaxHighlighter::collect_line_highlights(&lines, |line| {
            if line == "bad" {
                None
            } else {
                Some(vec![(Style::default(), line.to_string())])
            }
        });

        assert_eq!(highlighted.len(), lines.len());
        assert!(highlighted[0].is_some());
        assert!(highlighted[1].is_none());
        assert!(highlighted[2].is_some());
    }

    #[test]
    fn should_find_syntax_for_typescript() {
        let highlighter = SyntaxHighlighter::default();
        for ext in &["ts", "tsx", "mts", "cts", "jsx", "mjs", "cjs"] {
            let path = format!("file.{ext}");
            assert!(
                highlighter.get_syntax(Path::new(&path)).is_some(),
                "should find syntax for .{ext}"
            );
        }
    }

    #[test]
    fn should_find_syntax_for_fallback_extensions() {
        let highlighter = SyntaxHighlighter::default();
        let extensions = [
            "jsx", "mjs", "cjs", "hbs", "mustache", "ejs", "pug", "njk", "mdx", "jsonc", "json5",
            "prisma", "heex",
        ];
        for ext in &extensions {
            let path = format!("file.{ext}");
            assert!(
                highlighter.get_syntax(Path::new(&path)).is_some(),
                "should find syntax for .{ext}"
            );
        }
    }

    #[test]
    fn should_find_syntax_for_fallback_filenames() {
        let highlighter = SyntaxHighlighter::default();
        for name in &["Containerfile", "Justfile", "justfile"] {
            assert!(
                highlighter.get_syntax(Path::new(name)).is_some(),
                "should find syntax for {name}"
            );
        }
    }

    #[test]
    fn highlighted_spans_should_have_color() {
        let highlighter = SyntaxHighlighter::default();
        let lines = vec![
            "fn main() {".to_string(),
            "    let x = 42;".to_string(),
            "}".to_string(),
        ];
        let highlighted = highlighter
            .highlight_file_lines(Path::new("test.rs"), &lines)
            .unwrap();
        for (i, line) in highlighted.iter().enumerate() {
            let spans = line
                .as_ref()
                .unwrap_or_else(|| panic!("line {i} should be Some"));
            assert!(!spans.is_empty(), "line {i} should have spans");
            // At least one span should have a non-default foreground color
            let has_fg = spans.iter().any(|(style, _)| style.fg.is_some());
            assert!(has_fg, "line {i} should have foreground color: {spans:?}");
        }
    }

    #[test]
    fn should_detect_syntax_from_shebang_when_extensionless() {
        let highlighter = SyntaxHighlighter::default();
        let lines = vec![
            "#!/usr/bin/env python".to_string(),
            "print('hello')".to_string(),
        ];

        let highlighted = highlighter.highlight_file_lines(Path::new("script"), &lines);
        assert!(highlighted.is_some());
        assert_eq!(highlighted.unwrap().len(), lines.len());
    }

    #[test]
    fn should_preserve_empty_line_highlight_results() {
        let lines = vec!["value".to_string(), "".to_string()];
        let highlighted = SyntaxHighlighter::collect_line_highlights(&lines, |line| {
            if line.is_empty() {
                Some(Vec::new())
            } else {
                Some(vec![(Style::default(), line.to_string())])
            }
        });

        assert!(matches!(highlighted[1], Some(ref spans) if spans.is_empty()));
    }

    #[test]
    fn should_not_use_weak_fallback_mappings() {
        for ext in &["toml", "hcl", "tf", "tfvars", "nix", "swift", "zig", "v"] {
            assert_eq!(SyntaxHighlighter::fallback_extension(ext), None);
        }
    }

    #[test]
    fn split_diff_lines_for_highlighting_should_build_old_and_new_sequences() {
        let contents = vec![
            "ctx".to_string(),
            "del".to_string(),
            "add".to_string(),
            "ctx2".to_string(),
        ];
        let origins = vec![
            LineOrigin::Context,
            LineOrigin::Deletion,
            LineOrigin::Addition,
            LineOrigin::Context,
        ];

        let seq = SyntaxHighlighter::split_diff_lines_for_highlighting(&contents, &origins);
        assert_eq!(seq.old_lines, vec!["ctx", "del", "ctx2"]);
        assert_eq!(seq.new_lines, vec!["ctx", "add", "ctx2"]);
        assert_eq!(seq.old_line_indices, vec![Some(0), Some(1), None, Some(2)]);
        assert_eq!(seq.new_line_indices, vec![Some(0), None, Some(1), Some(2)]);
    }

    #[test]
    fn highlighted_line_for_diff_with_background_should_handle_none_per_line() {
        let highlighter = SyntaxHighlighter::default();
        let old_lines = vec![None];
        let new_lines = vec![None];
        let highlighted = highlighter.highlighted_line_for_diff_with_background(
            Some(&old_lines),
            Some(&new_lines),
            Some(0),
            Some(0),
            LineOrigin::Addition,
        );
        assert!(highlighted.is_none());
    }

    #[test]
    fn highlighted_line_for_diff_with_background_should_apply_background_on_success() {
        let highlighter = SyntaxHighlighter::default();
        let old_lines = vec![Some(vec![(Style::default(), "old".to_string())])];
        let new_lines = vec![Some(vec![(Style::default(), "new".to_string())])];

        let deletion = highlighter.highlighted_line_for_diff_with_background(
            Some(&old_lines),
            Some(&new_lines),
            Some(0),
            Some(0),
            LineOrigin::Deletion,
        );
        let addition = highlighter.highlighted_line_for_diff_with_background(
            Some(&old_lines),
            Some(&new_lines),
            Some(0),
            Some(0),
            LineOrigin::Addition,
        );
        let context = highlighter.highlighted_line_for_diff_with_background(
            Some(&old_lines),
            Some(&new_lines),
            Some(0),
            Some(0),
            LineOrigin::Context,
        );

        let deletion = deletion.unwrap();
        assert_eq!(deletion.len(), 1);
        assert_eq!(deletion[0].0.bg, Some(highlighter.del_bg));
        assert_eq!(deletion[0].1, "old");

        let addition = addition.unwrap();
        assert_eq!(addition.len(), 1);
        assert_eq!(addition[0].0.bg, Some(highlighter.add_bg));
        assert_eq!(addition[0].1, "new");

        let context = context.unwrap();
        assert_eq!(context.len(), 1);
        assert_eq!(context[0].0.bg, None);
        assert_eq!(context[0].1, "new");
    }

    #[test]
    fn should_not_include_trailing_newline_in_highlighted_spans() {
        // given - syntect requires a trailing \n for highlight_line, but the
        // resulting spans must not include it. A leaked \n occupies an extra
        // buffer cell in ratatui, misaligning side-by-side diff columns on
        // short (padded) lines while truncated lines stay correct.
        let highlighter = SyntaxHighlighter::default();
        let lines = vec![
            "fn main() {".to_string(),
            "    let x = 42;".to_string(),
            "}".to_string(),
        ];

        // when
        let highlighted = highlighter
            .highlight_file_lines(Path::new("test.rs"), &lines)
            .unwrap();

        // then
        for (i, line) in highlighted.iter().enumerate() {
            let spans = line.as_ref().unwrap();
            let full_text: String = spans.iter().map(|(_, t)| t.as_str()).collect();
            assert!(
                !full_text.contains('\n'),
                "line {i} spans should not contain newline, got: {full_text:?}"
            );
        }
    }
}