diffler-core 0.18.0

Core review engine for diffler: diffs, sessions, comments, viewed marks
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
//! Char-precise intra-line change emphasis driven by an AST diff (syndiff),
//! the structural counterpart to the textual engine in [`crate::pairing`].
//! Only the byte ranges that differ structurally are emphasized, so a
//! reformatted or re-wrapped block highlights just the tokens that changed.

use std::ops::Range;

use syndiff::{SyntaxDiffOptions, build_tree, diff_trees};

use crate::model::{FileDiff, Hunk, LineKind};
use crate::syntax::registry::LanguageRegistry;
use crate::syntax::{MAX_PARSE_BYTES, line_bounds, parse, split_range_by_line};

/// Emphasis byte ranges per line (one inner vec per source line).
type LineEmphasis = Vec<Vec<Range<usize>>>;

/// Bounds the AST-diff graph search so a huge, heavily rewritten file cannot
/// stall the render thread; beyond it `diff_trees` returns `None` and the
/// caller falls back to the textual engine. Well above any normal diff.
const GRAPH_LIMIT: usize = 250_000;

impl LanguageRegistry {
    /// Per-line emphasis byte ranges for both sides, from an AST diff of the
    /// full old/new content. `None` (caller falls back to the textual engine)
    /// when the language is unsupported, content is too large, parsing fails,
    /// or the diff exceeds its graph budget.
    fn line_emphasis(
        &self,
        path: &str,
        old_src: &str,
        new_src: &str,
    ) -> Option<(LineEmphasis, LineEmphasis)> {
        if old_src.len() > MAX_PARSE_BYTES || new_src.len() > MAX_PARSE_BYTES {
            return None;
        }
        let entry = self.for_path(path)?;
        // markdown's block tree is coarse (a paragraph is one opaque node); the
        // textual word-diff emphasizes prose edits far better than an AST diff.
        if entry.name == "markdown" {
            return None;
        }
        let old_ts = parse(entry, old_src)?;
        let new_ts = parse(entry, new_src)?;
        let old_tree = build_tree(old_ts.walk(), old_src);
        let new_tree = build_tree(new_ts.walk(), new_src);
        let options = SyntaxDiffOptions {
            graph_limit: GRAPH_LIMIT,
        };
        let (old_ranges, new_ranges) = diff_trees(&old_tree, &new_tree, None, None, Some(options))?;
        Some((
            per_line_emphasis(old_src, &old_ranges),
            per_line_emphasis(new_src, &new_ranges),
        ))
    }

    /// Set char-precise emphasis on `file`'s diff lines from the AST diff.
    /// `mark_reformat_only` additionally flags a paired deleted/added line the
    /// AST diff found no structural difference on at all (a pure reformat)
    /// for the structural algorithm's dimmed rendering. Returns `false` when
    /// the syntactic engine is unavailable, so the caller can fall back to
    /// the textual engine (and structural mode silently reads as a plain
    /// histogram diff for that file).
    pub fn syntactic_emphasis(&self, file: &mut FileDiff, mark_reformat_only: bool) -> bool {
        let emphasis = match (file.old_text.as_deref(), file.new_text.as_deref()) {
            (Some(old), Some(new)) => self.line_emphasis(&file.path, old, new),
            _ => None,
        };
        let Some((old_emph, new_emph)) = emphasis else {
            return false;
        };
        let mark_reformat_only = mark_reformat_only
            && self
                .for_path(&file.path)
                .is_some_and(|entry| !entry.layout_significant);
        for hunk in &mut file.hunks {
            for line in &mut hunk.lines {
                let ranges = match (line.new_no, line.old_no) {
                    (Some(n), _) => new_emph.get(n as usize - 1),
                    (None, Some(o)) => old_emph.get(o as usize - 1),
                    _ => None,
                };
                line.emphasis =
                    classify_line(line.kind, &line.text, ranges.map_or(&[], Vec::as_slice));
            }
            refine_partial_changes(hunk);
            if mark_reformat_only {
                mark_reformat_pairs(hunk, &old_emph, &new_emph);
            }
        }
        true
    }
}

