anchor.rs

  1use crate::{
  2    ExcerptSummary, MultiBufferDimension, MultiBufferOffset, MultiBufferOffsetUtf16, PathKey,
  3    PathKeyIndex, find_diff_state,
  4};
  5
  6use super::{MultiBufferSnapshot, ToOffset, ToPoint};
  7use language::{BufferSnapshot, Point};
  8use std::{
  9    cmp::Ordering,
 10    ops::{Add, AddAssign, Range, Sub},
 11};
 12use sum_tree::Bias;
 13use text::BufferId;
 14
 15/// A multibuffer anchor derived from an anchor into a specific excerpted buffer.
 16#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
 17pub struct ExcerptAnchor {
 18    pub(crate) text_anchor: text::Anchor,
 19    pub(crate) path: PathKeyIndex,
 20    pub(crate) diff_base_anchor: Option<text::Anchor>,
 21}
 22
 23/// A stable reference to a position within a [`MultiBuffer`](super::MultiBuffer).
 24///
 25/// Unlike simple offsets, anchors remain valid as the text is edited, automatically
 26/// adjusting to reflect insertions and deletions around them.
 27#[derive(Clone, Copy, Eq, PartialEq, Hash)]
 28pub enum Anchor {
 29    /// An anchor that always resolves to the start of the multibuffer.
 30    Min,
 31    /// An anchor that's attached to a specific excerpted buffer.
 32    Excerpt(ExcerptAnchor),
 33    /// An anchor that always resolves to the end of the multibuffer.
 34    Max,
 35}
 36
 37pub(crate) enum AnchorSeekTarget<'a> {
 38    // buffer no longer exists at its original path key in the multibuffer
 39    Missing {
 40        path_key: &'a PathKey,
 41    },
 42    // we have excerpts for the buffer at the expected path key
 43    Excerpt {
 44        path_key: &'a PathKey,
 45        path_key_index: PathKeyIndex,
 46        anchor: text::Anchor,
 47        snapshot: &'a BufferSnapshot,
 48    },
 49    // no excerpts and it's a min or max anchor
 50    Empty,
 51}
 52
 53impl std::fmt::Debug for AnchorSeekTarget<'_> {
 54    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 55        match self {
 56            Self::Excerpt {
 57                path_key,
 58                path_key_index: _,
 59                anchor,
 60                snapshot: _,
 61            } => f
 62                .debug_struct("Excerpt")
 63                .field("path_key", path_key)
 64                .field("anchor", anchor)
 65                .finish(),
 66            Self::Missing { path_key } => f
 67                .debug_struct("Missing")
 68                .field("path_key", path_key)
 69                .finish(),
 70            Self::Empty => f.debug_struct("Empty").finish(),
 71        }
 72    }
 73}
 74
 75impl std::fmt::Debug for Anchor {
 76    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 77        match self {
 78            Anchor::Min => write!(f, "Anchor::Min"),
 79            Anchor::Max => write!(f, "Anchor::Max"),
 80            Anchor::Excerpt(excerpt_anchor) => write!(f, "{excerpt_anchor:?}"),
 81        }
 82    }
 83}
 84
 85impl From<ExcerptAnchor> for Anchor {
 86    fn from(anchor: ExcerptAnchor) -> Self {
 87        Anchor::Excerpt(anchor)
 88    }
 89}
 90
 91impl ExcerptAnchor {
 92    pub(crate) fn buffer_id(&self) -> BufferId {
 93        self.text_anchor.buffer_id
 94    }
 95
 96    pub(crate) fn text_anchor(&self) -> text::Anchor {
 97        self.text_anchor
 98    }
 99
100    pub(crate) fn with_diff_base_anchor(mut self, diff_base_anchor: text::Anchor) -> Self {
101        self.diff_base_anchor = Some(diff_base_anchor);
102        self
103    }
104
105    pub(crate) fn cmp(&self, other: &Self, snapshot: &MultiBufferSnapshot) -> Ordering {
106        let Some(self_path_key) = snapshot.path_keys.get_index(self.path.0 as usize) else {
107            panic!("anchor's path was never added to multibuffer")
108        };
109        let Some(other_path_key) = snapshot.path_keys.get_index(other.path.0 as usize) else {
110            panic!("anchor's path was never added to multibuffer")
111        };
112
113        match self_path_key.cmp(other_path_key) {
114            Ordering::Equal => (),
115            ordering => return ordering,
116        }
117
118        // in the case that you removed the buffer containing self,
119        // and added the buffer containing other with the same path key
120        // (ordering is arbitrary but consistent)
121        if self.text_anchor.buffer_id != other.text_anchor.buffer_id {
122            return self.text_anchor.buffer_id.cmp(&other.text_anchor.buffer_id);
123        }
124
125        // two anchors into the same buffer at the same path
126        let Some(buffer) = snapshot
127            .buffers
128            .get(&self.text_anchor.buffer_id)
129            .filter(|buffer_state| buffer_state.path_key == *self_path_key)
130        else {
131            // buffer no longer exists at the original path (which may have been reused for a different buffer),
132            // so no way to compare the anchors
133            return Ordering::Equal;
134        };
135        // two anchors into the same buffer at the same path that still exists at that path in the multibuffer
136        let text_cmp = self
137            .text_anchor()
138            .cmp(&other.text_anchor(), &buffer.buffer_snapshot);
139        if text_cmp != Ordering::Equal {
140            return text_cmp;
141        }
142
143        if (self.diff_base_anchor.is_some() || other.diff_base_anchor.is_some())
144            && let Some(base_text) = find_diff_state(&snapshot.diffs, self.text_anchor.buffer_id)
145                .map(|diff| diff.base_text())
146        {
147            let self_anchor = self.diff_base_anchor.filter(|a| a.is_valid(base_text));
148            let other_anchor = other.diff_base_anchor.filter(|a| a.is_valid(base_text));
149            return match (self_anchor, other_anchor) {
150                (Some(a), Some(b)) => a.cmp(&b, base_text),
151                (Some(_), None) => match other.text_anchor().bias {
152                    Bias::Left => Ordering::Greater,
153                    Bias::Right => Ordering::Less,
154                },
155                (None, Some(_)) => match self.text_anchor().bias {
156                    Bias::Left => Ordering::Less,
157                    Bias::Right => Ordering::Greater,
158                },
159                (None, None) => Ordering::Equal,
160            };
161        }
162
163        Ordering::Equal
164    }
165
166    fn bias_left(&self, snapshot: &MultiBufferSnapshot) -> Self {
167        if self.text_anchor.bias == Bias::Left {
168            return *self;
169        }
170        let Some(buffer) = snapshot.buffer_for_id(self.text_anchor.buffer_id) else {
171            return *self;
172        };
173        let text_anchor = self.text_anchor().bias_left(&buffer);
174        let ret = Self::in_buffer(self.path, text_anchor);
175        if let Some(diff_base_anchor) = self.diff_base_anchor {
176            if let Some(diff) = find_diff_state(&snapshot.diffs, self.text_anchor.buffer_id)
177                && diff_base_anchor.is_valid(&diff.base_text())
178            {
179                ret.with_diff_base_anchor(diff_base_anchor.bias_left(diff.base_text()))
180            } else {
181                ret.with_diff_base_anchor(diff_base_anchor)
182            }
183        } else {
184            ret
185        }
186    }
187
188    fn bias_right(&self, snapshot: &MultiBufferSnapshot) -> Self {
189        if self.text_anchor.bias == Bias::Right {
190            return *self;
191        }
192        let Some(buffer) = snapshot.buffer_for_id(self.text_anchor.buffer_id) else {
193            return *self;
194        };
195        let text_anchor = self.text_anchor().bias_right(&buffer);
196        let ret = Self::in_buffer(self.path, text_anchor);
197        if let Some(diff_base_anchor) = self.diff_base_anchor {
198            if let Some(diff) = find_diff_state(&snapshot.diffs, self.text_anchor.buffer_id)
199                && diff_base_anchor.is_valid(&diff.base_text())
200            {
201                ret.with_diff_base_anchor(diff_base_anchor.bias_right(diff.base_text()))
202            } else {
203                ret.with_diff_base_anchor(diff_base_anchor)
204            }
205        } else {
206            ret
207        }
208    }
209
210    #[track_caller]
211    pub(crate) fn in_buffer(path: PathKeyIndex, text_anchor: text::Anchor) -> Self {
212        ExcerptAnchor {
213            path,
214            diff_base_anchor: None,
215            text_anchor,
216        }
217    }
218
219    fn is_valid(&self, snapshot: &MultiBufferSnapshot) -> bool {
220        let Some(target) = self.try_seek_target(snapshot) else {
221            return false;
222        };
223        let Some(buffer_snapshot) = snapshot.buffer_for_id(self.buffer_id()) else {
224            return false;
225        };
226        // Early check to avoid invalid comparisons when seeking
227        if !buffer_snapshot.can_resolve(&self.text_anchor) {
228            return false;
229        }
230        let mut cursor = snapshot.excerpts.cursor::<ExcerptSummary>(());
231        cursor.seek(&target, Bias::Left);
232        let Some(excerpt) = cursor.item() else {
233            return false;
234        };
235        let is_valid = self.text_anchor == excerpt.range.context.start
236            || self.text_anchor == excerpt.range.context.end
237            || self.text_anchor.is_valid(&buffer_snapshot);
238        is_valid
239            && excerpt
240                .range
241                .context
242                .start
243                .cmp(&self.text_anchor(), buffer_snapshot)
244                .is_le()
245            && excerpt
246                .range
247                .context
248                .end
249                .cmp(&self.text_anchor(), buffer_snapshot)
250                .is_ge()
251    }
252
253    pub(crate) fn seek_target<'a>(
254        &self,
255        snapshot: &'a MultiBufferSnapshot,
256    ) -> AnchorSeekTarget<'a> {
257        self.try_seek_target(snapshot)
258            .expect("anchor is from different multi-buffer")
259    }
260
261    pub(crate) fn try_seek_target<'a>(
262        &self,
263        snapshot: &'a MultiBufferSnapshot,
264    ) -> Option<AnchorSeekTarget<'a>> {
265        let path_key = snapshot.try_path_for_anchor(*self)?;
266
267        let Some(state) = snapshot
268            .buffers
269            .get(&self.buffer_id())
270            .filter(|state| &state.path_key == path_key)
271        else {
272            return Some(AnchorSeekTarget::Missing { path_key });
273        };
274
275        Some(AnchorSeekTarget::Excerpt {
276            path_key,
277            path_key_index: self.path,
278            anchor: self.text_anchor(),
279            snapshot: &state.buffer_snapshot,
280        })
281    }
282}
283
284impl ToOffset for ExcerptAnchor {
285    fn to_offset(&self, snapshot: &MultiBufferSnapshot) -> MultiBufferOffset {
286        Anchor::from(*self).to_offset(snapshot)
287    }
288
289    fn to_offset_utf16(&self, snapshot: &MultiBufferSnapshot) -> MultiBufferOffsetUtf16 {
290        Anchor::from(*self).to_offset_utf16(snapshot)
291    }
292}
293
294impl ToPoint for ExcerptAnchor {
295    fn to_point(&self, snapshot: &MultiBufferSnapshot) -> Point {
296        Anchor::from(*self).to_point(snapshot)
297    }
298
299    fn to_point_utf16(&self, snapshot: &MultiBufferSnapshot) -> rope::PointUtf16 {
300        Anchor::from(*self).to_point_utf16(snapshot)
301    }
302}
303
304impl Anchor {
305    pub fn is_min(&self) -> bool {
306        matches!(self, Self::Min)
307    }
308
309    pub fn is_max(&self) -> bool {
310        matches!(self, Self::Max)
311    }
312
313    pub(crate) fn in_buffer(path: PathKeyIndex, text_anchor: text::Anchor) -> Self {
314        Self::Excerpt(ExcerptAnchor::in_buffer(path, text_anchor))
315    }
316
317    pub(crate) fn range_in_buffer(path: PathKeyIndex, range: Range<text::Anchor>) -> Range<Self> {
318        Self::in_buffer(path, range.start)..Self::in_buffer(path, range.end)
319    }
320
321    pub fn cmp(&self, other: &Anchor, snapshot: &MultiBufferSnapshot) -> Ordering {
322        match (self, other) {
323            (Anchor::Min, Anchor::Min) => return Ordering::Equal,
324            (Anchor::Max, Anchor::Max) => return Ordering::Equal,
325            (Anchor::Min, _) => return Ordering::Less,
326            (Anchor::Max, _) => return Ordering::Greater,
327            (_, Anchor::Max) => return Ordering::Less,
328            (_, Anchor::Min) => return Ordering::Greater,
329            (Anchor::Excerpt(self_excerpt_anchor), Anchor::Excerpt(other_excerpt_anchor)) => {
330                self_excerpt_anchor.cmp(other_excerpt_anchor, snapshot)
331            }
332        }
333    }
334
335    pub fn bias(&self) -> Bias {
336        match self {
337            Anchor::Min => Bias::Left,
338            Anchor::Max => Bias::Right,
339            Anchor::Excerpt(anchor) => anchor.text_anchor.bias,
340        }
341    }
342
343    pub fn bias_left(&self, snapshot: &MultiBufferSnapshot) -> Anchor {
344        match self {
345            Anchor::Min => *self,
346            Anchor::Max => snapshot.anchor_before(snapshot.max_point()),
347            Anchor::Excerpt(anchor) => Anchor::Excerpt(anchor.bias_left(snapshot)),
348        }
349    }
350
351    pub fn bias_right(&self, snapshot: &MultiBufferSnapshot) -> Anchor {
352        match self {
353            Anchor::Max => *self,
354            Anchor::Min => snapshot.anchor_after(Point::zero()),
355            Anchor::Excerpt(anchor) => Anchor::Excerpt(anchor.bias_right(snapshot)),
356        }
357    }
358
359    pub fn summary<D>(&self, snapshot: &MultiBufferSnapshot) -> D
360    where
361        D: MultiBufferDimension
362            + Ord
363            + Sub<Output = D::TextDimension>
364            + Sub<D::TextDimension, Output = D>
365            + AddAssign<D::TextDimension>
366            + Add<D::TextDimension, Output = D>,
367        D::TextDimension: Sub<Output = D::TextDimension> + Ord,
368    {
369        snapshot.summary_for_anchor(self)
370    }
371
372    pub fn is_valid(&self, snapshot: &MultiBufferSnapshot) -> bool {
373        match self {
374            Anchor::Min | Anchor::Max => true,
375            Anchor::Excerpt(excerpt_anchor) => excerpt_anchor.is_valid(snapshot),
376        }
377    }
378
379    fn to_excerpt_anchor(&self, snapshot: &MultiBufferSnapshot) -> Option<ExcerptAnchor> {
380        match self {
381            Anchor::Min => {
382                let excerpt = snapshot.excerpts.first()?;
383
384                Some(ExcerptAnchor {
385                    text_anchor: excerpt.range.context.start,
386                    path: excerpt.path_key_index,
387                    diff_base_anchor: None,
388                })
389            }
390            Anchor::Excerpt(excerpt_anchor) => Some(*excerpt_anchor),
391            Anchor::Max => {
392                let excerpt = snapshot.excerpts.last()?;
393
394                Some(ExcerptAnchor {
395                    text_anchor: excerpt.range.context.end,
396                    path: excerpt.path_key_index,
397                    diff_base_anchor: None,
398                })
399            }
400        }
401    }
402
403    pub(crate) fn seek_target<'a>(
404        &self,
405        snapshot: &'a MultiBufferSnapshot,
406    ) -> AnchorSeekTarget<'a> {
407        let Some(excerpt_anchor) = self.to_excerpt_anchor(snapshot) else {
408            return AnchorSeekTarget::Empty;
409        };
410
411        excerpt_anchor.seek_target(snapshot)
412    }
413
414    pub(crate) fn excerpt_anchor(&self) -> Option<ExcerptAnchor> {
415        match self {
416            Anchor::Min | Anchor::Max => None,
417            Anchor::Excerpt(excerpt_anchor) => Some(*excerpt_anchor),
418        }
419    }
420
421    pub(crate) fn text_anchor(&self) -> Option<text::Anchor> {
422        match self {
423            Anchor::Min | Anchor::Max => None,
424            Anchor::Excerpt(excerpt_anchor) => Some(excerpt_anchor.text_anchor()),
425        }
426    }
427
428    pub fn opaque_id(&self) -> Option<[u8; 20]> {
429        self.text_anchor().map(|a| a.opaque_id())
430    }
431
432    /// Note: anchor_to_buffer_anchor is probably what you want
433    pub fn raw_text_anchor(&self) -> Option<text::Anchor> {
434        match self {
435            Anchor::Min | Anchor::Max => None,
436            Anchor::Excerpt(excerpt_anchor) => Some(excerpt_anchor.text_anchor),
437        }
438    }
439
440    pub(crate) fn try_seek_target<'a>(
441        &self,
442        snapshot: &'a MultiBufferSnapshot,
443    ) -> Option<AnchorSeekTarget<'a>> {
444        let Some(excerpt_anchor) = self.to_excerpt_anchor(snapshot) else {
445            return Some(AnchorSeekTarget::Empty);
446        };
447        excerpt_anchor.try_seek_target(snapshot)
448    }
449
450    /// Returns the text anchor for this anchor.
451    /// Panics if the anchor is from a different buffer.
452    pub fn text_anchor_in(&self, buffer: &BufferSnapshot) -> text::Anchor {
453        match self {
454            Anchor::Min => text::Anchor::min_for_buffer(buffer.remote_id()),
455            Anchor::Excerpt(excerpt_anchor) => {
456                let text_anchor = excerpt_anchor.text_anchor;
457                assert_eq!(text_anchor.buffer_id, buffer.remote_id());
458                text_anchor
459            }
460            Anchor::Max => text::Anchor::max_for_buffer(buffer.remote_id()),
461        }
462    }
463
464    pub fn diff_base_anchor(&self) -> Option<text::Anchor> {
465        self.excerpt_anchor()?.diff_base_anchor
466    }
467
468    #[cfg(any(test, feature = "test-support"))]
469    pub fn expect_text_anchor(&self) -> text::Anchor {
470        self.excerpt_anchor().unwrap().text_anchor
471    }
472
473    pub fn with_diff_base_anchor(mut self, diff_base_anchor: text::Anchor) -> Self {
474        match &mut self {
475            Anchor::Min | Anchor::Max => {}
476            Anchor::Excerpt(excerpt_anchor) => {
477                excerpt_anchor.diff_base_anchor = Some(diff_base_anchor);
478            }
479        }
480        self
481    }
482}
483
484impl ToOffset for Anchor {
485    fn to_offset(&self, snapshot: &MultiBufferSnapshot) -> MultiBufferOffset {
486        self.summary(snapshot)
487    }
488    fn to_offset_utf16(&self, snapshot: &MultiBufferSnapshot) -> MultiBufferOffsetUtf16 {
489        self.summary(snapshot)
490    }
491}
492
493impl ToPoint for Anchor {
494    fn to_point<'a>(&self, snapshot: &MultiBufferSnapshot) -> Point {
495        self.summary(snapshot)
496    }
497    fn to_point_utf16(&self, snapshot: &MultiBufferSnapshot) -> rope::PointUtf16 {
498        self.summary(snapshot)
499    }
500}
501
502pub trait AnchorRangeExt {
503    fn cmp(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> Ordering;
504    fn includes(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> bool;
505    fn overlaps(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> bool;
506    fn to_offset(&self, content: &MultiBufferSnapshot) -> Range<MultiBufferOffset>;
507    fn to_point(&self, content: &MultiBufferSnapshot) -> Range<Point>;
508}
509
510impl AnchorRangeExt for Range<Anchor> {
511    fn cmp(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> Ordering {
512        match self.start.cmp(&other.start, buffer) {
513            Ordering::Equal => other.end.cmp(&self.end, buffer),
514            ord => ord,
515        }
516    }
517
518    fn includes(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> bool {
519        self.start.cmp(&other.start, buffer).is_le() && other.end.cmp(&self.end, buffer).is_le()
520    }
521
522    fn overlaps(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> bool {
523        self.end.cmp(&other.start, buffer).is_ge() && self.start.cmp(&other.end, buffer).is_le()
524    }
525
526    fn to_offset(&self, content: &MultiBufferSnapshot) -> Range<MultiBufferOffset> {
527        self.start.to_offset(content)..self.end.to_offset(content)
528    }
529
530    fn to_point(&self, content: &MultiBufferSnapshot) -> Range<Point> {
531        self.start.to_point(content)..self.end.to_point(content)
532    }
533}