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

This commit is contained in:
Antonio Scandurra
2021-06-10 14:05:56 +02:00
parent f294bfdbd9
commit dc2805bb14
4 changed files with 82 additions and 76 deletions
+26 -26
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)]);
}
}