continuity-buffer 0.2.36

Rope-backed buffer, revisions, selections, and undo state for Continuity
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
//! Per-buffer undo **tree**, persisted in `undo_groups` (and the edit log).
//!
//! Branches form when the user undoes a group and then makes a different
//! edit instead of redoing — the new edit becomes a sibling rather than
//! overwriting the redo branch (per spec §8).
//!
//! **Thread ownership**: only the caller-selected engine thread mutates an `UndoTree`;
//! it lives inside [`crate::Buffer`].

use ahash::AHashMap;
use continuity_text::{EditOp, Selection};

use crate::{Revision, UndoGroupId};

/// One atomic edit recorded in an undo group, alongside the inverse op the
/// undo path replays to revert it.
#[derive(Clone, Debug)]
pub struct EditRecord {
    /// The op that was applied.
    pub op: EditOp,
    /// The op that, applied to the *post-edit* rope, restores the
    /// *pre-edit* rope. Computed at record time when both the original
    /// removed text and the post-edit positions are still in scope.
    pub inverse_op: EditOp,
    /// Buffer revision before [`Self::op`] was applied.
    pub revision_before: Revision,
    /// Buffer revision after [`Self::op`] was applied.
    pub revision_after: Revision,
    /// Selections at the time of the edit, before mutation.
    pub selections_before: Vec<Selection>,
    /// Selections after mutation.
    pub selections_after: Vec<Selection>,
}

/// A coalesced unit of undo (typing in a single 500ms run, one paste, one
/// multi-cursor edit, one find-replace fanout, etc.).
#[derive(Clone, Debug)]
pub struct UndoGroup {
    /// Stable id (UUIDv7).
    pub id: UndoGroupId,
    /// The group's parent in the tree. `None` for the root group.
    pub parent: Option<UndoGroupId>,
    /// Edits in application order.
    pub ops: Vec<EditRecord>,
    /// Wall-clock millis of group creation.
    pub timestamp_ms: i64,
    /// The command that produced this group (for display in the picker and
    /// the persisted `undo_groups.command_name` column).
    pub command: String,
}

impl UndoGroup {
    /// True iff this group has at least one recorded edit.
    #[must_use]
    pub fn has_ops(&self) -> bool {
        !self.ops.is_empty()
    }
}

/// The full per-buffer undo history.
///
/// The tree is append-only (groups never disappear from `groups` once
/// recorded; the history-cap policy is implemented by snapshotting the
/// content and dropping covered edit rows at the persistence layer).
#[derive(Debug, Default, Clone)]
pub struct UndoTree {
    groups: Vec<UndoGroup>,
    by_id: AHashMap<UndoGroupId, usize>,
    /// Index of the group whose ops have been applied to the buffer; `None`
    /// when the buffer is at the pre-history state.
    current: Option<usize>,
}

impl UndoTree {
    /// An empty tree (current = pre-history).
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Rebuild a tree from previously recorded groups and a head pointer.
    ///
    /// Distinct from replaying [`Self::insert_group`] per group: that call
    /// re-parents a `None` parent onto whatever the head currently is, which
    /// silently re-roots every root-level branch onto the group inserted
    /// before it. A restore has to preserve the recorded shape exactly, so it
    /// takes the groups as given. Unknown `current` ids fall back to
    /// pre-history rather than panicking, because the input crossed a
    /// serialization boundary and is not a caller invariant.
    #[must_use]
    pub fn restore(groups: Vec<UndoGroup>, current: Option<UndoGroupId>) -> Self {
        let by_id = groups
            .iter()
            .enumerate()
            .map(|(index, group)| (group.id, index))
            .collect::<AHashMap<_, _>>();
        let current = current.and_then(|id| by_id.get(&id).copied());
        Self {
            groups,
            by_id,
            current,
        }
    }

    /// Insert an empty group rooted at `parent` (or at the current head if
    /// `parent` is `None` and a current group exists). Returns the group's
    /// stable id.
    ///
    /// Used both at edit time (when the engine mints a new group) and
    /// at recovery time (when persisted rows are replayed).
    pub fn insert_group(
        &mut self,
        id: UndoGroupId,
        parent: Option<UndoGroupId>,
        timestamp_ms: i64,
        command: impl Into<String>,
    ) {
        let parent = parent.or_else(|| self.current_id());
        let group = UndoGroup {
            id,
            parent,
            ops: Vec::new(),
            timestamp_ms,
            command: command.into(),
        };
        let idx = self.groups.len();
        self.by_id.insert(id, idx);
        self.groups.push(group);
        self.current = Some(idx);
    }

    /// Append an edit record to the named group. Touches `current` to point
    /// at that group.
    ///
    /// # Panics
    ///
    /// Panics if `group_id` is unknown (caller-bug only — the core thread
    /// always inserts the group before recording into it).
    pub fn append_record(&mut self, group_id: UndoGroupId, record: EditRecord) {
        let idx = *self
            .by_id
            .get(&group_id)
            .expect("invariant: group recorded into without insert_group");
        self.groups[idx].ops.push(record);
        self.current = Some(idx);
    }

