Clamp UTF-16 coordinate while performing LSP edits rather than panicing

This commit is contained in:
Julia 2022-11-15 14:11:52 -05:00
parent 0078bea877
commit f9cbed5a1f
3 changed files with 46 additions and 14 deletions

View file

@ -1591,7 +1591,11 @@ impl BufferSnapshot {
}
pub fn point_utf16_to_offset(&self, point: PointUtf16) -> usize {
self.visible_text.point_utf16_to_offset(point)
self.visible_text.point_utf16_to_offset(point, false)
}
pub fn point_utf16_to_offset_clamped(&self, point: PointUtf16) -> usize {
self.visible_text.point_utf16_to_offset(point, true)
}
pub fn point_utf16_to_point(&self, point: PointUtf16) -> Point {
@ -1649,6 +1653,12 @@ impl BufferSnapshot {
self.visible_text.chunks_in_range(start..end)
}
pub fn text_for_clamped_range<T: ToOffsetClamped>(&self, range: Range<T>) -> Chunks<'_> {
let start = range.start.to_offset_clamped(self);
let end = range.end.to_offset_clamped(self);
self.visible_text.chunks_in_range(start..end)
}
pub fn line_len(&self, row: u32) -> u32 {
let row_start_offset = Point::new(row, 0).to_offset(self);
let row_end_offset = if row >= self.max_point().row {
@ -2390,6 +2400,16 @@ impl<'a, T: ToOffset> ToOffset for &'a T {
}
}
pub trait ToOffsetClamped {
fn to_offset_clamped(&self, snapshot: &BufferSnapshot) -> usize;
}
impl ToOffsetClamped for PointUtf16 {
fn to_offset_clamped<'a>(&self, snapshot: &BufferSnapshot) -> usize {
snapshot.point_utf16_to_offset_clamped(*self)
}
}
pub trait ToPoint {
fn to_point(&self, snapshot: &BufferSnapshot) -> Point;
}