mirror of
https://github.com/zed-industries/zed.git
synced 2024-11-09 21:26:14 +03:00
Implement TryFrom<proto::SelectionSet> on SelectionSet
More prep work for changing the selection set representation.
This commit is contained in:
parent
401bdf0ba1
commit
3ae5ba09fd
@ -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<Selection> = set
|
||||
.selections
|
||||
.into_iter()
|
||||
.map(TryFrom::try_from)
|
||||
.collect::<Result<_, _>>()?;
|
||||
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::<Result<_, _>>()?;
|
||||
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<Arc<[Selection]>>) -> 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<proto::Anchor> for Anchor {
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<proto::Selection> for Selection {
|
||||
type Error = anyhow::Error;
|
||||
|
||||
fn try_from(selection: proto::Selection) -> Result<Self, Self::Error> {
|
||||
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<Content<'a>>) -> usize;
|
||||
}
|
||||
|
@ -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<proto::Selection> for &'a Selection {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<proto::Selection> for Selection {
|
||||
type Error = anyhow::Error;
|
||||
|
||||
fn try_from(selection: proto::Selection) -> Result<Self, Self::Error> {
|
||||
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<proto::SelectionSet> for SelectionSet {
|
||||
type Error = anyhow::Error;
|
||||
|
||||
fn try_from(set: proto::SelectionSet) -> Result<Self, Self::Error> {
|
||||
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::<Result<Vec<Selection>, _>>()?,
|
||||
),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user