zed/crates/text/src/selection.rs

75 lines
1.7 KiB
Rust
Raw Normal View History

use crate::Anchor;
use crate::{rope::TextDimension, BufferSnapshot, ToOffset, ToPoint};
use std::cmp::Ordering;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum SelectionGoal {
None,
Column(u32),
ColumnRange { start: u32, end: u32 },
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Selection<T> {
2021-05-10 14:10:41 +00:00
pub id: usize,
pub start: T,
pub end: T,
pub reversed: bool,
pub goal: SelectionGoal,
}
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
}
pub fn tail(&self) -> T {
if self.reversed {
self.end.clone()
} else {
self.start.clone()
}
}
}
impl<T: ToOffset + ToPoint + Copy + Ord> Selection<T> {
pub fn is_empty(&self) -> bool {
self.start == self.end
}
pub fn set_head(&mut self, head: T) {
if head.cmp(&self.tail()) < Ordering::Equal {
if !self.reversed {
self.end = self.start;
self.reversed = true;
}
self.start = head;
} else {
if self.reversed {
self.start = self.end;
self.reversed = false;
}
self.end = head;
}
}
}
impl Selection<Anchor> {
pub fn resolve<'a, D: 'a + TextDimension>(
&'a self,
snapshot: &'a BufferSnapshot,
) -> 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,
}
}
}