Don't use an enum for anchors and model min/max more implicitly

This will make it easier to serialize an anchor.
This commit is contained in:
Antonio Scandurra 2021-12-09 12:00:51 +01:00
parent cbe136c0cb
commit 67686dd1c2
3 changed files with 115 additions and 140 deletions

View file

@ -21,6 +21,15 @@ pub struct Lamport {
} }
impl Local { impl Local {
pub const MIN: Self = Self {
replica_id: ReplicaId::MIN,
value: Seq::MIN,
};
pub const MAX: Self = Self {
replica_id: ReplicaId::MAX,
value: Seq::MAX,
};
pub fn new(replica_id: ReplicaId) -> Self { pub fn new(replica_id: ReplicaId) -> Self {
Self { Self {
replica_id, replica_id,

View file

@ -1,88 +1,57 @@
use crate::{rope::TextDimension, Snapshot}; use super::{rope::TextDimension, Buffer, Point, Snapshot, ToOffset};
use super::{Buffer, ToOffset};
use anyhow::Result; use anyhow::Result;
use std::{cmp::Ordering, fmt::Debug, ops::Range}; use std::{cmp::Ordering, fmt::Debug, ops::Range};
use sum_tree::Bias; use sum_tree::Bias;
#[derive(Clone, Eq, PartialEq, Debug, Hash)] #[derive(Clone, Eq, PartialEq, Debug, Hash)]
pub enum Anchor { pub struct Anchor {
Min, pub timestamp: clock::Local,
Insertion { pub offset: usize,
timestamp: clock::Local, pub bias: Bias,
offset: usize,
bias: Bias,
},
Max,
} }
impl Anchor { impl Anchor {
pub fn min() -> Self { pub fn min() -> Self {
Self::Min Self {
} timestamp: clock::Local::MIN,
offset: usize::MIN,
pub fn max() -> Self { bias: Bias::Left,
Self::Max
}
pub fn cmp<'a>(&self, other: &Anchor, buffer: &Snapshot) -> Result<Ordering> {
match (self, other) {
(Self::Min, Self::Min) => Ok(Ordering::Equal),
(Self::Min, _) => Ok(Ordering::Less),
(_, Self::Min) => Ok(Ordering::Greater),
(Self::Max, Self::Max) => Ok(Ordering::Equal),
(Self::Max, _) => Ok(Ordering::Greater),
(_, Self::Max) => Ok(Ordering::Less),
(
Self::Insertion {
timestamp: lhs_id,
bias: lhs_bias,
offset: lhs_offset,
},
Self::Insertion {
timestamp: rhs_id,
bias: rhs_bias,
offset: rhs_offset,
},
) => {
let offset_comparison = if lhs_id == rhs_id {
lhs_offset.cmp(&rhs_offset)
} else {
buffer
.full_offset_for_anchor(self)
.cmp(&buffer.full_offset_for_anchor(other))
};
Ok(offset_comparison.then_with(|| lhs_bias.cmp(&rhs_bias)))
}
} }
} }
pub fn max() -> Self {
Self {
timestamp: clock::Local::MAX,
offset: usize::MAX,
bias: Bias::Right,
}
}
pub fn cmp<'a>(&self, other: &Anchor, buffer: &Snapshot) -> Result<Ordering> {
let offset_comparison = if self.timestamp == other.timestamp {
self.offset.cmp(&other.offset)
} else {
buffer
.full_offset_for_anchor(self)
.cmp(&buffer.full_offset_for_anchor(other))
};
Ok(offset_comparison.then_with(|| self.bias.cmp(&other.bias)))
}
pub fn bias_left(&self, buffer: &Buffer) -> Anchor { pub fn bias_left(&self, buffer: &Buffer) -> Anchor {
match self { if self.bias == Bias::Left {
Anchor::Min => Anchor::Min, self.clone()
Anchor::Insertion { bias, .. } => { } else {
if *bias == Bias::Left { buffer.anchor_before(self)
self.clone()
} else {
buffer.anchor_before(self)
}
}
Anchor::Max => buffer.anchor_before(self),
} }
} }
pub fn bias_right(&self, buffer: &Buffer) -> Anchor { pub fn bias_right(&self, buffer: &Buffer) -> Anchor {
match self { if self.bias == Bias::Right {
Anchor::Min => buffer.anchor_after(self), self.clone()
Anchor::Insertion { bias, .. } => { } else {
if *bias == Bias::Right { buffer.anchor_after(self)
self.clone()
} else {
buffer.anchor_after(self)
}
}
Anchor::Max => Anchor::Max,
} }
} }
@ -97,6 +66,7 @@ impl Anchor {
pub trait AnchorRangeExt { pub trait AnchorRangeExt {
fn cmp(&self, b: &Range<Anchor>, buffer: &Snapshot) -> Result<Ordering>; fn cmp(&self, b: &Range<Anchor>, buffer: &Snapshot) -> Result<Ordering>;
fn to_offset(&self, content: &Snapshot) -> Range<usize>; fn to_offset(&self, content: &Snapshot) -> Range<usize>;
fn to_point(&self, content: &Snapshot) -> Range<Point>;
} }
impl AnchorRangeExt for Range<Anchor> { impl AnchorRangeExt for Range<Anchor> {
@ -110,4 +80,8 @@ impl AnchorRangeExt for Range<Anchor> {
fn to_offset(&self, content: &Snapshot) -> Range<usize> { fn to_offset(&self, content: &Snapshot) -> Range<usize> {
self.start.to_offset(&content)..self.end.to_offset(&content) self.start.to_offset(&content)..self.end.to_offset(&content)
} }
fn to_point(&self, content: &Snapshot) -> Range<Point> {
self.start.summary::<Point>(&content)..self.end.summary::<Point>(&content)
}
} }

View file

@ -1136,11 +1136,9 @@ impl Buffer {
} }
fn can_resolve(&self, anchor: &Anchor) -> bool { fn can_resolve(&self, anchor: &Anchor) -> bool {
match anchor { *anchor == Anchor::min()
Anchor::Min => true, || *anchor == Anchor::max()
Anchor::Insertion { timestamp, .. } => self.version.observed(*timestamp), || self.version.observed(anchor.timestamp)
Anchor::Max => true,
}
} }
pub fn peek_undo_stack(&self) -> Option<&Transaction> { pub fn peek_undo_stack(&self) -> Option<&Transaction> {
@ -1680,80 +1678,74 @@ impl Snapshot {
where where
D: TextDimension<'a>, D: TextDimension<'a>,
{ {
match anchor { if *anchor == Anchor::min() {
Anchor::Min => D::default(), D::default()
Anchor::Insertion { } else if *anchor == Anchor::max() {
timestamp, D::from_text_summary(&self.visible_text.summary())
offset, } else {
bias, let anchor_key = InsertionFragmentKey {
} => { timestamp: anchor.timestamp,
let anchor_key = InsertionFragmentKey { split_offset: anchor.offset,
timestamp: *timestamp, };
split_offset: *offset, let mut insertion_cursor = self.insertions.cursor::<InsertionFragmentKey>();
}; insertion_cursor.seek(&anchor_key, anchor.bias, &());
let mut insertion_cursor = self.insertions.cursor::<InsertionFragmentKey>(); if let Some(insertion) = insertion_cursor.item() {
insertion_cursor.seek(&anchor_key, *bias, &()); let comparison = sum_tree::KeyedItem::key(insertion).cmp(&anchor_key);
if let Some(insertion) = insertion_cursor.item() { if comparison == Ordering::Greater
let comparison = sum_tree::KeyedItem::key(insertion).cmp(&anchor_key); || (anchor.bias == Bias::Left
if comparison == Ordering::Greater && comparison == Ordering::Equal
|| (*bias == Bias::Left && comparison == Ordering::Equal && *offset > 0) && anchor.offset > 0)
{ {
insertion_cursor.prev(&());
}
} else {
insertion_cursor.prev(&()); insertion_cursor.prev(&());
} }
let insertion = insertion_cursor.item().expect("invalid insertion"); } else {
debug_assert_eq!(insertion.timestamp, *timestamp, "invalid insertion"); insertion_cursor.prev(&());
let mut fragment_cursor = self.fragments.cursor::<(Locator, usize)>();
fragment_cursor.seek(&insertion.fragment_id, Bias::Left, &None);
let fragment = fragment_cursor.item().unwrap();
let mut fragment_offset = fragment_cursor.start().1;
if fragment.visible {
fragment_offset += *offset - insertion.split_offset;
}
self.text_summary_for_range(0..fragment_offset)
} }
Anchor::Max => D::from_text_summary(&self.visible_text.summary()), let insertion = insertion_cursor.item().expect("invalid insertion");
debug_assert_eq!(insertion.timestamp, anchor.timestamp, "invalid insertion");
let mut fragment_cursor = self.fragments.cursor::<(Locator, usize)>();
fragment_cursor.seek(&insertion.fragment_id, Bias::Left, &None);
let fragment = fragment_cursor.item().unwrap();
let mut fragment_offset = fragment_cursor.start().1;
if fragment.visible {
fragment_offset += anchor.offset - insertion.split_offset;
}
self.text_summary_for_range(0..fragment_offset)
} }
} }
fn full_offset_for_anchor(&self, anchor: &Anchor) -> FullOffset { fn full_offset_for_anchor(&self, anchor: &Anchor) -> FullOffset {
match anchor { if *anchor == Anchor::min() {
Anchor::Min => Default::default(), Default::default()
Anchor::Insertion { } else if *anchor == Anchor::max() {
timestamp, let text = self.fragments.summary().text;
offset, FullOffset(text.visible + text.deleted)
bias, } else {
} => { let anchor_key = InsertionFragmentKey {
let anchor_key = InsertionFragmentKey { timestamp: anchor.timestamp,
timestamp: *timestamp, split_offset: anchor.offset,
split_offset: *offset, };
}; let mut insertion_cursor = self.insertions.cursor::<InsertionFragmentKey>();
let mut insertion_cursor = self.insertions.cursor::<InsertionFragmentKey>(); insertion_cursor.seek(&anchor_key, anchor.bias, &());
insertion_cursor.seek(&anchor_key, *bias, &()); if let Some(insertion) = insertion_cursor.item() {
if let Some(insertion) = insertion_cursor.item() { let comparison = sum_tree::KeyedItem::key(insertion).cmp(&anchor_key);
let comparison = sum_tree::KeyedItem::key(insertion).cmp(&anchor_key); if comparison == Ordering::Greater
if comparison == Ordering::Greater || (anchor.bias == Bias::Left
|| (*bias == Bias::Left && comparison == Ordering::Equal && *offset > 0) && comparison == Ordering::Equal
{ && anchor.offset > 0)
insertion_cursor.prev(&()); {
}
} else {
insertion_cursor.prev(&()); insertion_cursor.prev(&());
} }
let insertion = insertion_cursor.item().expect("invalid insertion"); } else {
debug_assert_eq!(insertion.timestamp, *timestamp, "invalid insertion"); insertion_cursor.prev(&());
}
let insertion = insertion_cursor.item().expect("invalid insertion");
debug_assert_eq!(insertion.timestamp, anchor.timestamp, "invalid insertion");
let mut fragment_cursor = self.fragments.cursor::<(Locator, FullOffset)>(); let mut fragment_cursor = self.fragments.cursor::<(Locator, FullOffset)>();
fragment_cursor.seek(&insertion.fragment_id, Bias::Left, &None); fragment_cursor.seek(&insertion.fragment_id, Bias::Left, &None);
fragment_cursor.start().1 + (*offset - insertion.split_offset) fragment_cursor.start().1 + (anchor.offset - insertion.split_offset)
}
Anchor::Max => {
let text = self.fragments.summary().text;
FullOffset(text.visible + text.deleted)
}
} }
} }
@ -1777,15 +1769,15 @@ impl Snapshot {
pub fn anchor_at<T: ToOffset>(&self, position: T, bias: Bias) -> Anchor { pub fn anchor_at<T: ToOffset>(&self, position: T, bias: Bias) -> Anchor {
let offset = position.to_offset(self); let offset = position.to_offset(self);
if bias == Bias::Left && offset == 0 { if bias == Bias::Left && offset == 0 {
Anchor::Min Anchor::min()
} else if bias == Bias::Right && offset == self.len() { } else if bias == Bias::Right && offset == self.len() {
Anchor::Max Anchor::max()
} else { } else {
let mut fragment_cursor = self.fragments.cursor::<(usize, Locator)>(); let mut fragment_cursor = self.fragments.cursor::<(usize, Locator)>();
fragment_cursor.seek(&offset, bias, &None); fragment_cursor.seek(&offset, bias, &None);
let fragment = fragment_cursor.item().unwrap(); let fragment = fragment_cursor.item().unwrap();
let overshoot = offset - fragment_cursor.start().0; let overshoot = offset - fragment_cursor.start().0;
Anchor::Insertion { Anchor {
timestamp: fragment.insertion_timestamp.local(), timestamp: fragment.insertion_timestamp.local(),
offset: fragment.insertion_offset + overshoot, offset: fragment.insertion_offset + overshoot,
bias, bias,