2021-10-22 20:12:16 +00:00
|
|
|
use crate::{AnchorRangeMap, Buffer, Content, Point, ToOffset, ToPoint};
|
2021-10-22 18:46:02 +00:00
|
|
|
use rpc::proto;
|
2021-10-22 20:12:16 +00:00
|
|
|
use std::{cmp::Ordering, ops::Range, sync::Arc};
|
2021-10-22 19:19:19 +00:00
|
|
|
use sum_tree::Bias;
|
2021-04-09 16:45:13 +00:00
|
|
|
|
2021-10-04 12:34:02 +00:00
|
|
|
pub type SelectionSetId = clock::Lamport;
|
2021-04-09 16:45:13 +00:00
|
|
|
pub type SelectionsVersion = usize;
|
|
|
|
|
2021-05-10 13:06:07 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
|
|
|
pub enum SelectionGoal {
|
|
|
|
None,
|
|
|
|
Column(u32),
|
|
|
|
ColumnRange { start: u32, end: u32 },
|
|
|
|
}
|
|
|
|
|
2021-04-09 16:45:13 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
2021-10-22 20:12:16 +00:00
|
|
|
pub struct Selection<T> {
|
2021-05-10 14:10:41 +00:00
|
|
|
pub id: usize,
|
2021-10-22 20:12:16 +00:00
|
|
|
pub start: T,
|
|
|
|
pub end: T,
|
2021-04-09 16:45:13 +00:00
|
|
|
pub reversed: bool,
|
2021-05-10 13:06:07 +00:00
|
|
|
pub goal: SelectionGoal,
|
2021-04-09 16:45:13 +00:00
|
|
|
}
|
|
|
|
|
2021-10-22 07:56:47 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
pub struct SelectionSet {
|
2021-10-22 18:46:02 +00:00
|
|
|
pub id: SelectionSetId,
|
2021-10-22 07:56:47 +00:00
|
|
|
pub active: bool,
|
2021-10-22 19:19:19 +00:00
|
|
|
pub selections: Arc<AnchorRangeMap<SelectionState>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
|
|
|
pub struct SelectionState {
|
|
|
|
pub id: usize,
|
|
|
|
pub reversed: bool,
|
|
|
|
pub goal: SelectionGoal,
|
2021-10-22 07:56:47 +00:00
|
|
|
}
|
|
|
|
|
2021-10-22 20:12:16 +00:00
|
|
|
impl<T: ToOffset + ToPoint + Copy + Ord> Selection<T> {
|
|
|
|
pub fn head(&self) -> T {
|
2021-04-09 16:45:13 +00:00
|
|
|
if self.reversed {
|
2021-10-22 20:12:16 +00:00
|
|
|
self.start
|
2021-04-09 16:45:13 +00:00
|
|
|
} else {
|
2021-10-22 20:12:16 +00:00
|
|
|
self.end
|
2021-04-09 16:45:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-22 20:12:16 +00:00
|
|
|
pub fn set_head(&mut self, head: T) {
|
|
|
|
if head.cmp(&self.tail()) < Ordering::Equal {
|
2021-04-09 16:45:13 +00:00
|
|
|
if !self.reversed {
|
2021-10-22 20:12:16 +00:00
|
|
|
self.end = self.start;
|
2021-04-09 16:45:13 +00:00
|
|
|
self.reversed = true;
|
|
|
|
}
|
2021-10-22 20:12:16 +00:00
|
|
|
self.start = head;
|
2021-04-09 16:45:13 +00:00
|
|
|
} else {
|
|
|
|
if self.reversed {
|
2021-10-22 20:12:16 +00:00
|
|
|
self.start = self.end;
|
2021-04-09 16:45:13 +00:00
|
|
|
self.reversed = false;
|
|
|
|
}
|
2021-10-22 20:12:16 +00:00
|
|
|
self.end = head;
|
2021-04-09 16:45:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-22 20:12:16 +00:00
|
|
|
pub fn tail(&self) -> T {
|
2021-04-09 16:45:13 +00:00
|
|
|
if self.reversed {
|
2021-10-22 20:12:16 +00:00
|
|
|
self.end
|
2021-04-09 16:45:13 +00:00
|
|
|
} else {
|
2021-10-22 20:12:16 +00:00
|
|
|
self.start
|
2021-04-09 16:45:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-28 18:07:30 +00:00
|
|
|
pub fn point_range(&self, buffer: &Buffer) -> Range<Point> {
|
2021-05-17 20:21:49 +00:00
|
|
|
let start = self.start.to_point(buffer);
|
|
|
|
let end = self.end.to_point(buffer);
|
2021-04-09 16:45:13 +00:00
|
|
|
if self.reversed {
|
|
|
|
end..start
|
|
|
|
} else {
|
|
|
|
start..end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-28 18:07:30 +00:00
|
|
|
pub fn offset_range(&self, buffer: &Buffer) -> Range<usize> {
|
|
|
|
let start = self.start.to_offset(buffer);
|
|
|
|
let end = self.end.to_offset(buffer);
|
|
|
|
if self.reversed {
|
|
|
|
end..start
|
|
|
|
} else {
|
|
|
|
start..end
|
|
|
|
}
|
|
|
|
}
|
2021-04-09 16:45:13 +00:00
|
|
|
}
|
2021-10-22 07:56:47 +00:00
|
|
|
|
2021-10-22 20:12:16 +00:00
|
|
|
impl SelectionSet {
|
|
|
|
pub fn offset_selections<'a>(
|
|
|
|
&'a self,
|
|
|
|
content: impl Into<Content<'a>> + 'a,
|
|
|
|
) -> impl 'a + Iterator<Item = Selection<usize>> {
|
|
|
|
self.selections
|
|
|
|
.offset_ranges(content)
|
|
|
|
.map(|(range, state)| Selection {
|
|
|
|
id: state.id,
|
|
|
|
start: range.start,
|
|
|
|
end: range.end,
|
|
|
|
reversed: state.reversed,
|
|
|
|
goal: state.goal,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn point_selections<'a>(
|
|
|
|
&'a self,
|
|
|
|
content: impl Into<Content<'a>> + 'a,
|
|
|
|
) -> impl 'a + Iterator<Item = Selection<Point>> {
|
|
|
|
self.selections
|
|
|
|
.point_ranges(content)
|
|
|
|
.map(|(range, state)| Selection {
|
|
|
|
id: state.id,
|
|
|
|
start: range.start,
|
|
|
|
end: range.end,
|
|
|
|
reversed: state.reversed,
|
|
|
|
goal: state.goal,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-22 19:19:19 +00:00
|
|
|
impl<'a> Into<proto::SelectionSet> for &'a SelectionSet {
|
|
|
|
fn into(self) -> proto::SelectionSet {
|
|
|
|
let version = self.selections.version();
|
|
|
|
let entries = self.selections.raw_entries();
|
|
|
|
proto::SelectionSet {
|
|
|
|
replica_id: self.id.replica_id as u32,
|
|
|
|
lamport_timestamp: self.id.value as u32,
|
|
|
|
is_active: self.active,
|
|
|
|
version: version.into(),
|
|
|
|
selections: entries
|
|
|
|
.iter()
|
|
|
|
.map(|(range, state)| proto::Selection {
|
|
|
|
id: state.id as u64,
|
|
|
|
start: range.start.0 as u64,
|
|
|
|
end: range.end.0 as u64,
|
|
|
|
reversed: state.reversed,
|
|
|
|
})
|
|
|
|
.collect(),
|
2021-10-22 07:56:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-22 18:46:02 +00:00
|
|
|
|
2021-10-22 19:19:19 +00:00
|
|
|
impl From<proto::SelectionSet> for SelectionSet {
|
|
|
|
fn from(set: proto::SelectionSet) -> Self {
|
|
|
|
Self {
|
2021-10-22 18:46:02 +00:00
|
|
|
id: clock::Lamport {
|
|
|
|
replica_id: set.replica_id as u16,
|
|
|
|
value: set.lamport_timestamp,
|
|
|
|
},
|
|
|
|
active: set.is_active,
|
2021-10-22 19:19:19 +00:00
|
|
|
selections: Arc::new(AnchorRangeMap::from_raw(
|
|
|
|
set.version.into(),
|
2021-10-22 18:46:02 +00:00
|
|
|
set.selections
|
|
|
|
.into_iter()
|
2021-10-22 19:19:19 +00:00
|
|
|
.map(|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(),
|
|
|
|
)),
|
|
|
|
}
|
2021-10-22 18:46:02 +00:00
|
|
|
}
|
|
|
|
}
|