| 1 | |
| 2 | |
| 3 | use super::model::{Anchor, Blk, Model}; |
| 4 | |
| 5 | |
| 6 | |
| 7 | pub type Pos = usize; |
| 8 | |
| 9 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 10 | pub enum Selection { |
| 11 | |
| 12 | Text { anchor: Pos, head: Pos }, |
| 13 | |
| 14 | Node(Pos), |
| 15 | |
| 16 | Gap(Pos), |
| 17 | |
| 18 | All, |
| 19 | } |
| 20 | |
| 21 | impl 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 | |
| 50 | |
| 51 | pub 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 | |
| 60 | pub 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 | |
| 74 | pub 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 | |
| 83 | pub 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 | |
| 105 | pub struct SelAnchors { |
| 106 | kind: u8, |
| 107 | a: Option<Anchor>, |
| 108 | b: Option<Anchor>, |
| 109 | } |
| 110 | |
| 111 | impl 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 | } |