text: Downgrade some more offset panics to error logs (#43925)

Release Notes:

- N/A *or* Added/Fixed/Improved ...
This commit is contained in:
Lukas Wirth
2025-12-01 18:04:01 +00:00
committed by GitHub
parent c8166abbcb
commit 61ae59708d
3 changed files with 36 additions and 14 deletions
+19 -9
View File
@@ -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::<usize, _>(&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
}
}