1use crate::{MultiBufferDimension, MultiBufferOffset, MultiBufferOffsetUtf16};
2
3use super::{ExcerptId, MultiBufferSnapshot, ToOffset, ToPoint};
4use language::Point;
5use std::{
6 cmp::Ordering,
7 ops::{Add, AddAssign, Range, Sub},
8};
9use sum_tree::Bias;
10
11/// A stable reference to a position within a [`MultiBuffer`](super::MultiBuffer).
12///
13/// Unlike simple offsets, anchors remain valid as the text is edited, automatically
14/// adjusting to reflect insertions and deletions around them.
15#[derive(Clone, Copy, Eq, PartialEq, Hash)]
16pub struct Anchor {
17 /// Identifies which excerpt within the multi-buffer this anchor belongs to.
18 /// A multi-buffer can contain multiple excerpts from different buffers.
19 pub excerpt_id: ExcerptId,
20 /// The position within the excerpt's underlying buffer. This is a stable
21 /// reference that remains valid as the buffer text is edited.
22 pub text_anchor: text::Anchor,
23 /// When present, indicates this anchor points into deleted text within an
24 /// expanded diff hunk. The anchor references a position in the diff base
25 /// (original) text rather than the current buffer text. This is used when
26 /// displaying inline diffs where deleted lines are shown.
27 pub diff_base_anchor: Option<text::Anchor>,
28}
29
30impl std::fmt::Debug for Anchor {
31 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32 if self.is_min() {
33 return write!(f, "Anchor::min({:?})", self.text_anchor.buffer_id);
34 }
35 if self.is_max() {
36 return write!(f, "Anchor::max({:?})", self.text_anchor.buffer_id);
37 }
38
39 f.debug_struct("Anchor")
40 .field("excerpt_id", &self.excerpt_id)
41 .field("text_anchor", &self.text_anchor)
42 .field("diff_base_anchor", &self.diff_base_anchor)
43 .finish()
44 }
45}
46
47impl Anchor {
48 pub fn with_diff_base_anchor(self, diff_base_anchor: text::Anchor) -> Self {
49 Self {
50 diff_base_anchor: Some(diff_base_anchor),
51 ..self
52 }
53 }
54
55 pub fn in_buffer(excerpt_id: ExcerptId, text_anchor: text::Anchor) -> Self {
56 Self {
57 excerpt_id,
58 text_anchor,
59 diff_base_anchor: None,
60 }
61 }
62
63 pub fn range_in_buffer(excerpt_id: ExcerptId, range: Range<text::Anchor>) -> Range<Self> {
64 Self::in_buffer(excerpt_id, range.start)..Self::in_buffer(excerpt_id, range.end)
65 }
66
67 pub fn min() -> Self {
68 Self {
69 excerpt_id: ExcerptId::min(),
70 text_anchor: text::Anchor::MIN,
71 diff_base_anchor: None,
72 }
73 }
74
75 pub fn max() -> Self {
76 Self {
77 excerpt_id: ExcerptId::max(),
78 text_anchor: text::Anchor::MAX,
79 diff_base_anchor: None,
80 }
81 }
82
83 pub fn is_min(&self) -> bool {
84 self.excerpt_id == ExcerptId::min()
85 && self.text_anchor.is_min()
86 && self.diff_base_anchor.is_none()
87 }
88
89 pub fn is_max(&self) -> bool {
90 self.excerpt_id == ExcerptId::max()
91 && self.text_anchor.is_max()
92 && self.diff_base_anchor.is_none()
93 }
94
95 pub fn cmp(&self, other: &Anchor, snapshot: &MultiBufferSnapshot) -> Ordering {
96 if self == other {
97 return Ordering::Equal;
98 }
99
100 let self_excerpt_id = snapshot.latest_excerpt_id(self.excerpt_id);
101 let other_excerpt_id = snapshot.latest_excerpt_id(other.excerpt_id);
102
103 let excerpt_id_cmp = self_excerpt_id.cmp(&other_excerpt_id, snapshot);
104 if excerpt_id_cmp.is_ne() {
105 return excerpt_id_cmp;
106 }
107 if self_excerpt_id == ExcerptId::max()
108 && self.text_anchor.is_max()
109 && self.text_anchor.is_max()
110 && self.diff_base_anchor.is_none()
111 && other.diff_base_anchor.is_none()
112 {
113 return Ordering::Equal;
114 }
115 if let Some(excerpt) = snapshot.excerpt(self_excerpt_id) {
116 let text_cmp = self.text_anchor.cmp(&other.text_anchor, &excerpt.buffer);
117 if text_cmp.is_ne() {
118 return text_cmp;
119 }
120 if (self.diff_base_anchor.is_some() || other.diff_base_anchor.is_some())
121 && let Some(base_text) = snapshot
122 .diff_state(excerpt.buffer_id)
123 .map(|diff| diff.base_text())
124 {
125 let self_anchor = self.diff_base_anchor.filter(|a| a.is_valid(base_text));
126 let other_anchor = other.diff_base_anchor.filter(|a| a.is_valid(base_text));
127 return match (self_anchor, other_anchor) {
128 (Some(a), Some(b)) => a.cmp(&b, base_text),
129 (Some(_), None) => match other.text_anchor.bias {
130 Bias::Left => Ordering::Greater,
131 Bias::Right => Ordering::Less,
132 },
133 (None, Some(_)) => match self.text_anchor.bias {
134 Bias::Left => Ordering::Less,
135 Bias::Right => Ordering::Greater,
136 },
137 (None, None) => Ordering::Equal,
138 };
139 }
140 }
141 Ordering::Equal
142 }
143
144 pub fn bias(&self) -> Bias {
145 self.text_anchor.bias
146 }
147
148 pub fn bias_left(&self, snapshot: &MultiBufferSnapshot) -> Anchor {
149 if self.text_anchor.bias != Bias::Left
150 && let Some(excerpt) = snapshot.excerpt(self.excerpt_id)
151 {
152 return Self {
153 excerpt_id: excerpt.id,
154 text_anchor: self.text_anchor.bias_left(&excerpt.buffer),
155 diff_base_anchor: self.diff_base_anchor.map(|a| {
156 if let Some(base_text) = snapshot
157 .diff_state(excerpt.buffer_id)
158 .map(|diff| diff.base_text())
159 && a.is_valid(&base_text)
160 {
161 return a.bias_left(base_text);
162 }
163 a
164 }),
165 };
166 }
167 *self
168 }
169
170 pub fn bias_right(&self, snapshot: &MultiBufferSnapshot) -> Anchor {
171 if self.text_anchor.bias != Bias::Right
172 && let Some(excerpt) = snapshot.excerpt(self.excerpt_id)
173 {
174 return Self {
175 excerpt_id: excerpt.id,
176 text_anchor: self.text_anchor.bias_right(&excerpt.buffer),
177 diff_base_anchor: self.diff_base_anchor.map(|a| {
178 if let Some(base_text) = snapshot
179 .diff_state(excerpt.buffer_id)
180 .map(|diff| diff.base_text())
181 && a.is_valid(&base_text)
182 {
183 return a.bias_right(base_text);
184 }
185 a
186 }),
187 };
188 }
189 *self
190 }
191
192 pub fn summary<D>(&self, snapshot: &MultiBufferSnapshot) -> D
193 where
194 D: MultiBufferDimension
195 + Ord
196 + Sub<Output = D::TextDimension>
197 + Sub<D::TextDimension, Output = D>
198 + AddAssign<D::TextDimension>
199 + Add<D::TextDimension, Output = D>,
200 D::TextDimension: Sub<Output = D::TextDimension> + Ord,
201 {
202 snapshot.summary_for_anchor(self)
203 }
204
205 pub fn is_valid(&self, snapshot: &MultiBufferSnapshot) -> bool {
206 if self.is_min() || self.is_max() {
207 true
208 } else if let Some(excerpt) = snapshot.excerpt(self.excerpt_id) {
209 (self.text_anchor == excerpt.range.context.start
210 || self.text_anchor == excerpt.range.context.end
211 || self.text_anchor.is_valid(&excerpt.buffer))
212 && excerpt.contains(self)
213 } else {
214 false
215 }
216 }
217}
218
219impl ToOffset for Anchor {
220 fn to_offset(&self, snapshot: &MultiBufferSnapshot) -> MultiBufferOffset {
221 self.summary(snapshot)
222 }
223 fn to_offset_utf16(&self, snapshot: &MultiBufferSnapshot) -> MultiBufferOffsetUtf16 {
224 self.summary(snapshot)
225 }
226}
227
228impl ToPoint for Anchor {
229 fn to_point<'a>(&self, snapshot: &MultiBufferSnapshot) -> Point {
230 self.summary(snapshot)
231 }
232 fn to_point_utf16(&self, snapshot: &MultiBufferSnapshot) -> rope::PointUtf16 {
233 self.summary(snapshot)
234 }
235}
236
237pub trait AnchorRangeExt {
238 fn cmp(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> Ordering;
239 fn includes(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> bool;
240 fn overlaps(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> bool;
241 fn to_offset(&self, content: &MultiBufferSnapshot) -> Range<MultiBufferOffset>;
242 fn to_point(&self, content: &MultiBufferSnapshot) -> Range<Point>;
243}
244
245impl AnchorRangeExt for Range<Anchor> {
246 fn cmp(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> Ordering {
247 match self.start.cmp(&other.start, buffer) {
248 Ordering::Equal => other.end.cmp(&self.end, buffer),
249 ord => ord,
250 }
251 }
252
253 fn includes(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> bool {
254 self.start.cmp(&other.start, buffer).is_le() && other.end.cmp(&self.end, buffer).is_le()
255 }
256
257 fn overlaps(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> bool {
258 self.end.cmp(&other.start, buffer).is_ge() && self.start.cmp(&other.end, buffer).is_le()
259 }
260
261 fn to_offset(&self, content: &MultiBufferSnapshot) -> Range<MultiBufferOffset> {
262 self.start.to_offset(content)..self.end.to_offset(content)
263 }
264
265 fn to_point(&self, content: &MultiBufferSnapshot) -> Range<Point> {
266 self.start.to_point(content)..self.end.to_point(content)
267 }
268}