1use crate::{ExcerptSummary, MultiBufferDimension, MultiBufferOffset, MultiBufferOffsetUtf16};
2
3use super::{MultiBufferSnapshot, ToOffset, ToPoint};
4use language::Point;
5use std::{
6 cmp::Ordering,
7 ops::{Add, AddAssign, Range, Sub},
8};
9use sum_tree::Bias;
10use text::BufferId;
11use util::debug_panic;
12
13/// A stable reference to a position within a [`MultiBuffer`](super::MultiBuffer).
14///
15/// Unlike simple offsets, anchors remain valid as the text is edited, automatically
16/// adjusting to reflect insertions and deletions around them.
17#[derive(Clone, Copy, Eq, PartialEq, Hash)]
18pub enum Anchor {
19 Min,
20 Max,
21 Text {
22 /// The position within the excerpt's underlying buffer. This is a stable
23 /// reference that remains valid as the buffer text is edited.
24 timestamp: clock::Lamport,
25
26 /// The byte offset into the text inserted in the operation
27 /// at `timestamp`.
28 offset: u32,
29 /// Whether this anchor stays attached to the character *before* or *after*
30 /// the offset.
31 bias: Bias,
32 buffer_id: BufferId,
33 /// When present, indicates this anchor points into deleted text within an
34 /// expanded diff hunk. The anchor references a position in the diff base
35 /// (original) text rather than the current buffer text. This is used when
36 /// displaying inline diffs where deleted lines are shown.
37 diff_base_anchor: Option<text::Anchor>,
38 },
39}
40
41impl std::fmt::Debug for Anchor {
42 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43 if self.is_min() {
44 return write!(f, "Anchor::Min");
45 }
46 if self.is_max() {
47 return write!(f, "Anchor::Max");
48 }
49
50 f.debug_struct("Anchor")
51 .field("text_anchor", &self.text_anchor().unwrap())
52 .field("diff_base_anchor", &self.diff_base_anchor())
53 .finish()
54 }
55}
56
57impl Anchor {
58 pub fn text_anchor(&self) -> Option<text::Anchor> {
59 match self {
60 Self::Min | Self::Max => None,
61 Self::Text {
62 timestamp,
63 offset,
64 bias,
65 buffer_id,
66 ..
67 } => Some(text::Anchor::new(
68 *timestamp,
69 *offset,
70 *bias,
71 Some(*buffer_id),
72 )),
73 }
74 }
75
76 pub fn diff_base_anchor(&self) -> Option<text::Anchor> {
77 match self {
78 Self::Min | Self::Max => None,
79 Self::Text {
80 diff_base_anchor, ..
81 } => *diff_base_anchor,
82 }
83 }
84
85 pub fn with_diff_base_anchor(mut self, diff_base_anchor: text::Anchor) -> Self {
86 match &mut self {
87 Self::Min | Self::Max => {
88 debug_panic!("with_diff_base_anchor called on min or max anchor");
89 }
90 Self::Text {
91 diff_base_anchor: self_diff_base_anchor,
92 ..
93 } => {
94 *self_diff_base_anchor = Some(diff_base_anchor);
95 }
96 };
97 self
98 }
99
100 pub fn text(text_anchor: text::Anchor) -> Self {
101 let Some(buffer_id) = text_anchor.buffer_id else {
102 panic!("text_anchor must have a buffer_id");
103 };
104 Self::Text {
105 diff_base_anchor: None,
106 timestamp: text_anchor.timestamp(),
107 buffer_id,
108 offset: text_anchor.offset,
109 bias: text_anchor.bias,
110 }
111 }
112
113 pub fn range_in_buffer(range: Range<text::Anchor>) -> Range<Self> {
114 Self::text(range.start)..Self::text(range.end)
115 }
116
117 pub fn min() -> Self {
118 Self::Min
119 }
120
121 pub fn max() -> Self {
122 Self::Max
123 }
124
125 pub fn is_min(&self) -> bool {
126 matches!(self, Self::Min)
127 }
128
129 pub fn is_max(&self) -> bool {
130 matches!(self, Self::Max)
131 }
132
133 pub fn cmp(&self, other: &Anchor, snapshot: &MultiBufferSnapshot) -> Ordering {
134 let (self_text_anchor, other_text_anchor) = match (self, other) {
135 (Anchor::Min, Anchor::Min) => return Ordering::Equal,
136 (Anchor::Max, Anchor::Max) => return Ordering::Equal,
137 (Anchor::Min, _) => return Ordering::Less,
138 (Anchor::Max, _) => return Ordering::Greater,
139 (_, Anchor::Max) => return Ordering::Less,
140 (_, Anchor::Min) => return Ordering::Greater,
141 (Anchor::Text { .. }, Anchor::Text { .. }) => {
142 (self.text_anchor().unwrap(), other.text_anchor().unwrap())
143 }
144 };
145 let self_buffer_id = self_text_anchor.buffer_id.unwrap();
146 let other_buffer_id = other_text_anchor.buffer_id.unwrap();
147
148 let Some(self_path_key) = snapshot.path_keys.get(&self_buffer_id) else {
149 panic!("path key was never set for buffer_id")
150 };
151 let Some(other_path_key) = snapshot.path_keys.get(&other_buffer_id) else {
152 panic!("path key was never set for buffer_id")
153 };
154
155 if self_path_key.cmp(other_path_key) != Ordering::Equal {
156 return self_path_key.cmp(other_path_key);
157 }
158
159 // in the case that you removed the buffer contianing self,
160 // and added the buffer containing other with the same path key
161 if self_buffer_id != other_buffer_id {
162 return self_buffer_id.cmp(&other_buffer_id);
163 }
164
165 let Some(buffer) = snapshot.buffer_for_path(&self_path_key) else {
166 return Ordering::Equal;
167 };
168 let text_cmp = self_text_anchor.cmp(&other_text_anchor, buffer);
169 if text_cmp != Ordering::Equal {
170 return text_cmp;
171 }
172
173 if (self.diff_base_anchor().is_some() || other.diff_base_anchor().is_some())
174 && let Some(base_text) = snapshot
175 .diffs
176 .get(&self_buffer_id)
177 .map(|diff| diff.base_text())
178 {
179 let self_anchor = self.diff_base_anchor().filter(|a| a.is_valid(base_text));
180 let other_anchor = other.diff_base_anchor().filter(|a| a.is_valid(base_text));
181 return match (self_anchor, other_anchor) {
182 (Some(a), Some(b)) => a.cmp(&b, base_text),
183 (Some(_), None) => match other_text_anchor.bias {
184 Bias::Left => Ordering::Greater,
185 Bias::Right => Ordering::Less,
186 },
187 (None, Some(_)) => match self_text_anchor.bias {
188 Bias::Left => Ordering::Less,
189 Bias::Right => Ordering::Greater,
190 },
191 (None, None) => Ordering::Equal,
192 };
193 }
194
195 Ordering::Equal
196 }
197
198 pub fn bias(&self) -> Bias {
199 match self {
200 Anchor::Min => Bias::Left,
201 Anchor::Max => Bias::Right,
202 Anchor::Text { bias, .. } => *bias,
203 }
204 }
205
206 pub fn bias_left(&self, snapshot: &MultiBufferSnapshot) -> Anchor {
207 match self {
208 Anchor::Min => *self,
209 Anchor::Max => snapshot.anchor_before(snapshot.max_point()),
210 Anchor::Text {
211 bias, buffer_id, ..
212 } => {
213 if *bias == Bias::Left {
214 return *self;
215 }
216 let Some(buffer) = snapshot.buffer_for_id(*buffer_id) else {
217 return *self;
218 };
219 let text_anchor = self.text_anchor().unwrap().bias_left(&buffer);
220 let ret = Self::text(text_anchor);
221 if let Some(diff_base_anchor) = self.diff_base_anchor() {
222 if let Some(diff) = snapshot.diffs.get(&buffer_id)
223 && diff_base_anchor.is_valid(&diff.base_text())
224 {
225 ret.with_diff_base_anchor(diff_base_anchor.bias_left(diff.base_text()))
226 } else {
227 ret.with_diff_base_anchor(diff_base_anchor)
228 }
229 } else {
230 ret
231 }
232 }
233 }
234 }
235
236 pub fn bias_right(&self, snapshot: &MultiBufferSnapshot) -> Anchor {
237 match self {
238 Anchor::Min => *self,
239 Anchor::Max => snapshot.anchor_after(Point::zero()),
240 Anchor::Text {
241 bias, buffer_id, ..
242 } => {
243 if *bias == Bias::Right {
244 return *self;
245 }
246 let Some(buffer) = snapshot.buffer_for_id(*buffer_id) else {
247 return *self;
248 };
249 let text_anchor = self.text_anchor().unwrap().bias_right(&buffer);
250 let ret = Self::text(text_anchor);
251 if let Some(diff_base_anchor) = self.diff_base_anchor() {
252 if let Some(diff) = snapshot.diffs.get(&buffer_id)
253 && diff_base_anchor.is_valid(&diff.base_text())
254 {
255 ret.with_diff_base_anchor(diff_base_anchor.bias_right(diff.base_text()))
256 } else {
257 ret.with_diff_base_anchor(diff_base_anchor)
258 }
259 } else {
260 ret
261 }
262 }
263 }
264 }
265
266 pub fn summary<D>(&self, snapshot: &MultiBufferSnapshot) -> D
267 where
268 D: MultiBufferDimension
269 + Ord
270 + Sub<Output = D::TextDimension>
271 + Sub<D::TextDimension, Output = D>
272 + AddAssign<D::TextDimension>
273 + Add<D::TextDimension, Output = D>,
274 D::TextDimension: Sub<Output = D::TextDimension> + Ord,
275 {
276 snapshot.summary_for_anchor(self)
277 }
278
279 pub fn is_valid(&self, snapshot: &MultiBufferSnapshot) -> bool {
280 let Some(text_anchor) = self.text_anchor() else {
281 return true;
282 };
283 let Some(buffer_id) = text_anchor.buffer_id else {
284 debug_panic!("missing buffer_id for anchor");
285 return false;
286 };
287
288 let Some(target) = snapshot.anchor_seek_target(*self) else {
289 return false;
290 };
291 let mut cursor = snapshot.excerpts.cursor::<ExcerptSummary>(());
292 cursor.seek(&target, Bias::Left);
293 let Some(excerpt) = cursor.item() else {
294 return false;
295 };
296 excerpt.buffer.remote_id() == buffer_id
297 && excerpt
298 .range
299 .context
300 .start
301 .cmp(&text_anchor, &excerpt.buffer)
302 .is_le()
303 && excerpt
304 .range
305 .context
306 .end
307 .cmp(&text_anchor, &excerpt.buffer)
308 .is_ge()
309 }
310}
311
312impl ToOffset for Anchor {
313 fn to_offset(&self, snapshot: &MultiBufferSnapshot) -> MultiBufferOffset {
314 self.summary(snapshot)
315 }
316 fn to_offset_utf16(&self, snapshot: &MultiBufferSnapshot) -> MultiBufferOffsetUtf16 {
317 self.summary(snapshot)
318 }
319}
320
321impl ToPoint for Anchor {
322 fn to_point<'a>(&self, snapshot: &MultiBufferSnapshot) -> Point {
323 self.summary(snapshot)
324 }
325 fn to_point_utf16(&self, snapshot: &MultiBufferSnapshot) -> rope::PointUtf16 {
326 self.summary(snapshot)
327 }
328}
329
330pub trait AnchorRangeExt {
331 fn cmp(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> Ordering;
332 fn includes(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> bool;
333 fn overlaps(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> bool;
334 fn to_offset(&self, content: &MultiBufferSnapshot) -> Range<MultiBufferOffset>;
335 fn to_point(&self, content: &MultiBufferSnapshot) -> Range<Point>;
336}
337
338impl AnchorRangeExt for Range<Anchor> {
339 fn cmp(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> Ordering {
340 match self.start.cmp(&other.start, buffer) {
341 Ordering::Equal => other.end.cmp(&self.end, buffer),
342 ord => ord,
343 }
344 }
345
346 fn includes(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> bool {
347 self.start.cmp(&other.start, buffer).is_le() && other.end.cmp(&self.end, buffer).is_le()
348 }
349
350 fn overlaps(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> bool {
351 self.end.cmp(&other.start, buffer).is_ge() && self.start.cmp(&other.end, buffer).is_le()
352 }
353
354 fn to_offset(&self, content: &MultiBufferSnapshot) -> Range<MultiBufferOffset> {
355 self.start.to_offset(content)..self.end.to_offset(content)
356 }
357
358 fn to_point(&self, content: &MultiBufferSnapshot) -> Range<Point> {
359 self.start.to_point(content)..self.end.to_point(content)
360 }
361}