436 lines
12 KiB
Rust
Raw Normal View History

use crate::core::flowy_str::FlowyStr;
use crate::core::interval::Interval;
use crate::core::operation::OperationBuilder;
use crate::errors::OTError;
2022-01-16 13:44:14 +08:00
use serde::{Deserialize, Serialize, __private::Formatter};
use std::fmt::Display;
2021-08-05 20:05:40 +08:00
use std::{
2021-08-06 22:25:09 +08:00
cmp::min,
2021-08-05 20:05:40 +08:00
fmt,
2021-12-07 10:39:01 +08:00
fmt::Debug,
2021-08-05 20:05:40 +08:00
ops::{Deref, DerefMut},
};
pub trait OperationTransformable {
/// Merges the operation with `other` into one operation while preserving
/// the changes of both.
///
/// # Arguments
///
/// * `other`: The delta gonna to merge.
///
/// # Examples
///
/// ```
/// use lib_ot::core::{OperationTransformable, PlainTextDeltaBuilder};
/// let document = PlainTextDeltaBuilder::new().build();
/// let delta = PlainTextDeltaBuilder::new().insert("abc").build();
/// let new_document = document.compose(&delta).unwrap();
/// assert_eq!(new_document.content_str().unwrap(), "abc".to_owned());
/// ```
fn compose(&self, other: &Self) -> Result<Self, OTError>
where
Self: Sized;
/// Transforms two operations a and b that happened concurrently and
/// produces two operations a' and b'.
/// (a', b') = a.transform(b)
/// a.compose(b') = b.compose(a')
///
fn transform(&self, other: &Self) -> Result<(Self, Self), OTError>
where
Self: Sized;
/// Return the invert delta from the other. It can be used to do the undo operation.
///
/// # Arguments
///
/// * `other`: Generate the undo delta for [Other]. [Other] can compose the undo delta to return
/// to the previous state.
///
/// # Examples
///
/// ```
/// use lib_ot::core::{OperationTransformable, PlainTextDeltaBuilder};
/// let original_document = PlainTextDeltaBuilder::new().build();
/// let delta = PlainTextDeltaBuilder::new().insert("abc").build();
///
/// let undo_delta = delta.invert(&original_document);
/// let new_document = original_document.compose(&delta).unwrap();
/// let document = new_document.compose(&undo_delta).unwrap();
///
/// assert_eq!(original_document, document);
///
/// ```
fn invert(&self, other: &Self) -> Self;
}
2021-12-07 10:39:01 +08:00
/// Each operation can carry attributes. For example, the [RichTextAttributes] has a list of key/value attributes.
/// Such as { bold: true, italic: true }.
///
/// Because [Operation] is generic over the T, so you must specify the T. For example, the [PlainTextDelta]. It use
/// use [PhantomAttributes] as the T. [PhantomAttributes] does nothing, just a phantom.
///
pub trait Attributes: Default + Display + Eq + PartialEq + Clone + Debug + OperationTransformable {
fn is_empty(&self) -> bool {
true
}
/// Remove the empty attribute which value is None.
fn remove_empty(&mut self) {
// Do nothing
}
2021-12-07 10:39:01 +08:00
fn extend_other(&mut self, _other: Self) {
// Do nothing
}
2021-12-07 10:39:01 +08:00
}
/// [Operation] consists of three types.
/// * Delete
/// * Retain
/// * Insert
///
/// The [T] should support serde if you want to serialize/deserialize the operation
/// to json string. You could check out the operation_serde.rs for more information.
///
2021-09-22 14:42:14 +08:00
#[derive(Debug, Clone, Eq, PartialEq)]
2021-12-07 10:39:01 +08:00
pub enum Operation<T: Attributes> {
2021-08-06 23:06:27 +08:00
Delete(usize),
2021-12-07 10:39:01 +08:00
Retain(Retain<T>),
Insert(Insert<T>),
2021-08-05 20:05:40 +08:00
}
2021-12-07 10:39:01 +08:00
impl<T> Operation<T>
where
T: Attributes,
{
2021-08-11 23:34:35 +08:00
pub fn get_data(&self) -> &str {
2021-08-05 20:05:40 +08:00
match self {
2021-08-11 23:34:35 +08:00
Operation::Delete(_) => "",
Operation::Retain(_) => "",
Operation::Insert(insert) => &insert.s,
2021-08-05 20:05:40 +08:00
}
}
2021-12-07 10:39:01 +08:00
pub fn get_attributes(&self) -> T {
2021-08-05 20:05:40 +08:00
match self {
2021-12-07 10:39:01 +08:00
Operation::Delete(_) => T::default(),
2021-08-05 20:05:40 +08:00
Operation::Retain(retain) => retain.attributes.clone(),
Operation::Insert(insert) => insert.attributes.clone(),
}
}
2021-12-07 10:39:01 +08:00
pub fn set_attributes(&mut self, attributes: T) {
2021-08-05 20:05:40 +08:00
match self {
2021-08-15 22:08:03 +08:00
Operation::Delete(_) => log::error!("Delete should not contains attributes"),
Operation::Retain(retain) => retain.attributes = attributes,
Operation::Insert(insert) => insert.attributes = attributes,
2021-08-05 20:05:40 +08:00
}
}
2022-01-23 12:14:00 +08:00
pub fn has_attribute(&self) -> bool {
!self.get_attributes().is_empty()
}
2021-08-05 20:05:40 +08:00
2021-08-15 22:08:03 +08:00
pub fn len(&self) -> usize {
2021-11-27 19:19:41 +08:00
match self {
2021-08-05 20:05:40 +08:00
Operation::Delete(n) => *n,
Operation::Retain(r) => r.n,
Operation::Insert(i) => i.utf16_size(),
2021-11-27 19:19:41 +08:00
}
2021-08-05 20:05:40 +08:00
}
2022-01-23 12:14:00 +08:00
pub fn is_empty(&self) -> bool {
self.len() == 0
}
2021-08-06 22:25:09 +08:00
2021-08-14 16:44:39 +08:00
#[allow(dead_code)]
2021-12-07 10:39:01 +08:00
pub fn split(&self, index: usize) -> (Option<Operation<T>>, Option<Operation<T>>) {
2021-08-15 22:08:03 +08:00
debug_assert!(index < self.len());
2021-08-15 00:05:18 +08:00
let left;
let right;
2021-08-13 14:13:31 +08:00
match self {
Operation::Delete(n) => {
left = Some(OperationBuilder::<T>::delete(index).build());
right = Some(OperationBuilder::<T>::delete(*n - index).build());
2022-01-23 12:14:00 +08:00
}
2021-08-13 14:13:31 +08:00
Operation::Retain(retain) => {
left = Some(OperationBuilder::<T>::delete(index).build());
right = Some(OperationBuilder::<T>::delete(retain.n - index).build());
2022-01-23 12:14:00 +08:00
}
2021-08-13 14:13:31 +08:00
Operation::Insert(insert) => {
let attributes = self.get_attributes();
left = Some(
OperationBuilder::<T>::insert(&insert.s[0..index])
.attributes(attributes.clone())
.build(),
);
2021-08-13 14:13:31 +08:00
right = Some(
OperationBuilder::<T>::insert(&insert.s[index..insert.utf16_size()])
2021-08-13 14:13:31 +08:00
.attributes(attributes)
.build(),
);
2022-01-23 12:14:00 +08:00
}
2021-08-13 14:13:31 +08:00
}
(left, right)
}
2021-12-07 10:39:01 +08:00
pub fn shrink(&self, interval: Interval) -> Option<Operation<T>> {
2021-08-08 22:29:16 +08:00
let op = match self {
Operation::Delete(n) => OperationBuilder::delete(min(*n, interval.size())).build(),
Operation::Retain(retain) => OperationBuilder::retain(min(retain.n, interval.size()))
2021-08-08 22:29:16 +08:00
.attributes(retain.attributes.clone())
.build(),
2021-08-06 22:25:09 +08:00
Operation::Insert(insert) => {
if interval.start > insert.utf16_size() {
OperationBuilder::insert("").build()
2021-08-08 22:29:16 +08:00
} else {
let s = insert.s.sub_str(interval).unwrap_or_else(|| "".to_owned());
OperationBuilder::insert(&s)
.attributes(insert.attributes.clone())
.build()
2021-08-06 22:25:09 +08:00
}
2022-01-23 12:14:00 +08:00
}
2021-08-08 22:29:16 +08:00
};
match op.is_empty() {
true => None,
false => Some(op),
2021-08-06 22:25:09 +08:00
}
}
2021-08-13 18:16:52 +08:00
pub fn is_delete(&self) -> bool {
if let Operation::Delete(_) = self {
return true;
}
false
}
pub fn is_insert(&self) -> bool {
if let Operation::Insert(_) = self {
return true;
}
false
}
pub fn is_retain(&self) -> bool {
if let Operation::Retain(_) = self {
return true;
}
false
}
2021-11-12 21:44:26 +08:00
pub fn is_plain(&self) -> bool {
match self {
Operation::Delete(_) => true,
Operation::Retain(retain) => retain.is_plain(),
Operation::Insert(insert) => insert.is_plain(),
}
}
2021-08-05 20:05:40 +08:00
}
2021-12-07 10:39:01 +08:00
impl<T> fmt::Display for Operation<T>
where
T: Attributes,
{
2021-08-05 20:05:40 +08:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2021-08-10 17:08:47 +08:00
f.write_str("{")?;
2021-08-05 20:05:40 +08:00
match self {
Operation::Delete(n) => {
f.write_fmt(format_args!("delete: {}", n))?;
2022-01-23 12:14:00 +08:00
}
2021-08-05 20:05:40 +08:00
Operation::Retain(r) => {
f.write_fmt(format_args!("{}", r))?;
2022-01-23 12:14:00 +08:00
}
2021-08-05 20:05:40 +08:00
Operation::Insert(i) => {
f.write_fmt(format_args!("{}", i))?;
2022-01-23 12:14:00 +08:00
}
2021-08-05 20:05:40 +08:00
}
2021-08-10 17:08:47 +08:00
f.write_str("}")?;
2021-08-05 20:05:40 +08:00
Ok(())
}
}
2021-12-07 10:39:01 +08:00
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Retain<T: Attributes> {
2021-08-06 23:06:27 +08:00
pub n: usize,
2021-12-07 10:39:01 +08:00
pub attributes: T,
2021-08-05 20:05:40 +08:00
}
2021-12-07 10:39:01 +08:00
impl<T> fmt::Display for Retain<T>
where
T: Attributes,
{
2021-09-21 15:07:07 +08:00
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
if self.attributes.is_empty() {
f.write_fmt(format_args!("retain: {}", self.n))
} else {
f.write_fmt(format_args!("retain: {}, attributes: {}", self.n, self.attributes))
}
}
}
2021-12-07 10:39:01 +08:00
impl<T> Retain<T>
where
T: Attributes,
{
pub fn merge_or_new(&mut self, n: usize, attributes: T) -> Option<Operation<T>> {
2022-01-12 17:08:50 +08:00
// tracing::trace!(
// "merge_retain_or_new_op: len: {:?}, l: {} - r: {}",
// n,
// self.attributes,
// attributes
// );
2021-08-11 17:18:10 +08:00
if self.attributes == attributes {
self.n += n;
None
} else {
Some(OperationBuilder::retain(n).attributes(attributes).build())
2021-08-05 20:05:40 +08:00
}
}
2022-01-23 12:14:00 +08:00
pub fn is_plain(&self) -> bool {
self.attributes.is_empty()
}
2021-08-05 20:05:40 +08:00
}
2021-12-07 10:39:01 +08:00
impl<T> std::convert::From<usize> for Retain<T>
where
T: Attributes,
{
2021-08-06 23:06:27 +08:00
fn from(n: usize) -> Self {
2021-08-05 20:05:40 +08:00
Retain {
n,
2021-12-07 10:39:01 +08:00
attributes: T::default(),
2021-08-05 20:05:40 +08:00
}
}
}
2021-12-07 10:39:01 +08:00
impl<T> Deref for Retain<T>
where
T: Attributes,
{
2021-08-06 23:06:27 +08:00
type Target = usize;
2021-08-05 20:05:40 +08:00
2022-01-23 12:14:00 +08:00
fn deref(&self) -> &Self::Target {
&self.n
}
2021-08-05 20:05:40 +08:00
}
2021-12-07 10:39:01 +08:00
impl<T> DerefMut for Retain<T>
where
T: Attributes,
{
2022-01-23 12:14:00 +08:00
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.n
}
2021-08-05 20:05:40 +08:00
}
2021-12-07 10:39:01 +08:00
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Insert<T: Attributes> {
2021-11-12 21:44:26 +08:00
pub s: FlowyStr,
2021-12-07 10:39:01 +08:00
pub attributes: T,
2021-08-05 20:05:40 +08:00
}
2021-12-07 10:39:01 +08:00
impl<T> fmt::Display for Insert<T>
where
T: Attributes,
{
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let mut s = self.s.clone();
2021-11-27 19:19:41 +08:00
if s.ends_with('\n') {
s.pop();
if s.is_empty() {
2021-11-12 21:44:26 +08:00
s = "new_line".into();
}
}
2021-09-21 15:07:07 +08:00
if self.attributes.is_empty() {
f.write_fmt(format_args!("insert: {}", s))
} else {
f.write_fmt(format_args!("insert: {}, attributes: {}", s, self.attributes))
}
}
}
2021-12-07 10:39:01 +08:00
impl<T> Insert<T>
where
T: Attributes,
{
2022-01-23 12:14:00 +08:00
pub fn utf16_size(&self) -> usize {
self.s.utf16_size()
}
2021-08-05 20:05:40 +08:00
2021-12-07 10:39:01 +08:00
pub fn merge_or_new_op(&mut self, s: &str, attributes: T) -> Option<Operation<T>> {
2021-08-11 17:18:10 +08:00
if self.attributes == attributes {
self.s += s;
None
} else {
Some(OperationBuilder::<T>::insert(s).attributes(attributes).build())
2021-08-05 20:05:40 +08:00
}
}
2021-11-12 21:44:26 +08:00
2022-01-23 12:14:00 +08:00
pub fn is_plain(&self) -> bool {
self.attributes.is_empty()
}
2021-08-05 20:05:40 +08:00
}
2021-12-07 10:39:01 +08:00
impl<T> std::convert::From<String> for Insert<T>
where
T: Attributes,
{
2021-08-05 20:05:40 +08:00
fn from(s: String) -> Self {
Insert {
2021-11-12 21:44:26 +08:00
s: s.into(),
2021-12-07 10:39:01 +08:00
attributes: T::default(),
2021-08-05 20:05:40 +08:00
}
}
}
2021-12-07 10:39:01 +08:00
impl<T> std::convert::From<&str> for Insert<T>
where
T: Attributes,
{
2022-01-23 12:14:00 +08:00
fn from(s: &str) -> Self {
Insert::from(s.to_owned())
}
2021-08-05 20:05:40 +08:00
}
2021-11-12 21:44:26 +08:00
2021-12-07 10:39:01 +08:00
impl<T> std::convert::From<FlowyStr> for Insert<T>
where
T: Attributes,
{
2021-11-12 21:44:26 +08:00
fn from(s: FlowyStr) -> Self {
Insert {
s,
2021-12-07 10:39:01 +08:00
attributes: T::default(),
2021-11-12 21:44:26 +08:00
}
}
}
2022-01-15 23:58:36 +08:00
2022-01-16 13:44:14 +08:00
#[derive(Debug, Clone, Eq, PartialEq, Default, Serialize, Deserialize)]
pub struct PhantomAttributes();
impl fmt::Display for PhantomAttributes {
2022-01-24 17:35:58 +08:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("PhantomAttributes")
2022-01-24 17:35:58 +08:00
}
2022-01-15 23:58:36 +08:00
}
impl Attributes for PhantomAttributes {}
2022-01-15 23:58:36 +08:00
impl OperationTransformable for PhantomAttributes {
2022-01-24 17:35:58 +08:00
fn compose(&self, _other: &Self) -> Result<Self, OTError> {
Ok(self.clone())
}
2022-01-15 23:58:36 +08:00
2022-01-24 17:35:58 +08:00
fn transform(&self, other: &Self) -> Result<(Self, Self), OTError> {
Ok((self.clone(), other.clone()))
}
2022-01-15 23:58:36 +08:00
2022-01-24 17:35:58 +08:00
fn invert(&self, _other: &Self) -> Self {
self.clone()
}
2022-01-15 23:58:36 +08:00
}