//! Positions and selections: UTF-16 offsets from the start of the body. use super::model::{Anchor, Blk, Model}; /// A caret offset. Inside a block it runs from the block's content start through its /// separator's offset (the content end). pub type Pos = usize; #[derive(Clone, Debug, PartialEq, Eq)] pub enum Selection { /// Text range or caret. `anchor` stays put while extending, `head` moves. Text { anchor: Pos, head: Pos }, /// A selected leaf: a rule (its block start) or an image (its char). Node(Pos), /// A caret before the block starting at the offset (or at the body end). Gap(Pos), /// Everything (Mod-a). All, } impl Selection { pub fn caret(p: Pos) -> Self { Selection::Text { anchor: p, head: p } } pub fn cursor(&self) -> Option { match *self { Selection::Text { anchor, head } if anchor == head => Some(head), _ => None, } } pub fn is_empty(&self) -> bool { match self { Selection::Text { anchor, head } => anchor == head, Selection::Gap(_) => true, _ => false, } } pub fn head(&self) -> Option { match *self { Selection::Text { head, .. } => Some(head), _ => None, } } } /// `(from, to)` of a text selection in order; `All` spans the first block's start to /// the last block's content end. pub fn text_range(m: &Model, sel: &Selection) -> Option<(Pos, Pos)> { match *sel { Selection::Text { anchor, head } => Some((anchor.min(head), anchor.max(head))), Selection::All => Some((0, m.last().sep)), _ => None, } } /// The selection moved to valid places: carets inside text blocks, nodes on leaves. pub fn clamp(m: &Model, sel: &Selection) -> Selection { let total = m.total(); match *sel { Selection::Text { anchor, head } => Selection::Text { anchor: snap(m, anchor.min(total)), head: snap(m, head.min(total)) }, Selection::Node(p) => { let b = m.at(p); if b.kind().is_text() && p >= b.sep { Selection::caret(snap(m, p)) } else { Selection::Node(p.min(b.sep)) } } Selection::Gap(p) => Selection::Gap(p.min(total)), Selection::All => Selection::All, } } /// The nearest caret place: inside the block holding `p`, or next to a rule. pub fn snap(m: &Model, p: Pos) -> Pos { let b = m.at(p); if b.kind().is_text() { return p.clamp(b.start, b.sep); } near_text(m, &b).unwrap_or(p) } /// The start of the next text block, else the end of the previous one. pub fn near_text(m: &Model, b: &Blk) -> Option { let mut out = None; m.scan(b.index + 1, false, |i, a| { if a.kind.is_text() { out = Some(i.start16); return false; } true }); if out.is_none() { m.scan(b.index, true, |i, a| { if a.kind.is_text() { out = Some(i.sep16()); return false; } true }); } out } /// A selection held by anchors, for undo and remote edits. pub struct SelAnchors { kind: u8, a: Option, b: Option, } impl SelAnchors { pub fn new(m: &Model, sel: &Selection) -> Self { match *sel { Selection::Text { anchor, head } => SelAnchors { kind: 0, a: Some(m.anchor(anchor)), b: Some(m.anchor(head)) }, Selection::Node(p) => SelAnchors { kind: 1, a: Some(m.anchor(p)), b: None }, Selection::Gap(p) => SelAnchors { kind: 2, a: Some(m.anchor(p)), b: None }, Selection::All => SelAnchors { kind: 3, a: None, b: None }, } } pub fn resolve(&self, m: &Model) -> Selection { let r = |x: &Option| x.as_ref().map_or(0, |a| m.resolve(a)); let sel = match self.kind { 0 => Selection::Text { anchor: r(&self.a), head: r(&self.b) }, 1 => Selection::Node(r(&self.a)), 2 => Selection::Gap(r(&self.a)), _ => Selection::All, }; clamp(m, &sel) } }