multi_buffer: Remove redundant buffer id field (#43459)

It is easy for us to get the two fields out of sync causing weird
problems, there is no reason to have both here so.

Release Notes:

- N/A *or* Added/Fixed/Improved ...

Co-authored by: Antonio Scandurra <antonio@zed.dev>
This commit is contained in:
Lukas Wirth
2025-11-25 17:13:16 +01:00
committed by GitHub
parent ab80ef1845
commit fafe1afa61
39 changed files with 378 additions and 306 deletions
+47 -5
View File
@@ -18,11 +18,11 @@ pub struct Anchor {
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.is_min() {
return write!(f, "Anchor::min({:?})", self.buffer_id);
}
if *self == Self::MAX {
return f.write_str("Anchor::MAX");
if self.is_max() {
return write!(f, "Anchor::max({:?})", self.buffer_id);
}
f.debug_struct("Anchor")
@@ -49,6 +49,36 @@ impl Anchor {
buffer_id: None,
};
pub fn min_for_buffer(buffer_id: BufferId) -> Self {
Self {
timestamp: clock::Lamport::MIN,
offset: usize::MIN,
bias: Bias::Left,
buffer_id: Some(buffer_id),
}
}
pub fn max_for_buffer(buffer_id: BufferId) -> Self {
Self {
timestamp: clock::Lamport::MAX,
offset: usize::MAX,
bias: Bias::Right,
buffer_id: Some(buffer_id),
}
}
pub fn min_min_range_for_buffer(buffer_id: BufferId) -> std::ops::Range<Self> {
let min = Self::min_for_buffer(buffer_id);
min..min
}
pub fn max_max_range_for_buffer(buffer_id: BufferId) -> std::ops::Range<Self> {
let max = Self::max_for_buffer(buffer_id);
max..max
}
pub fn min_max_range_for_buffer(buffer_id: BufferId) -> std::ops::Range<Self> {
Self::min_for_buffer(buffer_id)..Self::max_for_buffer(buffer_id)
}
pub fn cmp(&self, other: &Anchor, buffer: &BufferSnapshot) -> Ordering {
let fragment_id_comparison = if self.timestamp == other.timestamp {
Ordering::Equal
@@ -109,7 +139,7 @@ impl Anchor {
/// Returns true when the [`Anchor`] is located inside a visible fragment.
pub fn is_valid(&self, buffer: &BufferSnapshot) -> bool {
if *self == Anchor::MIN || *self == Anchor::MAX {
if self.is_min() || self.is_max() {
true
} else if self.buffer_id.is_none_or(|id| id != buffer.remote_id) {
false
@@ -127,6 +157,18 @@ impl Anchor {
item.is_some_and(|fragment| fragment.visible)
}
}
pub fn is_min(&self) -> bool {
self.timestamp == clock::Lamport::MIN
&& self.offset == usize::MIN
&& self.bias == Bias::Left
}
pub fn is_max(&self) -> bool {
self.timestamp == clock::Lamport::MAX
&& self.offset == usize::MAX
&& self.bias == Bias::Right
}
}
pub trait OffsetRangeExt {
+11 -14
View File
@@ -1652,10 +1652,7 @@ impl Buffer {
) -> impl 'static + Future<Output = Result<()>> + use<It> {
let mut futures = Vec::new();
for anchor in anchors {
if !self.version.observed(anchor.timestamp)
&& anchor != Anchor::MAX
&& anchor != Anchor::MIN
{
if !self.version.observed(anchor.timestamp) && !anchor.is_max() && !anchor.is_min() {
let (tx, rx) = oneshot::channel();
self.edit_id_resolvers
.entry(anchor.timestamp)
@@ -2258,9 +2255,9 @@ impl BufferSnapshot {
let mut position = D::zero(());
anchors.map(move |(anchor, payload)| {
if *anchor == Anchor::MIN {
if anchor.is_min() {
return (D::zero(()), payload);
} else if *anchor == Anchor::MAX {
} else if anchor.is_max() {
return (D::from_text_summary(&self.visible_text.summary()), payload);
}
@@ -2318,9 +2315,9 @@ impl BufferSnapshot {
}
pub fn offset_for_anchor(&self, anchor: &Anchor) -> usize {
if *anchor == Anchor::MIN {
if anchor.is_min() {
0
} else if *anchor == Anchor::MAX {
} else if anchor.is_max() {
self.visible_text.len()
} else {
debug_assert!(anchor.buffer_id == Some(self.remote_id));
@@ -2393,9 +2390,9 @@ impl BufferSnapshot {
}
fn try_fragment_id_for_anchor(&self, anchor: &Anchor) -> Option<&Locator> {
if *anchor == Anchor::MIN {
if anchor.is_min() {
Some(Locator::min_ref())
} else if *anchor == Anchor::MAX {
} else if anchor.is_max() {
Some(Locator::max_ref())
} else {
let anchor_key = InsertionFragmentKey {
@@ -2440,9 +2437,9 @@ impl BufferSnapshot {
fn anchor_at_offset(&self, offset: usize, bias: Bias) -> Anchor {
if bias == Bias::Left && offset == 0 {
Anchor::MIN
Anchor::min_for_buffer(self.remote_id)
} else if bias == Bias::Right && offset == self.len() {
Anchor::MAX
Anchor::max_for_buffer(self.remote_id)
} else {
if cfg!(debug_assertions) {
self.visible_text.assert_char_boundary(offset);
@@ -2462,8 +2459,8 @@ impl BufferSnapshot {
}
pub fn can_resolve(&self, anchor: &Anchor) -> bool {
*anchor == Anchor::MIN
|| *anchor == Anchor::MAX
anchor.is_min()
|| anchor.is_max()
|| (Some(self.remote_id) == anchor.buffer_id && self.version.observed(anchor.timestamp))
}