2021-08-13 18:16:52 +08:00
|
|
|
use crate::core::{Attribute, Attributes, Interval, OpBuilder};
|
2021-08-05 20:05:40 +08:00
|
|
|
use bytecount::num_chars;
|
2021-08-10 11:22:57 +08:00
|
|
|
use serde::__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,
|
|
|
|
ops::{Deref, DerefMut},
|
|
|
|
str::Chars,
|
|
|
|
};
|
|
|
|
|
2021-09-22 14:42:14 +08:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2021-08-05 20:05:40 +08:00
|
|
|
pub enum Operation {
|
2021-08-06 23:06:27 +08:00
|
|
|
Delete(usize),
|
2021-08-05 20:05:40 +08:00
|
|
|
Retain(Retain),
|
|
|
|
Insert(Insert),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Operation {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_attributes(&self) -> Attributes {
|
|
|
|
match self {
|
2021-08-10 14:24:41 +08:00
|
|
|
Operation::Delete(_) => Attributes::default(),
|
2021-08-05 20:05:40 +08:00
|
|
|
Operation::Retain(retain) => retain.attributes.clone(),
|
|
|
|
Operation::Insert(insert) => insert.attributes.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_attributes(&mut self, attributes: Attributes) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-11 17:18:10 +08:00
|
|
|
pub fn has_attribute(&self) -> bool { !self.get_attributes().is_empty() }
|
2021-08-05 20:05:40 +08:00
|
|
|
|
2021-09-13 15:51:13 +08:00
|
|
|
pub fn contain_attribute(&self, attribute: &Attribute) -> bool { self.get_attributes().contains_key(&attribute.key) }
|
2021-08-12 13:57:41 +08:00
|
|
|
|
2021-08-15 22:08:03 +08:00
|
|
|
pub fn len(&self) -> usize {
|
2021-08-05 20:05:40 +08:00
|
|
|
match self {
|
|
|
|
Operation::Delete(n) => *n,
|
|
|
|
Operation::Retain(r) => r.n,
|
|
|
|
Operation::Insert(i) => i.num_chars(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-15 22:08:03 +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-08-13 14:13:31 +08:00
|
|
|
pub fn split(&self, index: usize) -> (Option<Operation>, Option<Operation>) {
|
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-08-13 18:16:52 +08:00
|
|
|
left = Some(OpBuilder::delete(index).build());
|
|
|
|
right = Some(OpBuilder::delete(*n - index).build());
|
2021-08-13 14:13:31 +08:00
|
|
|
},
|
|
|
|
Operation::Retain(retain) => {
|
2021-08-13 18:16:52 +08:00
|
|
|
left = Some(OpBuilder::delete(index).build());
|
|
|
|
right = Some(OpBuilder::delete(retain.n - index).build());
|
2021-08-13 14:13:31 +08:00
|
|
|
},
|
|
|
|
Operation::Insert(insert) => {
|
|
|
|
let attributes = self.get_attributes();
|
2021-09-13 15:51:13 +08:00
|
|
|
left = Some(OpBuilder::insert(&insert.s[0..index]).attributes(attributes.clone()).build());
|
2021-08-13 14:13:31 +08:00
|
|
|
right = Some(
|
2021-08-13 18:16:52 +08:00
|
|
|
OpBuilder::insert(&insert.s[index..insert.num_chars()])
|
2021-08-13 14:13:31 +08:00
|
|
|
.attributes(attributes)
|
|
|
|
.build(),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
(left, right)
|
|
|
|
}
|
|
|
|
|
2021-08-08 22:29:16 +08:00
|
|
|
pub fn shrink(&self, interval: Interval) -> Option<Operation> {
|
|
|
|
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) => {
|
2021-08-14 16:44:39 +08:00
|
|
|
if interval.start > insert.num_chars() {
|
2021-08-13 18:16:52 +08:00
|
|
|
OpBuilder::insert("").build()
|
2021-08-08 22:29:16 +08:00
|
|
|
} else {
|
2021-08-14 16:44:39 +08:00
|
|
|
let chars = insert.chars().skip(interval.start);
|
2021-09-13 15:51:13 +08:00
|
|
|
let s = &chars.take(min(interval.size(), insert.num_chars())).collect::<String>();
|
2021-08-14 16:44:39 +08:00
|
|
|
|
2021-09-13 15:51:13 +08:00
|
|
|
OpBuilder::insert(s).attributes(insert.attributes.clone()).build()
|
2021-08-06 22:25:09 +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-08-05 20:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Operation {
|
|
|
|
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))?;
|
|
|
|
},
|
|
|
|
Operation::Retain(r) => {
|
2021-08-10 11:22:57 +08:00
|
|
|
f.write_fmt(format_args!("{}", r))?;
|
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))?;
|
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-09-22 14:42:14 +08:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
|
2021-08-05 20:05:40 +08:00
|
|
|
pub struct Retain {
|
|
|
|
#[serde(rename(serialize = "retain", deserialize = "retain"))]
|
2021-08-06 23:06:27 +08:00
|
|
|
pub n: usize,
|
2021-08-05 20:05:40 +08:00
|
|
|
#[serde(skip_serializing_if = "is_empty")]
|
|
|
|
pub attributes: Attributes,
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:22:57 +08:00
|
|
|
impl fmt::Display for Retain {
|
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-08-05 20:05:40 +08:00
|
|
|
impl Retain {
|
2021-09-13 15:51:13 +08:00
|
|
|
pub fn merge_or_new(&mut self, n: usize, attributes: Attributes) -> Option<Operation> {
|
2021-09-15 16:35:40 +08:00
|
|
|
log::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
|
|
|
|
2021-08-11 17:18:10 +08:00
|
|
|
pub fn is_plain(&self) -> bool { self.attributes.is_empty() }
|
2021-08-05 20:05:40 +08:00
|
|
|
}
|
|
|
|
|
2021-08-06 23:06:27 +08:00
|
|
|
impl std::convert::From<usize> for Retain {
|
|
|
|
fn from(n: usize) -> Self {
|
2021-08-05 20:05:40 +08:00
|
|
|
Retain {
|
|
|
|
n,
|
|
|
|
attributes: Attributes::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for Retain {
|
2021-08-06 23:06:27 +08:00
|
|
|
type Target = usize;
|
2021-08-05 20:05:40 +08:00
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target { &self.n }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for Retain {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target { &mut self.n }
|
|
|
|
}
|
|
|
|
|
2021-09-22 14:42:14 +08:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
|
2021-08-05 20:05:40 +08:00
|
|
|
pub struct Insert {
|
|
|
|
#[serde(rename(serialize = "insert", deserialize = "insert"))]
|
|
|
|
pub s: String,
|
|
|
|
|
|
|
|
#[serde(skip_serializing_if = "is_empty")]
|
|
|
|
pub attributes: Attributes,
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:22:57 +08:00
|
|
|
impl fmt::Display for Insert {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
|
|
|
let mut s = self.s.clone();
|
|
|
|
if s.ends_with("\n") {
|
|
|
|
s.pop();
|
|
|
|
if s.is_empty() {
|
|
|
|
s = "new_line".to_owned();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-08-05 20:05:40 +08:00
|
|
|
impl Insert {
|
|
|
|
pub fn as_bytes(&self) -> &[u8] { self.s.as_bytes() }
|
|
|
|
|
|
|
|
pub fn chars(&self) -> Chars<'_> { self.s.chars() }
|
|
|
|
|
2021-08-06 23:06:27 +08:00
|
|
|
pub fn num_chars(&self) -> usize { num_chars(self.s.as_bytes()) as _ }
|
2021-08-05 20:05:40 +08:00
|
|
|
|
|
|
|
pub fn merge_or_new_op(&mut self, s: &str, attributes: Attributes) -> Option<Operation> {
|
2021-08-11 17:18:10 +08:00
|
|
|
if self.attributes == attributes {
|
|
|
|
self.s += s;
|
|
|
|
None
|
|
|
|
} else {
|
2021-08-13 18:16:52 +08:00
|
|
|
Some(OpBuilder::insert(s).attributes(attributes).build())
|
2021-08-05 20:05:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::convert::From<String> for Insert {
|
|
|
|
fn from(s: String) -> Self {
|
|
|
|
Insert {
|
|
|
|
s,
|
|
|
|
attributes: Attributes::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::convert::From<&str> for Insert {
|
|
|
|
fn from(s: &str) -> Self { Insert::from(s.to_owned()) }
|
|
|
|
}
|
2021-08-11 17:18:10 +08:00
|
|
|
fn is_empty(attributes: &Attributes) -> bool { attributes.is_empty() }
|