This commit is contained in:
appflowy 2021-08-15 21:36:03 +08:00
parent aef5e54c3f
commit 0962d5cd0f
6 changed files with 48 additions and 48 deletions

View File

@ -27,7 +27,7 @@ impl FormatExt for FormatLinkAtCaretPositionExt {
let mut iter = DeltaIter::new(delta);
iter.seek::<CharMetric>(interval.start);
let (before, after) = (iter.next_op_with_len(interval.size()), iter.next());
let (before, after) = (iter.next_op_before(interval.size()), iter.next());
let mut start = interval.end;
let mut retain = 0;
@ -74,7 +74,7 @@ impl FormatExt for ResolveBlockFormatExt {
let mut start = 0;
let end = interval.size();
while start < end && iter.has_next() {
let next_op = iter.next_op_with_len(end - start).unwrap();
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()),
Some(_) => {
@ -121,7 +121,7 @@ impl FormatExt for ResolveInlineFormatExt {
let end = interval.size();
while start < end && iter.has_next() {
let next_op = iter.next_op_with_len(end - start).unwrap();
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()),
Some(_) => {

View File

@ -106,7 +106,7 @@ impl InsertExt for PreserveInlineStylesExt {
}
let mut iter = DeltaIter::new(delta);
let prev = iter.next_op_with_len(index)?;
let prev = iter.next_op_before(index)?;
if prev.get_data().contains(NEW_LINE) {
return None;
}

View File

@ -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) c_index: usize,
pub(crate) o_index: usize,
pub(crate) iter_char_count: usize,
pub(crate) iter_op_index: usize,
iter: Enumerate<Iter<'a, Operation>>,
next_op: Option<Operation>,
}
@ -22,8 +22,8 @@ impl<'a> Cursor<'a> {
delta,
origin_iv: interval,
next_iv: interval,
c_index: 0,
o_index: 0,
iter_char_count: 0,
iter_op_index: 0,
iter: delta.ops.iter().enumerate(),
next_op: None,
};
@ -31,9 +31,11 @@ impl<'a> Cursor<'a> {
cursor
}
pub fn next_interval(&self) -> Interval { self.next_op_interval_with_constraint(None) }
pub fn next_iv(&self) -> Interval { self.next_op_iv_before(None) }
pub fn next_op_with_len(&mut self, force_len: Option<usize>) -> Option<Operation> {
pub fn next_op(&mut self) -> Option<Operation> { self.next_op_before(None) }
pub fn next_op_before(&mut self, b_index: Option<usize>) -> Option<Operation> {
let mut find_op = None;
let next_op = self.next_op.take();
let mut next_op = next_op.as_ref();
@ -42,10 +44,10 @@ impl<'a> Cursor<'a> {
next_op = find_next_op(self);
}
let old_c_index = self.c_index;
let old_iter_char_count = self.iter_char_count;
while find_op.is_none() && next_op.is_some() {
let op = next_op.take().unwrap();
let interval = self.next_op_interval_with_constraint(force_len);
let interval = self.next_op_iv_before(b_index);
if interval.is_empty() {
self.next_op = Some(op.clone());
break;
@ -57,8 +59,8 @@ impl<'a> Cursor<'a> {
self.next_op = op.shrink(suffix);
}
self.c_index += interval.end;
self.next_iv.start = self.c_index;
self.iter_char_count += interval.end;
self.next_iv.start = self.iter_char_count;
if find_op.is_none() {
next_op = find_next_op(self);
@ -66,33 +68,31 @@ impl<'a> Cursor<'a> {
}
if find_op.is_some() {
let last = self.c_index - old_c_index;
let force_len = force_len.unwrap_or(0);
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_with_len(Some(len));
return self.next_op_before(Some(len));
}
}
return find_op;
}
pub fn next_op(&mut self) -> Option<Operation> { self.next_op_with_len(None) }
pub fn has_next(&self) -> bool { self.next_iter_op().is_some() }
fn descend(&mut self, index: usize) {
self.next_iv.start += index;
if self.c_index >= self.next_iv.start {
if self.iter_char_count >= self.next_iv.start {
return;
}
while let Some((o_index, op)) = self.iter.next() {
self.o_index = o_index;
let start = self.c_index;
self.iter_op_index = o_index;
let start = self.iter_char_count;
let end = start + op.length();
let intersect = Interval::new(start, end).intersect(self.next_iv);
if intersect.is_empty() {
self.c_index += op.length();
self.iter_char_count += op.length();
} else {
self.next_op = Some(op.clone());
break;
@ -106,7 +106,7 @@ impl<'a> Cursor<'a> {
let mut offset = 0;
for op in &self.delta.ops {
offset += op.length();
if offset > self.c_index {
if offset > self.iter_char_count {
next_op = Some(op);
break;
}
@ -115,17 +115,17 @@ impl<'a> Cursor<'a> {
next_op
}
fn next_op_interval_with_constraint(&self, force_len: Option<usize>) -> Interval {
fn next_op_iv_before(&self, b_index: Option<usize>) -> 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.c_index;
let end = match force_len {
None => self.c_index + op.length(),
Some(force_len) => self.c_index + min(force_len, op.length()),
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 intersect = Interval::new(start, end).intersect(self.next_iv);
let interval = intersect.translate_neg(start);
@ -137,7 +137,7 @@ fn find_next_op<'a>(cursor: &mut Cursor<'a>) -> Option<&'a Operation> {
match cursor.iter.next() {
None => None,
Some((o_index, op)) => {
cursor.o_index = o_index;
cursor.iter_op_index = o_index;
Some(op)
},
}
@ -152,7 +152,7 @@ pub struct OpMetric {}
impl Metric for OpMetric {
fn seek(cursor: &mut Cursor, index: usize) -> SeekResult {
let _ = check_bound(cursor.o_index, index)?;
let _ = check_bound(cursor.iter_op_index, index)?;
let mut temp_cursor = Cursor::new(cursor.delta, cursor.origin_iv);
let mut offset = 0;
while let Some((_, op)) = temp_cursor.iter.next() {
@ -170,8 +170,8 @@ pub struct CharMetric {}
impl Metric for CharMetric {
fn seek(cursor: &mut Cursor, index: usize) -> SeekResult {
let _ = check_bound(cursor.c_index, index)?;
let _ = cursor.next_op_with_len(Some(index));
let _ = check_bound(cursor.iter_char_count, index)?;
let _ = cursor.next_op_before(Some(index));
Ok(())
}

View File

@ -168,11 +168,11 @@ impl Delta {
);
let op = iter
.next_op_with_len(length)
.next_op_before(length)
.unwrap_or(OpBuilder::retain(length).build());
let other_op = other_iter
.next_op_with_len(length)
.next_op_before(length)
.unwrap_or(OpBuilder::retain(length).build());
debug_assert_eq!(op.length(), other_op.length());

View File

@ -24,7 +24,7 @@ impl<'a> DeltaIter<'a> {
pub fn next_op(&mut self) -> Option<Operation> { self.cursor.next_op() }
pub fn next_op_len(&self) -> Option<usize> {
let interval = self.cursor.next_interval();
let interval = self.cursor.next_iv();
if interval.is_empty() {
None
} else {
@ -32,8 +32,8 @@ impl<'a> DeltaIter<'a> {
}
}
pub fn next_op_with_len(&mut self, length: usize) -> Option<Operation> {
self.cursor.next_op_with_len(Some(length))
pub fn next_op_before(&mut self, index: usize) -> Option<Operation> {
self.cursor.next_op_before(Some(index))
}
pub fn seek<M: Metric>(&mut self, index: usize) {

View File

@ -154,7 +154,7 @@ fn delta_get_ops_in_interval_7() {
let mut iter_2 = DeltaIter::new(&delta);
assert_eq!(
iter_2.next_op_with_len(2).unwrap(),
iter_2.next_op_before(2).unwrap(),
OpBuilder::insert("12").build()
);
assert_eq!(iter_2.next_op().unwrap(), OpBuilder::insert("345").build());
@ -181,7 +181,7 @@ fn delta_seek_2() {
let mut iter = DeltaIter::new(&delta);
assert_eq!(
iter.next_op_with_len(1).unwrap(),
iter.next_op_before(1).unwrap(),
OpBuilder::insert("1").build()
);
}
@ -193,21 +193,21 @@ fn delta_seek_3() {
let mut iter = DeltaIter::new(&delta);
assert_eq!(
iter.next_op_with_len(2).unwrap(),
iter.next_op_before(2).unwrap(),
OpBuilder::insert("12").build()
);
assert_eq!(
iter.next_op_with_len(2).unwrap(),
iter.next_op_before(2).unwrap(),
OpBuilder::insert("34").build()
);
assert_eq!(
iter.next_op_with_len(2).unwrap(),
iter.next_op_before(2).unwrap(),
OpBuilder::insert("5").build()
);
assert_eq!(iter.next_op_with_len(1), None);
assert_eq!(iter.next_op_before(1), None);
}
#[test]
@ -218,7 +218,7 @@ fn delta_seek_4() {
let mut iter = DeltaIter::new(&delta);
iter.seek::<CharMetric>(3);
assert_eq!(
iter.next_op_with_len(2).unwrap(),
iter.next_op_before(2).unwrap(),
OpBuilder::insert("45").build()
);
}
@ -238,7 +238,7 @@ fn delta_seek_5() {
iter.seek::<CharMetric>(0);
assert_eq!(
iter.next_op_with_len(4).unwrap(),
iter.next_op_before(4).unwrap(),
OpBuilder::insert("1234").attributes(attributes).build(),
);
}
@ -252,7 +252,7 @@ fn delta_next_op_len_test() {
iter.seek::<CharMetric>(3);
assert_eq!(iter.next_op_len().unwrap(), 2);
assert_eq!(
iter.next_op_with_len(1).unwrap(),
iter.next_op_before(1).unwrap(),
OpBuilder::insert("4").build()
);
assert_eq!(iter.next_op_len().unwrap(), 1);
@ -267,7 +267,7 @@ fn delta_next_op_len_test2() {
assert_eq!(iter.next_op_len().unwrap(), 5);
assert_eq!(
iter.next_op_with_len(5).unwrap(),
iter.next_op_before(5).unwrap(),
OpBuilder::insert("12345").build()
);
assert_eq!(iter.next_op_len(), None);