Rename Cursor::{start,end} to Cursor::{sum_start,sum_end}

This commit is contained in:
Antonio Scandurra 2021-06-10 14:06:27 +02:00
parent dc2805bb14
commit 742241a903
5 changed files with 58 additions and 55 deletions

View file

@ -1210,7 +1210,7 @@ impl Buffer {
old_fragments.slice(&VersionedOffset::Offset(ranges[0].start), Bias::Left, &cx);
new_ropes.push_tree(new_fragments.summary().text);
let mut fragment_start = old_fragments.start().offset();
let mut fragment_start = old_fragments.sum_start().offset();
for range in ranges {
let fragment_end = old_fragments.end(&cx).offset();
@ -1219,7 +1219,7 @@ impl Buffer {
if fragment_end < range.start {
// If the current fragment has been partially consumed, then consume the rest of it
// and advance to the next fragment before slicing.
if fragment_start > old_fragments.start().offset() {
if fragment_start > old_fragments.sum_start().offset() {
if fragment_end > fragment_start {
let mut suffix = old_fragments.item().unwrap().clone();
suffix.len = fragment_end - fragment_start;
@ -1233,7 +1233,7 @@ impl Buffer {
old_fragments.slice(&VersionedOffset::Offset(range.start), Bias::Left, &cx);
new_ropes.push_tree(slice.summary().text);
new_fragments.push_tree(slice, &None);
fragment_start = old_fragments.start().offset();
fragment_start = old_fragments.sum_start().offset();
}
// If we are at the end of a non-concurrent fragment, advance to the next one.
@ -1244,7 +1244,7 @@ impl Buffer {
new_ropes.push_fragment(&fragment, fragment.visible);
new_fragments.push(fragment, &None);
old_fragments.next(&cx);
fragment_start = old_fragments.start().offset();
fragment_start = old_fragments.sum_start().offset();
}
// Skip over insertions that are concurrent to this edit, but have a lower lamport
@ -1312,7 +1312,7 @@ impl Buffer {
// If the current fragment has been partially consumed, then consume the rest of it
// and advance to the next fragment before slicing.
if fragment_start > old_fragments.start().offset() {
if fragment_start > old_fragments.sum_start().offset() {
let fragment_end = old_fragments.end(&cx).offset();
if fragment_end > fragment_start {
let mut suffix = old_fragments.item().unwrap().clone();
@ -1539,7 +1539,7 @@ impl Buffer {
let mut new_fragments = old_fragments.slice(&ranges[0].start, Bias::Right, &None);
new_ropes.push_tree(new_fragments.summary().text);
let mut fragment_start = old_fragments.start().visible;
let mut fragment_start = old_fragments.sum_start().visible;
for range in ranges {
let fragment_end = old_fragments.end(&None).visible;
@ -1548,7 +1548,7 @@ impl Buffer {
if fragment_end < range.start {
// If the current fragment has been partially consumed, then consume the rest of it
// and advance to the next fragment before slicing.
if fragment_start > old_fragments.start().visible {
if fragment_start > old_fragments.sum_start().visible {
if fragment_end > fragment_start {
let mut suffix = old_fragments.item().unwrap().clone();
suffix.len = fragment_end - fragment_start;
@ -1561,10 +1561,10 @@ impl Buffer {
let slice = old_fragments.slice(&range.start, Bias::Right, &None);
new_ropes.push_tree(slice.summary().text);
new_fragments.push_tree(slice, &None);
fragment_start = old_fragments.start().visible;
fragment_start = old_fragments.sum_start().visible;
}
let full_range_start = range.start + old_fragments.start().deleted;
let full_range_start = range.start + old_fragments.sum_start().deleted;
// Preserve any portion of the current fragment that precedes this range.
if fragment_start < range.start {
@ -1612,13 +1612,13 @@ impl Buffer {
}
}
let full_range_end = range.end + old_fragments.start().deleted;
let full_range_end = range.end + old_fragments.sum_start().deleted;
edit.ranges.push(full_range_start..full_range_end);
}
// If the current fragment has been partially consumed, then consume the rest of it
// and advance to the next fragment before slicing.
if fragment_start > old_fragments.start().visible {
if fragment_start > old_fragments.sum_start().visible {
let fragment_end = old_fragments.end(&None).visible;
if fragment_end > fragment_start {
let mut suffix = old_fragments.item().unwrap().clone();
@ -1663,7 +1663,7 @@ impl Buffer {
let mut cursor = self.fragments.cursor::<usize, FragmentTextSummary>();
cursor.seek(&offset, bias, &None);
Anchor::Middle {
offset: offset + cursor.start().deleted,
offset: offset + cursor.sum_start().deleted,
bias,
version: self.version(),
}
@ -1692,7 +1692,7 @@ impl Buffer {
0
};
self.text_summary_for_range(0..cursor.start() + overshoot)
self.text_summary_for_range(0..cursor.sum_start() + overshoot)
}
}
}
@ -1718,7 +1718,7 @@ impl Buffer {
&Some(version.clone()),
);
let overshoot = offset - cursor.seek_start().offset();
let summary = cursor.start();
let summary = cursor.sum_start();
summary.visible + summary.deleted + overshoot
}
}

View file

@ -131,7 +131,7 @@ impl Rope {
let mut cursor = self.chunks.cursor::<usize, Point>();
cursor.seek(&offset, Bias::Left, &());
let overshoot = offset - cursor.seek_start();
*cursor.start()
*cursor.sum_start()
+ cursor
.item()
.map_or(Point::zero(), |chunk| chunk.to_point(overshoot))
@ -142,7 +142,7 @@ impl Rope {
let mut cursor = self.chunks.cursor::<Point, usize>();
cursor.seek(&point, Bias::Left, &());
let overshoot = point - cursor.seek_start();
cursor.start() + cursor.item().map_or(0, |chunk| chunk.to_offset(overshoot))
cursor.sum_start() + cursor.item().map_or(0, |chunk| chunk.to_offset(overshoot))
}
pub fn clip_offset(&self, mut offset: usize, bias: Bias) -> usize {

View file

@ -245,7 +245,7 @@ impl FoldMap {
let mut cursor = transforms.cursor::<DisplayPoint, Point>();
cursor.seek(&display_point, Bias::Right, &());
let overshoot = display_point.0 - cursor.seek_start().0;
*cursor.start() + overshoot
*cursor.sum_start() + overshoot
}
pub fn to_display_point(&self, point: Point, cx: &AppContext) -> DisplayPoint {
@ -253,7 +253,10 @@ impl FoldMap {
let mut cursor = transforms.cursor::<Point, DisplayPoint>();
cursor.seek(&point, Bias::Right, &());
let overshoot = point - cursor.seek_start();
DisplayPoint(cmp::min(cursor.start().0 + overshoot, cursor.end(&()).0))
DisplayPoint(cmp::min(
cursor.sum_start().0 + overshoot,
cursor.end(&()).0,
))
}
fn sync(&self, cx: &AppContext) -> MutexGuard<SumTree<Transform>> {
@ -443,7 +446,7 @@ impl FoldMapSnapshot {
let mut transform_cursor = self.transforms.cursor::<DisplayOffset, usize>();
transform_cursor.seek(&offset, Bias::Right, &());
let overshoot = offset.0 - transform_cursor.seek_start().0;
let buffer_offset = transform_cursor.start() + overshoot;
let buffer_offset = transform_cursor.sum_start() + overshoot;
Chunks {
transform_cursor,
buffer_offset,
@ -456,11 +459,11 @@ impl FoldMapSnapshot {
transform_cursor.seek(&range.end, Bias::Right, &());
let overshoot = range.end.0 - transform_cursor.seek_start().0;
let buffer_end = transform_cursor.start() + overshoot;
let buffer_end = transform_cursor.sum_start() + overshoot;
transform_cursor.seek(&range.start, Bias::Right, &());
let overshoot = range.start.0 - transform_cursor.seek_start().0;
let buffer_start = transform_cursor.start() + overshoot;
let buffer_start = transform_cursor.sum_start() + overshoot;
HighlightedChunks {
transform_cursor,
@ -480,15 +483,15 @@ impl FoldMapSnapshot {
pub fn to_display_offset(&self, point: DisplayPoint) -> DisplayOffset {
let mut cursor = self.transforms.cursor::<DisplayPoint, TransformSummary>();
cursor.seek(&point, Bias::Right, &());
let overshoot = point.0 - cursor.start().display.lines;
let mut offset = cursor.start().display.bytes;
let overshoot = point.0 - cursor.sum_start().display.lines;
let mut offset = cursor.sum_start().display.bytes;
if !overshoot.is_zero() {
let transform = cursor.item().expect("display point out of range");
assert!(transform.display_text.is_none());
let end_buffer_offset = self
.buffer
.to_offset(cursor.start().buffer.lines + overshoot);
offset += end_buffer_offset - cursor.start().buffer.bytes;
.to_offset(cursor.sum_start().buffer.lines + overshoot);
offset += end_buffer_offset - cursor.sum_start().buffer.bytes;
}
DisplayOffset(offset)
}
@ -497,7 +500,7 @@ impl FoldMapSnapshot {
let mut cursor = self.transforms.cursor::<DisplayPoint, Point>();
cursor.seek(&point, Bias::Right, &());
let overshoot = point.0 - cursor.seek_start().0;
self.buffer.to_offset(*cursor.start() + overshoot)
self.buffer.to_offset(*cursor.sum_start() + overshoot)
}
#[cfg(test)]
@ -514,7 +517,7 @@ impl FoldMapSnapshot {
}
} else {
let overshoot = offset.0 - transform_start;
let buffer_offset = cursor.start() + overshoot;
let buffer_offset = cursor.sum_start() + overshoot;
let clipped_buffer_offset = self.buffer.clip_offset(buffer_offset, bias);
DisplayOffset(
(offset.0 as isize + (clipped_buffer_offset as isize - buffer_offset as isize))
@ -539,7 +542,7 @@ impl FoldMapSnapshot {
}
} else {
let overshoot = point.0 - transform_start;
let buffer_position = *cursor.start() + overshoot;
let buffer_position = *cursor.sum_start() + overshoot;
let clipped_buffer_position = self.buffer.clip_point(buffer_position, bias);
DisplayPoint::new(
point.row(),
@ -695,7 +698,7 @@ impl<'a> Iterator for BufferRows<'a> {
if self.cursor.item().is_some() {
let overshoot = self.display_point - self.cursor.seek_start().0;
let buffer_point = *self.cursor.start() + overshoot;
let buffer_point = *self.cursor.sum_start() + overshoot;
self.display_point.row += 1;
Some(buffer_point.row)
} else {

View file

@ -651,7 +651,7 @@ mod tests {
cursor.seek(&Count(pos), Bias::Right, &());
for i in 0..10 {
assert_eq!(cursor.start().0, pos);
assert_eq!(cursor.sum_start().0, pos);
if pos > 0 {
assert_eq!(cursor.prev_item().unwrap(), &reference_items[pos - 1]);
@ -710,7 +710,7 @@ mod tests {
);
assert_eq!(cursor.item(), None);
assert_eq!(cursor.prev_item(), None);
assert_eq!(cursor.start(), &Sum(0));
assert_eq!(cursor.sum_start(), &Sum(0));
// Single-element tree
let mut tree = SumTree::<u8>::new();
@ -722,23 +722,23 @@ mod tests {
);
assert_eq!(cursor.item(), Some(&1));
assert_eq!(cursor.prev_item(), None);
assert_eq!(cursor.start(), &Sum(0));
assert_eq!(cursor.sum_start(), &Sum(0));
cursor.next(&());
assert_eq!(cursor.item(), None);
assert_eq!(cursor.prev_item(), Some(&1));
assert_eq!(cursor.start(), &Sum(1));
assert_eq!(cursor.sum_start(), &Sum(1));
cursor.prev(&());
assert_eq!(cursor.item(), Some(&1));
assert_eq!(cursor.prev_item(), None);
assert_eq!(cursor.start(), &Sum(0));
assert_eq!(cursor.sum_start(), &Sum(0));
let mut cursor = tree.cursor::<Count, Sum>();
assert_eq!(cursor.slice(&Count(1), Bias::Right, &()).items(&()), [1]);
assert_eq!(cursor.item(), None);
assert_eq!(cursor.prev_item(), Some(&1));
assert_eq!(cursor.start(), &Sum(1));
assert_eq!(cursor.sum_start(), &Sum(1));
cursor.seek(&Count(0), Bias::Right, &());
assert_eq!(
@ -749,7 +749,7 @@ mod tests {
);
assert_eq!(cursor.item(), None);
assert_eq!(cursor.prev_item(), Some(&1));
assert_eq!(cursor.start(), &Sum(1));
assert_eq!(cursor.sum_start(), &Sum(1));
// Multiple-element tree
let mut tree = SumTree::new();
@ -759,68 +759,68 @@ mod tests {
assert_eq!(cursor.slice(&Count(2), Bias::Right, &()).items(&()), [1, 2]);
assert_eq!(cursor.item(), Some(&3));
assert_eq!(cursor.prev_item(), Some(&2));
assert_eq!(cursor.start(), &Sum(3));
assert_eq!(cursor.sum_start(), &Sum(3));
cursor.next(&());
assert_eq!(cursor.item(), Some(&4));
assert_eq!(cursor.prev_item(), Some(&3));
assert_eq!(cursor.start(), &Sum(6));
assert_eq!(cursor.sum_start(), &Sum(6));
cursor.next(&());
assert_eq!(cursor.item(), Some(&5));
assert_eq!(cursor.prev_item(), Some(&4));
assert_eq!(cursor.start(), &Sum(10));
assert_eq!(cursor.sum_start(), &Sum(10));
cursor.next(&());
assert_eq!(cursor.item(), Some(&6));
assert_eq!(cursor.prev_item(), Some(&5));
assert_eq!(cursor.start(), &Sum(15));
assert_eq!(cursor.sum_start(), &Sum(15));
cursor.next(&());
cursor.next(&());
assert_eq!(cursor.item(), None);
assert_eq!(cursor.prev_item(), Some(&6));
assert_eq!(cursor.start(), &Sum(21));
assert_eq!(cursor.sum_start(), &Sum(21));
cursor.prev(&());
assert_eq!(cursor.item(), Some(&6));
assert_eq!(cursor.prev_item(), Some(&5));
assert_eq!(cursor.start(), &Sum(15));
assert_eq!(cursor.sum_start(), &Sum(15));
cursor.prev(&());
assert_eq!(cursor.item(), Some(&5));
assert_eq!(cursor.prev_item(), Some(&4));
assert_eq!(cursor.start(), &Sum(10));
assert_eq!(cursor.sum_start(), &Sum(10));
cursor.prev(&());
assert_eq!(cursor.item(), Some(&4));
assert_eq!(cursor.prev_item(), Some(&3));
assert_eq!(cursor.start(), &Sum(6));
assert_eq!(cursor.sum_start(), &Sum(6));
cursor.prev(&());
assert_eq!(cursor.item(), Some(&3));
assert_eq!(cursor.prev_item(), Some(&2));
assert_eq!(cursor.start(), &Sum(3));
assert_eq!(cursor.sum_start(), &Sum(3));
cursor.prev(&());
assert_eq!(cursor.item(), Some(&2));
assert_eq!(cursor.prev_item(), Some(&1));
assert_eq!(cursor.start(), &Sum(1));
assert_eq!(cursor.sum_start(), &Sum(1));
cursor.prev(&());
assert_eq!(cursor.item(), Some(&1));
assert_eq!(cursor.prev_item(), None);
assert_eq!(cursor.start(), &Sum(0));
assert_eq!(cursor.sum_start(), &Sum(0));
cursor.prev(&());
assert_eq!(cursor.item(), None);
assert_eq!(cursor.prev_item(), None);
assert_eq!(cursor.start(), &Sum(0));
assert_eq!(cursor.sum_start(), &Sum(0));
cursor.next(&());
assert_eq!(cursor.item(), Some(&1));
assert_eq!(cursor.prev_item(), None);
assert_eq!(cursor.start(), &Sum(0));
assert_eq!(cursor.sum_start(), &Sum(0));
let mut cursor = tree.cursor::<Count, Sum>();
assert_eq!(
@ -831,7 +831,7 @@ mod tests {
);
assert_eq!(cursor.item(), None);
assert_eq!(cursor.prev_item(), Some(&6));
assert_eq!(cursor.start(), &Sum(21));
assert_eq!(cursor.sum_start(), &Sum(21));
cursor.seek(&Count(3), Bias::Right, &());
assert_eq!(
@ -842,7 +842,7 @@ mod tests {
);
assert_eq!(cursor.item(), None);
assert_eq!(cursor.prev_item(), Some(&6));
assert_eq!(cursor.start(), &Sum(21));
assert_eq!(cursor.sum_start(), &Sum(21));
// Seeking can bias left or right
cursor.seek(&Count(1), Bias::Left, &());

View file

@ -59,17 +59,17 @@ where
}
}
pub fn start(&self) -> &U {
pub fn sum_start(&self) -> &U {
&self.sum_dimension
}
pub fn end(&self, cx: &<T::Summary as Summary>::Context) -> U {
if let Some(item_summary) = self.item_summary() {
let mut end = self.start().clone();
let mut end = self.sum_start().clone();
end.add_summary(item_summary, cx);
end
} else {
self.start().clone()
self.sum_start().clone()
}
}
@ -627,7 +627,7 @@ where
}
pub fn start(&self) -> &U {
self.cursor.start()
self.cursor.sum_start()
}
pub fn item(&self) -> Option<&'a T> {