2021-12-07 19:59:08 +08:00
|
|
|
use crate::{
|
|
|
|
core::{FlowyStr, Interval, OpBuilder, OperationTransformable},
|
2022-01-15 23:58:36 +08:00
|
|
|
errors::OTError,
|
2021-12-07 19:59:08 +08:00
|
|
|
};
|
2022-01-16 13:44:14 +08:00
|
|
|
use serde::{Deserialize, Serialize, __private::Formatter};
|
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},
|
|
|
|
};
|
|
|
|
|
2021-12-07 10:39:01 +08:00
|
|
|
pub trait Attributes: fmt::Display + Eq + PartialEq + Default + Clone + Debug + OperationTransformable {
|
|
|
|
fn is_empty(&self) -> bool;
|
|
|
|
|
|
|
|
// Remove the empty attribute which value is None.
|
|
|
|
fn remove_empty(&mut self);
|
|
|
|
|
|
|
|
fn extend_other(&mut self, other: Self);
|
|
|
|
}
|
|
|
|
|
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,
|
2022-01-09 11:12:34 +08:00
|
|
|
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) => {
|
2021-12-07 10:39:01 +08:00
|
|
|
left = Some(OpBuilder::<T>::delete(index).build());
|
|
|
|
right = Some(OpBuilder::<T>::delete(*n - index).build());
|
2022-01-23 12:14:00 +08:00
|
|
|
}
|
2021-08-13 14:13:31 +08:00
|
|
|
Operation::Retain(retain) => {
|
2021-12-07 10:39:01 +08:00
|
|
|
left = Some(OpBuilder::<T>::delete(index).build());
|
|
|
|
right = Some(OpBuilder::<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();
|
2021-10-01 19:39:08 +08:00
|
|
|
left = Some(
|
2021-12-07 10:39:01 +08:00
|
|
|
OpBuilder::<T>::insert(&insert.s[0..index])
|
2021-10-01 19:39:08 +08:00
|
|
|
.attributes(attributes.clone())
|
|
|
|
.build(),
|
|
|
|
);
|
2021-08-13 14:13:31 +08:00
|
|
|
right = Some(
|
2022-01-09 11:12:34 +08:00
|
|
|
OpBuilder::<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 {
|
2021-08-13 18:16:52 +08:00
|
|
|
Operation::Delete(n) => OpBuilder::delete(min(*n, interval.size())).build(),
|
|
|
|
Operation::Retain(retain) => OpBuilder::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) => {
|
2022-01-09 11:12:34 +08:00
|
|
|
if interval.start > insert.utf16_size() {
|
2021-08-13 18:16:52 +08:00
|
|
|
OpBuilder::insert("").build()
|
2021-08-08 22:29:16 +08:00
|
|
|
} else {
|
2022-01-09 11:12:34 +08:00
|
|
|
let s = insert.s.sub_str(interval).unwrap_or_else(|| "".to_owned());
|
2021-11-12 21:44:26 +08:00
|
|
|
OpBuilder::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) => {
|
2021-08-10 11:22:57 +08:00
|
|
|
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) => {
|
2021-08-10 11:22:57 +08:00
|
|
|
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> {
|
|
|
|
// #[serde(rename(serialize = "retain", deserialize = "retain"))]
|
2021-08-06 23:06:27 +08:00
|
|
|
pub n: usize,
|
2021-12-07 10:39:01 +08:00
|
|
|
// #[serde(skip_serializing_if = "is_empty")]
|
|
|
|
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-08-10 11:22:57 +08:00
|
|
|
}
|
|
|
|
|
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 {
|
2021-08-13 18:16:52 +08:00
|
|
|
Some(OpBuilder::retain(n).attributes(attributes).build())
|
2021-08-05 20:05:40 +08:00
|
|
|
}
|
|
|
|
}
|
2021-08-10 11:22:57 +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> {
|
|
|
|
// #[serde(rename(serialize = "insert", deserialize = "insert"))]
|
2021-11-12 21:44:26 +08:00
|
|
|
pub s: FlowyStr,
|
2021-08-05 20:05:40 +08:00
|
|
|
|
2021-12-07 10:39:01 +08:00
|
|
|
// #[serde(skip_serializing_if = "is_empty")]
|
|
|
|
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,
|
|
|
|
{
|
2021-08-10 11:22:57 +08:00
|
|
|
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') {
|
2021-08-10 11:22:57 +08:00
|
|
|
s.pop();
|
|
|
|
if s.is_empty() {
|
2021-11-12 21:44:26 +08:00
|
|
|
s = "new_line".into();
|
2021-08-10 11:22:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-08-10 11:22:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-12-07 10:39:01 +08:00
|
|
|
Some(OpBuilder::<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)]
|
2022-01-15 23:58:36 +08:00
|
|
|
pub struct PlainTextAttributes();
|
|
|
|
impl fmt::Display for PlainTextAttributes {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("PlainTextAttributes") }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Attributes for PlainTextAttributes {
|
|
|
|
fn is_empty(&self) -> bool { true }
|
|
|
|
|
|
|
|
fn remove_empty(&mut self) {}
|
|
|
|
|
|
|
|
fn extend_other(&mut self, _other: Self) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OperationTransformable for PlainTextAttributes {
|
|
|
|
fn compose(&self, _other: &Self) -> Result<Self, OTError> { Ok(self.clone()) }
|
|
|
|
|
|
|
|
fn transform(&self, other: &Self) -> Result<(Self, Self), OTError> { Ok((self.clone(), other.clone())) }
|
|
|
|
|
|
|
|
fn invert(&self, _other: &Self) -> Self { self.clone() }
|
|
|
|
}
|