use crate::BlockId;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Point {
pub block: BlockId,
pub offset: u32,
}
impl Point {
#[must_use]
pub const fn new(block: BlockId, offset: u32) -> Self {
Self { block, offset }
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum Bias {
Before,
#[default]
After,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Selection {
Text {
anchor: Point,
head: Point,
},
Blocks {
anchor: BlockId,
head: BlockId,
},
}
impl Selection {
#[must_use]
pub const fn caret(point: Point) -> Self {
Self::Text {
anchor: point,
head: point,
}
}
#[must_use]
pub const fn text(anchor: Point, head: Point) -> Self {
Self::Text { anchor, head }
}
#[must_use]
pub const fn block(block: BlockId) -> Self {
Self::Blocks {
anchor: block,
head: block,
}
}
#[must_use]
pub fn is_caret(&self) -> bool {
matches!(self, Self::Text { anchor, head } if anchor == head)
}
#[must_use]
pub fn as_caret(&self) -> Option<Point> {
match self {
Self::Text { anchor, head } if anchor == head => Some(*head),
_ => None,
}
}
}