use std::ops::Deref;
use std::ops::DerefMut;
use canvist_model::Attrs;
use canvist_model::BlockId;
use canvist_model::Commit;
use canvist_model::MarkSet;
use canvist_model::Selection;
use canvist_model::StepMap;
use canvist_model::Transaction;
use crate::Affinity;
use crate::Composition;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum EditKind {
Selection,
Typing,
Deleting,
Composition,
Formatting,
Structure,
Paste,
InputRule,
Dictation,
Remote,
}
impl EditKind {
#[must_use]
pub const fn groups(self) -> bool {
matches!(self, Self::Typing | Self::Deleting | Self::Composition)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Origin {
Local,
History,
Remote,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Update {
pub commit: Commit,
pub selection_before: Selection,
pub selection: Selection,
pub kind: EditKind,
pub origin: Origin,
}
impl Update {
#[must_use]
pub fn doc_changed(&self) -> bool {
!self.commit.is_empty()
}
#[must_use]
pub fn selection_changed(&self) -> bool {
self.selection != self.selection_before
}
#[must_use]
pub fn then(self, next: Self) -> Self {
Self {
commit: compose(self.commit, next.commit),
selection_before: self.selection_before,
selection: next.selection,
kind: self.kind,
origin: self.origin,
}
}
}
pub(crate) fn compose(mut first: Commit, second: Commit) -> Commit {
let removed = removed_blocks(&second.maps);
first.touched.retain(|id| !removed.contains(id));
first.touched.extend(second.touched);
first.touched.sort_unstable();
first.touched.dedup();
first.steps.extend(second.steps);
first.inverses.extend(second.inverses);
first.maps.extend(second.maps);
first.structural |= second.structural;
first.version = second.version;
first
}
fn removed_blocks(maps: &[StepMap]) -> Vec<BlockId> {
let mut removed = Vec::new();
for map in maps {
match map {
StepMap::Removed { blocks, .. } => removed.extend_from_slice(blocks),
StepMap::Join { next, .. } => removed.push(*next),
StepMap::Identity | StepMap::Inline { .. } | StepMap::Split { .. } => {}
}
}
removed
}
#[derive(Clone, Debug, Default)]
pub(crate) enum Override<T> {
#[default]
Default,
Set(T),
}
#[derive(Clone, Debug)]
pub(crate) struct Effects {
pub(crate) kind: EditKind,
pub(crate) origin: Origin,
pub(crate) selection: Option<Selection>,
pub(crate) stored_marks: Override<Option<MarkSet>>,
pub(crate) composition: Override<Option<Composition>>,
pub(crate) goal: Option<f32>,
pub(crate) affinity: Option<Affinity>,
pub(crate) settings: Attrs,
}
impl Effects {
pub(crate) const fn new(kind: EditKind, origin: Origin) -> Self {
Self {
kind,
origin,
selection: None,
stored_marks: Override::Default,
composition: Override::Default,
goal: None,
affinity: None,
settings: Attrs::new(),
}
}
}
#[derive(Debug)]
pub struct EditorTransaction<'doc> {
inner: Transaction<'doc>,
selection_before: Selection,
effects: Effects,
}
impl<'doc> EditorTransaction<'doc> {
pub(crate) fn new(
inner: Transaction<'doc>,
selection_before: Selection,
kind: EditKind,
) -> Self {
Self {
inner,
selection_before,
effects: Effects::new(kind, Origin::Local),
}
}
#[must_use]
pub fn selection_before(&self) -> Selection {
self.selection_before
}
pub fn set_selection(&mut self, selection: Selection) {
self.effects.selection = Some(selection);
}
pub fn set_stored_marks(&mut self, marks: Option<MarkSet>) {
self.effects.stored_marks = Override::Set(marks);
}
pub fn set_setting(&mut self, key: &str, value: impl Into<canvist_model::Value>) {
self.effects.settings.set(key, value);
}
pub fn set_composition(&mut self, composition: Option<Composition>) {
self.effects.composition = Override::Set(composition);
}
pub fn set_goal(&mut self, goal: f32) {
self.effects.goal = Some(goal);
}
pub fn set_affinity(&mut self, affinity: Affinity) {
self.effects.affinity = Some(affinity);
}
pub fn set_kind(&mut self, kind: EditKind) {
self.effects.kind = kind;
}
pub(crate) fn finish(self) -> (Transaction<'doc>, Effects) {
(self.inner, self.effects)
}
}
impl<'doc> Deref for EditorTransaction<'doc> {
type Target = Transaction<'doc>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl DerefMut for EditorTransaction<'_> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}