-
Notifications
You must be signed in to change notification settings - Fork 526
/
Copy pathsnap.rs
111 lines (92 loc) · 2.45 KB
/
snap.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use std::ops::Range;
use unicode_width::UnicodeWidthChar;
use super::{InputMode, InputOp};
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(super) struct InputSnap {
pub(super) value: String,
pub(super) op: InputOp,
pub(super) mode: InputMode,
pub(super) offset: usize,
pub(super) cursor: usize,
}
impl InputSnap {
pub(super) fn new(value: String, limit: usize) -> Self {
let mut snap = Self {
value,
op: Default::default(),
mode: Default::default(),
offset: usize::MAX,
cursor: usize::MAX,
};
snap.reset(limit);
snap
}
#[inline]
pub(super) fn reset(&mut self, limit: usize) {
self.cursor = self.cursor.min(self.value.chars().count().saturating_sub(self.mode.delta()));
self.offset =
self.offset.min(self.cursor.saturating_sub(Self::find_window(&self.rev(), 0, limit).end));
}
}
impl InputSnap {
#[inline]
pub(super) fn len(&self) -> usize { self.value.len() }
#[inline]
pub(super) fn count(&self) -> usize { self.value.chars().count() }
#[inline]
pub(super) fn idx(&self, n: usize) -> Option<usize> {
self
.value
.char_indices()
.nth(n)
.map(|(i, _)| i)
.or_else(|| if n == self.count() { Some(self.len()) } else { None })
}
#[inline]
pub(super) fn slice(&self, range: Range<usize>) -> &str {
let (s, e) = (self.idx(range.start), self.idx(range.end));
&self.value[s.unwrap()..e.unwrap()]
}
#[inline]
pub(super) fn rev(&self) -> String { self.value.chars().rev().collect::<String>() }
#[inline]
pub(super) fn window(&self, limit: usize) -> Range<usize> {
Self::find_window(&self.value, self.offset, limit)
}
#[inline]
pub(super) fn find_window(s: &str, offset: usize, limit: usize) -> Range<usize> {
let mut width = 0;
let v: Vec<_> = s
.chars()
.enumerate()
.skip(offset)
.map_while(|(i, c)| {
width += c.width().unwrap_or(0);
if width < limit { Some(i) } else { None }
})
.collect();
if v.is_empty() {
return 0..0;
}
*v.first().unwrap()..v.last().unwrap() + 1
}
#[inline]
pub(super) fn find_window_backward(s: &str, offset: usize, limit: usize) -> Range<usize> {
let mut width = 0;
let len = s.chars().count();
let v: Vec<_> = s
.chars()
.rev()
.enumerate()
.skip(len.saturating_sub(offset + 1))
.map_while(|(i, c)| {
width += c.width().unwrap_or(0);
if width < limit { Some(len.saturating_sub(i + 1)) } else { None }
})
.collect();
if v.is_empty() {
return 0..0;
}
*v.last().unwrap()..v.first().unwrap() + 1
}
}