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