2023-04-05 15:48:39 +00:00
|
|
|
use crate::{
|
|
|
|
locator::Locator, BufferSnapshot, Point, PointUtf16, TextDimension, ToOffset, ToPoint,
|
|
|
|
ToPointUtf16,
|
|
|
|
};
|
2021-03-10 04:00:51 +00:00
|
|
|
use anyhow::Result;
|
2021-12-09 10:18:01 +00:00
|
|
|
use std::{cmp::Ordering, fmt::Debug, ops::Range};
|
|
|
|
use sum_tree::Bias;
|
2021-03-10 04:00:51 +00:00
|
|
|
|
2022-09-09 19:40:33 +00:00
|
|
|
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash, Default)]
|
2021-06-26 00:02:48 +00:00
|
|
|
pub struct Anchor {
|
2023-08-31 22:52:16 +00:00
|
|
|
pub timestamp: clock::Lamport,
|
2021-12-09 11:00:51 +00:00
|
|
|
pub offset: usize,
|
2021-06-26 00:02:48 +00:00
|
|
|
pub bias: Bias,
|
2022-04-27 14:11:42 +00:00
|
|
|
pub buffer_id: Option<u64>,
|
2021-10-26 01:05:40 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
impl Anchor {
|
2022-03-24 17:35:30 +00:00
|
|
|
pub const MIN: Self = Self {
|
2023-08-31 22:52:16 +00:00
|
|
|
timestamp: clock::Lamport::MIN,
|
2022-03-24 17:35:30 +00:00
|
|
|
offset: usize::MIN,
|
|
|
|
bias: Bias::Left,
|
2022-04-27 14:11:42 +00:00
|
|
|
buffer_id: None,
|
2022-03-24 17:35:30 +00:00
|
|
|
};
|
2021-06-26 00:02:48 +00:00
|
|
|
|
2022-03-24 17:35:30 +00:00
|
|
|
pub const MAX: Self = Self {
|
2023-08-31 22:52:16 +00:00
|
|
|
timestamp: clock::Lamport::MAX,
|
2022-03-24 17:35:30 +00:00
|
|
|
offset: usize::MAX,
|
|
|
|
bias: Bias::Right,
|
2022-04-27 14:11:42 +00:00
|
|
|
buffer_id: None,
|
2022-03-24 17:35:30 +00:00
|
|
|
};
|
2022-09-30 19:50:55 +00:00
|
|
|
|
2022-03-24 18:48:31 +00:00
|
|
|
pub fn cmp(&self, other: &Anchor, buffer: &BufferSnapshot) -> Ordering {
|
2021-12-10 08:16:58 +00:00
|
|
|
let fragment_id_comparison = if self.timestamp == other.timestamp {
|
|
|
|
Ordering::Equal
|
2021-06-26 00:02:48 +00:00
|
|
|
} else {
|
|
|
|
buffer
|
2021-12-10 08:16:58 +00:00
|
|
|
.fragment_id_for_anchor(self)
|
2022-08-10 21:39:24 +00:00
|
|
|
.cmp(buffer.fragment_id_for_anchor(other))
|
2021-06-26 00:02:48 +00:00
|
|
|
};
|
2021-06-02 10:24:00 +00:00
|
|
|
|
2022-03-24 18:48:31 +00:00
|
|
|
fragment_id_comparison
|
2021-12-10 08:16:58 +00:00
|
|
|
.then_with(|| self.offset.cmp(&other.offset))
|
2022-03-24 18:48:31 +00:00
|
|
|
.then_with(|| self.bias.cmp(&other.bias))
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
2022-03-24 17:22:47 +00:00
|
|
|
|
|
|
|
pub fn min(&self, other: &Self, buffer: &BufferSnapshot) -> Self {
|
2022-03-24 18:48:31 +00:00
|
|
|
if self.cmp(other, buffer).is_le() {
|
2022-08-10 21:39:24 +00:00
|
|
|
*self
|
2022-03-24 17:22:47 +00:00
|
|
|
} else {
|
2022-08-10 21:39:24 +00:00
|
|
|
*other
|
2022-03-24 17:22:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn max(&self, other: &Self, buffer: &BufferSnapshot) -> Self {
|
2022-03-24 18:48:31 +00:00
|
|
|
if self.cmp(other, buffer).is_ge() {
|
2022-08-10 21:39:24 +00:00
|
|
|
*self
|
2022-03-24 17:22:47 +00:00
|
|
|
} else {
|
2022-08-10 21:39:24 +00:00
|
|
|
*other
|
2022-03-24 17:22:47 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-14 17:02:44 +00:00
|
|
|
|
2022-01-29 09:10:53 +00:00
|
|
|
pub fn bias(&self, bias: Bias, buffer: &BufferSnapshot) -> Anchor {
|
|
|
|
if bias == Bias::Left {
|
|
|
|
self.bias_left(buffer)
|
|
|
|
} else {
|
|
|
|
self.bias_right(buffer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 20:56:09 +00:00
|
|
|
pub fn bias_left(&self, buffer: &BufferSnapshot) -> Anchor {
|
2021-06-26 00:02:48 +00:00
|
|
|
if self.bias == Bias::Left {
|
2022-08-10 21:39:24 +00:00
|
|
|
*self
|
2021-06-26 00:02:48 +00:00
|
|
|
} else {
|
|
|
|
buffer.anchor_before(self)
|
2021-04-14 17:02:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 20:56:09 +00:00
|
|
|
pub fn bias_right(&self, buffer: &BufferSnapshot) -> Anchor {
|
2021-06-26 00:02:48 +00:00
|
|
|
if self.bias == Bias::Right {
|
2022-08-10 21:39:24 +00:00
|
|
|
*self
|
2021-06-26 00:02:48 +00:00
|
|
|
} else {
|
|
|
|
buffer.anchor_after(self)
|
2021-04-14 17:02:44 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-03 18:07:06 +00:00
|
|
|
|
2022-08-10 21:39:24 +00:00
|
|
|
pub fn summary<D>(&self, content: &BufferSnapshot) -> D
|
2021-11-03 18:07:06 +00:00
|
|
|
where
|
2021-12-07 15:52:15 +00:00
|
|
|
D: TextDimension,
|
2021-11-03 18:07:06 +00:00
|
|
|
{
|
2021-11-30 21:29:04 +00:00
|
|
|
content.summary_for_anchor(self)
|
2021-11-03 18:07:06 +00:00
|
|
|
}
|
2023-04-05 15:48:39 +00:00
|
|
|
|
|
|
|
/// Returns true when the [Anchor] is located inside a visible fragment.
|
|
|
|
pub fn is_valid(&self, buffer: &BufferSnapshot) -> bool {
|
|
|
|
if *self == Anchor::MIN || *self == Anchor::MAX {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
let fragment_id = buffer.fragment_id_for_anchor(self);
|
|
|
|
let mut fragment_cursor = buffer.fragments.cursor::<(Option<&Locator>, usize)>();
|
|
|
|
fragment_cursor.seek(&Some(fragment_id), Bias::Left, &None);
|
|
|
|
fragment_cursor
|
|
|
|
.item()
|
|
|
|
.map_or(false, |fragment| fragment.visible)
|
|
|
|
}
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
2022-03-08 18:00:54 +00:00
|
|
|
pub trait OffsetRangeExt {
|
|
|
|
fn to_offset(&self, snapshot: &BufferSnapshot) -> Range<usize>;
|
|
|
|
fn to_point(&self, snapshot: &BufferSnapshot) -> Range<Point>;
|
|
|
|
fn to_point_utf16(&self, snapshot: &BufferSnapshot) -> Range<PointUtf16>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> OffsetRangeExt for Range<T>
|
|
|
|
where
|
|
|
|
T: ToOffset,
|
|
|
|
{
|
|
|
|
fn to_offset(&self, snapshot: &BufferSnapshot) -> Range<usize> {
|
2022-08-10 21:39:24 +00:00
|
|
|
self.start.to_offset(snapshot)..self.end.to_offset(snapshot)
|
2022-03-08 18:00:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn to_point(&self, snapshot: &BufferSnapshot) -> Range<Point> {
|
|
|
|
self.start.to_offset(snapshot).to_point(snapshot)
|
|
|
|
..self.end.to_offset(snapshot).to_point(snapshot)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_point_utf16(&self, snapshot: &BufferSnapshot) -> Range<PointUtf16> {
|
|
|
|
self.start.to_offset(snapshot).to_point_utf16(snapshot)
|
|
|
|
..self.end.to_offset(snapshot).to_point_utf16(snapshot)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
pub trait AnchorRangeExt {
|
2021-12-08 17:24:00 +00:00
|
|
|
fn cmp(&self, b: &Range<Anchor>, buffer: &BufferSnapshot) -> Result<Ordering>;
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AnchorRangeExt for Range<Anchor> {
|
2021-12-08 17:24:00 +00:00
|
|
|
fn cmp(&self, other: &Range<Anchor>, buffer: &BufferSnapshot) -> Result<Ordering> {
|
2022-03-24 18:48:31 +00:00
|
|
|
Ok(match self.start.cmp(&other.start, buffer) {
|
|
|
|
Ordering::Equal => other.end.cmp(&self.end, buffer),
|
2022-08-10 21:39:24 +00:00
|
|
|
ord => ord,
|
2021-03-10 04:00:51 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|