2021-12-09 22:49:04 +00:00
|
|
|
use crate::Anchor;
|
2022-03-22 23:05:01 +00:00
|
|
|
use crate::{rope::TextDimension, BufferSnapshot};
|
2021-12-11 07:29:34 +00:00
|
|
|
use std::cmp::Ordering;
|
2021-04-09 16:45:13 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-03-18 09:22:13 +00:00
|
|
|
impl Default for SelectionGoal {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-03-22 23:05:01 +00:00
|
|
|
impl<T: Copy + Ord> Selection<T> {
|
2021-11-03 18:07:06 +00:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.start == self.end
|
|
|
|
}
|
2021-04-09 16:45:13 +00:00
|
|
|
|
2022-03-22 23:05:01 +00:00
|
|
|
pub fn set_head(&mut self, head: T, new_goal: SelectionGoal) {
|
2021-10-22 20:12:16 +00:00
|
|
|
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
|
|
|
}
|
2022-03-22 23:05:01 +00:00
|
|
|
self.goal = new_goal;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn collapse_to(&mut self, point: T, new_goal: SelectionGoal) {
|
|
|
|
self.start = point;
|
|
|
|
self.end = point;
|
|
|
|
self.goal = new_goal;
|
|
|
|
self.reversed = false;
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|