forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits

pos.rs131 lines, 4.0 KiBHistoryRawDownload
1//! Positions and selections: UTF-16 offsets from the start of the body.
2
3use super::model::{Anchor, Blk, Model};
4
5/// A caret offset. Inside a block it runs from the block's content start through its
6/// separator's offset (the content end).
7pub type Pos = usize;
8
9#[derive(Clone, Debug, PartialEq, Eq)]
10pub enum Selection {
11 /// Text range or caret. `anchor` stays put while extending, `head` moves.
12 Text { anchor: Pos, head: Pos },
13 /// A selected leaf: a rule (its block start) or an image (its char).
14 Node(Pos),
15 /// A caret before the block starting at the offset (or at the body end).
16 Gap(Pos),
17 /// Everything (Mod-a).
18 All,
19}
20
21impl Selection {
22 pub fn caret(p: Pos) -> Self {
23 Selection::Text { anchor: p, head: p }
24 }
25
26 pub fn cursor(&self) -> Option<Pos> {
27 match *self {
28 Selection::Text { anchor, head } if anchor == head => Some(head),
29 _ => None,
30 }
31 }
32
33 pub fn is_empty(&self) -> bool {
34 match self {
35 Selection::Text { anchor, head } => anchor == head,
36 Selection::Gap(_) => true,
37 _ => false,
38 }
39 }
40
41 pub fn head(&self) -> Option<Pos> {
42 match *self {
43 Selection::Text { head, .. } => Some(head),
44 _ => None,
45 }
46 }
47}
48
49/// `(from, to)` of a text selection in order; `All` spans the first block's start to
50/// the last block's content end.
51pub fn text_range(m: &Model, sel: &Selection) -> Option<(Pos, Pos)> {
52 match *sel {
53 Selection::Text { anchor, head } => Some((anchor.min(head), anchor.max(head))),
54 Selection::All => Some((0, m.last().sep)),
55 _ => None,
56 }
57}
58
59/// The selection moved to valid places: carets inside text blocks, nodes on leaves.
60pub fn clamp(m: &Model, sel: &Selection) -> Selection {
61 let total = m.total();
62 match *sel {
63 Selection::Text { anchor, head } => Selection::Text { anchor: snap(m, anchor.min(total)), head: snap(m, head.min(total)) },
64 Selection::Node(p) => {
65 let b = m.at(p);
66 if b.kind().is_text() && p >= b.sep { Selection::caret(snap(m, p)) } else { Selection::Node(p.min(b.sep)) }
67 }
68 Selection::Gap(p) => Selection::Gap(p.min(total)),
69 Selection::All => Selection::All,
70 }
71}
72
73/// The nearest caret place: inside the block holding `p`, or next to a rule.
74pub fn snap(m: &Model, p: Pos) -> Pos {
75 let b = m.at(p);
76 if b.kind().is_text() {
77 return p.clamp(b.start, b.sep);
78 }
79 near_text(m, &b).unwrap_or(p)
80}
81
82/// The start of the next text block, else the end of the previous one.
83pub fn near_text(m: &Model, b: &Blk) -> Option<Pos> {
84 let mut out = None;
85 m.scan(b.index + 1, false, |i, a| {
86 if a.kind.is_text() {
87 out = Some(i.start16);
88 return false;
89 }
90 true
91 });
92 if out.is_none() {
93 m.scan(b.index, true, |i, a| {
94 if a.kind.is_text() {
95 out = Some(i.sep16());
96 return false;
97 }
98 true
99 });
100 }
101 out
102}
103
104/// A selection held by anchors, for undo and remote edits.
105pub struct SelAnchors {
106 kind: u8,
107 a: Option<Anchor>,
108 b: Option<Anchor>,
109}
110
111impl SelAnchors {
112 pub fn new(m: &Model, sel: &Selection) -> Self {
113 match *sel {
114 Selection::Text { anchor, head } => SelAnchors { kind: 0, a: Some(m.anchor(anchor)), b: Some(m.anchor(head)) },
115 Selection::Node(p) => SelAnchors { kind: 1, a: Some(m.anchor(p)), b: None },
116 Selection::Gap(p) => SelAnchors { kind: 2, a: Some(m.anchor(p)), b: None },
117 Selection::All => SelAnchors { kind: 3, a: None, b: None },
118 }
119 }
120
121 pub fn resolve(&self, m: &Model) -> Selection {
122 let r = |x: &Option<Anchor>| x.as_ref().map_or(0, |a| m.resolve(a));
123 let sel = match self.kind {
124 0 => Selection::Text { anchor: r(&self.a), head: r(&self.b) },
125 1 => Selection::Node(r(&self.a)),
126 2 => Selection::Gap(r(&self.a)),
127 _ => Selection::All,
128 };
129 clamp(m, &sel)
130 }
131}