use crate::change::ChangeSet;
use crate::editor::EditorId;
use crate::selection::Selections;
#[derive(Clone, Debug)]
pub struct Transaction {
pub changes: ChangeSet,
pub inverse: ChangeSet,
pub before: Selections,
pub after: Selections,
pub origin: EditorId,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum EditKind {
Typing,
Deleting,
Other,
}
#[derive(Clone, Debug)]
pub struct UndoStep {
pub transactions: Vec<Transaction>,
kind: EditKind,
}
#[derive(Clone, Debug)]
pub struct History {
undo: Vec<UndoStep>,
redo: Vec<UndoStep>,
group_open: bool,
limit: usize,
}
impl Default for History {
fn default() -> Self {
Self::with_limit(1000)
}
}
impl History {
pub fn with_limit(limit: usize) -> Self {
Self { undo: Vec::new(), redo: Vec::new(), group_open: false, limit: limit.max(1) }
}
pub fn push(&mut self, tx: Transaction, kind: EditKind) {
self.redo.clear();
match self.undo.last_mut() {
Some(step)
if self.group_open
&& kind != EditKind::Other
&& step.kind == kind
&& step.transactions.last().is_some_and(|t| t.origin == tx.origin) =>
{
step.transactions.push(tx);
}
_ => {
self.undo.push(UndoStep { transactions: vec![tx], kind });
if self.undo.len() > self.limit {
self.undo.remove(0);
}
}
}
self.group_open = true;
}
pub fn break_group(&mut self) {
self.group_open = false;
}
pub fn can_undo(&self) -> bool {
!self.undo.is_empty()
}
pub fn can_redo(&self) -> bool {
!self.redo.is_empty()
}
pub(crate) fn pop_undo(&mut self) -> Option<UndoStep> {
self.group_open = false;
self.undo.pop()
}
pub(crate) fn pop_redo(&mut self) -> Option<UndoStep> {
self.group_open = false;
self.redo.pop()
}
pub(crate) fn push_redo(&mut self, step: UndoStep) {
self.redo.push(step);
}
pub(crate) fn push_undo(&mut self, step: UndoStep) {
self.undo.push(step);
}
}