use ropey::Rope;
use crate::selection::Selections;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EditKind {
Insert,
Delete,
Other,
}
#[derive(Clone)]
pub struct Snapshot {
pub rope: Rope,
pub selections: Selections,
}
#[derive(Default)]
pub struct History {
undo: Vec<Snapshot>,
redo: Vec<Snapshot>,
open_run: Option<EditKind>,
}
impl History {
pub fn record(&mut self, kind: EditKind, before: Rope, selections: &Selections) {
self.redo.clear();
if self.open_run == Some(kind) && kind != EditKind::Other {
return;
}
self.undo.push(Snapshot {
rope: before,
selections: selections.clone(),
});
self.open_run = Some(kind);
}
pub fn boundary(&mut self) {
self.open_run = None;
}
pub fn undo(&mut self, current: Rope, selections: &Selections) -> Option<Snapshot> {
let previous = self.undo.pop()?;
self.redo.push(Snapshot {
rope: current,
selections: selections.clone(),
});
self.open_run = None;
Some(previous)
}
pub fn redo(&mut self, current: Rope, selections: &Selections) -> Option<Snapshot> {
let next = self.redo.pop()?;
self.undo.push(Snapshot {
rope: current,
selections: selections.clone(),
});
self.open_run = None;
Some(next)
}
}