2020-10-04 12:15:43 +04:00
|
|
|
use crate::{ChangeSet, Rope, State, Transaction};
|
2020-10-05 11:12:36 +04:00
|
|
|
use smallvec::{smallvec, SmallVec};
|
2020-10-04 12:15:43 +04:00
|
|
|
|
|
|
|
/// Undo-tree style history store.
|
2021-06-07 18:34:19 +04:00
|
|
|
#[derive(Debug)]
|
2020-10-04 12:15:43 +04:00
|
|
|
pub struct History {
|
|
|
|
revisions: Vec<Revision>,
|
|
|
|
cursor: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Revision {
|
|
|
|
parent: usize,
|
2020-10-05 11:12:36 +04:00
|
|
|
children: SmallVec<[(usize, Transaction); 1]>,
|
2020-10-04 12:15:43 +04:00
|
|
|
/// The transaction to revert to previous state.
|
|
|
|
revert: Transaction,
|
|
|
|
// selection before, selection after?
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for History {
|
|
|
|
fn default() -> Self {
|
|
|
|
// Add a dummy root revision with empty transaction
|
|
|
|
Self {
|
|
|
|
revisions: vec![Revision {
|
|
|
|
parent: 0,
|
2020-10-05 11:12:36 +04:00
|
|
|
children: SmallVec::new(),
|
2020-10-04 12:15:43 +04:00
|
|
|
revert: Transaction::from(ChangeSet::new(&Rope::new())),
|
|
|
|
}],
|
|
|
|
cursor: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl History {
|
|
|
|
pub fn commit_revision(&mut self, transaction: &Transaction, original: &State) {
|
2020-10-05 11:12:36 +04:00
|
|
|
// TODO: could store a single transaction, if deletes also stored the text they delete
|
2021-03-31 10:45:18 +04:00
|
|
|
let revert = transaction
|
|
|
|
.invert(&original.doc)
|
|
|
|
// Store the current cursor position
|
|
|
|
.with_selection(original.selection.clone());
|
2020-10-04 12:15:43 +04:00
|
|
|
|
|
|
|
let new_cursor = self.revisions.len();
|
|
|
|
self.revisions.push(Revision {
|
|
|
|
parent: self.cursor,
|
2020-10-05 11:12:36 +04:00
|
|
|
children: SmallVec::new(),
|
2020-10-04 12:15:43 +04:00
|
|
|
revert,
|
|
|
|
});
|
|
|
|
|
2020-10-05 11:12:36 +04:00
|
|
|
// add a reference to the parent
|
|
|
|
self.revisions
|
|
|
|
.get_mut(self.cursor)
|
|
|
|
.unwrap() // TODO: get_unchecked_mut
|
|
|
|
.children
|
|
|
|
.push((new_cursor, transaction.clone()));
|
|
|
|
|
|
|
|
self.cursor = new_cursor;
|
2020-10-04 12:15:43 +04:00
|
|
|
}
|
|
|
|
|
2021-05-03 12:23:11 +04:00
|
|
|
#[inline]
|
|
|
|
pub fn current_revision(&self) -> usize {
|
|
|
|
self.cursor
|
|
|
|
}
|
|
|
|
|
2020-10-04 12:15:43 +04:00
|
|
|
#[inline]
|
2021-03-24 09:52:13 +04:00
|
|
|
pub const fn at_root(&self) -> bool {
|
2020-10-04 12:15:43 +04:00
|
|
|
self.cursor == 0
|
|
|
|
}
|
|
|
|
|
2021-06-02 19:47:50 +04:00
|
|
|
pub fn undo(&mut self) -> Option<&Transaction> {
|
2020-10-04 12:15:43 +04:00
|
|
|
if self.at_root() {
|
|
|
|
// We're at the root of undo, nothing to do.
|
2020-10-23 06:32:25 +04:00
|
|
|
return None;
|
2020-10-04 12:15:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
let current_revision = &self.revisions[self.cursor];
|
2020-10-05 11:12:36 +04:00
|
|
|
|
2020-10-04 12:15:43 +04:00
|
|
|
self.cursor = current_revision.parent;
|
2020-10-23 06:32:25 +04:00
|
|
|
|
2021-06-02 19:47:50 +04:00
|
|
|
Some(¤t_revision.revert)
|
2020-10-04 12:15:43 +04:00
|
|
|
}
|
|
|
|
|
2021-06-02 19:47:50 +04:00
|
|
|
pub fn redo(&mut self) -> Option<&Transaction> {
|
2020-10-04 12:15:43 +04:00
|
|
|
let current_revision = &self.revisions[self.cursor];
|
|
|
|
|
2020-10-05 11:12:36 +04:00
|
|
|
// for now, simply pick the latest child (linear undo / redo)
|
|
|
|
if let Some((index, transaction)) = current_revision.children.last() {
|
|
|
|
self.cursor = *index;
|
2020-10-23 06:32:25 +04:00
|
|
|
|
2021-06-02 19:47:50 +04:00
|
|
|
return Some(&transaction);
|
2020-10-05 11:12:36 +04:00
|
|
|
}
|
2020-10-23 06:32:25 +04:00
|
|
|
None
|
2020-10-04 12:15:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_undo_redo() {
|
|
|
|
let mut history = History::default();
|
|
|
|
let doc = Rope::from("hello");
|
|
|
|
let mut state = State::new(doc);
|
|
|
|
|
|
|
|
let transaction1 =
|
2021-03-18 08:28:27 +04:00
|
|
|
Transaction::change(&state.doc, vec![(5, 5, Some(" world!".into()))].into_iter());
|
2020-10-04 12:15:43 +04:00
|
|
|
|
|
|
|
// Need to commit before applying!
|
|
|
|
history.commit_revision(&transaction1, &state);
|
2021-03-31 10:45:18 +04:00
|
|
|
transaction1.apply(&mut state.doc);
|
2021-03-14 05:04:03 +04:00
|
|
|
assert_eq!("hello world!", state.doc);
|
2020-10-04 12:15:43 +04:00
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
let transaction2 =
|
2021-03-18 08:28:27 +04:00
|
|
|
Transaction::change(&state.doc, vec![(6, 11, Some("世界".into()))].into_iter());
|
2020-10-04 12:15:43 +04:00
|
|
|
|
|
|
|
// Need to commit before applying!
|
|
|
|
history.commit_revision(&transaction2, &state);
|
2021-03-31 10:45:18 +04:00
|
|
|
transaction2.apply(&mut state.doc);
|
2021-03-14 05:04:03 +04:00
|
|
|
assert_eq!("hello 世界!", state.doc);
|
2020-10-04 12:15:43 +04:00
|
|
|
|
|
|
|
// ---
|
2020-10-23 06:32:25 +04:00
|
|
|
fn undo(history: &mut History, state: &mut State) {
|
|
|
|
if let Some(transaction) = history.undo() {
|
2021-03-31 10:45:18 +04:00
|
|
|
transaction.apply(&mut state.doc);
|
2020-10-23 06:32:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fn redo(history: &mut History, state: &mut State) {
|
|
|
|
if let Some(transaction) = history.redo() {
|
2021-03-31 10:45:18 +04:00
|
|
|
transaction.apply(&mut state.doc);
|
2020-10-23 06:32:25 +04:00
|
|
|
}
|
|
|
|
}
|
2020-10-04 12:15:43 +04:00
|
|
|
|
2020-10-23 06:32:25 +04:00
|
|
|
undo(&mut history, &mut state);
|
2021-03-14 05:04:03 +04:00
|
|
|
assert_eq!("hello world!", state.doc);
|
2020-10-23 06:32:25 +04:00
|
|
|
redo(&mut history, &mut state);
|
2021-03-14 05:04:03 +04:00
|
|
|
assert_eq!("hello 世界!", state.doc);
|
2020-10-23 06:32:25 +04:00
|
|
|
undo(&mut history, &mut state);
|
|
|
|
undo(&mut history, &mut state);
|
2021-03-14 05:04:03 +04:00
|
|
|
assert_eq!("hello", state.doc);
|
2020-10-04 12:15:43 +04:00
|
|
|
|
|
|
|
// undo at root is a no-op
|
2020-10-23 06:32:25 +04:00
|
|
|
undo(&mut history, &mut state);
|
2021-03-14 05:04:03 +04:00
|
|
|
assert_eq!("hello", state.doc);
|
2020-10-04 12:15:43 +04:00
|
|
|
}
|
|
|
|
}
|