Add Cursor::seek_end and audit the codebase to use seek_start more

This commit is contained in:
Antonio Scandurra 2021-06-10 13:55:15 +02:00
parent f294bfdbd9
commit dc2805bb14
4 changed files with 82 additions and 76 deletions

View file

@ -128,10 +128,10 @@ impl Rope {
pub fn to_point(&self, offset: usize) -> Point {
assert!(offset <= self.summary().bytes);
let mut cursor = self.chunks.cursor::<usize, TextSummary>();
let mut cursor = self.chunks.cursor::<usize, Point>();
cursor.seek(&offset, Bias::Left, &());
let overshoot = offset - cursor.start().bytes;
cursor.start().lines
let overshoot = offset - cursor.seek_start();
*cursor.start()
+ cursor
.item()
.map_or(Point::zero(), |chunk| chunk.to_point(overshoot))
@ -139,17 +139,17 @@ impl Rope {
pub fn to_offset(&self, point: Point) -> usize {
assert!(point <= self.summary().lines);
let mut cursor = self.chunks.cursor::<Point, TextSummary>();
let mut cursor = self.chunks.cursor::<Point, usize>();
cursor.seek(&point, Bias::Left, &());
let overshoot = point - cursor.start().lines;
cursor.start().bytes + cursor.item().map_or(0, |chunk| chunk.to_offset(overshoot))
let overshoot = point - cursor.seek_start();
cursor.start() + cursor.item().map_or(0, |chunk| chunk.to_offset(overshoot))
}
pub fn clip_offset(&self, mut offset: usize, bias: Bias) -> usize {
let mut cursor = self.chunks.cursor::<usize, usize>();
let mut cursor = self.chunks.cursor::<usize, ()>();
cursor.seek(&offset, Bias::Left, &());
if let Some(chunk) = cursor.item() {
let mut ix = offset - cursor.start();
let mut ix = offset - cursor.seek_start();
while !chunk.0.is_char_boundary(ix) {
match bias {
Bias::Left => {
@ -169,11 +169,11 @@ impl Rope {
}
pub fn clip_point(&self, point: Point, bias: Bias) -> Point {
let mut cursor = self.chunks.cursor::<Point, Point>();
let mut cursor = self.chunks.cursor::<Point, ()>();
cursor.seek(&point, Bias::Right, &());
if let Some(chunk) = cursor.item() {
let overshoot = point - cursor.start();
*cursor.start() + chunk.clip_point(overshoot, bias)
let overshoot = point - cursor.seek_start();
*cursor.seek_start() + chunk.clip_point(overshoot, bias)
} else {
self.summary().lines
}
@ -190,7 +190,7 @@ impl<'a> From<&'a str> for Rope {
pub struct Cursor<'a> {
rope: &'a Rope,
chunks: sum_tree::Cursor<'a, Chunk, usize, usize>,
chunks: sum_tree::Cursor<'a, Chunk, usize, ()>,
offset: usize,
}
@ -222,18 +222,18 @@ impl<'a> Cursor<'a> {
let mut slice = Rope::new();
if let Some(start_chunk) = self.chunks.item() {
let start_ix = self.offset - self.chunks.start();
let end_ix = cmp::min(end_offset, self.chunks.end(&())) - self.chunks.start();
let start_ix = self.offset - self.chunks.seek_start();
let end_ix = cmp::min(end_offset, self.chunks.seek_end(&())) - self.chunks.seek_start();
slice.push(&start_chunk.0[start_ix..end_ix]);
}
if end_offset > self.chunks.end(&()) {
if end_offset > self.chunks.seek_end(&()) {
self.chunks.next(&());
slice.append(Rope {
chunks: self.chunks.slice(&end_offset, Bias::Right, &()),
});
if let Some(end_chunk) = self.chunks.item() {
let end_ix = end_offset - self.chunks.start();
let end_ix = end_offset - self.chunks.seek_start();
slice.push(&end_chunk.0[..end_ix]);
}
}
@ -247,16 +247,16 @@ impl<'a> Cursor<'a> {
let mut summary = TextSummary::default();
if let Some(start_chunk) = self.chunks.item() {
let start_ix = self.offset - self.chunks.start();
let end_ix = cmp::min(end_offset, self.chunks.end(&())) - self.chunks.start();
let start_ix = self.offset - self.chunks.seek_start();
let end_ix = cmp::min(end_offset, self.chunks.seek_end(&())) - self.chunks.seek_start();
summary = TextSummary::from(&start_chunk.0[start_ix..end_ix]);
}
if end_offset > self.chunks.end(&()) {
if end_offset > self.chunks.seek_end(&()) {
self.chunks.next(&());
summary += &self.chunks.summary(&end_offset, Bias::Right, &());
if let Some(end_chunk) = self.chunks.item() {
let end_ix = end_offset - self.chunks.start();
let end_ix = end_offset - self.chunks.seek_start();
summary += TextSummary::from(&end_chunk.0[..end_ix]);
}
}
@ -274,7 +274,7 @@ impl<'a> Cursor<'a> {
}
pub struct Chunks<'a> {
chunks: sum_tree::Cursor<'a, Chunk, usize, usize>,
chunks: sum_tree::Cursor<'a, Chunk, usize, ()>,
range: Range<usize>,
}
@ -286,11 +286,11 @@ impl<'a> Chunks<'a> {
}
pub fn offset(&self) -> usize {
self.range.start.max(*self.chunks.start())
self.range.start.max(*self.chunks.seek_start())
}
pub fn seek(&mut self, offset: usize) {
if offset >= self.chunks.end(&()) {
if offset >= self.chunks.seek_end(&()) {
self.chunks.seek_forward(&offset, Bias::Right, &());
} else {
self.chunks.seek(&offset, Bias::Right, &());
@ -300,10 +300,10 @@ impl<'a> Chunks<'a> {
pub fn peek(&self) -> Option<&'a str> {
if let Some(chunk) = self.chunks.item() {
let offset = *self.chunks.start();
let offset = *self.chunks.seek_start();
if self.range.end > offset {
let start = self.range.start.saturating_sub(*self.chunks.start());
let end = self.range.end - self.chunks.start();
let start = self.range.start.saturating_sub(*self.chunks.seek_start());
let end = self.range.end - self.chunks.seek_start();
return Some(&chunk.0[start..chunk.0.len().min(end)]);
}
}

View file

@ -210,20 +210,20 @@ impl FoldMap {
let buffer = self.buffer.read(cx);
let offset = offset.to_offset(buffer);
let transforms = self.sync(cx);
let mut cursor = transforms.cursor::<usize, usize>();
let mut cursor = transforms.cursor::<usize, ()>();
cursor.seek(&offset, Bias::Right, &());
cursor.item().map_or(false, |t| t.display_text.is_some())
}
pub fn is_line_folded(&self, display_row: u32, cx: &AppContext) -> bool {
let transforms = self.sync(cx);
let mut cursor = transforms.cursor::<DisplayPoint, DisplayPoint>();
let mut cursor = transforms.cursor::<DisplayPoint, ()>();
cursor.seek(&DisplayPoint::new(display_row, 0), Bias::Right, &());
while let Some(transform) = cursor.item() {
if transform.display_text.is_some() {
return true;
}
if cursor.end(&()).row() == display_row {
if cursor.seek_end(&()).row() == display_row {
cursor.next(&())
} else {
break;
@ -242,21 +242,18 @@ impl FoldMap {
pub fn to_buffer_point(&self, display_point: DisplayPoint, cx: &AppContext) -> Point {
let transforms = self.sync(cx);
let mut cursor = transforms.cursor::<DisplayPoint, TransformSummary>();
let mut cursor = transforms.cursor::<DisplayPoint, Point>();
cursor.seek(&display_point, Bias::Right, &());
let overshoot = display_point.0 - cursor.start().display.lines;
cursor.start().buffer.lines + overshoot
let overshoot = display_point.0 - cursor.seek_start().0;
*cursor.start() + overshoot
}
pub fn to_display_point(&self, point: Point, cx: &AppContext) -> DisplayPoint {
let transforms = self.sync(cx);
let mut cursor = transforms.cursor::<Point, TransformSummary>();
let mut cursor = transforms.cursor::<Point, DisplayPoint>();
cursor.seek(&point, Bias::Right, &());
let overshoot = point - cursor.start().buffer.lines;
DisplayPoint(cmp::min(
cursor.start().display.lines + overshoot,
cursor.end(&()).display.lines,
))
let overshoot = point - cursor.seek_start();
DisplayPoint(cmp::min(cursor.start().0 + overshoot, cursor.end(&()).0))
}
fn sync(&self, cx: &AppContext) -> MutexGuard<SumTree<Transform>> {
@ -275,20 +272,20 @@ impl FoldMap {
let mut new_transforms = SumTree::new();
let mut transforms = self.transforms.lock();
let mut cursor = transforms.cursor::<usize, usize>();
let mut cursor = transforms.cursor::<usize, ()>();
cursor.seek(&0, Bias::Right, &());
while let Some(mut edit) = edits.next() {
new_transforms.push_tree(cursor.slice(&edit.old_range.start, Bias::Left, &()), &());
edit.new_range.start -= edit.old_range.start - cursor.start();
edit.old_range.start = *cursor.start();
edit.new_range.start -= edit.old_range.start - cursor.seek_start();
edit.old_range.start = *cursor.seek_start();
cursor.seek(&edit.old_range.end, Bias::Right, &());
cursor.next(&());
let mut delta = edit.delta();
loop {
edit.old_range.end = *cursor.start();
edit.old_range.end = *cursor.seek_start();
if let Some(next_edit) = edits.peek() {
if next_edit.old_range.start > edit.old_range.end {
@ -443,10 +440,10 @@ impl FoldMapSnapshot {
}
pub fn chunks_at(&self, offset: DisplayOffset) -> Chunks {
let mut transform_cursor = self.transforms.cursor::<DisplayOffset, TransformSummary>();
let mut transform_cursor = self.transforms.cursor::<DisplayOffset, usize>();
transform_cursor.seek(&offset, Bias::Right, &());
let overshoot = offset.0 - transform_cursor.start().display.bytes;
let buffer_offset = transform_cursor.start().buffer.bytes + overshoot;
let overshoot = offset.0 - transform_cursor.seek_start().0;
let buffer_offset = transform_cursor.start() + overshoot;
Chunks {
transform_cursor,
buffer_offset,
@ -455,15 +452,15 @@ impl FoldMapSnapshot {
}
pub fn highlighted_chunks(&mut self, range: Range<DisplayOffset>) -> HighlightedChunks {
let mut transform_cursor = self.transforms.cursor::<DisplayOffset, TransformSummary>();
let mut transform_cursor = self.transforms.cursor::<DisplayOffset, usize>();
transform_cursor.seek(&range.end, Bias::Right, &());
let overshoot = range.end.0 - transform_cursor.start().display.bytes;
let buffer_end = transform_cursor.start().buffer.bytes + overshoot;
let overshoot = range.end.0 - transform_cursor.seek_start().0;
let buffer_end = transform_cursor.start() + overshoot;
transform_cursor.seek(&range.start, Bias::Right, &());
let overshoot = range.start.0 - transform_cursor.start().display.bytes;
let buffer_start = transform_cursor.start().buffer.bytes + overshoot;
let overshoot = range.start.0 - transform_cursor.seek_start().0;
let buffer_start = transform_cursor.start() + overshoot;
HighlightedChunks {
transform_cursor,
@ -497,28 +494,27 @@ impl FoldMapSnapshot {
}
pub fn to_buffer_offset(&self, point: DisplayPoint) -> usize {
let mut cursor = self.transforms.cursor::<DisplayPoint, TransformSummary>();
let mut cursor = self.transforms.cursor::<DisplayPoint, Point>();
cursor.seek(&point, Bias::Right, &());
let overshoot = point.0 - cursor.start().display.lines;
self.buffer
.to_offset(cursor.start().buffer.lines + overshoot)
let overshoot = point.0 - cursor.seek_start().0;
self.buffer.to_offset(*cursor.start() + overshoot)
}
#[cfg(test)]
pub fn clip_offset(&self, offset: DisplayOffset, bias: Bias) -> DisplayOffset {
let mut cursor = self.transforms.cursor::<DisplayOffset, TransformSummary>();
let mut cursor = self.transforms.cursor::<DisplayOffset, usize>();
cursor.seek(&offset, Bias::Right, &());
if let Some(transform) = cursor.item() {
let transform_start = cursor.start().display.bytes;
let transform_start = cursor.seek_start().0;
if transform.display_text.is_some() {
if offset.0 == transform_start || matches!(bias, Bias::Left) {
DisplayOffset(transform_start)
} else {
DisplayOffset(cursor.end(&()).display.bytes)
DisplayOffset(cursor.seek_end(&()).0)
}
} else {
let overshoot = offset.0 - transform_start;
let buffer_offset = cursor.start().buffer.bytes + overshoot;
let buffer_offset = cursor.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))
@ -531,19 +527,19 @@ impl FoldMapSnapshot {
}
pub fn clip_point(&self, point: DisplayPoint, bias: Bias) -> DisplayPoint {
let mut cursor = self.transforms.cursor::<DisplayPoint, TransformSummary>();
let mut cursor = self.transforms.cursor::<DisplayPoint, Point>();
cursor.seek(&point, Bias::Right, &());
if let Some(transform) = cursor.item() {
let transform_start = cursor.start().display.lines;
let transform_start = cursor.seek_start().0;
if transform.display_text.is_some() {
if point.0 == transform_start || matches!(bias, Bias::Left) {
DisplayPoint(transform_start)
} else {
DisplayPoint(cursor.end(&()).display.lines)
DisplayPoint(cursor.seek_end(&()).0)
}
} else {
let overshoot = point.0 - transform_start;
let buffer_position = cursor.start().buffer.lines + overshoot;
let buffer_position = *cursor.start() + overshoot;
let clipped_buffer_position = self.buffer.clip_point(buffer_position, bias);
DisplayPoint::new(
point.row(),
@ -681,7 +677,7 @@ impl<'a> sum_tree::Dimension<'a, FoldSummary> for usize {
}
pub struct BufferRows<'a> {
cursor: Cursor<'a, Transform, DisplayPoint, TransformSummary>,
cursor: Cursor<'a, Transform, DisplayPoint, Point>,
display_point: Point,
}
@ -689,7 +685,7 @@ impl<'a> Iterator for BufferRows<'a> {
type Item = u32;
fn next(&mut self) -> Option<Self::Item> {
while self.display_point > self.cursor.end(&()).display.lines {
while self.display_point > self.cursor.seek_end(&()).0 {
self.cursor.next(&());
if self.cursor.item().is_none() {
// TODO: Return a bool from next?
@ -698,8 +694,8 @@ impl<'a> Iterator for BufferRows<'a> {
}
if self.cursor.item().is_some() {
let overshoot = self.display_point - self.cursor.start().display.lines;
let buffer_point = self.cursor.start().buffer.lines + overshoot;
let overshoot = self.display_point - self.cursor.seek_start().0;
let buffer_point = *self.cursor.start() + overshoot;
self.display_point.row += 1;
Some(buffer_point.row)
} else {
@ -709,7 +705,7 @@ impl<'a> Iterator for BufferRows<'a> {
}
pub struct Chunks<'a> {
transform_cursor: Cursor<'a, Transform, DisplayOffset, TransformSummary>,
transform_cursor: Cursor<'a, Transform, DisplayOffset, usize>,
buffer_chunks: buffer::Chunks<'a>,
buffer_offset: usize,
}
@ -730,7 +726,7 @@ impl<'a> Iterator for Chunks<'a> {
self.buffer_offset += transform.summary.buffer.bytes;
self.buffer_chunks.seek(self.buffer_offset);
while self.buffer_offset >= self.transform_cursor.end(&()).buffer.bytes
while self.buffer_offset >= self.transform_cursor.end(&())
&& self.transform_cursor.item().is_some()
{
self.transform_cursor.next(&());
@ -745,7 +741,7 @@ impl<'a> Iterator for Chunks<'a> {
chunk = &chunk[offset_in_chunk..];
// Truncate the chunk so that it ends at the next fold.
let region_end = self.transform_cursor.end(&()).buffer.bytes - self.buffer_offset;
let region_end = self.transform_cursor.end(&()) - self.buffer_offset;
if chunk.len() >= region_end {
chunk = &chunk[0..region_end];
self.transform_cursor.next(&());
@ -762,7 +758,7 @@ impl<'a> Iterator for Chunks<'a> {
}
pub struct HighlightedChunks<'a> {
transform_cursor: Cursor<'a, Transform, DisplayOffset, TransformSummary>,
transform_cursor: Cursor<'a, Transform, DisplayOffset, usize>,
buffer_chunks: buffer::HighlightedChunks<'a>,
buffer_chunk: Option<(usize, &'a str, StyleId)>,
buffer_offset: usize,
@ -785,7 +781,7 @@ impl<'a> Iterator for HighlightedChunks<'a> {
self.buffer_offset += transform.summary.buffer.bytes;
self.buffer_chunks.seek(self.buffer_offset);
while self.buffer_offset >= self.transform_cursor.end(&()).buffer.bytes
while self.buffer_offset >= self.transform_cursor.end(&())
&& self.transform_cursor.item().is_some()
{
self.transform_cursor.next(&());
@ -809,7 +805,7 @@ impl<'a> Iterator for HighlightedChunks<'a> {
chunk = &chunk[offset_in_chunk..];
// Truncate the chunk so that it ends at the next fold.
let region_end = self.transform_cursor.end(&()).buffer.bytes - self.buffer_offset;
let region_end = self.transform_cursor.end(&()) - self.buffer_offset;
if chunk.len() >= region_end {
chunk = &chunk[0..region_end];
self.transform_cursor.next(&());

View file

@ -49,6 +49,16 @@ where
&self.seek_dimension
}
pub fn seek_end(&self, cx: &<T::Summary as Summary>::Context) -> S {
if let Some(item_summary) = self.item_summary() {
let mut end = self.seek_start().clone();
end.add_summary(item_summary, cx);
end
} else {
self.seek_start().clone()
}
}
pub fn start(&self) -> &U {
&self.sum_dimension
}

View file

@ -1290,8 +1290,8 @@ impl WorktreeHandle for ModelHandle<Worktree> {
}
pub enum FileIter<'a> {
All(Cursor<'a, Entry, FileCount, FileCount>),
Visible(Cursor<'a, Entry, VisibleFileCount, VisibleFileCount>),
All(Cursor<'a, Entry, FileCount, ()>),
Visible(Cursor<'a, Entry, VisibleFileCount, ()>),
}
impl<'a> FileIter<'a> {
@ -1310,11 +1310,11 @@ impl<'a> FileIter<'a> {
fn next_internal(&mut self) {
match self {
Self::All(cursor) => {
let ix = *cursor.start();
let ix = *cursor.seek_start();
cursor.seek_forward(&FileCount(ix.0 + 1), Bias::Right, &());
}
Self::Visible(cursor) => {
let ix = *cursor.start();
let ix = *cursor.seek_start();
cursor.seek_forward(&VisibleFileCount(ix.0 + 1), Bias::Right, &());
}
}