Skip to main content

continuity_engine/
reconcile_diff.rs

1//! Minimal splice bounds for full-document host reconciliation.
2
3use ropey::Rope;
4
5/// Byte bounds of the smallest single replacement turning the rope into `new_text`.
6///
7/// `start..old_end` is the replaced rope range; `start..new_end` indexes the
8/// replacement slice inside `new_text`. All three bounds sit on UTF-8 character
9/// boundaries. A host reconcile that splices only this range keeps selections,
10/// scroll anchors, and incremental line mirrors stable everywhere else in the
11/// document, instead of invalidating every line with a whole-document replace.
12#[must_use]
13pub fn compute_reconcile_splice(rope: &Rope, new_text: &str) -> ReconcileSplice {
14    let old_len = rope.len_bytes();
15    let new_bytes = new_text.as_bytes();
16    let mut prefix = 0;
17    'prefix: for chunk in rope.chunks() {
18        for &byte in chunk.as_bytes() {
19            if prefix >= new_bytes.len() || new_bytes[prefix] != byte {
20                break 'prefix;
21            }
22            prefix += 1;
23        }
24    }
25    while prefix > 0 && !new_text.is_char_boundary(prefix) {
26        prefix -= 1;
27    }
28    let max_suffix = old_len.min(new_bytes.len()) - prefix;
29    let mut suffix = 0;
30    for byte in rope.bytes_at(old_len).reversed() {
31        if suffix >= max_suffix || new_bytes[new_bytes.len() - 1 - suffix] != byte {
32            break;
33        }
34        suffix += 1;
35    }
36    // The shared suffix bytes are identical in both texts, so one boundary
37    // check covers the rope and the replacement string alike.
38    while suffix > 0 && !new_text.is_char_boundary(new_bytes.len() - suffix) {
39        suffix -= 1;
40    }
41    ReconcileSplice {
42        start: prefix,
43        old_end: old_len - suffix,
44        new_end: new_bytes.len() - suffix,
45    }
46}
47
48/// Character-boundary-safe replacement bounds returned by [`compute_reconcile_splice`].
49#[derive(Clone, Copy, Debug, Eq, PartialEq)]
50pub struct ReconcileSplice {
51    /// First differing byte, shared by both texts.
52    pub start: usize,
53    /// End of the replaced range in the rope.
54    pub old_end: usize,
55    /// End of the replacement slice in the new text.
56    pub new_end: usize,
57}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    fn splice(old: &str, new: &str) -> ReconcileSplice {
64        let bounds = compute_reconcile_splice(&Rope::from_str(old), new);
65        let mut patched = String::new();
66        patched.push_str(&old[..bounds.start]);
67        patched.push_str(&new[bounds.start..bounds.new_end]);
68        patched.push_str(&old[bounds.old_end..]);
69        assert_eq!(patched, new, "splice must reproduce the new text");
70        bounds
71    }
72
73    #[test]
74    fn mid_document_insertion_bounds_only_the_inserted_word() {
75        let bounds = splice("alpha beta gamma", "alpha beta word gamma");
76        assert_eq!(bounds.start, 11);
77        assert_eq!(bounds.old_end, 11);
78        assert_eq!(bounds.new_end, 16);
79    }
80
81    #[test]
82    fn mid_document_deletion_bounds_only_the_removed_run() {
83        let bounds = splice("alpha beta gamma", "alpha gamma");
84        assert_eq!(bounds.old_end - bounds.start, 5);
85        assert_eq!(bounds.new_end, bounds.start);
86    }
87
88    #[test]
89    fn overlapping_repeats_never_cross_prefix_and_suffix() {
90        splice("aaaa", "aaa");
91        splice("aaa", "aaaa");
92        splice("abab", "ababab");
93    }
94
95    #[test]
96    fn multibyte_replacement_stays_on_character_boundaries() {
97        let bounds = splice("héllo wörld", "héllo wärld");
98        assert!("héllo wärld".is_char_boundary(bounds.start));
99        assert!("héllo wärld".is_char_boundary(bounds.new_end));
100    }
101
102    #[test]
103    fn disjoint_texts_replace_everything() {
104        let bounds = splice("one", "two");
105        assert_eq!(bounds.start, 0);
106        assert_eq!(bounds.old_end, 3);
107        assert_eq!(bounds.new_end, 3);
108    }
109
110    #[test]
111    fn empty_old_and_empty_new_are_pure_insert_and_delete() {
112        splice("", "fresh");
113        splice("stale", "");
114    }
115}