From 888cc81b13f9e2619bf4c958edd642341d82cd78 Mon Sep 17 00:00:00 2001 From: appflowy Date: Sun, 15 Aug 2021 22:08:03 +0800 Subject: [PATCH] rename some struct and fix warnings --- .../flowy-ot/src/client/view/format_ext.rs | 20 +++--- .../flowy-ot/src/client/view/insert_ext.rs | 4 +- rust-lib/flowy-ot/src/client/view/view.rs | 2 +- rust-lib/flowy-ot/src/core/delta/cursor.rs | 72 ++++++++++--------- rust-lib/flowy-ot/src/core/delta/delta.rs | 8 +-- .../flowy-ot/src/core/operation/operation.rs | 18 ++--- .../src/core/operation/operation_serde.rs | 6 +- rust-lib/flowy-ot/tests/helper/mod.rs | 7 +- 8 files changed, 65 insertions(+), 72 deletions(-) diff --git a/rust-lib/flowy-ot/src/client/view/format_ext.rs b/rust-lib/flowy-ot/src/client/view/format_ext.rs index 1eb8429a8e..0bd1789241 100644 --- a/rust-lib/flowy-ot/src/client/view/format_ext.rs +++ b/rust-lib/flowy-ot/src/client/view/format_ext.rs @@ -1,5 +1,5 @@ use crate::{ - client::view::{util::find_newline, FormatExt, NEW_LINE}, + client::view::{util::find_newline, FormatExt}, core::{ Attribute, AttributeKey, @@ -33,15 +33,15 @@ impl FormatExt for FormatLinkAtCaretPositionExt { if let Some(before) = before { if before.contain_attribute(attribute) { - start -= before.length(); - retain += before.length(); + start -= before.len(); + retain += before.len(); } } if let Some(after) = after { if after.contain_attribute(attribute) { if retain != 0 { - retain += after.length(); + retain += after.len(); } } } @@ -76,14 +76,14 @@ impl FormatExt for ResolveBlockFormatExt { while start < end && iter.has_next() { let next_op = iter.next_op_before(end - start).unwrap(); match find_newline(next_op.get_data()) { - None => new_delta.retain(next_op.length(), Attributes::empty()), + None => new_delta.retain(next_op.len(), Attributes::empty()), Some(_) => { let tmp_delta = line_break(&next_op, attribute, AttributeScope::Block); new_delta.extend(tmp_delta); }, } - start += next_op.length(); + start += next_op.len(); } while iter.has_next() { @@ -92,7 +92,7 @@ impl FormatExt for ResolveBlockFormatExt { .expect("Unexpected None, iter.has_next() must return op"); match find_newline(op.get_data()) { - None => new_delta.retain(op.length(), Attributes::empty()), + None => new_delta.retain(op.len(), Attributes::empty()), Some(line_break) => { debug_assert_eq!(line_break, 0); new_delta.retain(1, attribute.clone().into()); @@ -123,14 +123,14 @@ impl FormatExt for ResolveInlineFormatExt { while start < end && iter.has_next() { let next_op = iter.next_op_before(end - start).unwrap(); match find_newline(next_op.get_data()) { - None => new_delta.retain(next_op.length(), attribute.clone().into()), + None => new_delta.retain(next_op.len(), attribute.clone().into()), Some(_) => { let tmp_delta = line_break(&next_op, attribute, AttributeScope::Inline); new_delta.extend(tmp_delta); }, } - start += next_op.length(); + start += next_op.len(); } Some(new_delta) @@ -140,7 +140,7 @@ impl FormatExt for ResolveInlineFormatExt { fn line_break(op: &Operation, attribute: &Attribute, scope: AttributeScope) -> Delta { let mut new_delta = Delta::new(); let mut start = 0; - let end = op.length(); + let end = op.len(); let mut s = op.get_data(); while let Some(line_break) = find_newline(s) { diff --git a/rust-lib/flowy-ot/src/client/view/insert_ext.rs b/rust-lib/flowy-ot/src/client/view/insert_ext.rs index 000b172b46..917cd6a138 100644 --- a/rust-lib/flowy-ot/src/client/view/insert_ext.rs +++ b/rust-lib/flowy-ot/src/client/view/insert_ext.rs @@ -1,6 +1,6 @@ use crate::{ client::view::InsertExt, - core::{AttributeKey, Attributes, CharMetric, Delta, DeltaBuilder, DeltaIter, Operation}, + core::{AttributeKey, Attributes, CharMetric, Delta, DeltaBuilder, DeltaIter}, }; pub const NEW_LINE: &'static str = "\n"; @@ -169,7 +169,7 @@ impl InsertExt for DefaultInsertExt { fn ext_name(&self) -> &str { "DefaultInsertExt" } fn apply(&self, delta: &Delta, replace_len: usize, text: &str, index: usize) -> Option { - let mut iter = DeltaIter::new(delta); + let iter = DeltaIter::new(delta); let mut attributes = Attributes::new(); if text.ends_with(NEW_LINE) { diff --git a/rust-lib/flowy-ot/src/client/view/view.rs b/rust-lib/flowy-ot/src/client/view/view.rs index 090a82249f..95e6c74d26 100644 --- a/rust-lib/flowy-ot/src/client/view/view.rs +++ b/rust-lib/flowy-ot/src/client/view/view.rs @@ -1,6 +1,6 @@ use crate::{ client::view::*, - core::{Attribute, Delta, Interval, Operation}, + core::{Attribute, Delta, Interval}, errors::{ErrorBuilder, OTError, OTErrorCode}, }; diff --git a/rust-lib/flowy-ot/src/core/delta/cursor.rs b/rust-lib/flowy-ot/src/core/delta/cursor.rs index 27a5077cbb..0086c491b8 100644 --- a/rust-lib/flowy-ot/src/core/delta/cursor.rs +++ b/rust-lib/flowy-ot/src/core/delta/cursor.rs @@ -9,8 +9,8 @@ pub struct Cursor<'a> { pub(crate) delta: &'a Delta, pub(crate) origin_iv: Interval, pub(crate) next_iv: Interval, - pub(crate) iter_char_count: usize, - pub(crate) iter_op_index: usize, + pub(crate) cur_char_count: usize, + pub(crate) cur_op_index: usize, iter: Enumerate>, next_op: Option, } @@ -22,8 +22,8 @@ impl<'a> Cursor<'a> { delta, origin_iv: interval, next_iv: interval, - iter_char_count: 0, - iter_op_index: 0, + cur_char_count: 0, + cur_op_index: 0, iter: delta.ops.iter().enumerate(), next_op: None, }; @@ -31,11 +31,13 @@ impl<'a> Cursor<'a> { cursor } - pub fn next_iv(&self) -> Interval { self.next_op_iv_before(None) } + // get the next operation interval + pub fn next_iv(&self) -> Interval { self.next_iv_before(None) } pub fn next_op(&mut self) -> Option { self.next_op_before(None) } - pub fn next_op_before(&mut self, b_index: Option) -> Option { + // get the last operation before the index + pub fn next_op_before(&mut self, index: Option) -> Option { let mut find_op = None; let next_op = self.next_op.take(); let mut next_op = next_op.as_ref(); @@ -44,35 +46,35 @@ impl<'a> Cursor<'a> { next_op = find_next_op(self); } - let old_iter_char_count = self.iter_char_count; + let pre_char_count = self.cur_char_count; while find_op.is_none() && next_op.is_some() { let op = next_op.take().unwrap(); - let interval = self.next_op_iv_before(b_index); + let interval = self.next_iv_before(index); if interval.is_empty() { self.next_op = Some(op.clone()); break; } find_op = op.shrink(interval); - let suffix = Interval::new(0, op.length()).suffix(interval); + let suffix = Interval::new(0, op.len()).suffix(interval); if !suffix.is_empty() { self.next_op = op.shrink(suffix); } - self.iter_char_count += interval.end; - self.next_iv.start = self.iter_char_count; + self.cur_char_count += interval.end; + self.next_iv.start = self.cur_char_count; if find_op.is_none() { next_op = find_next_op(self); } } - if find_op.is_some() { - let last = self.iter_char_count - old_iter_char_count; - let force_len = b_index.unwrap_or(0); - if force_len > last { - let len = force_len - last; - return self.next_op_before(Some(len)); + if find_op.is_some() && index.is_some() { + // try to find the next op before the index if iter_char_count less than index + let pos = self.cur_char_count - pre_char_count; + let end = index.unwrap(); + if end > pos { + return self.next_op_before(Some(end - pos)); } } return find_op; @@ -83,16 +85,16 @@ impl<'a> Cursor<'a> { fn descend(&mut self, index: usize) { self.next_iv.start += index; - if self.iter_char_count >= self.next_iv.start { + if self.cur_char_count >= self.next_iv.start { return; } while let Some((o_index, op)) = self.iter.next() { - self.iter_op_index = o_index; - let start = self.iter_char_count; - let end = start + op.length(); + self.cur_op_index = o_index; + let start = self.cur_char_count; + let end = start + op.len(); let intersect = Interval::new(start, end).intersect(self.next_iv); if intersect.is_empty() { - self.iter_char_count += op.length(); + self.cur_char_count += op.len(); } else { self.next_op = Some(op.clone()); break; @@ -105,8 +107,8 @@ impl<'a> Cursor<'a> { if next_op.is_none() { let mut offset = 0; for op in &self.delta.ops { - offset += op.length(); - if offset > self.iter_char_count { + offset += op.len(); + if offset > self.cur_char_count { next_op = Some(op); break; } @@ -115,17 +117,17 @@ impl<'a> Cursor<'a> { next_op } - fn next_op_iv_before(&self, b_index: Option) -> Interval { + fn next_iv_before(&self, index: Option) -> Interval { let next_op = self.next_iter_op(); if next_op.is_none() { return Interval::new(0, 0); } let op = next_op.unwrap(); - let start = self.iter_char_count; - let end = match b_index { - None => self.iter_char_count + op.length(), - Some(b_index) => self.iter_char_count + min(b_index, op.length()), + let start = self.cur_char_count; + let end = match index { + None => self.cur_char_count + op.len(), + Some(index) => self.cur_char_count + min(index, op.len()), }; let intersect = Interval::new(start, end).intersect(self.next_iv); let interval = intersect.translate_neg(start); @@ -137,7 +139,7 @@ fn find_next_op<'a>(cursor: &mut Cursor<'a>) -> Option<&'a Operation> { match cursor.iter.next() { None => None, Some((o_index, op)) => { - cursor.iter_op_index = o_index; + cursor.cur_op_index = o_index; Some(op) }, } @@ -152,11 +154,11 @@ pub struct OpMetric {} impl Metric for OpMetric { fn seek(cursor: &mut Cursor, index: usize) -> SeekResult { - let _ = check_bound(cursor.iter_op_index, index)?; - let mut temp_cursor = Cursor::new(cursor.delta, cursor.origin_iv); + let _ = check_bound(cursor.cur_op_index, index)?; + let mut seek_cursor = Cursor::new(cursor.delta, cursor.origin_iv); let mut offset = 0; - while let Some((_, op)) = temp_cursor.iter.next() { - offset += op.length(); + while let Some((_, op)) = seek_cursor.iter.next() { + offset += op.len(); if offset > index { break; } @@ -170,7 +172,7 @@ pub struct CharMetric {} impl Metric for CharMetric { fn seek(cursor: &mut Cursor, index: usize) -> SeekResult { - let _ = check_bound(cursor.iter_char_count, index)?; + let _ = check_bound(cursor.cur_char_count, index)?; let _ = cursor.next_op_before(Some(index)); Ok(()) diff --git a/rust-lib/flowy-ot/src/core/delta/delta.rs b/rust-lib/flowy-ot/src/core/delta/delta.rs index 0970f02588..6dbb86d94e 100644 --- a/rust-lib/flowy-ot/src/core/delta/delta.rs +++ b/rust-lib/flowy-ot/src/core/delta/delta.rs @@ -175,7 +175,7 @@ impl Delta { .next_op_before(length) .unwrap_or(OpBuilder::retain(length).build()); - debug_assert_eq!(op.length(), other_op.length()); + debug_assert_eq!(op.len(), other_op.len()); match (&op, &other_op) { (Operation::Retain(retain), Operation::Retain(other_retain)) => { @@ -554,7 +554,7 @@ impl Delta { log::debug!("other: {}", other); let mut index = 0; for op in &self.ops { - let len: usize = op.length() as usize; + let len: usize = op.len() as usize; match op { Operation::Delete(n) => { invert_from_other(&mut inverted, other, op, index, index + *n); @@ -629,10 +629,10 @@ fn invert_from_other( log::debug!( "invert retain: {} by retain len: {}, {}", retain, - other_op.length(), + other_op.len(), inverted_attrs ); - base.retain(other_op.length(), inverted_attrs); + base.retain(other_op.len(), inverted_attrs); }, Operation::Insert(_) => { log::error!("Impossible to here. Insert operation should be treated as delete") diff --git a/rust-lib/flowy-ot/src/core/operation/operation.rs b/rust-lib/flowy-ot/src/core/operation/operation.rs index 68e045c86e..c085bae01b 100644 --- a/rust-lib/flowy-ot/src/core/operation/operation.rs +++ b/rust-lib/flowy-ot/src/core/operation/operation.rs @@ -34,15 +34,9 @@ impl Operation { pub fn set_attributes(&mut self, attributes: Attributes) { match self { - Operation::Delete(_) => { - log::error!("Delete should not contains attributes"); - }, - Operation::Retain(retain) => { - retain.attributes = attributes; - }, - Operation::Insert(insert) => { - insert.attributes = attributes; - }, + Operation::Delete(_) => log::error!("Delete should not contains attributes"), + Operation::Retain(retain) => retain.attributes = attributes, + Operation::Insert(insert) => insert.attributes = attributes, } } @@ -52,7 +46,7 @@ impl Operation { self.get_attributes().contains_key(&attribute.key) } - pub fn length(&self) -> usize { + pub fn len(&self) -> usize { match self { Operation::Delete(n) => *n, Operation::Retain(r) => r.n, @@ -60,11 +54,11 @@ impl Operation { } } - pub fn is_empty(&self) -> bool { self.length() == 0 } + pub fn is_empty(&self) -> bool { self.len() == 0 } #[allow(dead_code)] pub fn split(&self, index: usize) -> (Option, Option) { - debug_assert!(index < self.length()); + debug_assert!(index < self.len()); let left; let right; match self { diff --git a/rust-lib/flowy-ot/src/core/operation/operation_serde.rs b/rust-lib/flowy-ot/src/core/operation/operation_serde.rs index b99739a082..e2a28a75f2 100644 --- a/rust-lib/flowy-ot/src/core/operation/operation_serde.rs +++ b/rust-lib/flowy-ot/src/core/operation/operation_serde.rs @@ -1,8 +1,8 @@ -use crate::core::{Attributes, Delta, Operation}; +use crate::core::{Attributes, Operation}; use serde::{ de, - de::{MapAccess, SeqAccess, Visitor}, - ser::{SerializeMap, SerializeSeq}, + de::{MapAccess, Visitor}, + ser::SerializeMap, Deserialize, Deserializer, Serialize, diff --git a/rust-lib/flowy-ot/tests/helper/mod.rs b/rust-lib/flowy-ot/tests/helper/mod.rs index 4073c6ac50..2ccc5db34f 100644 --- a/rust-lib/flowy-ot/tests/helper/mod.rs +++ b/rust-lib/flowy-ot/tests/helper/mod.rs @@ -1,8 +1,5 @@ use derive_more::Display; -use flowy_ot::{ - client::Document, - core::{REMOVE_FLAG, *}, -}; +use flowy_ot::{client::Document, core::*}; use rand::{prelude::*, Rng as WrappedRng}; use std::{sync::Once, time::Duration}; @@ -63,7 +60,7 @@ impl OpTester { static INIT: Once = Once::new(); INIT.call_once(|| { color_eyre::install().unwrap(); - std::env::set_var("RUST_LOG", "debug"); + std::env::set_var("RUST_LOG", "info"); env_logger::init(); });