multi_buffer: Fix up some anchor checks (#43454)
Release Notes: - N/A *or* Added/Fixed/Improved ...
This commit is contained in:
@@ -6,7 +6,7 @@ use std::{cmp::Ordering, fmt::Debug, ops::Range};
|
||||
use sum_tree::{Bias, Dimensions};
|
||||
|
||||
/// A timestamped position in a buffer
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
|
||||
pub struct Anchor {
|
||||
pub timestamp: clock::Lamport,
|
||||
/// The byte offset in the buffer
|
||||
@@ -16,6 +16,24 @@ pub struct Anchor {
|
||||
pub buffer_id: Option<BufferId>,
|
||||
}
|
||||
|
||||
impl Debug for Anchor {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
if *self == Self::MIN {
|
||||
return f.write_str("Anchor::MIN");
|
||||
}
|
||||
if *self == Self::MAX {
|
||||
return f.write_str("Anchor::MAX");
|
||||
}
|
||||
|
||||
f.debug_struct("Anchor")
|
||||
.field("timestamp", &self.timestamp)
|
||||
.field("offset", &self.offset)
|
||||
.field("bias", &self.bias)
|
||||
.field("buffer_id", &self.buffer_id)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl Anchor {
|
||||
pub const MIN: Self = Self {
|
||||
timestamp: clock::Lamport::MIN,
|
||||
|
||||
@@ -2444,7 +2444,9 @@ impl BufferSnapshot {
|
||||
} else if bias == Bias::Right && offset == self.len() {
|
||||
Anchor::MAX
|
||||
} else {
|
||||
if offset > self.visible_text.len() {
|
||||
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)
|
||||
}
|
||||
let (start, _, item) = self.fragments.find::<usize, _>(&None, &offset, bias);
|
||||
@@ -3137,12 +3139,9 @@ impl ToOffset for Point {
|
||||
|
||||
impl ToOffset for usize {
|
||||
fn to_offset(&self, snapshot: &BufferSnapshot) -> usize {
|
||||
assert!(
|
||||
*self <= snapshot.len(),
|
||||
"offset {} is out of range, snapshot length is {}",
|
||||
self,
|
||||
snapshot.len()
|
||||
);
|
||||
if cfg!(debug_assertions) {
|
||||
snapshot.as_rope().assert_char_boundary(*self);
|
||||
}
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user