    /// True when no groups have been recorded.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.groups.is_empty()
    }

    /// Number of groups in the tree.
    #[must_use]
    pub fn len(&self) -> usize {
        self.groups.len()
    }

    /// Number of [`UndoGroup`]s recorded. Alias for [`Self::len`],
    /// surfaced separately because the trace stream emits
    /// `undo_tree_groups=` and `undo_tree_records=` side by side.
    #[must_use]
    pub fn group_count(&self) -> usize {
        self.groups.len()
    }

    /// Total number of [`EditRecord`]s across every group. Bumped by
    /// every [`Self::append_record`] call; combined with
    /// [`Self::byte_size_estimate`] this lets the memory_breakdown line
    /// report bytes-per-record so Block 4.1 can prove its
    /// `SmallVec<[Selection; 1]>` rewrite shrank per-record overhead.
    #[must_use]
    pub fn record_count(&self) -> usize {
        self.groups.iter().map(|group| group.ops.len()).sum()
    }

    /// Estimated heap bytes retained by the undo tree.
    #[must_use]
    pub fn byte_size_estimate(&self) -> usize {
        self.groups.capacity() * std::mem::size_of::<UndoGroup>()
            + self.by_id.capacity()
                * (std::mem::size_of::<UndoGroupId>() + std::mem::size_of::<usize>())
            + self.groups.iter().map(undo_group_heap_bytes).sum::<usize>()
    }

    /// The currently-applied group, if any.
    #[must_use]
    pub fn current(&self) -> Option<&UndoGroup> {
        self.current.map(|i| &self.groups[i])
    }

    /// Id of the currently-applied group.
    #[must_use]
    pub fn current_id(&self) -> Option<UndoGroupId> {
        self.current().map(|g| g.id)
    }

    /// Borrow a group by id.
    #[must_use]
    pub fn get(&self, id: UndoGroupId) -> Option<&UndoGroup> {
        self.by_id.get(&id).map(|&i| &self.groups[i])
    }

    /// Borrow every group in insertion order. Used by the picker UI.
    #[must_use]
    pub fn groups(&self) -> &[UndoGroup] {
        &self.groups
    }

    /// Step the current pointer to `id`, asserting the group exists.
    ///
    /// # Panics
    ///
    /// Panics if `id` is unknown — the call sites compute targets only from
    /// existing groups, so this is a structural invariant.
    pub fn set_current(&mut self, id: Option<UndoGroupId>) {
        match id {
            None => self.current = None,
            Some(id) => {
                let idx = *self
                    .by_id
                    .get(&id)
                    .expect("invariant: set_current with unknown group id");
                self.current = Some(idx);
            }
        }
    }

    /// The group that an `editor.undo` from the current head would revert,
    /// i.e. the current group itself.
    #[must_use]
    pub fn group_to_undo(&self) -> Option<&UndoGroup> {
        self.current()
    }

    /// The group that an `editor.redo` from the current head would re-apply
    /// — the most-recently-recorded direct child of the current head (or of
    /// the pre-history root if `current` is `None`).
    ///
    /// The "most recent" tie-breaker matches the user's most recent intent:
    /// if you undo and then redo, you redo the branch you just left.
    #[must_use]
    pub fn group_to_redo(&self) -> Option<&UndoGroup> {
        self.children(self.current_id()).into_iter().next_back()
    }

    /// An alternate redo target: a child of the current pointer that is not
    /// the one [`Self::group_to_redo`] would pick. Returns `None` when the
    /// current pointer has fewer than two children.
    ///
    /// Used to step through fork branches after the user has undone past
    /// the divergence point.
    #[must_use]
    pub fn group_to_redo_alternate(&self) -> Option<&UndoGroup> {
        let kids = self.children(self.current_id());
        if kids.len() < 2 {
            return None;
        }
        let primary = kids.last()?.id;
        kids.into_iter().find(|g| g.id != primary)
    }

    /// Direct children of `parent` in insertion order (oldest first).
    /// `parent = None` returns the root-level branches.
    #[must_use]
    pub fn children(&self, parent: Option<UndoGroupId>) -> Vec<&UndoGroup> {
        self.groups.iter().filter(|g| g.parent == parent).collect()
    }
}

// Reserved trace event: `event:undo_tree_trim`. Emitted from the
// trimming code path introduced by Block 4.2 of the memory
// optimization plan (Emacs-style three-tier byte-budget undo cap).
// The event is not produced anywhere yet — when the trimmer lands it
// should emit `event:undo_tree_trim buffer=<id> tier=<soft|hard|panic>
// dropped_groups=<n> dropped_records=<n> freed_bytes=<n>` so xtask
// perf-report can correlate undo-tree shrinkage with the budget
// trigger.