/// Flag a paired deleted/added line as `reformat_only` when the two differ in
/// whitespace alone and the AST diff found no token changed on either side,
/// which keeps a whitespace edit inside a string literal a real change.
fn mark_reformat_pairs(hunk: &mut Hunk, old_emph: &LineEmphasis, new_emph: &LineEmphasis) {
    let unchanged = |emph: &LineEmphasis, number: Option<u32>| {
        number
            .and_then(|n| n.checked_sub(1))
            .and_then(|i| emph.get(i as usize))
            .is_some_and(Vec::is_empty)
    };
    let squeezed = |text: &str| text.split_whitespace().collect::<String>();
    for (del_idx, add_idx) in crate::pairing::paired_run_indices(&hunk.lines) {
        let (Some(del), Some(add)) = (hunk.lines.get(del_idx), hunk.lines.get(add_idx)) else {
            continue;
        };
        if unchanged(old_emph, del.old_no)
            && unchanged(new_emph, add.new_no)
            && squeezed(&del.text) == squeezed(&add.text)
        {
            if let Some(line) = hunk.lines.get_mut(del_idx) {
                line.reformat_only = true;
            }
            if let Some(line) = hunk.lines.get_mut(add_idx) {
                line.reformat_only = true;
            }
        }
    }
}

/// Where the AST diff flagged a *partial* line change (some token ranges, not
/// the whole line and not a reformat), replace the coarse token ranges with a
/// word-level diff of the paired lines, so only the tokens that actually
/// changed are emphasized (an edit inside a string scalar shouldn't light up the
/// whole scalar). Emphasis means "differs from the homolog": a line with no
/// pair (wholly new or wholly gone) renders plain, keeping off the stray
/// fragments the AST diff leaves when it matches a token of new code against
/// something elsewhere in the old tree.
fn refine_partial_changes(hunk: &mut Hunk) {
    let pairs = crate::pairing::paired_run_indices(&hunk.lines);
    let paired: std::collections::HashSet<usize> =
        pairs.iter().flat_map(|&(d, a)| [d, a]).collect();
    for (index, line) in hunk.lines.iter_mut().enumerate() {
        if matches!(line.kind, LineKind::Deleted | LineKind::Added) && !paired.contains(&index) {
            line.emphasis = Vec::new();
        }
    }
    for (del_idx, add_idx) in pairs {
        let partial = hunk
            .lines
            .get(del_idx)
            .is_some_and(|l| !l.emphasis.is_empty())
            || hunk
                .lines
                .get(add_idx)
                .is_some_and(|l| !l.emphasis.is_empty());
        if !partial {
            continue;
        }
        let (Some(old), Some(new)) = (
            hunk.lines.get(del_idx).map(|l| l.text.clone()),
            hunk.lines.get(add_idx).map(|l| l.text.clone()),
        ) else {
            continue;
        };
        // the same pair gate as the textual engine, so a refinement that
        // comes back scattered or near-total drops to plain lines too
        let (old_emph, new_emph) = crate::pairing::gated_pair_emphasis(&old, &new);
        if let Some(line) = hunk.lines.get_mut(del_idx) {
            line.emphasis = old_emph;
        }
        if let Some(line) = hunk.lines.get_mut(add_idx) {
            line.emphasis = new_emph;
        }
    }
}

/// Map whole-file changed byte ranges to the raw per-line, within-line ranges.
fn per_line_emphasis(src: &str, ranges: &[Range<usize>]) -> LineEmphasis {
    let bounds = line_bounds(src);
    let starts: Vec<usize> = bounds.iter().map(|&(s, _)| s).collect();
    let mut out = vec![Vec::new(); bounds.len()];
    for r in ranges {
        split_range_by_line(&bounds, &starts, r, |li, rr| {
            if let Some(v) = out.get_mut(li) {
                v.push(rr);
            }
        });
    }
    out
}

/// Emphasis for an added/deleted `line` from its raw changed byte `ranges`.
/// Every changed line keeps its full +/- background; emphasis only marks
/// punctual edits: a line that changed mostly or entirely gets none, because
/// highlighting almost everything highlights nothing.
fn classify_line(kind: LineKind, text: &str, ranges: &[Range<usize>]) -> Vec<Range<usize>> {
    let _ = kind;
    let ranges = clamp(ranges, text.len());
    if ranges.is_empty() || !crate::pairing::emphasis_is_punctual(text, &ranges) {
        return Vec::new();
    }
    ranges
}

