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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use oatie::doc::*;
use oatie::rtf::*;
use oatie::stepper::*;
use take_mut;
pub fn is_block(attrs: &Attrs) -> bool {
RtfSchema::track_type_from_attrs(attrs) == Some(RtfTrack::Blocks)
}
pub fn is_block_object(attrs: &Attrs) -> bool {
RtfSchema::track_type_from_attrs(attrs) == Some(RtfTrack::BlockObjects)
}
pub fn is_caret(attrs: &Attrs, client_id: Option<&str>, focus: bool) -> bool {
if let Attrs::Caret {
client_id: caret_client_id,
focus: caret_focus,
} = attrs
{
client_id == Some(caret_client_id) && *caret_focus == focus
} else {
false
}
}
pub fn is_any_caret(attrs: &Attrs) -> bool {
if let Attrs::Caret { .. } = attrs {
true
} else {
false
}
}
#[derive(Clone, Debug, PartialEq, Copy)]
pub enum Pos {
Start,
Anchor,
Focus,
End,
}
#[derive(Clone, Debug)]
pub struct CaretStepper<'a> {
pub doc: DocStepper<'a, RtfSchema>,
pub caret_pos: isize,
}
impl<'a> CaretStepper<'a> {
pub fn new(doc: DocStepper<'a, RtfSchema>) -> CaretStepper<'a> {
let mut stepper = CaretStepper { doc, caret_pos: -1 };
while stepper.caret_pos != 0 {
if stepper.next().is_none() {
break;
}
}
stepper
}
pub fn rev(self) -> ReverseCaretStepper<'a> {
ReverseCaretStepper {
doc: self.doc,
caret_pos: self.caret_pos,
}
}
pub fn is_valid_caret_pos(&self) -> bool {
if let Some(DocText(..)) = self.doc.unhead() {
return true;
} else if self.doc.unhead().is_none() && !self.doc.is_back_done() {
if is_block(self.doc.parent_attrs()) {
return true;
}
}
return false;
}
pub fn skip_element(&mut self) -> Option<()> {
let len = match self.doc.head() {
Some(DocText(_, val)) => {
let len = val.char_len();
self.doc.next();
len
}
Some(DocGroup(..)) => {
self.doc.enter();
1
}
None => {
if self.doc.is_done() {
return None;
} else {
self.doc.exit();
1
}
}
};
if self.is_valid_caret_pos() {
self.caret_pos += len as isize;
}
Some(())
}
}
impl<'a> Iterator for CaretStepper<'a> {
type Item = ();
fn next(&mut self) -> Option<()> {
match self.doc.head() {
Some(DocText(..)) => {
self.doc.skip(1);
}
Some(DocGroup(..)) => {
self.doc.enter();
}
None => {
if self.doc.is_done() {
return None;
} else {
self.doc.exit();
}
}
}
if self.is_valid_caret_pos() {
self.caret_pos += 1;
}
Some(())
}
}
#[derive(Clone, Debug)]
pub struct ReverseCaretStepper<'a> {
pub doc: DocStepper<'a, RtfSchema>,
pub caret_pos: isize,
}
impl<'a> ReverseCaretStepper<'a> {
pub fn rev(self) -> CaretStepper<'a> {
CaretStepper {
doc: self.doc,
caret_pos: self.caret_pos,
}
}
pub fn is_valid_caret_pos(&self) -> bool {
if let Some(DocText(..)) = self.doc.unhead() {
return true;
} else if self.doc.unhead().is_none() {
if self.doc.at_root() {
return false;
}
if is_block(self.doc.parent_attrs()) {
return true;
}
}
let mut doc2 = self.doc.clone();
while let Some(DocGroup(ref attrs, _)) = doc2.unhead() {
if is_any_caret(attrs) {
doc2.unskip(1);
} else {
break;
}
}
if let Some(DocText(..)) = doc2.unhead() {
return true;
} else if doc2.unhead().is_none() {
if doc2.at_root() {
return false;
}
if is_block(doc2.parent_attrs()) {
return true;
}
}
return false;
}
}
impl<'a> Iterator for ReverseCaretStepper<'a> {
type Item = ();
fn next(&mut self) -> Option<()> {
while let Some(DocGroup(ref attrs, _)) = self.doc.unhead() {
if is_any_caret(attrs) {
self.doc.unskip(1);
} else {
break;
}
}
match self.doc.unhead() {
Some(DocText(..)) => {
self.doc.unskip(1);
}
Some(DocGroup(..)) => {
self.doc.unexit();
}
None => {
if self.doc.at_root() {
return None;
} else {
self.doc.unenter();
}
}
}
if self.is_valid_caret_pos() {
self.caret_pos -= 1;
} else if let (None, true) = (self.doc.unhead(), self.doc.at_root()) {
self.caret_pos = -1;
}
Some(())
}
}