2021-11-25 20:19:49 +00:00
|
|
|
use sum_tree::Bias;
|
|
|
|
|
2021-11-30 21:29:04 +00:00
|
|
|
use crate::{rope::TextDimension, Snapshot};
|
2021-11-03 16:51:57 +00:00
|
|
|
|
2021-11-30 21:29:04 +00:00
|
|
|
use super::{AnchorRangeMap, Buffer, Point, ToOffset, ToPoint};
|
2021-10-22 20:12:16 +00:00
|
|
|
use std::{cmp::Ordering, ops::Range, sync::Arc};
|
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-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-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 {
|
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,
|
|
|
|
content: &'a Snapshot,
|
|
|
|
) -> impl 'a + Iterator<Item = Selection<D>>
|
2021-11-03 16:51:57 +00:00
|
|
|
where
|
|
|
|
D: 'a + TextDimension<'a>,
|
|
|
|
{
|
2021-10-22 20:12:16 +00:00
|
|
|
self.selections
|
2021-11-03 16:51:57 +00:00
|
|
|
.ranges(content)
|
2021-10-22 20:12:16 +00:00
|
|
|
.map(|(range, state)| Selection {
|
|
|
|
id: state.id,
|
|
|
|
start: range.start,
|
|
|
|
end: range.end,
|
|
|
|
reversed: state.reversed,
|
|
|
|
goal: state.goal,
|
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-11-30 21:29:04 +00:00
|
|
|
content: &'a Snapshot,
|
2021-11-25 20:19:49 +00:00
|
|
|
) -> impl 'a + Iterator<Item = Selection<D>>
|
|
|
|
where
|
|
|
|
D: 'a + TextDimension<'a>,
|
|
|
|
I: 'a + ToOffset,
|
|
|
|
{
|
|
|
|
self.selections
|
|
|
|
.intersecting_ranges(range, content)
|
|
|
|
.map(|(range, state)| Selection {
|
|
|
|
id: state.id,
|
|
|
|
start: range.start,
|
|
|
|
end: range.end,
|
|
|
|
reversed: state.reversed,
|
|
|
|
goal: state.goal,
|
2021-10-22 20:12:16 +00:00
|
|
|
})
|
|
|
|
}
|
2021-11-18 14:04:12 +00:00
|
|
|
|
2021-11-30 21:29:04 +00:00
|
|
|
pub fn oldest_selection<'a, D>(&'a self, content: &'a Snapshot) -> Option<Selection<D>>
|
2021-11-18 14:04:12 +00:00
|
|
|
where
|
|
|
|
D: 'a + TextDimension<'a>,
|
|
|
|
{
|
|
|
|
self.selections
|
|
|
|
.min_by_key(content, |selection| selection.id)
|
|
|
|
.map(|(range, state)| Selection {
|
|
|
|
id: state.id,
|
|
|
|
start: range.start,
|
|
|
|
end: range.end,
|
|
|
|
reversed: state.reversed,
|
|
|
|
goal: state.goal,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-30 21:29:04 +00:00
|
|
|
pub fn newest_selection<'a, D>(&'a self, content: &'a Snapshot) -> Option<Selection<D>>
|
2021-11-18 14:04:12 +00:00
|
|
|
where
|
|
|
|
D: 'a + TextDimension<'a>,
|
|
|
|
{
|
|
|
|
self.selections
|
|
|
|
.max_by_key(content, |selection| selection.id)
|
|
|
|
.map(|(range, state)| Selection {
|
|
|
|
id: state.id,
|
|
|
|
start: range.start,
|
|
|
|
end: range.end,
|
|
|
|
reversed: state.reversed,
|
|
|
|
goal: state.goal,
|
|
|
|
})
|
|
|
|
}
|
2021-10-22 20:12:16 +00:00
|
|
|
}
|