Clamp UTF-16 to point conversions

This commit is contained in:
Julia 2022-11-16 13:29:26 -05:00
parent bb32599ded
commit 074e3cfbd6
6 changed files with 65 additions and 45 deletions

View file

@ -72,6 +72,10 @@ pub trait ToOffset: 'static + fmt::Debug {
fn to_offset(&self, snapshot: &MultiBufferSnapshot) -> usize;
}
pub trait ToOffsetClamped: 'static + fmt::Debug {
fn to_offset_clamped(&self, snapshot: &MultiBufferSnapshot) -> usize;
}
pub trait ToOffsetUtf16: 'static + fmt::Debug {
fn to_offset_utf16(&self, snapshot: &MultiBufferSnapshot) -> OffsetUtf16;
}
@ -1945,9 +1949,9 @@ impl MultiBufferSnapshot {
}
}
pub fn point_utf16_to_offset(&self, point: PointUtf16) -> usize {
pub fn point_utf16_to_offset_clamped(&self, point: PointUtf16) -> usize {
if let Some((_, _, buffer)) = self.as_singleton() {
return buffer.point_utf16_to_offset(point);
return buffer.point_utf16_to_offset_clamped(point);
}
let mut cursor = self.excerpts.cursor::<(PointUtf16, usize)>();
@ -1961,7 +1965,7 @@ impl MultiBufferSnapshot {
.offset_to_point_utf16(excerpt.range.context.start.to_offset(&excerpt.buffer));
let buffer_offset = excerpt
.buffer
.point_utf16_to_offset(excerpt_start_point + overshoot);
.point_utf16_to_offset_clamped(excerpt_start_point + overshoot);
*start_offset + (buffer_offset - excerpt_start_offset)
} else {
self.excerpts.summary().text.len
@ -3274,12 +3278,6 @@ impl ToOffset for Point {
}
}
impl ToOffset for PointUtf16 {
fn to_offset<'a>(&self, snapshot: &MultiBufferSnapshot) -> usize {
snapshot.point_utf16_to_offset(*self)
}
}
impl ToOffset for usize {
fn to_offset<'a>(&self, snapshot: &MultiBufferSnapshot) -> usize {
assert!(*self <= snapshot.len(), "offset is out of range");
@ -3293,6 +3291,12 @@ impl ToOffset for OffsetUtf16 {
}
}
impl ToOffsetClamped for PointUtf16 {
fn to_offset_clamped<'a>(&self, snapshot: &MultiBufferSnapshot) -> usize {
snapshot.point_utf16_to_offset_clamped(*self)
}
}
impl ToOffsetUtf16 for OffsetUtf16 {
fn to_offset_utf16(&self, _snapshot: &MultiBufferSnapshot) -> OffsetUtf16 {
*self