Implement anchor resolution using locators
This commit is contained in:
parent
b7535dfba4
commit
cbe136c0cb
4 changed files with 244 additions and 814 deletions
|
@ -27,7 +27,7 @@ use rope::TextDimension;
|
|||
pub use rope::{Chunks, Rope, TextSummary};
|
||||
pub use selection::*;
|
||||
use std::{
|
||||
cmp::{self, Reverse},
|
||||
cmp::{self, Ordering},
|
||||
iter::Iterator,
|
||||
ops::{self, Deref, Range, Sub},
|
||||
str,
|
||||
|
@ -67,8 +67,8 @@ pub struct Transaction {
|
|||
end: clock::Global,
|
||||
edits: Vec<clock::Local>,
|
||||
ranges: Vec<Range<FullOffset>>,
|
||||
selections_before: HashMap<SelectionSetId, Arc<AnchorRangeMap<SelectionState>>>,
|
||||
selections_after: HashMap<SelectionSetId, Arc<AnchorRangeMap<SelectionState>>>,
|
||||
selections_before: HashMap<SelectionSetId, Arc<[Selection<Anchor>]>>,
|
||||
selections_after: HashMap<SelectionSetId, Arc<[Selection<Anchor>]>>,
|
||||
first_edit_at: Instant,
|
||||
last_edit_at: Instant,
|
||||
}
|
||||
|
@ -155,7 +155,7 @@ impl History {
|
|||
fn start_transaction(
|
||||
&mut self,
|
||||
start: clock::Global,
|
||||
selections_before: HashMap<SelectionSetId, Arc<AnchorRangeMap<SelectionState>>>,
|
||||
selections_before: HashMap<SelectionSetId, Arc<[Selection<Anchor>]>>,
|
||||
now: Instant,
|
||||
) {
|
||||
self.transaction_depth += 1;
|
||||
|
@ -175,7 +175,7 @@ impl History {
|
|||
|
||||
fn end_transaction(
|
||||
&mut self,
|
||||
selections_after: HashMap<SelectionSetId, Arc<AnchorRangeMap<SelectionState>>>,
|
||||
selections_after: HashMap<SelectionSetId, Arc<[Selection<Anchor>]>>,
|
||||
now: Instant,
|
||||
) -> Option<&Transaction> {
|
||||
assert_ne!(self.transaction_depth, 0);
|
||||
|
@ -430,7 +430,7 @@ pub enum Operation {
|
|||
},
|
||||
UpdateSelections {
|
||||
set_id: SelectionSetId,
|
||||
selections: Arc<AnchorRangeMap<SelectionState>>,
|
||||
selections: Arc<[Selection<Anchor>]>,
|
||||
lamport_timestamp: clock::Lamport,
|
||||
},
|
||||
RemoveSelections {
|
||||
|
@ -1122,9 +1122,9 @@ impl Buffer {
|
|||
match op {
|
||||
Operation::Edit(edit) => self.version.ge(&edit.version),
|
||||
Operation::Undo { undo, .. } => self.version.ge(&undo.version),
|
||||
Operation::UpdateSelections { selections, .. } => {
|
||||
self.version.ge(selections.version())
|
||||
}
|
||||
Operation::UpdateSelections { selections, .. } => selections
|
||||
.iter()
|
||||
.all(|s| self.can_resolve(&s.start) && self.can_resolve(&s.end)),
|
||||
Operation::RemoveSelections { .. } => true,
|
||||
Operation::SetActiveSelections { set_id, .. } => {
|
||||
set_id.map_or(true, |set_id| self.selections.contains_key(&set_id))
|
||||
|
@ -1135,6 +1135,14 @@ impl Buffer {
|
|||
}
|
||||
}
|
||||
|
||||
fn can_resolve(&self, anchor: &Anchor) -> bool {
|
||||
match anchor {
|
||||
Anchor::Min => true,
|
||||
Anchor::Insertion { timestamp, .. } => self.version.observed(*timestamp),
|
||||
Anchor::Max => true,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn peek_undo_stack(&self) -> Option<&Transaction> {
|
||||
self.history.undo_stack.last()
|
||||
}
|
||||
|
@ -1280,25 +1288,22 @@ impl Buffer {
|
|||
self.selections.iter()
|
||||
}
|
||||
|
||||
fn build_selection_anchor_range_map<T: ToOffset>(
|
||||
fn build_anchor_selection_set<T: ToOffset>(
|
||||
&self,
|
||||
selections: &[Selection<T>],
|
||||
) -> Arc<AnchorRangeMap<SelectionState>> {
|
||||
Arc::new(self.anchor_range_map(
|
||||
Bias::Left,
|
||||
Bias::Left,
|
||||
selections.iter().map(|selection| {
|
||||
let start = selection.start.to_offset(self);
|
||||
let end = selection.end.to_offset(self);
|
||||
let range = start..end;
|
||||
let state = SelectionState {
|
||||
) -> Arc<[Selection<Anchor>]> {
|
||||
Arc::from(
|
||||
selections
|
||||
.iter()
|
||||
.map(|selection| Selection {
|
||||
id: selection.id,
|
||||
start: self.anchor_before(&selection.start),
|
||||
end: self.anchor_before(&selection.end),
|
||||
reversed: selection.reversed,
|
||||
goal: selection.goal,
|
||||
};
|
||||
(range, state)
|
||||
}),
|
||||
))
|
||||
})
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn update_selection_set<T: ToOffset>(
|
||||
|
@ -1306,7 +1311,7 @@ impl Buffer {
|
|||
set_id: SelectionSetId,
|
||||
selections: &[Selection<T>],
|
||||
) -> Result<Operation> {
|
||||
let selections = self.build_selection_anchor_range_map(selections);
|
||||
let selections = self.build_anchor_selection_set(selections);
|
||||
let set = self
|
||||
.selections
|
||||
.get_mut(&set_id)
|
||||
|
@ -1322,7 +1327,7 @@ impl Buffer {
|
|||
pub fn restore_selection_set(
|
||||
&mut self,
|
||||
set_id: SelectionSetId,
|
||||
selections: Arc<AnchorRangeMap<SelectionState>>,
|
||||
selections: Arc<[Selection<Anchor>]>,
|
||||
) -> Result<Operation> {
|
||||
let set = self
|
||||
.selections
|
||||
|
@ -1337,7 +1342,7 @@ impl Buffer {
|
|||
}
|
||||
|
||||
pub fn add_selection_set<T: ToOffset>(&mut self, selections: &[Selection<T>]) -> Operation {
|
||||
let selections = self.build_selection_anchor_range_map(selections);
|
||||
let selections = self.build_anchor_selection_set(selections);
|
||||
let set_id = self.lamport_clock.tick();
|
||||
self.selections.insert(
|
||||
set_id,
|
||||
|
@ -1675,19 +1680,81 @@ impl Snapshot {
|
|||
where
|
||||
D: TextDimension<'a>,
|
||||
{
|
||||
let cx = Some(anchor.version.clone());
|
||||
let mut cursor = self.fragments.cursor::<(VersionedFullOffset, usize)>();
|
||||
cursor.seek(
|
||||
&VersionedFullOffset::Offset(anchor.full_offset),
|
||||
anchor.bias,
|
||||
&cx,
|
||||
);
|
||||
let overshoot = if cursor.item().map_or(false, |fragment| fragment.visible) {
|
||||
anchor.full_offset - cursor.start().0.full_offset()
|
||||
} else {
|
||||
0
|
||||
};
|
||||
self.text_summary_for_range(0..cursor.start().1 + overshoot)
|
||||
match anchor {
|
||||
Anchor::Min => D::default(),
|
||||
Anchor::Insertion {
|
||||
timestamp,
|
||||
offset,
|
||||
bias,
|
||||
} => {
|
||||
let anchor_key = InsertionFragmentKey {
|
||||
timestamp: *timestamp,
|
||||
split_offset: *offset,
|
||||
};
|
||||
let mut insertion_cursor = self.insertions.cursor::<InsertionFragmentKey>();
|
||||
insertion_cursor.seek(&anchor_key, *bias, &());
|
||||
if let Some(insertion) = insertion_cursor.item() {
|
||||
let comparison = sum_tree::KeyedItem::key(insertion).cmp(&anchor_key);
|
||||
if comparison == Ordering::Greater
|
||||
|| (*bias == Bias::Left && comparison == Ordering::Equal && *offset > 0)
|
||||
{
|
||||
insertion_cursor.prev(&());
|
||||
}
|
||||
} else {
|
||||
insertion_cursor.prev(&());
|
||||
}
|
||||
let insertion = insertion_cursor.item().expect("invalid insertion");
|
||||
debug_assert_eq!(insertion.timestamp, *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 += *offset - insertion.split_offset;
|
||||
}
|
||||
self.text_summary_for_range(0..fragment_offset)
|
||||
}
|
||||
Anchor::Max => D::from_text_summary(&self.visible_text.summary()),
|
||||
}
|
||||
}
|
||||
|
||||
fn full_offset_for_anchor(&self, anchor: &Anchor) -> FullOffset {
|
||||
match anchor {
|
||||
Anchor::Min => Default::default(),
|
||||
Anchor::Insertion {
|
||||
timestamp,
|
||||
offset,
|
||||
bias,
|
||||
} => {
|
||||
let anchor_key = InsertionFragmentKey {
|
||||
timestamp: *timestamp,
|
||||
split_offset: *offset,
|
||||
};
|
||||
let mut insertion_cursor = self.insertions.cursor::<InsertionFragmentKey>();
|
||||
insertion_cursor.seek(&anchor_key, *bias, &());
|
||||
if let Some(insertion) = insertion_cursor.item() {
|
||||
let comparison = sum_tree::KeyedItem::key(insertion).cmp(&anchor_key);
|
||||
if comparison == Ordering::Greater
|
||||
|| (*bias == Bias::Left && comparison == Ordering::Equal && *offset > 0)
|
||||
{
|
||||
insertion_cursor.prev(&());
|
||||
}
|
||||
} else {
|
||||
insertion_cursor.prev(&());
|
||||
}
|
||||
let insertion = insertion_cursor.item().expect("invalid insertion");
|
||||
debug_assert_eq!(insertion.timestamp, *timestamp, "invalid insertion");
|
||||
|
||||
let mut fragment_cursor = self.fragments.cursor::<(Locator, FullOffset)>();
|
||||
fragment_cursor.seek(&insertion.fragment_id, Bias::Left, &None);
|
||||
fragment_cursor.start().1 + (*offset - insertion.split_offset)
|
||||
}
|
||||
Anchor::Max => {
|
||||
let text = self.fragments.summary().text;
|
||||
FullOffset(text.visible + text.deleted)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn text_summary_for_range<'a, D, O: ToOffset>(&'a self, range: Range<O>) -> D
|
||||
|
@ -1699,70 +1766,6 @@ impl Snapshot {
|
|||
.summary(range.end.to_offset(self))
|
||||
}
|
||||
|
||||
fn summaries_for_anchors<'a, D, I>(
|
||||
&'a self,
|
||||
version: clock::Global,
|
||||
bias: Bias,
|
||||
ranges: I,
|
||||
) -> impl 'a + Iterator<Item = D>
|
||||
where
|
||||
D: 'a + TextDimension<'a>,
|
||||
I: 'a + IntoIterator<Item = &'a FullOffset>,
|
||||
{
|
||||
let cx = Some(version.clone());
|
||||
let mut summary = D::default();
|
||||
let mut rope_cursor = self.visible_text.cursor(0);
|
||||
let mut cursor = self.fragments.cursor::<(VersionedFullOffset, usize)>();
|
||||
ranges.into_iter().map(move |offset| {
|
||||
cursor.seek_forward(&VersionedFullOffset::Offset(*offset), bias, &cx);
|
||||
let overshoot = if cursor.item().map_or(false, |fragment| fragment.visible) {
|
||||
*offset - cursor.start().0.full_offset()
|
||||
} else {
|
||||
0
|
||||
};
|
||||
summary.add_assign(&rope_cursor.summary(cursor.start().1 + overshoot));
|
||||
summary.clone()
|
||||
})
|
||||
}
|
||||
|
||||
fn summaries_for_anchor_ranges<'a, D, I>(
|
||||
&'a self,
|
||||
version: clock::Global,
|
||||
start_bias: Bias,
|
||||
end_bias: Bias,
|
||||
ranges: I,
|
||||
) -> impl 'a + Iterator<Item = Range<D>>
|
||||
where
|
||||
D: 'a + TextDimension<'a>,
|
||||
I: 'a + IntoIterator<Item = &'a Range<FullOffset>>,
|
||||
{
|
||||
let cx = Some(version);
|
||||
let mut summary = D::default();
|
||||
let mut rope_cursor = self.visible_text.cursor(0);
|
||||
let mut cursor = self.fragments.cursor::<(VersionedFullOffset, usize)>();
|
||||
ranges.into_iter().map(move |range| {
|
||||
cursor.seek_forward(&VersionedFullOffset::Offset(range.start), start_bias, &cx);
|
||||
let overshoot = if cursor.item().map_or(false, |fragment| fragment.visible) {
|
||||
range.start - cursor.start().0.full_offset()
|
||||
} else {
|
||||
0
|
||||
};
|
||||
summary.add_assign(&rope_cursor.summary::<D>(cursor.start().1 + overshoot));
|
||||
let start_summary = summary.clone();
|
||||
|
||||
cursor.seek_forward(&VersionedFullOffset::Offset(range.end), end_bias, &cx);
|
||||
let overshoot = if cursor.item().map_or(false, |fragment| fragment.visible) {
|
||||
range.end - cursor.start().0.full_offset()
|
||||
} else {
|
||||
0
|
||||
};
|
||||
summary.add_assign(&rope_cursor.summary::<D>(cursor.start().1 + overshoot));
|
||||
let end_summary = summary.clone();
|
||||
|
||||
start_summary..end_summary
|
||||
})
|
||||
}
|
||||
|
||||
pub fn anchor_before<T: ToOffset>(&self, position: T) -> Anchor {
|
||||
self.anchor_at(position, Bias::Left)
|
||||
}
|
||||
|
@ -1772,139 +1775,22 @@ impl Snapshot {
|
|||
}
|
||||
|
||||
pub fn anchor_at<T: ToOffset>(&self, position: T, bias: Bias) -> Anchor {
|
||||
Anchor {
|
||||
full_offset: position.to_full_offset(self, bias),
|
||||
bias,
|
||||
version: self.version.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn anchor_map<T, E>(&self, bias: Bias, entries: E) -> AnchorMap<T>
|
||||
where
|
||||
E: IntoIterator<Item = (usize, T)>,
|
||||
{
|
||||
let version = self.version.clone();
|
||||
let mut cursor = self.fragments.cursor::<FragmentTextSummary>();
|
||||
let entries = entries
|
||||
.into_iter()
|
||||
.map(|(offset, value)| {
|
||||
cursor.seek_forward(&offset, bias, &None);
|
||||
let full_offset = FullOffset(cursor.start().deleted + offset);
|
||||
(full_offset, value)
|
||||
})
|
||||
.collect();
|
||||
|
||||
AnchorMap {
|
||||
version,
|
||||
bias,
|
||||
entries,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn anchor_range_map<T, E>(
|
||||
&self,
|
||||
start_bias: Bias,
|
||||
end_bias: Bias,
|
||||
entries: E,
|
||||
) -> AnchorRangeMap<T>
|
||||
where
|
||||
E: IntoIterator<Item = (Range<usize>, T)>,
|
||||
{
|
||||
let version = self.version.clone();
|
||||
let mut cursor = self.fragments.cursor::<FragmentTextSummary>();
|
||||
let entries = entries
|
||||
.into_iter()
|
||||
.map(|(range, value)| {
|
||||
let Range {
|
||||
start: start_offset,
|
||||
end: end_offset,
|
||||
} = range;
|
||||
cursor.seek_forward(&start_offset, start_bias, &None);
|
||||
let full_start_offset = FullOffset(cursor.start().deleted + start_offset);
|
||||
cursor.seek_forward(&end_offset, end_bias, &None);
|
||||
let full_end_offset = FullOffset(cursor.start().deleted + end_offset);
|
||||
(full_start_offset..full_end_offset, value)
|
||||
})
|
||||
.collect();
|
||||
|
||||
AnchorRangeMap {
|
||||
version,
|
||||
start_bias,
|
||||
end_bias,
|
||||
entries,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn anchor_set<E>(&self, bias: Bias, entries: E) -> AnchorSet
|
||||
where
|
||||
E: IntoIterator<Item = usize>,
|
||||
{
|
||||
AnchorSet(self.anchor_map(bias, entries.into_iter().map(|range| (range, ()))))
|
||||
}
|
||||
|
||||
pub fn anchor_range_set<E>(
|
||||
&self,
|
||||
start_bias: Bias,
|
||||
end_bias: Bias,
|
||||
entries: E,
|
||||
) -> AnchorRangeSet
|
||||
where
|
||||
E: IntoIterator<Item = Range<usize>>,
|
||||
{
|
||||
AnchorRangeSet(self.anchor_range_map(
|
||||
start_bias,
|
||||
end_bias,
|
||||
entries.into_iter().map(|range| (range, ())),
|
||||
))
|
||||
}
|
||||
|
||||
pub fn anchor_range_multimap<T, E, O>(
|
||||
&self,
|
||||
start_bias: Bias,
|
||||
end_bias: Bias,
|
||||
entries: E,
|
||||
) -> AnchorRangeMultimap<T>
|
||||
where
|
||||
T: Clone,
|
||||
E: IntoIterator<Item = (Range<O>, T)>,
|
||||
O: ToOffset,
|
||||
{
|
||||
let mut entries = entries
|
||||
.into_iter()
|
||||
.map(|(range, value)| AnchorRangeMultimapEntry {
|
||||
range: FullOffsetRange {
|
||||
start: range.start.to_full_offset(self, start_bias),
|
||||
end: range.end.to_full_offset(self, end_bias),
|
||||
},
|
||||
value,
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
entries.sort_unstable_by_key(|i| (i.range.start, Reverse(i.range.end)));
|
||||
AnchorRangeMultimap {
|
||||
entries: SumTree::from_iter(entries, &()),
|
||||
version: self.version.clone(),
|
||||
start_bias,
|
||||
end_bias,
|
||||
}
|
||||
}
|
||||
|
||||
fn full_offset_for_anchor(&self, anchor: &Anchor) -> FullOffset {
|
||||
let cx = Some(anchor.version.clone());
|
||||
let mut cursor = self
|
||||
.fragments
|
||||
.cursor::<(VersionedFullOffset, FragmentTextSummary)>();
|
||||
cursor.seek(
|
||||
&VersionedFullOffset::Offset(anchor.full_offset),
|
||||
anchor.bias,
|
||||
&cx,
|
||||
);
|
||||
let overshoot = if cursor.item().is_some() {
|
||||
anchor.full_offset - cursor.start().0.full_offset()
|
||||
let offset = position.to_offset(self);
|
||||
if bias == Bias::Left && offset == 0 {
|
||||
Anchor::Min
|
||||
} else if bias == Bias::Right && offset == self.len() {
|
||||
Anchor::Max
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let summary = cursor.start().1;
|
||||
FullOffset(summary.visible + summary.deleted + overshoot)
|
||||
let mut fragment_cursor = self.fragments.cursor::<(usize, Locator)>();
|
||||
fragment_cursor.seek(&offset, bias, &None);
|
||||
let fragment = fragment_cursor.item().unwrap();
|
||||
let overshoot = offset - fragment_cursor.start().0;
|
||||
Anchor::Insertion {
|
||||
timestamp: fragment.insertion_timestamp.local(),
|
||||
offset: fragment.insertion_offset + overshoot,
|
||||
bias,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clip_offset(&self, offset: usize, bias: Bias) -> usize {
|
||||
|
@ -2200,10 +2086,6 @@ impl sum_tree::Summary for InsertionFragmentKey {
|
|||
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct FullOffset(pub usize);
|
||||
|
||||
impl FullOffset {
|
||||
const MAX: Self = FullOffset(usize::MAX);
|
||||
}
|
||||
|
||||
impl ops::AddAssign<usize> for FullOffset {
|
||||
fn add_assign(&mut self, rhs: usize) {
|
||||
self.0 += rhs;
|
||||
|
@ -2239,6 +2121,12 @@ impl<'a> sum_tree::Dimension<'a, FragmentSummary> for FullOffset {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> sum_tree::Dimension<'a, FragmentSummary> for Locator {
|
||||
fn add_summary(&mut self, summary: &FragmentSummary, _: &Option<clock::Global>) {
|
||||
*self = summary.max_id.clone();
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> sum_tree::SeekTarget<'a, FragmentSummary, FragmentTextSummary> for usize {
|
||||
fn cmp(
|
||||
&self,
|
||||
|
@ -2363,9 +2251,9 @@ impl ToOffset for Anchor {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> ToOffset for &'a Anchor {
|
||||
impl<'a, T: ToOffset> ToOffset for &'a T {
|
||||
fn to_offset(&self, content: &Snapshot) -> usize {
|
||||
content.summary_for_anchor(self)
|
||||
(*self).to_offset(content)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue