diff --git a/crates/buffer/src/lib.rs b/crates/buffer/src/lib.rs index 4511531cf4..6deaf228a1 100644 --- a/crates/buffer/src/lib.rs +++ b/crates/buffer/src/lib.rs @@ -20,7 +20,7 @@ use rpc::proto; pub use selection::*; use std::{ cmp, - convert::{TryFrom, TryInto}, + convert::TryFrom, iter::Iterator, ops::Range, str, @@ -486,20 +486,8 @@ impl Buffer { .selections .into_iter() .map(|set| { - let set_id = clock::Lamport { - replica_id: set.replica_id as ReplicaId, - value: set.lamport_timestamp, - }; - let selections: Vec = set - .selections - .into_iter() - .map(TryFrom::try_from) - .collect::>()?; - let set = SelectionSet { - selections: Arc::from(selections), - active: set.is_active, - }; - Result::<_, anyhow::Error>::Ok((set_id, set)) + let set = SelectionSet::try_from(set)?; + Result::<_, anyhow::Error>::Ok((set.id, set)) }) .collect::>()?; Ok(buffer) @@ -859,6 +847,7 @@ impl Buffer { self.selections.insert( set_id, SelectionSet { + id: set_id, selections, active: false, }, @@ -1290,18 +1279,19 @@ impl Buffer { pub fn add_selection_set(&mut self, selections: impl Into>) -> Operation { let selections = selections.into(); - let lamport_timestamp = self.lamport_clock.tick(); + let set_id = self.lamport_clock.tick(); self.selections.insert( - lamport_timestamp, + set_id, SelectionSet { + id: set_id, selections: selections.clone(), active: false, }, ); Operation::UpdateSelections { - set_id: lamport_timestamp, + set_id, selections, - lamport_timestamp, + lamport_timestamp: set_id, } } @@ -2390,26 +2380,6 @@ impl TryFrom for Anchor { } } -impl TryFrom for Selection { - type Error = anyhow::Error; - - fn try_from(selection: proto::Selection) -> Result { - Ok(Selection { - 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, - }) - } -} - pub trait ToOffset { fn to_offset<'a>(&self, content: impl Into>) -> usize; } diff --git a/crates/buffer/src/selection.rs b/crates/buffer/src/selection.rs index df0834b67f..589746803b 100644 --- a/crates/buffer/src/selection.rs +++ b/crates/buffer/src/selection.rs @@ -1,7 +1,13 @@ -use rpc::proto; - use crate::{Anchor, Buffer, Point, ToOffset as _, ToPoint as _}; -use std::{cmp::Ordering, mem, ops::Range, sync::Arc}; +use anyhow::anyhow; +use rpc::proto; +use std::{ + cmp::Ordering, + convert::{TryFrom, TryInto}, + mem, + ops::Range, + sync::Arc, +}; pub type SelectionSetId = clock::Lamport; pub type SelectionsVersion = usize; @@ -24,8 +30,9 @@ pub struct Selection { #[derive(Clone, Debug, Eq, PartialEq)] pub struct SelectionSet { - pub selections: Arc<[Selection]>, + pub id: SelectionSetId, pub active: bool, + pub selections: Arc<[Selection]>, } impl Selection { @@ -92,3 +99,43 @@ impl<'a> Into for &'a Selection { } } } + +impl TryFrom for Selection { + type Error = anyhow::Error; + + fn try_from(selection: proto::Selection) -> Result { + Ok(Selection { + 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, + }) + } +} + +impl TryFrom for SelectionSet { + type Error = anyhow::Error; + + fn try_from(set: proto::SelectionSet) -> Result { + Ok(Self { + id: clock::Lamport { + replica_id: set.replica_id as u16, + value: set.lamport_timestamp, + }, + active: set.is_active, + selections: Arc::from( + set.selections + .into_iter() + .map(TryInto::try_into) + .collect::, _>>()?, + ), + }) + } +}