/// Clip ranges to the line's length and drop any that become empty.
fn clamp(ranges: &[Range<usize>], len: usize) -> Vec<Range<usize>> {
    ranges
        .iter()
        .filter_map(|r| {
            let end = r.end.min(len);
            (r.start < end).then_some(r.start..end)
        })
        .collect()
}

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

    fn line_with(src: &str, needle: &str) -> usize {
        src.lines()
            .position(|l| l.contains(needle))
            .unwrap_or_else(|| panic!("no line with {needle:?}"))
    }

    #[test]
    fn pure_reindent_is_not_emphasized() {
        let reg = LanguageRegistry::build();
        let old = "fn f() {\n    let x = compute();\n    use_it(x);\n}\n";
        let new = "fn f() {\n        let x = compute();\n        use_it(x);\n}\n";
        let (_, new_e) = reg.line_emphasis("a.rs", old, new).expect("rust parses");
        assert!(
            new_e.iter().all(Vec::is_empty),
            "reindentation must produce no emphasis, got {new_e:?}"
        );
    }

    #[test]
    fn a_real_token_change_is_emphasized() {
        let reg = LanguageRegistry::build();
        let old = "fn f() {\n    let x = 1;\n}\n";
        let new = "fn f() {\n    let x = 2;\n}\n";
        let (_, new_e) = reg.line_emphasis("a.rs", old, new).expect("rust parses");
        let changed = line_with(new, "let x = 2");
        let signature = line_with(new, "fn f()");
        assert!(!new_e[changed].is_empty(), "the changed line is emphasized");
        assert!(
            new_e[signature].is_empty(),
            "the unchanged signature line is not"
        );
    }

    #[test]
    fn in_string_edit_is_char_precise_not_whole_token() {
        use crate::model::{DiffLine, FileDiff, FileStatus, Hunk, HunkId, LineKind};
        let old_line = "fn f() { let s = \"foo/bar\"; }";
        let new_line = "fn f() { let s = \"foo/EXTRA/bar\"; }";
        let mut file = FileDiff {
            path: "a.rs".into(),
            old_path: None,
            status: FileStatus::Modified,
            binary: false,
            old_text: Some(format!("{old_line}\n")),
            new_text: Some(format!("{new_line}\n")),
            hunks: vec![Hunk {
                id: HunkId("h".into()),
                old_start: 1,
                old_lines: 1,
                new_start: 1,
                new_lines: 1,
                context: String::new(),
                lines: vec![
                    DiffLine::new(LineKind::Deleted, Some(1), None, old_line.to_owned()),
                    DiffLine::new(LineKind::Added, None, Some(1), new_line.to_owned()),
                ],
            }],
            hashes: crate::model::HashCache::default(),
            blobs: crate::model::BlobIds::default(),
        };
        assert!(LanguageRegistry::build().syntactic_emphasis(&mut file, false));
        let added = &file.hunks[0].lines[1];
        assert!(!added.emphasis.is_empty(), "the changed line is emphasized");
        let covered: String = added
            .emphasis
            .iter()
            .filter_map(|r| new_line.get(r.clone()))
            .collect();
        // only the inserted run is emphasized, not the whole "foo/EXTRA/bar" token
        assert!(
            covered.contains("EXTRA"),
            "covers the insertion: {covered:?}"
        );
        assert!(
            !covered.contains("foo"),
            "the unchanged prefix is not emphasized: {covered:?}"
        );
    }

    /// The changed lines the structural algorithm flags reformat-only.
    fn reformat_flagged(path: &str, old: &str, new: &str) -> Vec<String> {
        let mut file = crate::model::FileDiff {
            path: path.into(),
            old_path: None,
            status: crate::model::FileStatus::Modified,
            binary: false,
            old_text: Some(old.into()),
            new_text: Some(new.into()),
            hunks: crate::diffalgo::histogram_hunks(old, new, path, 3, true),
            hashes: crate::model::HashCache::default(),
            blobs: crate::model::BlobIds::default(),
        };
        assert!(LanguageRegistry::build().syntactic_emphasis(&mut file, true));
        file.hunks
            .iter()
            .flat_map(|h| &h.lines)
            .filter(|l| l.reformat_only)
            .map(|l| l.text.clone())
            .collect()
    }

    #[test]
    fn structural_mode_marks_a_pure_reindent_pair_reformat_only() {
        let flagged = reformat_flagged(
            "a.rs",
            "fn f() {\n    let x = compute();\n}\n",
            "fn f() {\n        let x = compute();\n}\n",
        );
        assert_eq!(
            flagged,
            ["    let x = compute();", "        let x = compute();"]
        );
    }

    #[test]
    fn structural_mode_leaves_a_real_change_unflagged() {
        let flagged = reformat_flagged(
            "a.rs",
            "fn f() {\n    let x = 1;\n}\n",
            "fn f() {\n    let x = 2;\n}\n",
        );
        assert!(flagged.is_empty(), "{flagged:?}");
    }

    #[test]
    fn structural_mode_keeps_whitespace_inside_a_string_a_change() {
        let flagged = reformat_flagged(
            "a.rs",
            "fn f() {\n    let s = \"a b\";\n}\n",
            "fn f() {\n    let s = \"a  b\";\n}\n",
        );
        assert!(flagged.is_empty(), "{flagged:?}");
    }

    #[test]
    fn structural_mode_never_flags_a_reindent_where_layout_is_syntax() {
        let python = reformat_flagged(
            "a.py",
            "x = 1\nif c:\n    pass\ny = 2\n",
            "x = 1\nif c:\n    pass\n    y = 2\n",
        );
        assert!(python.is_empty(), "moving into the block: {python:?}");
        let yaml = reformat_flagged("a.yaml", "a:\n  b: 1\nc: 2\n", "a:\n  b: 1\n  c: 2\n");
        assert!(yaml.is_empty(), "nesting a key: {yaml:?}");
    }

    /// A block of wholly-new code where the AST diff matches stray tokens
    /// (a `}`, an identifier) against the old tree and would light up
    /// fragments inside plain added lines.
    #[test]
    fn wholly_new_code_never_carries_fragment_emphasis() {
        use crate::model::{DiffLine, FileDiff, FileStatus, Hunk, HunkId, LineKind};
        let old_src = "function keep(path: string): string {\n    return path;\n}\n";
        let added = [
            "function fresh(path: string): string {",
            "    if (!path) {",
            "        return \"missing\";",
            "    }",
            "    return path;",
            "}",
        ];
        let new_src = format!("{old_src}\n{}\n", added.join("\n"));
        let lines = added
            .iter()
            .enumerate()
            .map(|(i, text)| {
                DiffLine::new(
                    LineKind::Added,
                    None,
                    Some(5 + i as u32),
                    (*text).to_owned(),
                )
            })
            .collect();
        let mut file = FileDiff {
            path: "a.ts".into(),
            old_path: None,
            status: FileStatus::Modified,
            binary: false,
            old_text: Some(old_src.to_owned()),
            new_text: Some(new_src),
            hunks: vec![Hunk {
                id: HunkId("h".into()),
                old_start: 3,
                old_lines: 0,
                new_start: 5,
                new_lines: 6,
                context: String::new(),
                lines,
            }],
            hashes: crate::model::HashCache::default(),
            blobs: crate::model::BlobIds::default(),
        };
        assert!(LanguageRegistry::build().syntactic_emphasis(&mut file, false));
        for line in &file.hunks[0].lines {
            assert!(
                line.emphasis.is_empty(),
                "no pair, no emphasis: {:?} got {:?}",
                line.text,
                line.emphasis
            );
        }
    }

    #[test]
    fn tsx_wrap_and_reindent_marks_only_real_changes() {
        let reg = LanguageRegistry::build();
        let old = "<Form>\n  <Button onClick={onApply}>Apply</Button>\n</Form>\n";
        let new = "{(values) => (\n  <Form>\n    <Button onClick={() => apply(values)}>Apply</Button>\n  </Form>\n)}\n";
        let (_, new_e) = reg.line_emphasis("a.tsx", old, new).expect("tsx parses");
        let reindented = line_with(new, "<Form>");
        let changed = line_with(new, "apply(values)");
        assert!(
            new_e[reindented].is_empty(),
            "a reindented-but-identical line is not emphasized, got {:?}",
            new_e[reindented]
        );
        assert!(
            !new_e[changed].is_empty(),
            "the structurally changed line is emphasized"
        );
    }

    #[test]
    fn unsupported_language_returns_none() {
        let reg = LanguageRegistry::build();
        assert!(reg.line_emphasis("a.zzz", "a\n", "b\n").is_none());
    }

    #[test]
    fn classify_unchanged_line_gets_no_emphasis() {
        // a reindent/move: nothing changed within the line, plain +/- bg
        let emph = classify_line(LineKind::Added, "    <Form>", &[]);
        assert!(emph.is_empty());
    }

    #[test]
    fn classify_whole_line_change_keeps_background_without_emphasis() {
        // every non-whitespace byte changed -> full +/- bg, no char emphasis
        let text = "    let entirely_new = compute();";
        let ranges = [4..7, 8..20, 21..22, 23..text.len()];
        let emph = classify_line(LineKind::Added, text, &ranges);
        assert!(
            emph.is_empty(),
            "no char emphasis when the whole line changed"
        );
    }

    #[test]
    fn classify_mostly_changed_line_drops_emphasis() {
        // more than the punctual share changed: highlighting it all says nothing
        let text = "    let entirely_new = compute();";
        let ranges = [4..7, 8..20, 23..30];
        let emph = classify_line(LineKind::Added, text, &ranges);
        assert!(emph.is_empty(), "{emph:?}");
    }

    #[test]
    fn classify_partial_change_keeps_emphasis() {
        // only `2` changed in `    let x = 2;`
        let text = "    let x = 2;";
        let changed = 12..13;
        let emph = classify_line(LineKind::Added, text, std::slice::from_ref(&changed));
        assert_eq!(emph.len(), 1);
        assert_eq!(emph[0], changed);
    }
}