Skip to main content

loro_internal/
undo.rs

1use std::{cell::RefCell, collections::VecDeque, sync::Arc};
2
3use crate::sync::{AtomicU64, Mutex};
4use either::Either;
5use loro_common::{
6    ContainerID, Counter, CounterSpan, HasIdSpan, IdSpan, LoroError, LoroResult, LoroValue, PeerID,
7};
8use parking_lot::lock_api::ReentrantMutex;
9use rustc_hash::{FxHashMap, FxHashSet};
10use tracing::{debug_span, info_span, instrument};
11
12use crate::{
13    change::{get_sys_timestamp, Timestamp},
14    cursor::{AbsolutePosition, Cursor},
15    delta::TreeExternalDiff,
16    event::{Diff, EventTriggerKind},
17    version::Frontiers,
18    ContainerDiff, DiffEvent, DocDiff, LoroDoc, Subscription,
19};
20
21/// A batch of diffs.
22///
23/// You can use `loroDoc.apply_diff(diff)` to apply the diff to the document.
24#[derive(Debug, Clone, Default)]
25pub struct DiffBatch {
26    pub cid_to_events: FxHashMap<ContainerID, Diff>,
27    pub order: Vec<ContainerID>,
28}
29
30impl DiffBatch {
31    pub fn new(diff: Vec<DocDiff>) -> Self {
32        let mut map: FxHashMap<ContainerID, Diff> = Default::default();
33        let mut order: Vec<ContainerID> = Vec::with_capacity(diff.len());
34        for d in diff.into_iter() {
35            for item in d.diff.into_iter() {
36                let old = map.insert(item.id.clone(), item.diff);
37                assert!(old.is_none(), "Duplicate container ID in diff events");
38                order.push(item.id.clone());
39            }
40        }
41
42        Self {
43            cid_to_events: map,
44            order,
45        }
46    }
47
48    pub fn compose(&mut self, other: &Self) {
49        if other.cid_to_events.is_empty() {
50            return;
51        }
52
53        for (id, diff) in other.iter() {
54            if let Some(this_diff) = self.cid_to_events.get_mut(id) {
55                this_diff.compose_ref(diff);
56            } else {
57                self.cid_to_events.insert(id.clone(), diff.clone());
58                self.order.push(id.clone());
59            }
60        }
61    }
62
63    pub fn transform(&mut self, other: &Self, left_priority: bool) {
64        if other.cid_to_events.is_empty() || self.cid_to_events.is_empty() {
65            return;
66        }
67
68        for (idx, diff) in self.cid_to_events.iter_mut() {
69            if let Some(b_diff) = other.cid_to_events.get(idx) {
70                diff.transform(b_diff, left_priority);
71            }
72        }
73    }
74
75    pub fn clear(&mut self) {
76        self.cid_to_events.clear();
77        self.order.clear();
78    }
79
80    pub fn iter(&self) -> impl Iterator<Item = (&ContainerID, &Diff)> + '_ {
81        self.order
82            .iter()
83            .map(|cid| (cid, self.cid_to_events.get(cid).unwrap()))
84    }
85
86    #[allow(clippy::should_implement_trait)]
87    pub fn into_iter(self) -> impl Iterator<Item = (ContainerID, Diff)> {
88        let mut cid_to_events = self.cid_to_events;
89        self.order.into_iter().map(move |cid| {
90            let d = cid_to_events.remove(&cid).unwrap();
91            (cid, d)
92        })
93    }
94}
95
96fn transform_cursor(
97    cursor_with_pos: &mut CursorWithPos,
98    remote_diff: &DiffBatch,
99    doc: &LoroDoc,
100    container_remap: &FxHashMap<ContainerID, ContainerID>,
101) {
102    let mut container_changed = false;
103    let mut cid = &cursor_with_pos.cursor.container;
104    while let Some(new_cid) = container_remap.get(cid) {
105        cid = new_cid;
106        container_changed = true;
107    }
108
109    if cursor_with_pos.cursor.id.is_none() {
110        // We don't need to transform a cursor that always points to the leftmost or rightmost position
111        if container_changed {
112            cursor_with_pos.cursor.container = cid.clone();
113        }
114        return;
115    }
116
117    if let Some(diff) = remote_diff.cid_to_events.get(cid) {
118        let new_pos = diff.transform_cursor(cursor_with_pos.pos.pos, false);
119        cursor_with_pos.pos.pos = new_pos;
120    };
121
122    let new_pos = cursor_with_pos.pos.pos;
123    match doc.get_handler(cid.clone()).unwrap() {
124        crate::handler::Handler::Text(h) => {
125            let Some(new_cursor) = h.get_cursor_internal(new_pos, cursor_with_pos.pos.side, false)
126            else {
127                return;
128            };
129
130            cursor_with_pos.cursor = new_cursor;
131        }
132        crate::handler::Handler::List(h) => {
133            let Some(new_cursor) = h.get_cursor(new_pos, cursor_with_pos.pos.side) else {
134                return;
135            };
136
137            cursor_with_pos.cursor = new_cursor;
138        }
139        crate::handler::Handler::MovableList(h) => {
140            let Some(new_cursor) = h.get_cursor(new_pos, cursor_with_pos.pos.side) else {
141                return;
142            };
143
144            cursor_with_pos.cursor = new_cursor;
145        }
146        crate::handler::Handler::Map(_) => {}
147        crate::handler::Handler::Tree(_) => {}
148        crate::handler::Handler::Unknown(_) => {}
149        #[cfg(feature = "counter")]
150        crate::handler::Handler::Counter(_) => {}
151    }
152}
153
154/// UndoManager is responsible for managing undo/redo from the current peer's perspective.
155///
156/// Undo/local is local: it cannot be used to undone the changes made by other peers.
157/// If you want to undo changes made by other peers, you may need to use the time travel feature.
158///
159/// PeerID cannot be changed during the lifetime of the UndoManager
160pub struct UndoManager {
161    peer: Arc<AtomicU64>,
162    container_remap: Arc<Mutex<FxHashMap<ContainerID, ContainerID>>>,
163    inner: Arc<parking_lot::ReentrantMutex<RefCell<UndoManagerInner>>>,
164    _peer_id_change_sub: Subscription,
165    _undo_sub: Subscription,
166    doc: LoroDoc,
167}
168
169impl std::fmt::Debug for UndoManager {
170    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
171        f.debug_struct("UndoManager")
172            .field("peer", &self.peer)
173            .field("container_remap", &self.container_remap)
174            .field("inner", &self.inner)
175            .finish()
176    }
177}
178
179#[derive(Debug, Clone, Copy, PartialEq, Eq)]
180pub enum UndoOrRedo {
181    Undo,
182    Redo,
183}
184
185impl UndoOrRedo {
186    fn opposite(&self) -> UndoOrRedo {
187        match self {
188            Self::Undo => Self::Redo,
189            Self::Redo => Self::Undo,
190        }
191    }
192}
193
194/// When a undo/redo item is pushed, the undo manager will call the on_push callback to get the meta data of the undo item.
195/// The returned cursors will be recorded for a new pushed undo item.
196pub type OnPush = Box<
197    dyn for<'a> Fn(UndoOrRedo, CounterSpan, Option<DiffEvent<'a>>) -> UndoItemMeta + Send + Sync,
198>;
199pub type OnPop = Box<dyn Fn(UndoOrRedo, CounterSpan, UndoItemMeta) + Send + Sync>;
200
201struct UndoManagerInner {
202    next_counter: Option<Counter>,
203    undo_stack: Stack,
204    redo_stack: Stack,
205    processing_undo: bool,
206    last_undo_time: i64,
207    merge_interval_in_ms: i64,
208    max_stack_size: usize,
209    exclude_origin_prefixes: Vec<Box<str>>,
210    last_popped_selection: Option<Vec<CursorWithPos>>,
211    on_push: Option<OnPush>,
212    on_pop: Option<OnPop>,
213    group: Option<UndoGroup>,
214}
215
216impl std::fmt::Debug for UndoManagerInner {
217    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
218        f.debug_struct("UndoManagerInner")
219            .field("latest_counter", &self.next_counter)
220            .field("undo_stack", &self.undo_stack)
221            .field("redo_stack", &self.redo_stack)
222            .field("processing_undo", &self.processing_undo)
223            .field("last_undo_time", &self.last_undo_time)
224            .field("merge_interval", &self.merge_interval_in_ms)
225            .field("max_stack_size", &self.max_stack_size)
226            .field("exclude_origin_prefixes", &self.exclude_origin_prefixes)
227            .field("group", &self.group)
228            .finish()
229    }
230}
231
232#[derive(Debug, Clone, Default)]
233struct UndoGroup {
234    start_counter: Counter,
235    affected_cids: FxHashSet<ContainerID>,
236}
237
238impl UndoGroup {
239    pub fn new(start_counter: Counter) -> Self {
240        Self {
241            start_counter,
242            affected_cids: Default::default(),
243        }
244    }
245}
246
247#[derive(Debug)]
248struct Stack {
249    stack: VecDeque<(VecDeque<StackItem>, Arc<Mutex<DiffBatch>>)>,
250    size: usize,
251}
252
253#[derive(Debug, Clone)]
254struct StackItem {
255    span: CounterSpan,
256    meta: UndoItemMeta,
257}
258
259/// The metadata of an undo item.
260///
261/// The cursors inside the metadata will be transformed by remote operations as well.
262/// So that when the item is popped, users can restore the cursors position correctly.
263#[derive(Debug, Default, Clone)]
264pub struct UndoItemMeta {
265    pub value: LoroValue,
266    pub cursors: Vec<CursorWithPos>,
267}
268
269#[derive(Debug, Clone)]
270pub struct CursorWithPos {
271    pub cursor: Cursor,
272    pub pos: AbsolutePosition,
273}
274
275impl UndoItemMeta {
276    pub fn new() -> Self {
277        Self {
278            value: LoroValue::Null,
279            cursors: Default::default(),
280        }
281    }
282
283    /// It's assumed that the cursor is just acquired before the ops that
284    /// need to be undo/redo.
285    ///
286    /// We need to rely on the validity of the original pos value
287    pub fn add_cursor(&mut self, cursor: &Cursor) {
288        self.cursors.push(CursorWithPos {
289            cursor: cursor.clone(),
290            pos: AbsolutePosition {
291                pos: cursor.origin_pos,
292                side: cursor.side,
293            },
294        });
295    }
296
297    pub fn set_value(&mut self, value: LoroValue) {
298        self.value = value;
299    }
300}
301
302impl Stack {
303    pub fn new() -> Self {
304        let mut stack = VecDeque::new();
305        stack.push_back((VecDeque::new(), Arc::new(Mutex::new(Default::default()))));
306        Stack { stack, size: 0 }
307    }
308
309    /// Peek the top-most StackItem's metadata without modifying the stack.
310    ///
311    /// Returns None if the stack is empty.
312    fn peek_top_meta(&self) -> Option<UndoItemMeta> {
313        if self.is_empty() {
314            return None;
315        }
316
317        for (items, _) in self.stack.iter().rev() {
318            if let Some(item) = items.back() {
319                return Some(item.meta.clone());
320            }
321        }
322
323        None
324    }
325
326    pub fn pop(&mut self) -> Option<(StackItem, Arc<Mutex<DiffBatch>>)> {
327        while self.stack.back().unwrap().0.is_empty() && self.stack.len() > 1 {
328            let (_, diff) = self.stack.pop_back().unwrap();
329            let diff = diff.lock();
330            if !diff.cid_to_events.is_empty() {
331                self.stack.back_mut().unwrap().1.lock().compose(&diff);
332            }
333        }
334
335        if self.stack.len() == 1 && self.stack.back().unwrap().0.is_empty() {
336            // If the stack is empty, we need to clear the remote diff
337            self.stack.back_mut().unwrap().1.lock().clear();
338            return None;
339        }
340
341        self.size -= 1;
342        let last = self.stack.back_mut().unwrap();
343        last.0.pop_back().map(|x| (x, last.1.clone()))
344        // If this row in stack is empty, we don't pop it right away
345        // Because we still need the remote diff to be available.
346        // Cursor position transformation relies on the remote diff in the same row.
347    }
348
349    pub fn push(&mut self, span: CounterSpan, meta: UndoItemMeta) {
350        self.push_with_merge(span, meta, false, None)
351    }
352
353    pub fn push_with_merge(
354        &mut self,
355        span: CounterSpan,
356        meta: UndoItemMeta,
357        can_merge: bool,
358        group: Option<&UndoGroup>,
359    ) {
360        let last = self.stack.back_mut().unwrap();
361        let last_remote_diff = last.1.lock();
362
363        // Check if the remote diff is disjoint with the current undo group
364        let is_disjoint_group = group.is_some_and(|g| {
365            g.affected_cids.iter().all(|cid| {
366                last_remote_diff
367                    .cid_to_events
368                    .get(cid)
369                    .is_none_or(|diff| diff.is_empty())
370            })
371        });
372
373        // Can't merge if remote diffs exist and it's not disjoint with the current undo group
374        let should_create_new_entry =
375            !last_remote_diff.cid_to_events.is_empty() && !is_disjoint_group;
376
377        if should_create_new_entry {
378            // Create a new entry in the stack
379            drop(last_remote_diff);
380            let mut v = VecDeque::new();
381            v.push_back(StackItem { span, meta });
382            self.stack
383                .push_back((v, Arc::new(Mutex::new(DiffBatch::default()))));
384            self.size += 1;
385            return;
386        }
387
388        // Try to merge with the previous entry if allowed
389        if can_merge {
390            if let Some(last_span) = last.0.back_mut() {
391                if last_span.span.end == span.start {
392                    // Merge spans by extending the end of the last span
393                    last_span.span.end = span.end;
394                    return;
395                }
396            }
397        }
398
399        // Add as a new item to the existing entry
400        self.size += 1;
401        last.0.push_back(StackItem { span, meta });
402    }
403
404    pub fn compose_remote_event(&mut self, diff: &[&ContainerDiff]) {
405        if self.is_empty() {
406            return;
407        }
408
409        let remote_diff = &mut self.stack.back_mut().unwrap().1;
410        let mut remote_diff = remote_diff.lock();
411        for e in diff {
412            if let Some(d) = remote_diff.cid_to_events.get_mut(&e.id) {
413                d.compose_ref(&e.diff);
414            } else {
415                remote_diff
416                    .cid_to_events
417                    .insert(e.id.clone(), e.diff.clone());
418                remote_diff.order.push(e.id.clone());
419            }
420        }
421    }
422
423    pub fn transform_based_on_this_delta(&mut self, diff: &DiffBatch) {
424        if self.is_empty() {
425            return;
426        }
427        let remote_diff = &mut self.stack.back_mut().unwrap().1;
428        remote_diff.lock().transform(diff, false);
429    }
430
431    pub fn clear(&mut self) {
432        self.stack = VecDeque::new();
433        self.stack.push_back((VecDeque::new(), Default::default()));
434        self.size = 0;
435    }
436
437    pub fn is_empty(&self) -> bool {
438        self.size == 0
439    }
440
441    pub fn len(&self) -> usize {
442        self.size
443    }
444
445    fn discard_empty_front_rows(&mut self) {
446        // Undo pop can leave an empty front row that only carries remote diffs.
447        // There is no older stack item for that diff to transform during trimming.
448        while self
449            .stack
450            .front()
451            .is_some_and(|(items, _)| items.is_empty())
452        {
453            self.stack.pop_front();
454        }
455    }
456
457    fn ensure_trailing_empty_row(&mut self) {
458        if self.stack.is_empty() {
459            self.stack
460                .push_back((VecDeque::new(), Arc::new(Mutex::new(Default::default()))));
461        }
462    }
463
464    fn pop_front(&mut self) {
465        if self.is_empty() {
466            return;
467        }
468
469        self.discard_empty_front_rows();
470        self.size -= 1;
471        let first = self.stack.front_mut().unwrap();
472        let f = first.0.pop_front();
473        assert!(f.is_some());
474        if first.0.is_empty() {
475            self.stack.pop_front();
476        }
477
478        self.ensure_trailing_empty_row();
479    }
480
481    fn set_top_meta(&mut self, meta: UndoItemMeta) {
482        let Some(top) = self.stack.back_mut() else {
483            return;
484        };
485        let Some(last) = top.0.back_mut() else {
486            return;
487        };
488        last.meta = meta;
489    }
490}
491
492impl Default for Stack {
493    fn default() -> Self {
494        Stack::new()
495    }
496}
497
498impl UndoManagerInner {
499    fn new(last_counter: Counter) -> Self {
500        Self {
501            next_counter: Some(last_counter),
502            undo_stack: Default::default(),
503            redo_stack: Default::default(),
504            processing_undo: false,
505            merge_interval_in_ms: 0,
506            last_undo_time: 0,
507            max_stack_size: usize::MAX,
508            exclude_origin_prefixes: vec![],
509            last_popped_selection: None,
510            on_pop: None,
511            on_push: None,
512            group: None,
513        }
514    }
515
516    /// Returns true if a given container diff is disjoint with the current group.
517    /// They are disjoint if they have no overlap in changed container ids.
518    fn is_disjoint_with_group(&self, diff: &[&ContainerDiff]) -> bool {
519        let Some(group) = &self.group else {
520            return false;
521        };
522
523        diff.iter().all(|d| !group.affected_cids.contains(&d.id))
524    }
525
526    fn record_checkpoint(this: &RefCell<Self>, latest_counter: Counter, event: Option<DiffEvent>) {
527        let previous_counter = this.borrow().next_counter;
528
529        if Some(latest_counter) == this.borrow().next_counter {
530            return;
531        }
532
533        if this.borrow().next_counter.is_none() {
534            this.borrow_mut().next_counter = Some(latest_counter);
535            return;
536        }
537
538        if let Some(group) = &mut this.borrow_mut().group {
539            event.iter().for_each(|e| {
540                e.events.iter().for_each(|e| {
541                    group.affected_cids.insert(e.id.clone());
542                })
543            });
544        }
545
546        let now = get_sys_timestamp() as Timestamp;
547        let span = CounterSpan::new(this.borrow().next_counter.unwrap(), latest_counter);
548        let meta = this
549            .borrow()
550            .on_push
551            .as_ref()
552            .map(|x| x(UndoOrRedo::Undo, span, event))
553            .unwrap_or_default();
554
555        let mut this = this.borrow_mut();
556        let this: &mut Self = &mut this;
557        // Wether the change is within the accepted merge interval
558        let in_merge_interval = now - this.last_undo_time < this.merge_interval_in_ms;
559
560        // If group is active, but there is nothing in the group, don't merge
561        // If the group is active and it's not the first push in the group, merge
562        let group_should_merge = this.group.is_some()
563            && match (
564                previous_counter,
565                this.group.as_ref().map(|g| g.start_counter),
566            ) {
567                (Some(previous), Some(active)) => previous != active,
568                _ => true,
569            };
570
571        let should_merge = !this.undo_stack.is_empty() && (in_merge_interval || group_should_merge);
572
573        if should_merge {
574            this.undo_stack
575                .push_with_merge(span, meta, true, this.group.as_ref());
576        } else {
577            this.last_undo_time = now;
578            this.undo_stack.push(span, meta);
579        }
580
581        this.next_counter = Some(latest_counter);
582        this.redo_stack.clear();
583        while this.undo_stack.len() > this.max_stack_size {
584            this.undo_stack.pop_front();
585        }
586    }
587}
588
589fn get_counter_end(doc: &LoroDoc, peer: PeerID) -> Counter {
590    doc.oplog().lock().vv().get(&peer).cloned().unwrap_or(0)
591}
592
593impl UndoManager {
594    pub fn new(doc: &LoroDoc) -> Self {
595        let peer = Arc::new(AtomicU64::new(doc.peer_id()));
596        let peer_clone = peer.clone();
597        let peer_clone2 = peer.clone();
598        let inner = Arc::new(ReentrantMutex::new(RefCell::new(UndoManagerInner::new(
599            get_counter_end(doc, doc.peer_id()),
600        ))));
601        let inner_clone = inner.clone();
602        let inner_clone2 = inner.clone();
603        let remap_containers = Arc::new(Mutex::new(FxHashMap::default()));
604        let remap_containers_clone = remap_containers.clone();
605        let undo_sub = doc.subscribe_root(Arc::new(move |event| match event.event_meta.by {
606            EventTriggerKind::Local => {
607                // TODO: PERF undo can be significantly faster if we can get
608                // the DiffBatch for undo here
609                let lock = inner_clone.lock();
610                if lock.borrow().processing_undo {
611                    return;
612                }
613                if let Some(id) = event
614                    .event_meta
615                    .to
616                    .iter()
617                    .find(|x| x.peer == peer_clone.load(std::sync::atomic::Ordering::Relaxed))
618                {
619                    let should_exclude = lock
620                        .borrow()
621                        .exclude_origin_prefixes
622                        .iter()
623                        .any(|x| event.event_meta.origin.starts_with(&**x));
624                    if should_exclude {
625                        // If the event is from the excluded origin, we don't record it
626                        // in the undo stack. But we need to record its effect like it's
627                        // a remote event.
628                        let mut inner = lock.borrow_mut();
629                        inner.undo_stack.compose_remote_event(event.events);
630                        inner.redo_stack.compose_remote_event(event.events);
631                        inner.next_counter = Some(id.counter + 1);
632                    } else {
633                        UndoManagerInner::record_checkpoint(&lock, id.counter + 1, Some(event));
634                    }
635                }
636            }
637            EventTriggerKind::Import => {
638                let lock = inner_clone.lock();
639                let mut inner = lock.borrow_mut();
640
641                for e in event.events {
642                    if let Diff::Tree(tree) = &e.diff {
643                        for item in &tree.diff {
644                            let target = item.target;
645                            if let TreeExternalDiff::Create { .. } = &item.action {
646                                // If the concurrent event is a create event, it may bring the deleted tree node back,
647                                // so we need to remove it from the remap of the container.
648                                remap_containers_clone
649                                    .lock()
650                                    .remove(&target.associated_meta_container());
651                            }
652                        }
653                    }
654                }
655
656                let is_import_disjoint = inner.is_disjoint_with_group(event.events);
657
658                inner.undo_stack.compose_remote_event(event.events);
659                inner.redo_stack.compose_remote_event(event.events);
660
661                // If the import is not disjoint, we end the active group
662                // all subsequent changes will be new undo items
663                if !is_import_disjoint {
664                    inner.group = None;
665                }
666            }
667            EventTriggerKind::Checkout => {
668                let lock = inner_clone.lock();
669                let mut inner = lock.borrow_mut();
670                inner.undo_stack.clear();
671                inner.redo_stack.clear();
672                inner.next_counter = None;
673            }
674        }));
675
676        let sub = doc.subscribe_peer_id_change(Box::new(move |id| {
677            let lock = inner_clone2.lock();
678            let mut inner = lock.borrow_mut();
679            inner.undo_stack.clear();
680            inner.redo_stack.clear();
681            inner.next_counter = Some(id.counter);
682            peer_clone2.store(id.peer, std::sync::atomic::Ordering::Relaxed);
683            true
684        }));
685
686        UndoManager {
687            peer,
688            container_remap: remap_containers,
689            inner,
690            _peer_id_change_sub: sub,
691            _undo_sub: undo_sub,
692            doc: doc.clone(),
693        }
694    }
695
696    pub fn group_start(&self) -> LoroResult<()> {
697        let lock = self.inner.lock();
698        let mut inner = lock.borrow_mut();
699
700        if inner.group.is_some() {
701            return Err(LoroError::UndoGroupAlreadyStarted);
702        }
703
704        inner.group =
705            Some(UndoGroup::new(inner.next_counter.ok_or_else(|| {
706                LoroError::Unknown("UndoManager is not ready".into())
707            })?));
708
709        Ok(())
710    }
711
712    pub fn group_end(&self) {
713        self.inner.lock().borrow_mut().group = None;
714    }
715
716    pub fn peer(&self) -> PeerID {
717        self.peer.load(std::sync::atomic::Ordering::Relaxed)
718    }
719
720    pub fn set_merge_interval(&self, interval: i64) {
721        self.inner.lock().borrow_mut().merge_interval_in_ms = interval;
722    }
723
724    pub fn set_max_undo_steps(&self, size: usize) {
725        self.inner.lock().borrow_mut().max_stack_size = size;
726    }
727
728    pub fn add_exclude_origin_prefix(&self, prefix: &str) {
729        self.inner
730            .lock()
731            .borrow_mut()
732            .exclude_origin_prefixes
733            .push(prefix.into());
734    }
735
736    pub fn record_new_checkpoint(&self) -> LoroResult<()> {
737        // Use implicit-style barrier to preserve next-commit options across
738        // an empty commit before undo/redo processing.
739        self.doc.with_barrier(|| {});
740        let counter = get_counter_end(&self.doc, self.peer());
741        UndoManagerInner::record_checkpoint(&self.inner.lock(), counter, None);
742        Ok(())
743    }
744
745    #[instrument(skip_all)]
746    pub fn undo(&self) -> LoroResult<bool> {
747        self.perform(
748            |x| &mut x.undo_stack,
749            |x| &mut x.redo_stack,
750            UndoOrRedo::Undo,
751        )
752    }
753
754    #[instrument(skip_all)]
755    pub fn redo(&self) -> LoroResult<bool> {
756        self.perform(
757            |x| &mut x.redo_stack,
758            |x| &mut x.undo_stack,
759            UndoOrRedo::Redo,
760        )
761    }
762
763    fn perform(
764        &self,
765        get_stack: impl Fn(&mut UndoManagerInner) -> &mut Stack,
766        get_opposite: impl Fn(&mut UndoManagerInner) -> &mut Stack,
767        kind: UndoOrRedo,
768    ) -> LoroResult<bool> {
769        let doc = &self.doc.clone();
770        // When in the undo/redo loop, the new undo/redo stack item should restore the selection
771        // to the state it was in before the item that was popped two steps ago from the stack.
772        //
773        //                          ┌────────────┐
774        //                          │Selection 1 │
775        //                          └─────┬──────┘
776        //                                │   Some
777        //                                ▼   ops
778        //                          ┌────────────┐
779        //                          │Selection 2 │
780        //                          └─────┬──────┘
781        //                                │   Some
782        //                                ▼   ops
783        //                          ┌────────────┐
784        //                          │Selection 3 │◁ ─ ─ ─ ─ ─ ─ ─  Restore  ─ ─ ─
785        //                          └─────┬──────┘                               │
786        //                                │
787        //                                │                                      │
788        //                                │                              ┌ ─ ─ ─ ─ ─ ─ ─
789        //           Enter the            │   Undo ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─▶   Push Redo   │
790        //           undo/redo ─ ─ ─ ▶    ▼                              └ ─ ─ ─ ─ ─ ─ ─
791        //             loop         ┌────────────┐                               │
792        //                          │Selection 2 │◁─ ─ ─  Restore  ─
793        //                          └─────┬──────┘                  │            │
794        //                                │
795        //                                │                         │            │
796        //                                │                 ┌ ─ ─ ─ ─ ─ ─ ─
797        //                                │   Undo ─ ─ ─ ─ ▶   Push Redo   │     │
798        //                                ▼                 └ ─ ─ ─ ─ ─ ─ ─
799        //                          ┌────────────┐                  │            │
800        //                          │Selection 1 │
801        //                          └─────┬──────┘                  │            │
802        //                                │   Redo ◀ ─ ─ ─ ─ ─ ─ ─ ─
803        //                                ▼                                      │
804        //                          ┌────────────┐
805        //         ┌   Restore   ─ ▷│Selection 2 │                               │
806        //                          └─────┬──────┘
807        //         │                      │                                      │
808        // ┌ ─ ─ ─ ─ ─ ─ ─                │
809        //    Push Undo   │◀─ ─ ─ ─ ─ ─ ─ │   Redo ◀ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘
810        // └ ─ ─ ─ ─ ─ ─ ─                ▼
811        //         │                ┌────────────┐
812        //                          │Selection 3 │
813        //         │                └─────┬──────┘
814        //          ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ▶ │   Undo
815        //                                ▼
816        //                          ┌────────────┐
817        //                          │Selection 2 │
818        //                          └────────────┘
819        //
820        // Because users may change the selections during the undo/redo loop, it's
821        // more stable to keep the selection stored in the last stack item
822        // rather than using the current selection directly.
823        self.record_new_checkpoint()?;
824        let end_counter = get_counter_end(doc, self.peer());
825        let mut top = {
826            let lock = self.inner.lock();
827            let mut inner = lock.borrow_mut();
828            inner.processing_undo = true;
829            get_stack(&mut inner).pop()
830        };
831
832        let mut executed = false;
833        while let Some((mut span, remote_diff)) = top {
834            let mut next_push_selection = None;
835            {
836                let inner = self.inner.clone();
837                // We need to clone this because otherwise <transform_delta> will be applied to the same remote diff
838                let remote_change_clone = remote_diff.lock().clone();
839                let commit = doc.undo_internal(
840                    IdSpan {
841                        peer: self.peer(),
842                        counter: span.span,
843                    },
844                    &mut self.container_remap.lock(),
845                    Some(&remote_change_clone),
846                    &mut |diff| {
847                        info_span!("transform remote diff").in_scope(|| {
848                            let inner = inner.lock();
849                            // <transform_delta>
850                            get_stack(&mut inner.borrow_mut()).transform_based_on_this_delta(diff);
851                        });
852                    },
853                )?;
854                drop(commit);
855                let inner = self.inner.lock();
856                let mut is_some = false;
857
858                if let Some(on_pop) = inner.borrow().on_pop.as_ref() {
859                    is_some = true;
860                    for cursor in span.meta.cursors.iter_mut() {
861                        // <cursor_transform> We need to transform cursor here.
862                        // Note that right now <transform_delta> is already done,
863                        // remote_diff is also transformed by it now (that's what we need).
864                        transform_cursor(
865                            cursor,
866                            &remote_diff.lock(),
867                            doc,
868                            &self.container_remap.lock(),
869                        );
870                    }
871
872                    on_pop(kind, span.span, span.meta.clone());
873                }
874                if is_some {
875                    let take = inner.borrow_mut().last_popped_selection.take();
876                    next_push_selection = take;
877                    inner.borrow_mut().last_popped_selection = Some(span.meta.cursors);
878                }
879            }
880            let new_counter = get_counter_end(doc, self.peer());
881            if end_counter != new_counter {
882                let inner = self.inner.lock();
883                let mut meta = inner
884                    .borrow()
885                    .on_push
886                    .as_ref()
887                    .map(|x| {
888                        x(
889                            kind.opposite(),
890                            CounterSpan::new(end_counter, new_counter),
891                            None,
892                        )
893                    })
894                    .unwrap_or_default();
895
896                if matches!(kind, UndoOrRedo::Undo)
897                    && get_opposite(&mut inner.borrow_mut()).is_empty()
898                {
899                    // If it's the first undo, we use the cursors from the users
900                } else if let Some(inner) = next_push_selection.take() {
901                    // Otherwise, we use the cursors from the undo/redo loop
902                    meta.cursors = inner;
903                }
904
905                get_opposite(&mut inner.borrow_mut())
906                    .push(CounterSpan::new(end_counter, new_counter), meta);
907                inner.borrow_mut().next_counter = Some(new_counter);
908                executed = true;
909                break;
910            } else {
911                // continue to pop the undo item as this undo is a no-op
912                top = get_stack(&mut self.inner.lock().borrow_mut()).pop();
913                continue;
914            }
915        }
916
917        self.inner.lock().borrow_mut().processing_undo = false;
918        Ok(executed)
919    }
920
921    pub fn can_undo(&self) -> bool {
922        !self.inner.lock().borrow().undo_stack.is_empty()
923    }
924
925    pub fn can_redo(&self) -> bool {
926        !self.inner.lock().borrow().redo_stack.is_empty()
927    }
928
929    pub fn undo_count(&self) -> usize {
930        self.inner.lock().borrow().undo_stack.len()
931    }
932
933    pub fn redo_count(&self) -> usize {
934        self.inner.lock().borrow().redo_stack.len()
935    }
936
937    /// Get the metadata of the top undo stack item, if any.
938    pub fn top_undo_meta(&self) -> Option<UndoItemMeta> {
939        self.inner.lock().borrow().undo_stack.peek_top_meta()
940    }
941
942    /// Get the metadata of the top redo stack item, if any.
943    pub fn top_redo_meta(&self) -> Option<UndoItemMeta> {
944        self.inner.lock().borrow().redo_stack.peek_top_meta()
945    }
946
947    /// Get the value associated with the top undo stack item, if any.
948    pub fn top_undo_value(&self) -> Option<LoroValue> {
949        self.top_undo_meta().map(|m| m.value)
950    }
951
952    /// Get the value associated with the top redo stack item, if any.
953    pub fn top_redo_value(&self) -> Option<LoroValue> {
954        self.top_redo_meta().map(|m| m.value)
955    }
956
957    pub fn set_on_push(&self, on_push: Option<OnPush>) {
958        self.inner.lock().borrow_mut().on_push = on_push;
959    }
960
961    pub fn set_on_pop(&self, on_pop: Option<OnPop>) {
962        self.inner.lock().borrow_mut().on_pop = on_pop;
963    }
964
965    pub fn clear(&self) {
966        self.inner.lock().borrow_mut().undo_stack.clear();
967        self.inner.lock().borrow_mut().redo_stack.clear();
968    }
969
970    /// Clear only the redo stack, preserving the undo stack.
971    pub fn clear_redo(&self) {
972        self.inner.lock().borrow_mut().redo_stack.clear();
973    }
974
975    /// Clear only the undo stack, preserving the redo stack.
976    pub fn clear_undo(&self) {
977        self.inner.lock().borrow_mut().undo_stack.clear();
978    }
979
980    pub fn set_top_undo_meta(&self, meta: UndoItemMeta) {
981        self.inner.lock().borrow_mut().undo_stack.set_top_meta(meta);
982    }
983
984    pub fn set_top_redo_meta(&self, meta: UndoItemMeta) {
985        self.inner.lock().borrow_mut().redo_stack.set_top_meta(meta);
986    }
987}
988
989/// Undo the given spans of operations.
990///
991/// # Parameters
992///
993/// - `spans`: A vector of tuples where each tuple contains an `IdSpan` and its associated `Frontiers`.
994///   - `IdSpan`: Represents a span of operations identified by an ID.
995///   - `Frontiers`: Represents the deps of the given id_span
996/// - `latest_frontiers`: The latest frontiers of the document
997/// - `calc_diff`: A closure that takes two `Frontiers` and calculates the difference between them, returning a `DiffBatch`.
998///
999/// # Returns
1000///
1001/// - `DiffBatch`: Applying this batch on the `latest_frontiers` will undo the ops in the given spans.
1002pub(crate) fn undo(
1003    spans: Vec<(IdSpan, Frontiers)>,
1004    last_frontiers_or_last_bi: Either<&Frontiers, &DiffBatch>,
1005    calc_diff: impl Fn(&Frontiers, &Frontiers) -> DiffBatch,
1006    on_last_event_a: &mut dyn FnMut(&DiffBatch),
1007) -> DiffBatch {
1008    // The process of performing undo is:
1009    //
1010    // 0. Split the span into a series of continuous spans. There is no external dep within each continuous span.
1011    //
1012    // For each continuous span_i:
1013    //
1014    // 1. a. Calculate the event of checkout from id_span.last to id_span.deps, call it Ai. It undo the ops in the current span.
1015    //    b. Calculate A'i = Ai + T(Ci-1, Ai) if i > 0, otherwise A'i = Ai.
1016    //       NOTE: A'i can undo the ops in the current span and the previous spans, if it's applied on the id_span.last version.
1017    // 2. Calculate the event of checkout from id_span.last to [the next span's last id] or [the latest version], call it Bi.
1018    // 3. Transform event A'i based on Bi, call it Ci
1019    // 4. If span_i is the last span, apply Ci to the current state.
1020
1021    // -------------------------------------------------------
1022    // 0. Split the span into a series of continuous spans
1023    // -------------------------------------------------------
1024
1025    let mut last_ci: Option<DiffBatch> = None;
1026    for i in 0..spans.len() {
1027        debug_span!("Undo", ?i, "Undo span {:?}", &spans[i]).in_scope(|| {
1028            let (this_id_span, this_deps) = &spans[i];
1029            // ---------------------------------------
1030            // 1.a Calc event A_i
1031            // ---------------------------------------
1032            let mut event_a_i = debug_span!("1. Calc event A_i").in_scope(|| {
1033                // Checkout to the last id of the id_span
1034                calc_diff(&this_id_span.id_last().into(), this_deps)
1035            });
1036
1037            // println!("event_a_i: {:?}", event_a_i);
1038
1039            // ---------------------------------------
1040            // 2. Calc event B_i
1041            // ---------------------------------------
1042            let stack_diff_batch;
1043            let event_b_i = 'block: {
1044                let next = if i + 1 < spans.len() {
1045                    spans[i + 1].0.id_last().into()
1046                } else {
1047                    match last_frontiers_or_last_bi {
1048                        Either::Left(last_frontiers) => last_frontiers.clone(),
1049                        Either::Right(right) => break 'block right,
1050                    }
1051                };
1052                stack_diff_batch = Some(calc_diff(&this_id_span.id_last().into(), &next));
1053                stack_diff_batch.as_ref().unwrap()
1054            };
1055
1056            // println!("event_b_i: {:?}", event_b_i);
1057
1058            // event_a_prime can undo the ops in the current span and the previous spans
1059            let mut event_a_prime = if let Some(mut last_ci) = last_ci.take() {
1060                // ------------------------------------------------------------------------------
1061                // 1.b Transform and apply Ci-1 based on Ai, call it A'i
1062                // ------------------------------------------------------------------------------
1063                last_ci.transform(&event_a_i, true);
1064
1065                event_a_i.compose(&last_ci);
1066                event_a_i
1067            } else {
1068                event_a_i
1069            };
1070            if i == spans.len() - 1 {
1071                on_last_event_a(&event_a_prime);
1072            }
1073            // --------------------------------------------------
1074            // 3. Transform event A'_i based on B_i, call it C_i
1075            // --------------------------------------------------
1076            event_a_prime.transform(event_b_i, true);
1077
1078            // println!("event_a_prime: {:?}", event_a_prime);
1079
1080            let c_i = event_a_prime;
1081            last_ci = Some(c_i);
1082        });
1083    }
1084
1085    last_ci.unwrap()
1086}