1use crate::{
2 BufferId, BufferSnapshot, Point, PointUtf16, TextDimension, ToOffset, ToPoint, ToPointUtf16,
3 locator::Locator,
4};
5use std::{cmp::Ordering, fmt::Debug, ops::Range};
6use sum_tree::{Bias, Dimensions};
7
8/// A timestamped position in a buffer
9#[derive(Copy, Clone, Eq, PartialEq, Hash)]
10pub struct Anchor {
11 pub timestamp: clock::Lamport,
12 /// The byte offset in the buffer
13 pub offset: usize,
14 /// Describes which character the anchor is biased towards
15 pub bias: Bias,
16 pub buffer_id: Option<BufferId>,
17}
18
19impl Debug for Anchor {
20 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21 if self.is_min() {
22 return write!(f, "Anchor::min({:?})", self.buffer_id);
23 }
24 if self.is_max() {
25 return write!(f, "Anchor::max({:?})", self.buffer_id);
26 }
27
28 f.debug_struct("Anchor")
29 .field("timestamp", &self.timestamp)
30 .field("offset", &self.offset)
31 .field("bias", &self.bias)
32 .field("buffer_id", &self.buffer_id)
33 .finish()
34 }
35}
36
37impl Anchor {
38 pub const MIN: Self = Self {
39 timestamp: clock::Lamport::MIN,
40 offset: usize::MIN,
41 bias: Bias::Left,
42 buffer_id: None,
43 };
44
45 pub const MAX: Self = Self {
46 timestamp: clock::Lamport::MAX,
47 offset: usize::MAX,
48 bias: Bias::Right,
49 buffer_id: None,
50 };
51
52 pub fn min_for_buffer(buffer_id: BufferId) -> Self {
53 Self {
54 timestamp: clock::Lamport::MIN,
55 offset: usize::MIN,
56 bias: Bias::Left,
57 buffer_id: Some(buffer_id),
58 }
59 }
60
61 pub fn max_for_buffer(buffer_id: BufferId) -> Self {
62 Self {
63 timestamp: clock::Lamport::MAX,
64 offset: usize::MAX,
65 bias: Bias::Right,
66 buffer_id: Some(buffer_id),
67 }
68 }
69
70 pub fn min_min_range_for_buffer(buffer_id: BufferId) -> std::ops::Range<Self> {
71 let min = Self::min_for_buffer(buffer_id);
72 min..min
73 }
74 pub fn max_max_range_for_buffer(buffer_id: BufferId) -> std::ops::Range<Self> {
75 let max = Self::max_for_buffer(buffer_id);
76 max..max
77 }
78 pub fn min_max_range_for_buffer(buffer_id: BufferId) -> std::ops::Range<Self> {
79 Self::min_for_buffer(buffer_id)..Self::max_for_buffer(buffer_id)
80 }
81
82 pub fn cmp(&self, other: &Anchor, buffer: &BufferSnapshot) -> Ordering {
83 let fragment_id_comparison = if self.timestamp == other.timestamp {
84 Ordering::Equal
85 } else {
86 buffer
87 .fragment_id_for_anchor(self)
88 .cmp(buffer.fragment_id_for_anchor(other))
89 };
90
91 fragment_id_comparison
92 .then_with(|| self.offset.cmp(&other.offset))
93 .then_with(|| self.bias.cmp(&other.bias))
94 }
95
96 pub fn min<'a>(&'a self, other: &'a Self, buffer: &BufferSnapshot) -> &'a Self {
97 if self.cmp(other, buffer).is_le() {
98 self
99 } else {
100 other
101 }
102 }
103
104 pub fn max<'a>(&'a self, other: &'a Self, buffer: &BufferSnapshot) -> &'a Self {
105 if self.cmp(other, buffer).is_ge() {
106 self
107 } else {
108 other
109 }
110 }
111
112 pub fn bias(&self, bias: Bias, buffer: &BufferSnapshot) -> Anchor {
113 match bias {
114 Bias::Left => self.bias_left(buffer),
115 Bias::Right => self.bias_right(buffer),
116 }
117 }
118
119 pub fn bias_left(&self, buffer: &BufferSnapshot) -> Anchor {
120 match self.bias {
121 Bias::Left => *self,
122 Bias::Right => buffer.anchor_before(self),
123 }
124 }
125
126 pub fn bias_right(&self, buffer: &BufferSnapshot) -> Anchor {
127 match self.bias {
128 Bias::Left => buffer.anchor_after(self),
129 Bias::Right => *self,
130 }
131 }
132
133 pub fn summary<D>(&self, content: &BufferSnapshot) -> D
134 where
135 D: TextDimension,
136 {
137 content.summary_for_anchor(self)
138 }
139
140 /// Returns true when the [`Anchor`] is located inside a visible fragment.
141 pub fn is_valid(&self, buffer: &BufferSnapshot) -> bool {
142 if self.is_min() || self.is_max() {
143 true
144 } else if self.buffer_id.is_none_or(|id| id != buffer.remote_id) {
145 false
146 } else {
147 let Some(fragment_id) = buffer.try_fragment_id_for_anchor(self) else {
148 return false;
149 };
150 let (.., item) = buffer
151 .fragments
152 .find::<Dimensions<Option<&Locator>, usize>, _>(
153 &None,
154 &Some(fragment_id),
155 Bias::Left,
156 );
157 item.is_some_and(|fragment| fragment.visible)
158 }
159 }
160
161 pub fn is_min(&self) -> bool {
162 self.timestamp == clock::Lamport::MIN
163 && self.offset == usize::MIN
164 && self.bias == Bias::Left
165 }
166
167 pub fn is_max(&self) -> bool {
168 self.timestamp == clock::Lamport::MAX
169 && self.offset == usize::MAX
170 && self.bias == Bias::Right
171 }
172}
173
174pub trait OffsetRangeExt {
175 fn to_offset(&self, snapshot: &BufferSnapshot) -> Range<usize>;
176 fn to_point(&self, snapshot: &BufferSnapshot) -> Range<Point>;
177 fn to_point_utf16(&self, snapshot: &BufferSnapshot) -> Range<PointUtf16>;
178}
179
180impl<T> OffsetRangeExt for Range<T>
181where
182 T: ToOffset,
183{
184 fn to_offset(&self, snapshot: &BufferSnapshot) -> Range<usize> {
185 self.start.to_offset(snapshot)..self.end.to_offset(snapshot)
186 }
187
188 fn to_point(&self, snapshot: &BufferSnapshot) -> Range<Point> {
189 self.start.to_offset(snapshot).to_point(snapshot)
190 ..self.end.to_offset(snapshot).to_point(snapshot)
191 }
192
193 fn to_point_utf16(&self, snapshot: &BufferSnapshot) -> Range<PointUtf16> {
194 self.start.to_offset(snapshot).to_point_utf16(snapshot)
195 ..self.end.to_offset(snapshot).to_point_utf16(snapshot)
196 }
197}
198
199pub trait AnchorRangeExt {
200 fn cmp(&self, b: &Range<Anchor>, buffer: &BufferSnapshot) -> Ordering;
201 fn overlaps(&self, b: &Range<Anchor>, buffer: &BufferSnapshot) -> bool;
202}
203
204impl AnchorRangeExt for Range<Anchor> {
205 fn cmp(&self, other: &Range<Anchor>, buffer: &BufferSnapshot) -> Ordering {
206 match self.start.cmp(&other.start, buffer) {
207 Ordering::Equal => other.end.cmp(&self.end, buffer),
208 ord => ord,
209 }
210 }
211
212 fn overlaps(&self, other: &Range<Anchor>, buffer: &BufferSnapshot) -> bool {
213 self.start.cmp(&other.end, buffer).is_lt() && other.start.cmp(&self.end, buffer).is_lt()
214 }
215}