omnidiff 0.2.0

Fast, robust, syntax-aware code diffing using tree-sitter ASTs
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
/*  This file is part of the OmniDiff code diffing tool.
 *
 *  Copyright (C) 2026 Marko Ivankovic
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published
 *  by the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
//! What the viewer is sent for a pair: the computed diff, and a re-highlighting of it.
//!
//! **Every column here is a UTF-16 code unit, not a byte.** The rest of the crate works in bytes,
//! as tree-sitter does; a browser indexes strings in UTF-16, so the conversion happens once, here.
//! `tui::json_output` stays in bytes on purpose: Neovim consumes bytes.
//!
//! Highlighting comes as per-line foreground colour spans from the TUI's own syntect setup, so the
//! browser paints what the terminal would.

use serde::Serialize;
use std::path::Path;

use crate::code::{Language, language::language_for_path_and_content};
use crate::diff::text::{
    ChangeCounts, DiffSummary, RangeMatch, RenderOptions, TextOperation, change_counts,
    ranges_for_options, summarize_diff_with_comment_check,
};
use crate::diff::text_range::{TextRange, floor_char_boundary};
use crate::tui::actions::DiffSessionData;
use crate::tui::widgets::code_viewer::{language_to_syntect, syntax_set, theme_set};

/// syntect's own fallback, the one `tui::widgets::code_viewer::CodeViewerWidget::get_theme` also
/// reaches for when the configured name is unknown.
pub const DEFAULT_SYNTAX_THEME: &str = "base16-ocean.dark";

/// The UTF-16 index of `byte_column` in `line`. Past the end clamps to the line's length; inside a
/// multi-byte character rounds down to its start, as every renderer in this crate does.
fn utf16_column(line: &str, byte_column: usize) -> usize {
    let end = floor_char_boundary(line, byte_column.min(line.len()));
    line[..end].encode_utf16().count()
}

fn convert_range(range: &TextRange, lines: &[&str]) -> [usize; 4] {
    let column = |row: usize, byte_column: usize| match lines.get(row) {
        Some(line) => utf16_column(line, byte_column),
        None => 0,
    };
    [
        range.start_row,
        column(range.start_row, range.start_column),
        range.end_row,
        column(range.end_row, range.end_column),
    ]
}

fn operation_name(operation: &TextOperation) -> &'static str {
    match operation {
        TextOperation::Insert => "insert",
        TextOperation::Delete => "delete",
        TextOperation::Update => "update",
        TextOperation::Move => "move",
        TextOperation::Identical => "identical",
        TextOperation::NotYetSet => "unset",
    }
}

/// One `RangeMatch`, both ranges as `[start_row, start_column, end_row, end_column]`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct RangePayload {
    pub op: &'static str,
    pub source: [usize; 4],
    pub destination: [usize; 4],
}

/// `[start, end, "#rrggbb"]` - a run of one foreground colour within a line.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SpanPayload(pub usize, pub usize, pub String);

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SidePayload {
    pub path: String,
    /// The basename, what the panel title shows.
    pub name: String,
    /// The detected language's name, or `Plain Text` - the TUI's `language_name`.
    pub language: String,
    pub lines: Vec<String>,
    /// Already filtered through `ranges_for_options`, so the page paints them as they are.
    pub ranges: Vec<RangePayload>,
    /// One entry per line.
    pub spans: Vec<Vec<SpanPayload>>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub struct CountsPayload {
    pub insertions: usize,
    pub deletions: usize,
    pub updates: usize,
    pub moves: usize,
}

impl From<ChangeCounts> for CountsPayload {
    fn from(counts: ChangeCounts) -> Self {
        Self {
            insertions: counts.insertions,
            deletions: counts.deletions,
            updates: counts.updates,
            moves: counts.moves,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SummaryPayload {
    /// The same snake_case tag `tui::json_output` uses for its `summary` field.
    pub kind: &'static str,
    pub label: &'static str,
}

fn summary_payload(summary: DiffSummary) -> SummaryPayload {
    let kind = match summary {
        DiffSummary::NoChanges => "no_changes",
        DiffSummary::NewFile => "new_file",
        DiffSummary::DeletedFile => "deleted_file",
        DiffSummary::WhitespaceOnly => "whitespace_only",
        DiffSummary::CommentOnly => "comment_only",
        DiffSummary::RefactorMovedOnly => "refactor_moved_only",
    };
    SummaryPayload {
        kind,
        label: summary.label(),
    }
}

/// The syntect theme a highlighting was done with, and its page colours: a light theme's code
/// is unreadable on a dark page, so the page takes its base colours from the theme it shows.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SyntaxPayload {
    pub theme: String,
    pub background: String,
    pub foreground: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct DiffPayload {
    pub before: SidePayload,
    pub after: SidePayload,
    pub summary: Option<SummaryPayload>,
    pub change_counts: CountsPayload,
    pub plain_text_fallback: bool,
    pub large_residual: bool,
    /// The options the ranges were filtered with, so the page's badge and `M` panel agree with
    /// what it is looking at.
    pub render_options: RenderOptions,
    pub syntax: SyntaxPayload,
}

fn hex(color: syntect::highlighting::Color) -> String {
    format!("#{:02x}{:02x}{:02x}", color.r, color.g, color.b)
}

fn resolve_theme(name: Option<&str>) -> (String, &'static syntect::highlighting::Theme) {
    let themes = &theme_set().themes;
    let chosen = name
        .filter(|name| themes.contains_key(*name))
        .unwrap_or(DEFAULT_SYNTAX_THEME);
    let theme = themes
        .get(chosen)
        .expect("base16-ocean.dark is one of syntect's own bundled default themes");
    (chosen.to_string(), theme)
}

/// The page colours of a theme; a theme without them falls back to dark-on-light.
fn syntax_payload(name: Option<&str>) -> SyntaxPayload {
    let (theme, resolved) = resolve_theme(name);
    SyntaxPayload {
        theme,
        background: resolved
            .settings
            .background
            .map(hex)
            .unwrap_or_else(|| "#1e1e1e".to_string()),
        foreground: resolved
            .settings
            .foreground
            .map(hex)
            .unwrap_or_else(|| "#d4d4d4".to_string()),
    }
}

/// Highlights `lines` as `CodeViewerWidget::highlight_lines` does, one span list per line in UTF-16
/// columns, with adjacent same-colour runs merged. A language syntect lacks yields empty lists.
fn highlight(
    lines: &[&str],
    language: Option<Language>,
    theme_name: Option<&str>,
) -> Vec<Vec<SpanPayload>> {
    let Some(syntax) = language
        .and_then(|language| language_to_syntect(&language))
        .and_then(|name| syntax_set().find_syntax_by_name(name))
    else {
        return vec![Vec::new(); lines.len()];
    };
    let (_, theme) = resolve_theme(theme_name);
    let mut highlighter = syntect::easy::HighlightLines::new(syntax, theme);
    lines
        .iter()
        .map(|&line| {
            let Ok(regions) = highlighter.highlight_line(line, syntax_set()) else {
                return Vec::new();
            };
            let mut spans: Vec<SpanPayload> = Vec::new();
            let mut byte = 0;
            for (style, text) in regions {
                let start = utf16_column(line, byte);
                byte += text.len();
                let end = utf16_column(line, byte);
                if start == end {
                    continue;
                }
                let color = hex(style.foreground);
                match spans.last_mut() {
                    Some(last) if last.1 == start && last.2 == color => last.1 = end,
                    _ => spans.push(SpanPayload(start, end, color)),
                }
            }
            spans
        })
        .collect()
}

/// The language a side is shown as. `/dev/null` takes the other side's, as
/// `tui::app::substitute_missing_language` parses it.
fn side_language(path: &Path, contents: &str, other: Option<Language>) -> Option<Language> {
    match language_for_path_and_content(path, contents) {
        None if contents.is_empty() => other,
        detected => detected,
    }
}

fn language_name(language: Option<Language>) -> String {
    language
        .map(crate::code::language::human_name)
        .unwrap_or_else(|| "Plain Text".to_string())
}

fn side_payload(
    path: &Path,
    contents: &str,
    full_ranges: &[RangeMatch],
    language: Option<Language>,
    options: RenderOptions,
    syntax_theme: Option<&str>,
) -> SidePayload {
    let lines: Vec<&str> = contents.lines().collect();
    let ranges = ranges_for_options(full_ranges, contents, options)
        .iter()
        .map(|range_match| RangePayload {
            op: operation_name(&range_match.operation),
            source: convert_range(&range_match.source, &lines),
            destination: convert_range(&range_match.destination, &lines),
        })
        .collect();
    SidePayload {
        path: path.display().to_string(),
        name: path
            .file_name()
            .map(|name| name.to_string_lossy().into_owned())
            .unwrap_or_else(|| "Untitled".to_string()),
        language: language_name(language),
        spans: highlight(&lines, language, syntax_theme),
        lines: lines.iter().map(|line| line.to_string()).collect(),
        ranges,
    }
}

/// Both sides' languages, each falling back to the other's for an empty `/dev/null` side.
fn pair_languages(data: &DiffSessionData) -> (Option<Language>, Option<Language>) {
    let before_detected = language_for_path_and_content(&data.before_path, &data.before_contents);
    let after_detected = language_for_path_and_content(&data.after_path, &data.after_contents);
    (
        side_language(&data.before_path, &data.before_contents, after_detected),
        side_language(&data.after_path, &data.after_contents, before_detected),
    )
}

/// The whole diff as the page wants it. `data` holds the *unfiltered* ranges, as
/// `DiffViewer::load_diff` keeps them; the summary and the change counts read those (as
/// `App::handle_diff_ready` does), while the ranges sent are filtered through `options`.
pub fn diff_payload(
    data: &DiffSessionData,
    large_residual: bool,
    options: RenderOptions,
    syntax_theme: Option<&str>,
) -> DiffPayload {
    let (before_language, after_language) = pair_languages(data);
    let summary = summarize_diff_with_comment_check(
        &data.before_contents,
        &data.after_contents,
        &data.before_ranges,
        &data.after_ranges,
        data.comment_only,
    )
    .map(summary_payload);
    let counts = change_counts(
        &data.before_contents,
        &data.after_contents,
        &data.before_ranges,
        &data.after_ranges,
    );
    DiffPayload {
        before: side_payload(
            &data.before_path,
            &data.before_contents,
            &data.before_ranges,
            before_language,
            options,
            syntax_theme,
        ),
        after: side_payload(
            &data.after_path,
            &data.after_contents,
            &data.after_ranges,
            after_language,
            options,
            syntax_theme,
        ),
        summary,
        change_counts: counts.into(),
        plain_text_fallback: data.plain_text_fallback,
        large_residual,
        render_options: options,
        syntax: syntax_payload(syntax_theme),
    }
}

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

    #[test]
    fn utf16_columns_count_code_units_not_bytes() {
        // 'é' is two bytes, one code unit; '😀' is four bytes, two code units.
        let line = "aé😀b";
        assert_eq!(utf16_column(line, 0), 0);
        assert_eq!(utf16_column(line, 1), 1);
        assert_eq!(utf16_column(line, 3), 2, "after the two-byte é");
        assert_eq!(
            utf16_column(line, 7),
            4,
            "after the four-byte emoji: a surrogate pair"
        );
        assert_eq!(utf16_column(line, 8), 5);
        assert_eq!(utf16_column(line, 99), 5, "past the end clamps");
        assert_eq!(
            utf16_column(line, 2),
            1,
            "inside é rounds down to its start"
        );
    }

    #[test]
    fn ranges_convert_each_end_against_its_own_row() {
        let lines = ["é=1", "x"];
        let range = TextRange::new(0, 3, 1, 1);
        assert_eq!(convert_range(&range, &lines), [0, 2, 1, 1]);
        let to_eof = TextRange::new(0, 0, 2, 0);
        assert_eq!(
            convert_range(&to_eof, &lines),
            [0, 0, 2, 0],
            "a row past the end stays 0"
        );
    }

    #[test]
    fn highlighting_covers_a_rust_line_in_utf16_columns_with_merged_runs() {
        let spans = highlight(&["let é = 1;"], Some(Language::Rust), None);
        assert_eq!(spans.len(), 1);
        let line = &spans[0];
        assert!(
            !line.is_empty(),
            "rust has a syntect definition, so something is coloured"
        );
        assert_eq!(line[0].0, 0);
        assert_eq!(line.last().unwrap().1, "let é = 1;".encode_utf16().count());
        for pair in line.windows(2) {
            assert_eq!(pair[0].1, pair[1].0, "spans tile the line without gaps");
            assert_ne!(
                pair[0].2, pair[1].2,
                "adjacent same-colour runs were merged"
            );
        }
    }

    #[test]
    fn a_language_without_a_syntax_yields_empty_spans_not_an_error() {
        let spans = highlight(&["a", "b"], None, None);
        assert_eq!(spans, vec![Vec::new(), Vec::new()]);
    }

    #[test]
    fn an_unknown_syntax_theme_falls_back_to_syntects_default() {
        assert_eq!(
            syntax_payload(Some("no-such-theme")).theme,
            DEFAULT_SYNTAX_THEME
        );
        assert_eq!(
            syntax_payload(Some("Solarized (light)")).theme,
            "Solarized (light)"
        );
        let payload = syntax_payload(None);
        assert!(payload.background.starts_with('#') && payload.background.len() == 7);
    }

    #[test]
    fn a_dev_null_side_reports_the_other_sides_language() {
        let dev_null = PathBuf::from("/dev/null");
        assert_eq!(
            side_language(&dev_null, "", Some(Language::Rust)),
            Some(Language::Rust)
        );
        assert_eq!(
            side_language(&dev_null, "not empty", Some(Language::Rust)),
            None,
            "only an empty side borrows a language"
        );
    }

    fn sample_data() -> DiffSessionData {
        let before = "fn main() {\n    let x = 1;\n}\n".to_string();
        let after = "fn main() {\n    let y = 1;\n}\n".to_string();
        let before_ranges = vec![
            RangeMatch {
                source: TextRange::new(1, 8, 1, 9),
                destination: TextRange::new(1, 8, 1, 9),
                operation: TextOperation::Update,
            },
            RangeMatch {
                source: TextRange::new(0, 0, 0, 2),
                destination: TextRange::new(0, 0, 0, 2),
                operation: TextOperation::Identical,
            },
        ];
        let after_ranges = vec![RangeMatch {
            source: TextRange::new(1, 8, 1, 9),
            destination: TextRange::new(1, 8, 1, 9),
            operation: TextOperation::Update,
        }];
        DiffSessionData {
            before_path: PathBuf::from("a/before.rs"),
            after_path: PathBuf::from("b/after.rs"),
            before_contents: before,
            after_contents: after,
            before_ranges,
            after_ranges,
            comment_only: false,
            plain_text_fallback: false,
        }
    }

    #[test]
    fn the_diff_payload_carries_lines_ranges_counts_and_the_syntax_theme() {
        let payload = diff_payload(&sample_data(), false, RenderOptions::FULL, None);
        assert_eq!(payload.before.name, "before.rs");
        assert_eq!(payload.before.language, "Rust");
        assert_eq!(payload.before.lines.len(), 3);
        assert_eq!(payload.before.spans.len(), 3, "one span list per line");
        assert!(payload.before.ranges.iter().any(|r| r.op == "identical"));
        assert!(payload.before.ranges.iter().any(|r| r.op == "update"));
        assert_eq!(payload.change_counts.updates, 1);
        assert_eq!(payload.summary, None, "an ordinary edit has no summary");
        assert_eq!(payload.syntax.theme, DEFAULT_SYNTAX_THEME);
        assert_eq!(payload.render_options, RenderOptions::FULL);
    }

    #[test]
    fn identical_files_summarize_as_no_changes() {
        let mut data = sample_data();
        data.after_contents = data.before_contents.clone();
        data.before_ranges.clear();
        data.after_ranges.clear();
        let payload = diff_payload(&data, false, RenderOptions::FULL, None);
        let summary = payload
            .summary
            .expect("identical content is worth a summary");
        assert_eq!(summary.kind, "no_changes");
        assert_eq!(summary.label, DiffSummary::NoChanges.label());
    }

    #[test]
    fn serialized_spans_are_compact_triples() {
        let json = serde_json::to_string(&SpanPayload(1, 4, "#aabbcc".to_string())).unwrap();
        assert_eq!(json, r##"[1,4,"#aabbcc"]"##);
    }
}