2021-12-09 22:49:04 +00:00
|
|
|
use crate::Anchor;
|
|
|
|
use crate::{rope::TextDimension, BufferSnapshot, ToOffset, ToPoint};
|
2021-10-22 20:12:16 +00:00
|
|
|
use std::{cmp::Ordering, ops::Range, sync::Arc};
|
2021-12-07 15:52:15 +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-12-09 10:18:01 +00:00
|
|
|
pub selections: Arc<[Selection<Anchor>]>,
|
2021-10-22 19:19:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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-11-03 18:07:06 +00:00
|
|
|
impl<T: Clone> Selection<T> {
|
|
|
|
pub fn head(&self) -> T {
|
|
|
|
if self.reversed {
|
|
|
|
self.start.clone()
|
|
|
|
} else {
|
|
|
|
self.end.clone()
|
|
|
|
}
|
2021-10-28 22:42:24 +00:00
|
|
|
}
|
|
|
|
|
2021-11-03 18:07:06 +00:00
|
|
|
pub fn tail(&self) -> T {
|
2021-04-09 16:45:13 +00:00
|
|
|
if self.reversed {
|
2021-11-03 18:07:06 +00:00
|
|
|
self.end.clone()
|
2021-04-09 16:45:13 +00:00
|
|
|
} else {
|
2021-11-03 18:07:06 +00:00
|
|
|
self.start.clone()
|
2021-04-09 16:45:13 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-03 18:07:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: ToOffset + ToPoint + Copy + Ord> Selection<T> {
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.start == 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 07:56:47 +00:00
|
|
|
|
2021-12-09 10:18:01 +00:00
|
|
|
impl Selection<Anchor> {
|
2021-12-09 22:49:04 +00:00
|
|
|
pub fn resolve<'a, D: 'a + TextDimension>(
|
2021-12-09 10:18:01 +00:00
|
|
|
&'a self,
|
2021-12-09 22:49:04 +00:00
|
|
|
snapshot: &'a BufferSnapshot,
|
2021-12-09 10:18:01 +00:00
|
|
|
) -> Selection<D> {
|
|
|
|
Selection {
|
|
|
|
id: self.id,
|
|
|
|
start: snapshot.summary_for_anchor(&self.start),
|
|
|
|
end: snapshot.summary_for_anchor(&self.end),
|
|
|
|
reversed: self.reversed,
|
|
|
|
goal: self.goal,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-22 20:12:16 +00:00
|
|
|
impl SelectionSet {
|
2021-10-28 17:36:43 +00:00
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.selections.len()
|
|
|
|
}
|
|
|
|
|
2021-11-30 21:29:04 +00:00
|
|
|
pub fn selections<'a, D>(
|
|
|
|
&'a self,
|
2021-12-09 22:49:04 +00:00
|
|
|
snapshot: &'a BufferSnapshot,
|
2021-11-30 21:29:04 +00:00
|
|
|
) -> impl 'a + Iterator<Item = Selection<D>>
|
2021-11-03 16:51:57 +00:00
|
|
|
where
|
2021-12-07 15:52:15 +00:00
|
|
|
D: TextDimension,
|
2021-11-03 16:51:57 +00:00
|
|
|
{
|
2021-12-09 18:42:44 +00:00
|
|
|
let anchors = self
|
|
|
|
.selections
|
|
|
|
.iter()
|
|
|
|
.flat_map(|selection| [&selection.start, &selection.end].into_iter());
|
|
|
|
let mut positions = snapshot.summaries_for_anchors::<D, _>(anchors);
|
|
|
|
self.selections.iter().map(move |selection| Selection {
|
|
|
|
start: positions.next().unwrap(),
|
|
|
|
end: positions.next().unwrap(),
|
|
|
|
goal: selection.goal,
|
|
|
|
reversed: selection.reversed,
|
|
|
|
id: selection.id,
|
|
|
|
})
|
2021-11-25 20:19:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-30 21:29:04 +00:00
|
|
|
pub fn intersecting_selections<'a, D, I>(
|
2021-11-25 20:19:49 +00:00
|
|
|
&'a self,
|
|
|
|
range: Range<(I, Bias)>,
|
2021-12-09 22:49:04 +00:00
|
|
|
snapshot: &'a BufferSnapshot,
|
2021-11-25 20:19:49 +00:00
|
|
|
) -> impl 'a + Iterator<Item = Selection<D>>
|
|
|
|
where
|
2021-12-07 15:52:15 +00:00
|
|
|
D: TextDimension,
|
2021-11-25 20:19:49 +00:00
|
|
|
I: 'a + ToOffset,
|
|
|
|
{
|
2021-12-09 10:18:01 +00:00
|
|
|
let start = snapshot.anchor_at(range.start.0, range.start.1);
|
|
|
|
let end = snapshot.anchor_at(range.end.0, range.end.1);
|
|
|
|
let start_ix = match self
|
|
|
|
.selections
|
2021-12-09 15:38:46 +00:00
|
|
|
.binary_search_by(|probe| probe.end.cmp(&start, snapshot).unwrap())
|
2021-12-09 10:18:01 +00:00
|
|
|
{
|
|
|
|
Ok(ix) | Err(ix) => ix,
|
|
|
|
};
|
|
|
|
let end_ix = match self
|
|
|
|
.selections
|
2021-12-09 15:38:46 +00:00
|
|
|
.binary_search_by(|probe| probe.start.cmp(&end, snapshot).unwrap())
|
2021-12-09 10:18:01 +00:00
|
|
|
{
|
|
|
|
Ok(ix) | Err(ix) => ix,
|
|
|
|
};
|
|
|
|
self.selections[start_ix..end_ix]
|
|
|
|
.iter()
|
|
|
|
.map(|s| s.resolve(snapshot))
|
2021-10-22 20:12:16 +00:00
|
|
|
}
|
2021-11-18 14:04:12 +00:00
|
|
|
|
2021-12-09 22:49:04 +00:00
|
|
|
pub fn oldest_selection<'a, D>(&'a self, snapshot: &'a BufferSnapshot) -> Option<Selection<D>>
|
2021-11-18 14:04:12 +00:00
|
|
|
where
|
2021-12-07 15:52:15 +00:00
|
|
|
D: TextDimension,
|
2021-11-18 14:04:12 +00:00
|
|
|
{
|
|
|
|
self.selections
|
2021-12-09 10:18:01 +00:00
|
|
|
.iter()
|
|
|
|
.min_by_key(|s| s.id)
|
|
|
|
.map(|s| s.resolve(snapshot))
|
2021-11-18 14:04:12 +00:00
|
|
|
}
|
|
|
|
|
2021-12-09 22:49:04 +00:00
|
|
|
pub fn newest_selection<'a, D>(&'a self, snapshot: &'a BufferSnapshot) -> Option<Selection<D>>
|
2021-11-18 14:04:12 +00:00
|
|
|
where
|
2021-12-07 15:52:15 +00:00
|
|
|
D: TextDimension,
|
2021-11-18 14:04:12 +00:00
|
|
|
{
|
|
|
|
self.selections
|
2021-12-09 10:18:01 +00:00
|
|
|
.iter()
|
|
|
|
.max_by_key(|s| s.id)
|
|
|
|
.map(|s| s.resolve(snapshot))
|
2021-11-18 14:04:12 +00:00
|
|
|
}
|
2021-10-22 20:12:16 +00:00
|
|
|
}
|