anchor.rs

  1use super::{ExcerptId, MultiBufferSnapshot, ToOffset, ToPoint};
  2use anyhow::{anyhow, Result};
  3use std::{
  4    cmp::Ordering,
  5    ops::{Range, Sub},
  6};
  7use sum_tree::Bias;
  8use text::{rope::TextDimension, Point};
  9
 10#[derive(Clone, Eq, PartialEq, Debug, Hash)]
 11pub struct Anchor {
 12    pub(crate) excerpt_id: ExcerptId,
 13    pub(crate) text_anchor: text::Anchor,
 14}
 15
 16impl Anchor {
 17    pub fn min() -> Self {
 18        Self {
 19            excerpt_id: ExcerptId::min(),
 20            text_anchor: text::Anchor::min(),
 21        }
 22    }
 23
 24    pub fn max() -> Self {
 25        Self {
 26            excerpt_id: ExcerptId::max(),
 27            text_anchor: text::Anchor::max(),
 28        }
 29    }
 30
 31    pub fn cmp<'a>(&self, other: &Anchor, snapshot: &MultiBufferSnapshot) -> Result<Ordering> {
 32        let excerpt_id_cmp = self.excerpt_id.cmp(&other.excerpt_id);
 33        if excerpt_id_cmp.is_eq() {
 34            if self.excerpt_id == ExcerptId::max() {
 35                return Ok(Ordering::Equal);
 36            }
 37            self.text_anchor.cmp(
 38                &other.text_anchor,
 39                snapshot
 40                    .buffer_snapshot_for_excerpt(&self.excerpt_id)
 41                    .ok_or_else(|| anyhow!("excerpt {:?} not found", self.excerpt_id))?,
 42            )
 43        } else {
 44            return Ok(excerpt_id_cmp);
 45        }
 46    }
 47
 48    pub fn bias_left(&self, snapshot: &MultiBufferSnapshot) -> Anchor {
 49        if self.text_anchor.bias != Bias::Left {
 50            if let Some(buffer_snapshot) = snapshot.buffer_snapshot_for_excerpt(&self.excerpt_id) {
 51                return Self {
 52                    excerpt_id: self.excerpt_id.clone(),
 53                    text_anchor: self.text_anchor.bias_left(buffer_snapshot),
 54                };
 55            }
 56        }
 57        self.clone()
 58    }
 59
 60    pub fn bias_right(&self, snapshot: &MultiBufferSnapshot) -> Anchor {
 61        if self.text_anchor.bias != Bias::Right {
 62            if let Some(buffer_snapshot) = snapshot.buffer_snapshot_for_excerpt(&self.excerpt_id) {
 63                return Self {
 64                    excerpt_id: self.excerpt_id.clone(),
 65                    text_anchor: self.text_anchor.bias_right(buffer_snapshot),
 66                };
 67            }
 68        }
 69        self.clone()
 70    }
 71
 72    pub fn summary<D>(&self, snapshot: &MultiBufferSnapshot) -> D
 73    where
 74        D: TextDimension + Ord + Sub<D, Output = D>,
 75    {
 76        snapshot.summary_for_anchor(self)
 77    }
 78}
 79
 80impl ToOffset for Anchor {
 81    fn to_offset<'a>(&self, snapshot: &MultiBufferSnapshot) -> usize {
 82        self.summary(snapshot)
 83    }
 84}
 85
 86impl ToPoint for Anchor {
 87    fn to_point<'a>(&self, snapshot: &MultiBufferSnapshot) -> Point {
 88        self.summary(snapshot)
 89    }
 90}
 91
 92pub trait AnchorRangeExt {
 93    fn cmp(&self, b: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> Result<Ordering>;
 94    fn to_offset(&self, content: &MultiBufferSnapshot) -> Range<usize>;
 95    fn to_point(&self, content: &MultiBufferSnapshot) -> Range<Point>;
 96}
 97
 98impl AnchorRangeExt for Range<Anchor> {
 99    fn cmp(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> Result<Ordering> {
100        Ok(match self.start.cmp(&other.start, buffer)? {
101            Ordering::Equal => other.end.cmp(&self.end, buffer)?,
102            ord @ _ => ord,
103        })
104    }
105
106    fn to_offset(&self, content: &MultiBufferSnapshot) -> Range<usize> {
107        self.start.to_offset(&content)..self.end.to_offset(&content)
108    }
109
110    fn to_point(&self, content: &MultiBufferSnapshot) -> Range<Point> {
111        self.start.to_point(&content)..self.end.to_point(&content)
112    }
113}