anchor.rs

  1use super::{ExcerptId, MultiBufferSnapshot, ToOffset, ToOffsetUtf16, ToPoint};
  2use language::{OffsetUtf16, Point, TextDimension};
  3use std::{
  4    cmp::Ordering,
  5    ops::{Range, Sub},
  6};
  7use sum_tree::Bias;
  8use text::BufferId;
  9
 10#[derive(Clone, Copy, Eq, PartialEq, Debug, Hash)]
 11pub struct Anchor {
 12    pub buffer_id: Option<BufferId>,
 13    pub excerpt_id: ExcerptId,
 14    pub text_anchor: text::Anchor,
 15    pub diff_base_anchor: Option<text::Anchor>,
 16}
 17
 18impl Anchor {
 19    pub fn in_buffer(
 20        excerpt_id: ExcerptId,
 21        buffer_id: BufferId,
 22        text_anchor: text::Anchor,
 23    ) -> Self {
 24        Self {
 25            buffer_id: Some(buffer_id),
 26            excerpt_id,
 27            text_anchor,
 28            diff_base_anchor: None,
 29        }
 30    }
 31
 32    pub fn range_in_buffer(
 33        excerpt_id: ExcerptId,
 34        buffer_id: BufferId,
 35        range: Range<text::Anchor>,
 36    ) -> Range<Self> {
 37        Self::in_buffer(excerpt_id, buffer_id, range.start)
 38            ..Self::in_buffer(excerpt_id, buffer_id, range.end)
 39    }
 40
 41    pub fn min() -> Self {
 42        Self {
 43            buffer_id: None,
 44            excerpt_id: ExcerptId::min(),
 45            text_anchor: text::Anchor::MIN,
 46            diff_base_anchor: None,
 47        }
 48    }
 49
 50    pub fn max() -> Self {
 51        Self {
 52            buffer_id: None,
 53            excerpt_id: ExcerptId::max(),
 54            text_anchor: text::Anchor::MAX,
 55            diff_base_anchor: None,
 56        }
 57    }
 58
 59    pub fn cmp(&self, other: &Anchor, snapshot: &MultiBufferSnapshot) -> Ordering {
 60        if self == other {
 61            return Ordering::Equal;
 62        }
 63
 64        let excerpt_id_cmp = self.excerpt_id.cmp(&other.excerpt_id, snapshot);
 65        if excerpt_id_cmp.is_ne() {
 66            return excerpt_id_cmp;
 67        }
 68        if self.excerpt_id == ExcerptId::min() || self.excerpt_id == ExcerptId::max() {
 69            return Ordering::Equal;
 70        }
 71        if let Some(excerpt) = snapshot.excerpt(self.excerpt_id) {
 72            let text_cmp = self.text_anchor.cmp(&other.text_anchor, &excerpt.buffer);
 73            if text_cmp.is_ne() {
 74                return text_cmp;
 75            }
 76            if self.diff_base_anchor.is_some() || other.diff_base_anchor.is_some() {
 77                if let Some(base_text) = snapshot
 78                    .diffs
 79                    .get(&excerpt.buffer_id)
 80                    .map(|diff| diff.base_text())
 81                {
 82                    let self_anchor = self.diff_base_anchor.filter(|a| base_text.can_resolve(a));
 83                    let other_anchor = other.diff_base_anchor.filter(|a| base_text.can_resolve(a));
 84                    return match (self_anchor, other_anchor) {
 85                        (Some(a), Some(b)) => a.cmp(&b, base_text),
 86                        (Some(_), None) => match other.text_anchor.bias {
 87                            Bias::Left => Ordering::Greater,
 88                            Bias::Right => Ordering::Less,
 89                        },
 90                        (None, Some(_)) => match self.text_anchor.bias {
 91                            Bias::Left => Ordering::Less,
 92                            Bias::Right => Ordering::Greater,
 93                        },
 94                        (None, None) => Ordering::Equal,
 95                    };
 96                }
 97            }
 98        }
 99        Ordering::Equal
100    }
101
102    pub fn bias(&self) -> Bias {
103        self.text_anchor.bias
104    }
105
106    pub fn bias_left(&self, snapshot: &MultiBufferSnapshot) -> Anchor {
107        if self.text_anchor.bias != Bias::Left {
108            if let Some(excerpt) = snapshot.excerpt(self.excerpt_id) {
109                return Self {
110                    buffer_id: self.buffer_id,
111                    excerpt_id: self.excerpt_id,
112                    text_anchor: self.text_anchor.bias_left(&excerpt.buffer),
113                    diff_base_anchor: self.diff_base_anchor.map(|a| {
114                        if let Some(base_text) = snapshot
115                            .diffs
116                            .get(&excerpt.buffer_id)
117                            .map(|diff| diff.base_text())
118                        {
119                            if a.buffer_id == Some(base_text.remote_id()) {
120                                return a.bias_left(base_text);
121                            }
122                        }
123                        a
124                    }),
125                };
126            }
127        }
128        *self
129    }
130
131    pub fn bias_right(&self, snapshot: &MultiBufferSnapshot) -> Anchor {
132        if self.text_anchor.bias != Bias::Right {
133            if let Some(excerpt) = snapshot.excerpt(self.excerpt_id) {
134                return Self {
135                    buffer_id: self.buffer_id,
136                    excerpt_id: self.excerpt_id,
137                    text_anchor: self.text_anchor.bias_right(&excerpt.buffer),
138                    diff_base_anchor: self.diff_base_anchor.map(|a| {
139                        if let Some(base_text) = snapshot
140                            .diffs
141                            .get(&excerpt.buffer_id)
142                            .map(|diff| diff.base_text())
143                        {
144                            if a.buffer_id == Some(base_text.remote_id()) {
145                                return a.bias_right(&base_text);
146                            }
147                        }
148                        a
149                    }),
150                };
151            }
152        }
153        *self
154    }
155
156    pub fn summary<D>(&self, snapshot: &MultiBufferSnapshot) -> D
157    where
158        D: TextDimension + Ord + Sub<D, Output = D>,
159    {
160        snapshot.summary_for_anchor(self)
161    }
162
163    pub fn is_valid(&self, snapshot: &MultiBufferSnapshot) -> bool {
164        if *self == Anchor::min() || *self == Anchor::max() {
165            true
166        } else if let Some(excerpt) = snapshot.excerpt(self.excerpt_id) {
167            excerpt.contains(self)
168                && (self.text_anchor == excerpt.range.context.start
169                    || self.text_anchor == excerpt.range.context.end
170                    || self.text_anchor.is_valid(&excerpt.buffer))
171        } else {
172            false
173        }
174    }
175}
176
177impl ToOffset for Anchor {
178    fn to_offset(&self, snapshot: &MultiBufferSnapshot) -> usize {
179        self.summary(snapshot)
180    }
181}
182
183impl ToOffsetUtf16 for Anchor {
184    fn to_offset_utf16(&self, snapshot: &MultiBufferSnapshot) -> OffsetUtf16 {
185        self.summary(snapshot)
186    }
187}
188
189impl ToPoint for Anchor {
190    fn to_point<'a>(&self, snapshot: &MultiBufferSnapshot) -> Point {
191        self.summary(snapshot)
192    }
193}
194
195pub trait AnchorRangeExt {
196    fn cmp(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> Ordering;
197    fn includes(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> bool;
198    fn overlaps(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> bool;
199    fn to_offset(&self, content: &MultiBufferSnapshot) -> Range<usize>;
200    fn to_point(&self, content: &MultiBufferSnapshot) -> Range<Point>;
201}
202
203impl AnchorRangeExt for Range<Anchor> {
204    fn cmp(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> Ordering {
205        match self.start.cmp(&other.start, buffer) {
206            Ordering::Equal => other.end.cmp(&self.end, buffer),
207            ord => ord,
208        }
209    }
210
211    fn includes(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> bool {
212        self.start.cmp(&other.start, &buffer).is_le() && other.end.cmp(&self.end, &buffer).is_le()
213    }
214
215    fn overlaps(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> bool {
216        self.end.cmp(&other.start, buffer).is_ge() && self.start.cmp(&other.end, buffer).is_le()
217    }
218
219    fn to_offset(&self, content: &MultiBufferSnapshot) -> Range<usize> {
220        self.start.to_offset(content)..self.end.to_offset(content)
221    }
222
223    fn to_point(&self, content: &MultiBufferSnapshot) -> Range<Point> {
224        self.start.to_point(content)..self.end.to_point(content)
225    }
226}
227
228#[derive(Clone, Copy, Eq, PartialEq, Debug, Hash, Ord, PartialOrd)]
229pub struct Offset(pub usize);