diff --git a/crates/rope/src/chunk.rs b/crates/rope/src/chunk.rs index 95df79d64bb401edf6220ba573be854297226cfe..a2a8e8d58df2d5ddc3336e8e56dd8446f4dcf118 100644 --- a/crates/rope/src/chunk.rs +++ b/crates/rope/src/chunk.rs @@ -220,7 +220,11 @@ impl<'a> ChunkSlice<'a> { range.start = self.text.ceil_char_boundary(range.start); } if !self.assert_char_boundary::(range.end) { - range.end = self.text.floor_char_boundary(range.end); + range.end = if range.end < range.start { + range.start + } else { + self.text.floor_char_boundary(range.end) + }; } let mask = (1 as Bitmap) .unbounded_shl(range.end as u32) diff --git a/crates/rope/src/rope.rs b/crates/rope/src/rope.rs index 8379045be245cadaf79800f1d57ff418cdd24b40..2d3c811e179fbd47cada7c2bebb89b03acd3eeb0 100644 --- a/crates/rope/src/rope.rs +++ b/crates/rope/src/rope.rs @@ -50,23 +50,31 @@ impl Rope { #[track_caller] #[inline(always)] - pub fn assert_char_boundary(&self, offset: usize) { + pub fn assert_char_boundary(&self, offset: usize) -> bool { if self.chunks.is_empty() && offset == 0 { - return; + return true; } let (start, _, item) = self.chunks.find::((), &offset, Bias::Left); match item { Some(chunk) => { let chunk_offset = offset - start; - chunk.assert_char_boundary::(chunk_offset); + chunk.assert_char_boundary::(chunk_offset) } - None => { + None if PANIC => { panic!( "byte index {} is out of bounds of rope (length: {})", offset, self.len() ); } + None => { + log::error!( + "byte index {} is out of bounds of rope (length: {})", + offset, + self.len() + ); + false + } } } diff --git a/crates/text/src/text.rs b/crates/text/src/text.rs index c16c6a7b27e2b1fc4c945007395dbe26f98adcda..acd712f40da23af4c364649b14860e41a346389c 100644 --- a/crates/text/src/text.rs +++ b/crates/text/src/text.rs @@ -2435,16 +2435,22 @@ impl BufferSnapshot { self.anchor_at_offset(position.to_offset(self), bias) } - fn anchor_at_offset(&self, offset: usize, bias: Bias) -> Anchor { + fn anchor_at_offset(&self, mut offset: usize, bias: Bias) -> Anchor { if bias == Bias::Left && offset == 0 { Anchor::min_for_buffer(self.remote_id) - } else if bias == Bias::Right && offset == self.len() { + } else if bias == Bias::Right + && ((cfg!(debug_assertions) && offset >= self.len()) || offset == self.len()) + { Anchor::max_for_buffer(self.remote_id) } else { - if cfg!(debug_assertions) { - self.visible_text.assert_char_boundary(offset); - } else if offset > self.visible_text.len() { - panic!("offset {} is out of bounds", offset) + if self + .visible_text + .assert_char_boundary::<{ cfg!(debug_assertions) }>(offset) + { + offset = match bias { + Bias::Left => self.visible_text.floor_char_boundary(offset), + Bias::Right => self.visible_text.ceil_char_boundary(offset), + }; } let (start, _, item) = self.fragments.find::(&None, &offset, bias); let fragment = item.unwrap(); @@ -3136,10 +3142,14 @@ impl ToOffset for Point { impl ToOffset for usize { fn to_offset(&self, snapshot: &BufferSnapshot) -> usize { - if cfg!(debug_assertions) { - snapshot.as_rope().assert_char_boundary(*self); + if snapshot + .as_rope() + .assert_char_boundary::<{ cfg!(debug_assertions) }>(*self) + { + snapshot.as_rope().floor_char_boundary(*self) + } else { + *self } - *self } }