fn undo_group_heap_bytes(group: &UndoGroup) -> usize {
    group.command.capacity()
        + group.ops.capacity() * std::mem::size_of::<EditRecord>()
        + group.ops.iter().map(edit_record_heap_bytes).sum::<usize>()
}

fn edit_record_heap_bytes(record: &EditRecord) -> usize {
    edit_op_heap_bytes(&record.op)
        + edit_op_heap_bytes(&record.inverse_op)
        + record.selections_before.capacity() * std::mem::size_of::<Selection>()
        + record.selections_after.capacity() * std::mem::size_of::<Selection>()
}

fn edit_op_heap_bytes(op: &EditOp) -> usize {
    match op {
        EditOp::Insert { text, .. } | EditOp::Replace { text, .. } => text.capacity(),
        EditOp::Delete { .. } => 0,
    }
}

#[cfg(test)]
mod tests {
    use continuity_text::{Position, Range};

    use super::*;

    fn record(rev: u64) -> EditRecord {
        EditRecord {
            op: EditOp::insert(Position::ZERO, "x"),
            inverse_op: EditOp::delete(Range::new(Position::ZERO, Position::new(0, 1))),
            revision_before: Revision(rev - 1),
            revision_after: Revision(rev),
            selections_before: vec![],
            selections_after: vec![],
        }
    }

    #[test]
    fn empty_tree_has_no_current() {
        let t = UndoTree::new();
        assert!(t.is_empty());
        assert!(t.current().is_none());
    }

    #[test]
    fn inserting_groups_links_parent_chain() {
        let mut t = UndoTree::new();
        let a = UndoGroupId::new();
        t.insert_group(a, None, 0, "editor.insert_char");
        t.append_record(a, record(1));
        let b = UndoGroupId::new();
        t.insert_group(b, None, 1, "editor.insert_char");
        t.append_record(b, record(2));
        assert_eq!(t.len(), 2);
        assert_eq!(t.get(b).unwrap().parent, Some(a));
        assert_eq!(t.current_id(), Some(b));
    }

    #[test]
    fn group_to_undo_is_current() {
        let mut t = UndoTree::new();
        let a = UndoGroupId::new();
        t.insert_group(a, None, 0, "x");
        t.append_record(a, record(1));
        assert_eq!(t.group_to_undo().map(|g| g.id), Some(a));
    }

    #[test]
    fn group_to_redo_picks_most_recent_child() {
        let mut t = UndoTree::new();
        let a = UndoGroupId::new();
        t.insert_group(a, None, 0, "x");
        t.append_record(a, record(1));
        let b = UndoGroupId::new();
        t.insert_group(b, Some(a), 1, "y");
        t.append_record(b, record(2));
        // Branch after undoing back to a:
        t.set_current(Some(a));
        let c = UndoGroupId::new();
        t.insert_group(c, Some(a), 2, "z");
        t.append_record(c, record(3));
        t.set_current(Some(a));
        assert_eq!(t.group_to_redo().map(|g| g.id), Some(c));
    }

    #[test]
    fn restore_preserves_shape_and_head() {
        let mut original = UndoTree::new();
        let a = UndoGroupId::new();
        original.insert_group(a, None, 0, "x");
        original.append_record(a, record(1));
        let b = UndoGroupId::new();
        original.insert_group(b, Some(a), 1, "y");
        original.append_record(b, record(2));
        original.set_current(Some(a));

        let restored = UndoTree::restore(original.groups().to_vec(), original.current_id());
        assert_eq!(restored.len(), 2);
        assert_eq!(restored.current_id(), Some(a));
        assert_eq!(restored.get(b).unwrap().parent, Some(a));
        assert_eq!(restored.group_to_redo().map(|g| g.id), Some(b));
    }

    #[test]
    fn restore_with_unknown_head_falls_back_to_pre_history() {
        let mut original = UndoTree::new();
        let a = UndoGroupId::new();
        original.insert_group(a, None, 0, "x");
        original.append_record(a, record(1));
        let restored = UndoTree::restore(original.groups().to_vec(), Some(UndoGroupId::new()));
        assert!(restored.current().is_none());
        assert_eq!(restored.len(), 1);
    }

    #[test]
    fn redo_alternate_picks_other_child() {
        let mut t = UndoTree::new();
        let a = UndoGroupId::new();
        t.insert_group(a, None, 0, "x");
        t.append_record(a, record(1));
        let b = UndoGroupId::new();
        t.insert_group(b, Some(a), 1, "y");
        t.append_record(b, record(2));
        t.set_current(Some(a));
        let c = UndoGroupId::new();
        t.insert_group(c, Some(a), 2, "z");
        t.append_record(c, record(3));
        t.set_current(Some(a));
        // From parent `a`, the most-recent child is `c`; the alternate is `b`.
        assert_eq!(t.group_to_redo().map(|g| g.id), Some(c));
        assert_eq!(t.group_to_redo_alternate().map(|g| g.id), Some(b));
    }
}