WIP: Start converting SelectionSet to use AnchorRangeMap

This commit is contained in:
Nathan Sobo 2021-10-22 13:19:19 -06:00
parent 3ae5ba09fd
commit 6ba4af3e26
5 changed files with 97 additions and 67 deletions

View file

@ -2,7 +2,11 @@ use crate::Point;
use super::{Buffer, Content}; use super::{Buffer, Content};
use anyhow::Result; use anyhow::Result;
use std::{cmp::Ordering, ops::Range}; use std::{
cmp::Ordering,
fmt::{Debug, Formatter},
ops::Range,
};
use sum_tree::Bias; use sum_tree::Bias;
#[derive(Clone, Eq, PartialEq, Debug, Hash)] #[derive(Clone, Eq, PartialEq, Debug, Hash)]
@ -108,6 +112,14 @@ impl AnchorSet {
} }
impl<T> AnchorRangeMap<T> { impl<T> AnchorRangeMap<T> {
pub fn from_raw(version: clock::Global, entries: Vec<(Range<(usize, Bias)>, T)>) -> Self {
Self { version, entries }
}
pub fn raw_entries(&self) -> &[(Range<(usize, Bias)>, T)] {
&self.entries
}
pub fn to_point_ranges<'a>( pub fn to_point_ranges<'a>(
&'a self, &'a self,
content: impl Into<Content<'a>> + 'a, content: impl Into<Content<'a>> + 'a,
@ -123,6 +135,25 @@ impl<T> AnchorRangeMap<T> {
} }
} }
impl<T: PartialEq> PartialEq for AnchorRangeMap<T> {
fn eq(&self, other: &Self) -> bool {
self.version == other.version && self.entries == other.entries
}
}
impl<T: Eq> Eq for AnchorRangeMap<T> {}
impl<T: Debug> Debug for AnchorRangeMap<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
let f = f.debug_map();
for (range, value) in &self.entries {
f.key(range);
f.value(value);
}
f.finish()
}
}
impl AnchorRangeSet { impl AnchorRangeSet {
pub fn to_point_ranges<'a>( pub fn to_point_ranges<'a>(
&'a self, &'a self,

View file

@ -408,7 +408,7 @@ pub enum Operation {
}, },
UpdateSelections { UpdateSelections {
set_id: SelectionSetId, set_id: SelectionSetId,
selections: Arc<[Selection]>, selections: Arc<AnchorRangeMap<SelectionState>>,
lamport_timestamp: clock::Lamport, lamport_timestamp: clock::Lamport,
}, },
RemoveSelections { RemoveSelections {
@ -502,12 +502,7 @@ impl Buffer {
selections: self selections: self
.selections .selections
.iter() .iter()
.map(|(set_id, set)| proto::SelectionSet { .map(|(set_id, set)| set.into())
replica_id: set_id.replica_id as u32,
lamport_timestamp: set_id.value,
selections: set.selections.iter().map(Into::into).collect(),
is_active: set.active,
})
.collect(), .collect(),
} }
} }
@ -1123,11 +1118,7 @@ impl Buffer {
Operation::Edit(edit) => self.version >= edit.version, Operation::Edit(edit) => self.version >= edit.version,
Operation::Undo { undo, .. } => self.version >= undo.version, Operation::Undo { undo, .. } => self.version >= undo.version,
Operation::UpdateSelections { selections, .. } => { Operation::UpdateSelections { selections, .. } => {
selections.iter().all(|selection| { self.version >= *selections.version()
let contains_start = self.version >= selection.start.version;
let contains_end = self.version >= selection.end.version;
contains_start && contains_end
})
} }
Operation::RemoveSelections { .. } => true, Operation::RemoveSelections { .. } => true,
Operation::SetActiveSelections { set_id, .. } => { Operation::SetActiveSelections { set_id, .. } => {
@ -1262,14 +1253,14 @@ impl Buffer {
pub fn update_selection_set( pub fn update_selection_set(
&mut self, &mut self,
set_id: SelectionSetId, set_id: SelectionSetId,
selections: impl Into<Arc<[Selection]>>, selections: &[Selection],
) -> Result<Operation> { ) -> Result<Operation> {
let selections = selections.into(); let selections = selections.into();
let set = self let set = self
.selections .selections
.get_mut(&set_id) .get_mut(&set_id)
.ok_or_else(|| anyhow!("invalid selection set id {:?}", set_id))?; .ok_or_else(|| anyhow!("invalid selection set id {:?}", set_id))?;
set.selections = selections.clone(); set.selections = todo!();
Ok(Operation::UpdateSelections { Ok(Operation::UpdateSelections {
set_id, set_id,
selections, selections,

View file

@ -1,13 +1,7 @@
use crate::{Anchor, Buffer, Point, ToOffset as _, ToPoint as _}; use crate::{Anchor, AnchorRangeMap, Buffer, Point, ToOffset as _, ToPoint as _};
use anyhow::anyhow;
use rpc::proto; use rpc::proto;
use std::{ use std::{cmp::Ordering, mem, ops::Range, sync::Arc};
cmp::Ordering, use sum_tree::Bias;
convert::{TryFrom, TryInto},
mem,
ops::Range,
sync::Arc,
};
pub type SelectionSetId = clock::Lamport; pub type SelectionSetId = clock::Lamport;
pub type SelectionsVersion = usize; pub type SelectionsVersion = usize;
@ -32,7 +26,14 @@ pub struct Selection {
pub struct SelectionSet { pub struct SelectionSet {
pub id: SelectionSetId, pub id: SelectionSetId,
pub active: bool, pub active: bool,
pub selections: Arc<[Selection]>, pub selections: Arc<AnchorRangeMap<SelectionState>>,
}
#[derive(Debug, Eq, PartialEq)]
pub struct SelectionState {
pub id: usize,
pub reversed: bool,
pub goal: SelectionGoal,
} }
impl Selection { impl Selection {
@ -89,53 +90,52 @@ impl Selection {
} }
} }
impl<'a> Into<proto::Selection> for &'a Selection { impl<'a> Into<proto::SelectionSet> for &'a SelectionSet {
fn into(self) -> proto::Selection { fn into(self) -> proto::SelectionSet {
proto::Selection { let version = self.selections.version();
id: self.id as u64, let entries = self.selections.raw_entries();
start: Some((&self.start).into()), proto::SelectionSet {
end: Some((&self.end).into()), replica_id: self.id.replica_id as u32,
reversed: self.reversed, lamport_timestamp: self.id.value as u32,
} is_active: self.active,
} version: version.into(),
} selections: entries
.iter()
impl TryFrom<proto::Selection> for Selection { .map(|(range, state)| proto::Selection {
type Error = anyhow::Error; id: state.id as u64,
start: range.start.0 as u64,
fn try_from(selection: proto::Selection) -> Result<Self, Self::Error> { end: range.end.0 as u64,
Ok(Selection { reversed: state.reversed,
id: selection.id as usize,
start: selection
.start
.ok_or_else(|| anyhow!("missing selection start"))?
.try_into()?,
end: selection
.end
.ok_or_else(|| anyhow!("missing selection end"))?
.try_into()?,
reversed: selection.reversed,
goal: SelectionGoal::None,
}) })
.collect(),
}
} }
} }
impl TryFrom<proto::SelectionSet> for SelectionSet { impl From<proto::SelectionSet> for SelectionSet {
type Error = anyhow::Error; fn from(set: proto::SelectionSet) -> Self {
Self {
fn try_from(set: proto::SelectionSet) -> Result<Self, Self::Error> {
Ok(Self {
id: clock::Lamport { id: clock::Lamport {
replica_id: set.replica_id as u16, replica_id: set.replica_id as u16,
value: set.lamport_timestamp, value: set.lamport_timestamp,
}, },
active: set.is_active, active: set.is_active,
selections: Arc::from( selections: Arc::new(AnchorRangeMap::from_raw(
set.version.into(),
set.selections set.selections
.into_iter() .into_iter()
.map(TryInto::try_into) .map(|selection| {
.collect::<Result<Vec<Selection>, _>>()?, let range = (selection.start as usize, Bias::Left)
), ..(selection.end as usize, Bias::Right);
let state = SelectionState {
id: selection.id as usize,
reversed: selection.reversed,
goal: SelectionGoal::None,
};
(range, state)
}) })
.collect(),
)),
}
} }
} }

View file

@ -86,6 +86,12 @@ impl<'a> From<&'a Global> for Vec<rpc::proto::VectorClockEntry> {
} }
} }
impl From<Global> for Vec<rpc::proto::VectorClockEntry> {
fn from(version: Global) -> Self {
(&version).into()
}
}
impl Global { impl Global {
pub fn new() -> Self { pub fn new() -> Self {
Self::default() Self::default()

View file

@ -233,14 +233,15 @@ message Buffer {
message SelectionSet { message SelectionSet {
uint32 replica_id = 1; uint32 replica_id = 1;
uint32 lamport_timestamp = 2; uint32 lamport_timestamp = 2;
repeated Selection selections = 3; bool is_active = 3;
bool is_active = 4; repeated VectorClockEntry version = 4;
repeated Selection selections = 5;
} }
message Selection { message Selection {
uint64 id = 1; uint64 id = 1;
Anchor start = 2; uint64 start = 2;
Anchor end = 3; uint64 end = 3;
bool reversed = 4; bool reversed = 4;
} }
@ -291,7 +292,8 @@ message Operation {
uint32 replica_id = 1; uint32 replica_id = 1;
uint32 local_timestamp = 2; uint32 local_timestamp = 2;
uint32 lamport_timestamp = 3; uint32 lamport_timestamp = 3;
repeated Selection selections = 4; repeated VectorClockEntry version = 4;
repeated Selection selections = 5;
} }
message RemoveSelections { message RemoveSelections {