use super::{Position, Range};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Selection {
pub anchor: Position,
pub cursor: Position,
}
impl Selection {
#[must_use]
pub const fn new(anchor: Position, cursor: Position) -> Self {
Self { anchor, cursor }
}
#[must_use]
pub const fn empty(pos: Position) -> Self {
Self {
anchor: pos,
cursor: pos,
}
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.anchor.offset == self.cursor.offset
}
#[must_use]
pub fn range(&self) -> Range {
Range::new(self.anchor, self.cursor)
}
#[must_use]
pub const fn is_reversed(&self) -> bool {
self.cursor.offset < self.anchor.offset
}
#[must_use]
pub const fn extend_to(&self, pos: Position) -> Self {
Self {
anchor: self.anchor,
cursor: pos,
}
}
}