use std::time::Duration;
use canvist_model::Selection;
use canvist_model::Step;
use crate::CommandResult;
use crate::EditKind;
use crate::EditorState;
use crate::Origin;
use crate::Update;
pub trait History: Send {
fn record(&mut self, update: &Update, now: Duration);
fn undo(&mut self, state: &mut EditorState) -> CommandResult;
fn redo(&mut self, state: &mut EditorState) -> CommandResult;
fn can_undo(&self) -> bool;
fn can_redo(&self) -> bool;
fn close_group(&mut self);
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct HistoryConfig {
pub group_delay: Duration,
pub depth: usize,
}
impl Default for HistoryConfig {
fn default() -> Self {
Self {
group_delay: Duration::from_millis(500),
depth: 100,
}
}
}
#[derive(Clone, Debug, Default)]
pub struct UndoGrouping {
config: HistoryConfig,
last: Option<GroupEnd>,
}
#[derive(Clone, Copy, Debug)]
struct GroupEnd {
kind: EditKind,
at: Duration,
selection: Selection,
}
impl UndoGrouping {
#[must_use]
pub fn new(config: HistoryConfig) -> Self {
Self { config, last: None }
}
pub fn joins(&mut self, update: &Update, now: Duration) -> bool {
let joins = self.last.is_some_and(|last| {
group(update.kind).is_some()
&& group(update.kind) == group(last.kind)
&& now.saturating_sub(last.at) <= self.config.group_delay
&& update.selection_before == last.selection
});
self.last = Some(GroupEnd {
kind: update.kind,
at: now,
selection: update.selection,
});
joins
}
pub fn close(&mut self) {
self.last = None;
}
}
#[derive(Clone, Debug)]
struct Entry {
inverses: Vec<Step>,
selection: Selection,
selection_after: Selection,
kind: EditKind,
at: Duration,
}
#[derive(Clone, Debug, Default)]
pub struct UndoHistory {
config: HistoryConfig,
grouping: UndoGrouping,
done: Vec<Entry>,
undone: Vec<Entry>,
}
impl UndoHistory {
#[must_use]
pub fn new(config: HistoryConfig) -> Self {
Self {
config,
grouping: UndoGrouping::new(config),
..Self::default()
}
}
#[must_use]
pub fn undo_depth(&self) -> usize {
self.done.len()
}
fn revert(
from: &mut Vec<Entry>,
to: &mut Vec<Entry>,
state: &mut EditorState,
) -> CommandResult {
let Some(entry) = from.pop() else {
return Ok(None);
};
let steps = entry.inverses.iter().rev().cloned();
let update =
state.apply_steps(steps, entry.kind, Origin::History, Some(entry.selection))?;
to.push(Entry {
inverses: update.commit.inverses.clone(),
selection: entry.selection_after,
selection_after: entry.selection,
kind: entry.kind,
at: entry.at,
});
Ok(Some(update))
}
}
const fn group(kind: EditKind) -> Option<u8> {
match kind {
EditKind::Typing | EditKind::Composition => Some(0),
EditKind::Deleting => Some(1),
_ => None,
}
}
impl History for UndoHistory {
fn record(&mut self, update: &Update, now: Duration) {
match update.origin {
Origin::History => return,
Origin::Remote => {
self.done.clear();
self.undone.clear();
self.grouping.close();
return;
}
Origin::Local => {}
}
if !update.doc_changed() {
return;
}
self.undone.clear();
if self.grouping.joins(update, now) {
if let Some(last) = self.done.last_mut() {
last.inverses.extend(update.commit.inverses.iter().cloned());
last.selection_after = update.selection;
last.at = now;
}
return;
}
self.done.push(Entry {
inverses: update.commit.inverses.clone(),
selection: update.selection_before,
selection_after: update.selection,
kind: update.kind,
at: now,
});
if self.done.len() > self.config.depth {
self.done.remove(0);
}
}
fn undo(&mut self, state: &mut EditorState) -> CommandResult {
self.grouping.close();
Self::revert(&mut self.done, &mut self.undone, state)
}
fn redo(&mut self, state: &mut EditorState) -> CommandResult {
self.grouping.close();
Self::revert(&mut self.undone, &mut self.done, state)
}
fn can_undo(&self) -> bool {
!self.done.is_empty()
}
fn can_redo(&self) -> bool {
!self.undone.is_empty()
}
fn close_group(&mut self) {
self.grouping.close();
}
}