Skip to main content

diffler_core/syntax/
intraline.rs

1//! Char-precise intra-line change emphasis driven by an AST diff (syndiff),
2//! the structural counterpart to the textual engine in [`crate::pairing`].
3//! Only the byte ranges that differ structurally are emphasized, so a
4//! reformatted or re-wrapped block highlights just the tokens that changed.
5
6use std::ops::Range;
7
8use syndiff::{SyntaxDiffOptions, build_tree, diff_trees};
9
10use crate::model::{FileDiff, Hunk, LineKind};
11use crate::syntax::registry::LanguageRegistry;
12use crate::syntax::{MAX_PARSE_BYTES, line_bounds, parse, split_range_by_line};
13
14/// Emphasis byte ranges per line (one inner vec per source line).
15type LineEmphasis = Vec<Vec<Range<usize>>>;
16
17/// Bounds the AST-diff graph search so a huge, heavily rewritten file cannot
18/// stall the render thread; beyond it `diff_trees` returns `None` and the
19/// caller falls back to the textual engine. Well above any normal diff.
20const GRAPH_LIMIT: usize = 250_000;
21
22impl LanguageRegistry {
23    /// Per-line emphasis byte ranges for both sides, from an AST diff of the
24    /// full old/new content. `None` (caller falls back to the textual engine)
25    /// when the language is unsupported, content is too large, parsing fails,
26    /// or the diff exceeds its graph budget.
27    fn line_emphasis(
28        &self,
29        path: &str,
30        old_src: &str,
31        new_src: &str,
32    ) -> Option<(LineEmphasis, LineEmphasis)> {
33        if old_src.len() > MAX_PARSE_BYTES || new_src.len() > MAX_PARSE_BYTES {
34            return None;
35        }
36        let entry = self.for_path(path)?;
37        // markdown's block tree is coarse (a paragraph is one opaque node); the
38        // textual word-diff emphasizes prose edits far better than an AST diff.
39        if entry.name == "markdown" {
40            return None;
41        }
42        let old_ts = parse(entry, old_src)?;
43        let new_ts = parse(entry, new_src)?;
44        let old_tree = build_tree(old_ts.walk(), old_src);
45        let new_tree = build_tree(new_ts.walk(), new_src);
46        let options = SyntaxDiffOptions {
47            graph_limit: GRAPH_LIMIT,
48        };
49        let (old_ranges, new_ranges) = diff_trees(&old_tree, &new_tree, None, None, Some(options))?;
50        Some((
51            per_line_emphasis(old_src, &old_ranges),
52            per_line_emphasis(new_src, &new_ranges),
53        ))
54    }
55
56    /// Set char-precise emphasis on `file`'s diff lines from the AST diff.
57    /// `mark_reformat_only` additionally flags a paired deleted/added line the
58    /// AST diff found no structural difference on at all (a pure reformat)
59    /// for the structural algorithm's dimmed rendering. Returns `false` when
60    /// the syntactic engine is unavailable, so the caller can fall back to
61    /// the textual engine (and structural mode silently reads as a plain
62    /// histogram diff for that file).
63    pub fn syntactic_emphasis(&self, file: &mut FileDiff, mark_reformat_only: bool) -> bool {
64        let emphasis = match (file.old_text.as_deref(), file.new_text.as_deref()) {
65            (Some(old), Some(new)) => self.line_emphasis(&file.path, old, new),
66            _ => None,
67        };
68        let Some((old_emph, new_emph)) = emphasis else {
69            return false;
70        };
71        let mark_reformat_only = mark_reformat_only
72            && self
73                .for_path(&file.path)
74                .is_some_and(|entry| !entry.layout_significant);
75        for hunk in &mut file.hunks {
76            for line in &mut hunk.lines {
77                let ranges = match (line.new_no, line.old_no) {
78                    (Some(n), _) => new_emph.get(n as usize - 1),
79                    (None, Some(o)) => old_emph.get(o as usize - 1),
80                    _ => None,
81                };
82                line.emphasis =
83                    classify_line(line.kind, &line.text, ranges.map_or(&[], Vec::as_slice));
84            }
85            refine_partial_changes(hunk);
86            if mark_reformat_only {
87                mark_reformat_pairs(hunk, &old_emph, &new_emph);
88            }
89        }
90        true
91    }
92}
93
94/// Flag a paired deleted/added line as `reformat_only` when the two differ in
95/// whitespace alone and the AST diff found no token changed on either side,
96/// which keeps a whitespace edit inside a string literal a real change.
97fn mark_reformat_pairs(hunk: &mut Hunk, old_emph: &LineEmphasis, new_emph: &LineEmphasis) {
98    let unchanged = |emph: &LineEmphasis, number: Option<u32>| {
99        number
100            .and_then(|n| n.checked_sub(1))
101            .and_then(|i| emph.get(i as usize))
102            .is_some_and(Vec::is_empty)
103    };
104    let squeezed = |text: &str| text.split_whitespace().collect::<String>();
105    for (del_idx, add_idx) in crate::pairing::paired_run_indices(&hunk.lines) {
106        let (Some(del), Some(add)) = (hunk.lines.get(del_idx), hunk.lines.get(add_idx)) else {
107            continue;
108        };
109        if unchanged(old_emph, del.old_no)
110            && unchanged(new_emph, add.new_no)
111            && squeezed(&del.text) == squeezed(&add.text)
112        {
113            if let Some(line) = hunk.lines.get_mut(del_idx) {
114                line.reformat_only = true;
115            }
116            if let Some(line) = hunk.lines.get_mut(add_idx) {
117                line.reformat_only = true;
118            }
119        }
120    }
121}
122
123/// Where the AST diff flagged a *partial* line change (some token ranges, not
124/// the whole line and not a reformat), replace the coarse token ranges with a
125/// word-level diff of the paired lines, so only the tokens that actually
126/// changed are emphasized (an edit inside a string scalar shouldn't light up the
127/// whole scalar). Emphasis means "differs from the homolog": a line with no
128/// pair (wholly new or wholly gone) renders plain, keeping off the stray
129/// fragments the AST diff leaves when it matches a token of new code against
130/// something elsewhere in the old tree.
131fn refine_partial_changes(hunk: &mut Hunk) {
132    let pairs = crate::pairing::paired_run_indices(&hunk.lines);
133    let paired: std::collections::HashSet<usize> =
134        pairs.iter().flat_map(|&(d, a)| [d, a]).collect();
135    for (index, line) in hunk.lines.iter_mut().enumerate() {
136        if matches!(line.kind, LineKind::Deleted | LineKind::Added) && !paired.contains(&index) {
137            line.emphasis = Vec::new();
138        }
139    }
140    for (del_idx, add_idx) in pairs {
141        let partial = hunk
142            .lines
143            .get(del_idx)
144            .is_some_and(|l| !l.emphasis.is_empty())
145            || hunk
146                .lines
147                .get(add_idx)
148                .is_some_and(|l| !l.emphasis.is_empty());
149        if !partial {
150            continue;
151        }
152        let (Some(old), Some(new)) = (
153            hunk.lines.get(del_idx).map(|l| l.text.clone()),
154            hunk.lines.get(add_idx).map(|l| l.text.clone()),
155        ) else {
156            continue;
157        };
158        // the same pair gate as the textual engine, so a refinement that
159        // comes back scattered or near-total drops to plain lines too
160        let (old_emph, new_emph) = crate::pairing::gated_pair_emphasis(&old, &new);
161        if let Some(line) = hunk.lines.get_mut(del_idx) {
162            line.emphasis = old_emph;
163        }
164        if let Some(line) = hunk.lines.get_mut(add_idx) {
165            line.emphasis = new_emph;
166        }
167    }
168}
169
170/// Map whole-file changed byte ranges to the raw per-line, within-line ranges.
171fn per_line_emphasis(src: &str, ranges: &[Range<usize>]) -> LineEmphasis {
172    let bounds = line_bounds(src);
173    let starts: Vec<usize> = bounds.iter().map(|&(s, _)| s).collect();
174    let mut out = vec![Vec::new(); bounds.len()];
175    for r in ranges {
176        split_range_by_line(&bounds, &starts, r, |li, rr| {
177            if let Some(v) = out.get_mut(li) {
178                v.push(rr);
179            }
180        });
181    }
182    out
183}
184
185/// Emphasis for an added/deleted `line` from its raw changed byte `ranges`.
186/// Every changed line keeps its full +/- background; emphasis only marks
187/// punctual edits: a line that changed mostly or entirely gets none, because
188/// highlighting almost everything highlights nothing.
189fn classify_line(kind: LineKind, text: &str, ranges: &[Range<usize>]) -> Vec<Range<usize>> {
190    let _ = kind;
191    let ranges = clamp(ranges, text.len());
192    if ranges.is_empty() || !crate::pairing::emphasis_is_punctual(text, &ranges) {
193        return Vec::new();
194    }
195    ranges
196}
197
198/// Clip ranges to the line's length and drop any that become empty.
199fn clamp(ranges: &[Range<usize>], len: usize) -> Vec<Range<usize>> {
200    ranges
201        .iter()
202        .filter_map(|r| {
203            let end = r.end.min(len);
204            (r.start < end).then_some(r.start..end)
205        })
206        .collect()
207}
208
209#[cfg(test)]
210mod tests {
211    use super::*;
212
213    fn line_with(src: &str, needle: &str) -> usize {
214        src.lines()
215            .position(|l| l.contains(needle))
216            .unwrap_or_else(|| panic!("no line with {needle:?}"))
217    }
218
219    #[test]
220    fn pure_reindent_is_not_emphasized() {
221        let reg = LanguageRegistry::build();
222        let old = "fn f() {\n    let x = compute();\n    use_it(x);\n}\n";
223        let new = "fn f() {\n        let x = compute();\n        use_it(x);\n}\n";
224        let (_, new_e) = reg.line_emphasis("a.rs", old, new).expect("rust parses");
225        assert!(
226            new_e.iter().all(Vec::is_empty),
227            "reindentation must produce no emphasis, got {new_e:?}"
228        );
229    }
230
231    #[test]
232    fn a_real_token_change_is_emphasized() {
233        let reg = LanguageRegistry::build();
234        let old = "fn f() {\n    let x = 1;\n}\n";
235        let new = "fn f() {\n    let x = 2;\n}\n";
236        let (_, new_e) = reg.line_emphasis("a.rs", old, new).expect("rust parses");
237        let changed = line_with(new, "let x = 2");
238        let signature = line_with(new, "fn f()");
239        assert!(!new_e[changed].is_empty(), "the changed line is emphasized");
240        assert!(
241            new_e[signature].is_empty(),
242            "the unchanged signature line is not"
243        );
244    }
245
246    #[test]
247    fn in_string_edit_is_char_precise_not_whole_token() {
248        use crate::model::{DiffLine, FileDiff, FileStatus, Hunk, HunkId, LineKind};
249        let old_line = "fn f() { let s = \"foo/bar\"; }";
250        let new_line = "fn f() { let s = \"foo/EXTRA/bar\"; }";
251        let mut file = FileDiff {
252            path: "a.rs".into(),
253            old_path: None,
254            status: FileStatus::Modified,
255            binary: false,
256            old_text: Some(format!("{old_line}\n")),
257            new_text: Some(format!("{new_line}\n")),
258            hunks: vec![Hunk {
259                id: HunkId("h".into()),
260                old_start: 1,
261                old_lines: 1,
262                new_start: 1,
263                new_lines: 1,
264                context: String::new(),
265                lines: vec![
266                    DiffLine::new(LineKind::Deleted, Some(1), None, old_line.to_owned()),
267                    DiffLine::new(LineKind::Added, None, Some(1), new_line.to_owned()),
268                ],
269            }],
270            hashes: crate::model::HashCache::default(),
271        };
272        assert!(LanguageRegistry::build().syntactic_emphasis(&mut file, false));
273        let added = &file.hunks[0].lines[1];
274        assert!(!added.emphasis.is_empty(), "the changed line is emphasized");
275        let covered: String = added
276            .emphasis
277            .iter()
278            .filter_map(|r| new_line.get(r.clone()))
279            .collect();
280        // only the inserted run is emphasized, not the whole "foo/EXTRA/bar" token
281        assert!(
282            covered.contains("EXTRA"),
283            "covers the insertion: {covered:?}"
284        );
285        assert!(
286            !covered.contains("foo"),
287            "the unchanged prefix is not emphasized: {covered:?}"
288        );
289    }
290
291    /// The changed lines the structural algorithm flags reformat-only.
292    fn reformat_flagged(path: &str, old: &str, new: &str) -> Vec<String> {
293        let mut file = crate::model::FileDiff {
294            path: path.into(),
295            old_path: None,
296            status: crate::model::FileStatus::Modified,
297            binary: false,
298            old_text: Some(old.into()),
299            new_text: Some(new.into()),
300            hunks: crate::diffalgo::histogram_hunks(old, new, path, 3, true),
301            hashes: crate::model::HashCache::default(),
302        };
303        assert!(LanguageRegistry::build().syntactic_emphasis(&mut file, true));
304        file.hunks
305            .iter()
306            .flat_map(|h| &h.lines)
307            .filter(|l| l.reformat_only)
308            .map(|l| l.text.clone())
309            .collect()
310    }
311
312    #[test]
313    fn structural_mode_marks_a_pure_reindent_pair_reformat_only() {
314        let flagged = reformat_flagged(
315            "a.rs",
316            "fn f() {\n    let x = compute();\n}\n",
317            "fn f() {\n        let x = compute();\n}\n",
318        );
319        assert_eq!(
320            flagged,
321            ["    let x = compute();", "        let x = compute();"]
322        );
323    }
324
325    #[test]
326    fn structural_mode_leaves_a_real_change_unflagged() {
327        let flagged = reformat_flagged(
328            "a.rs",
329            "fn f() {\n    let x = 1;\n}\n",
330            "fn f() {\n    let x = 2;\n}\n",
331        );
332        assert!(flagged.is_empty(), "{flagged:?}");
333    }
334
335    #[test]
336    fn structural_mode_keeps_whitespace_inside_a_string_a_change() {
337        let flagged = reformat_flagged(
338            "a.rs",
339            "fn f() {\n    let s = \"a b\";\n}\n",
340            "fn f() {\n    let s = \"a  b\";\n}\n",
341        );
342        assert!(flagged.is_empty(), "{flagged:?}");
343    }
344
345    #[test]
346    fn structural_mode_never_flags_a_reindent_where_layout_is_syntax() {
347        let python = reformat_flagged(
348            "a.py",
349            "x = 1\nif c:\n    pass\ny = 2\n",
350            "x = 1\nif c:\n    pass\n    y = 2\n",
351        );
352        assert!(python.is_empty(), "moving into the block: {python:?}");
353        let yaml = reformat_flagged("a.yaml", "a:\n  b: 1\nc: 2\n", "a:\n  b: 1\n  c: 2\n");
354        assert!(yaml.is_empty(), "nesting a key: {yaml:?}");
355    }
356
357    /// A block of wholly-new code where the AST diff matches stray tokens
358    /// (a `}`, an identifier) against the old tree and would light up
359    /// fragments inside plain added lines.
360    #[test]
361    fn wholly_new_code_never_carries_fragment_emphasis() {
362        use crate::model::{DiffLine, FileDiff, FileStatus, Hunk, HunkId, LineKind};
363        let old_src = "function keep(path: string): string {\n    return path;\n}\n";
364        let added = [
365            "function fresh(path: string): string {",
366            "    if (!path) {",
367            "        return \"missing\";",
368            "    }",
369            "    return path;",
370            "}",
371        ];
372        let new_src = format!("{old_src}\n{}\n", added.join("\n"));
373        let lines = added
374            .iter()
375            .enumerate()
376            .map(|(i, text)| {
377                DiffLine::new(
378                    LineKind::Added,
379                    None,
380                    Some(5 + i as u32),
381                    (*text).to_owned(),
382                )
383            })
384            .collect();
385        let mut file = FileDiff {
386            path: "a.ts".into(),
387            old_path: None,
388            status: FileStatus::Modified,
389            binary: false,
390            old_text: Some(old_src.to_owned()),
391            new_text: Some(new_src),
392            hunks: vec![Hunk {
393                id: HunkId("h".into()),
394                old_start: 3,
395                old_lines: 0,
396                new_start: 5,
397                new_lines: 6,
398                context: String::new(),
399                lines,
400            }],
401            hashes: crate::model::HashCache::default(),
402        };
403        assert!(LanguageRegistry::build().syntactic_emphasis(&mut file, false));
404        for line in &file.hunks[0].lines {
405            assert!(
406                line.emphasis.is_empty(),
407                "no pair, no emphasis: {:?} got {:?}",
408                line.text,
409                line.emphasis
410            );
411        }
412    }
413
414    #[test]
415    fn tsx_wrap_and_reindent_marks_only_real_changes() {
416        let reg = LanguageRegistry::build();
417        let old = "<Form>\n  <Button onClick={onApply}>Apply</Button>\n</Form>\n";
418        let new = "{(values) => (\n  <Form>\n    <Button onClick={() => apply(values)}>Apply</Button>\n  </Form>\n)}\n";
419        let (_, new_e) = reg.line_emphasis("a.tsx", old, new).expect("tsx parses");
420        let reindented = line_with(new, "<Form>");
421        let changed = line_with(new, "apply(values)");
422        assert!(
423            new_e[reindented].is_empty(),
424            "a reindented-but-identical line is not emphasized, got {:?}",
425            new_e[reindented]
426        );
427        assert!(
428            !new_e[changed].is_empty(),
429            "the structurally changed line is emphasized"
430        );
431    }
432
433    #[test]
434    fn unsupported_language_returns_none() {
435        let reg = LanguageRegistry::build();
436        assert!(reg.line_emphasis("a.zzz", "a\n", "b\n").is_none());
437    }
438
439    #[test]
440    fn classify_unchanged_line_gets_no_emphasis() {
441        // a reindent/move: nothing changed within the line, plain +/- bg
442        let emph = classify_line(LineKind::Added, "    <Form>", &[]);
443        assert!(emph.is_empty());
444    }
445
446    #[test]
447    fn classify_whole_line_change_keeps_background_without_emphasis() {
448        // every non-whitespace byte changed -> full +/- bg, no char emphasis
449        let text = "    let entirely_new = compute();";
450        let ranges = [4..7, 8..20, 21..22, 23..text.len()];
451        let emph = classify_line(LineKind::Added, text, &ranges);
452        assert!(
453            emph.is_empty(),
454            "no char emphasis when the whole line changed"
455        );
456    }
457
458    #[test]
459    fn classify_mostly_changed_line_drops_emphasis() {
460        // more than the punctual share changed: highlighting it all says nothing
461        let text = "    let entirely_new = compute();";
462        let ranges = [4..7, 8..20, 23..30];
463        let emph = classify_line(LineKind::Added, text, &ranges);
464        assert!(emph.is_empty(), "{emph:?}");
465    }
466
467    #[test]
468    fn classify_partial_change_keeps_emphasis() {
469        // only `2` changed in `    let x = 2;`
470        let text = "    let x = 2;";
471        let changed = 12..13;
472        let emph = classify_line(LineKind::Added, text, std::slice::from_ref(&changed));
473        assert_eq!(emph.len(), 1);
474        assert_eq!(emph[0], changed);
475    }
476}