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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
use super::*;
use serde_json::json;
use wasm_bindgen::prelude::*;
impl Program {
pub fn new() -> Program {
Program(vec![])
}
pub fn place(&mut self, mut code: Bytecode) {
use self::Bytecode::*;
match (self.0.last_mut(), &mut code) {
(Some(&mut AdvanceElements(ref mut last_n)), AdvanceElements(n)) => {
*last_n += *n;
}
(Some(&mut DeleteElements(ref mut last_n)), DeleteElements(n)) => {
*last_n += *n;
}
_ => self.0.push(code.clone()),
}
}
}
#[allow(non_snake_case)]
pub trait DocMutator<S: Schema> {
fn flush_chars(&mut self) -> bool {
unimplemented!();
}
fn flush(&mut self) {
unimplemented!();
}
fn Enter(&mut self) {
unimplemented!();
}
fn Exit(&mut self) {
unimplemented!();
}
fn AdvanceElements(&mut self, _count: usize) {
unimplemented!();
}
fn delete(&mut self, _count: usize) {
unimplemented!();
}
fn DeleteElements(&mut self, _count: usize) {
unimplemented!();
}
fn InsertDocString(&mut self, _docstring: DocString, _styles: S::CharsProperties) {
unimplemented!();
}
fn UnwrapSelf(&mut self) {
unimplemented!();
}
fn WrapPrevious(&mut self, _count: usize, _attrs: S::GroupProperties) {
unimplemented!();
}
fn skip(&mut self, _count: usize) {
unimplemented!();
}
}
pub struct NullDocMutator {}
impl<S: Schema> DocMutator<S> for NullDocMutator {
fn Enter(&mut self) {
}
fn Exit(&mut self) {
}
fn AdvanceElements(&mut self, _count: usize) {
}
fn delete(&mut self, _count: usize) {
}
fn DeleteElements(&mut self, _count: usize) {
}
fn InsertDocString(&mut self, _docstring: DocString, _style: S::CharsProperties) {
}
fn UnwrapSelf(&mut self) {
}
fn WrapPrevious(&mut self, _count: usize, _attrs: S::GroupProperties) {
}
fn skip(&mut self, _count: usize) {
}
}
#[derive(Clone, Debug, Serialize, Deserialize, TypescriptDefinition)]
#[serde(tag = "tag", content = "fields")]
pub enum Bytecode {
Enter,
Exit,
AdvanceElements(usize),
DeleteElements(usize),
InsertDocString(DocString, serde_json::Value),
WrapPrevious(usize, serde_json::Value),
UnwrapSelf,
JoinTextLeft,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Program(pub Vec<Bytecode>);
#[derive(Clone, Debug)]
pub struct RecordingDocMutator<'a, S: Schema> {
bc: Program,
stepper: DocStepper<'a, S>,
writer: DocWriter<S>,
}
impl<'a, S: Schema> RecordingDocMutator<'a, S> {
pub fn stepper(&'a self) -> &'a DocStepper<'_, S> {
&self.stepper
}
pub fn result(mut self) -> Result<(DocSpan<S>, Program), Error> {
self.flush();
let RecordingDocMutator { writer, bc, .. } = self;
(writer.result().map(|doc| (doc, bc)))
}
pub fn new(stepper: DocStepper<'_, S>) -> RecordingDocMutator<'_, S> {
RecordingDocMutator {
bc: Program(vec![]),
stepper,
writer: DocWriter::new(),
}
}
fn flush_chars(&mut self) -> bool {
if let Some(index) = self.stepper.char_index() {
if index > 0 {
let partial = self.stepper.char_cursor_expect().right().expect("hey now");
if let Some(&DocText(ref styles, ref _text)) = self.stepper.head_raw() {
self.bc
.place(Bytecode::InsertDocString(partial.clone(), json!(styles)));
self.writer.place(&DocText(styles.clone(), partial.clone()));
} else {
unreachable!();
}
self.stepper.next();
return true;
} else if let (Some(ref previous), Some(ref head)) =
(self.writer.past.last(), self.stepper.head())
{
if can_element_join(previous, head) {
self.bc.place(Bytecode::JoinTextLeft);
self.writer.place(head);
self.stepper.next();
return true;
}
}
}
false
}
fn flush(&mut self) {
self.flush_chars();
while let Some(item) = self.stepper.head() {
self.writer.place(&item);
self.stepper.next();
}
}
}
#[allow(non_snake_case)]
impl<'a, S: Schema> DocMutator<S> for RecordingDocMutator<'a, S> {
fn Enter(&mut self) {
self.bc.place(Bytecode::Enter);
self.stepper.enter();
self.writer.begin();
}
fn Exit(&mut self) {
self.flush();
self.bc.place(Bytecode::Exit);
let attrs = self.stepper.exit_with_attrs();
self.writer.close(attrs);
}
fn AdvanceElements(&mut self, mut count: usize) {
if self.flush_chars() {
count -= 1;
}
for _ in 0..count {
self.bc.place(Bytecode::AdvanceElements(1));
self.writer.place(match self.stepper.head_raw() {
Some(head) => head,
_ => panic!("no head element"),
});
self.stepper.next();
}
}
fn delete(&mut self, count: usize) {
let mut bytecode_count = count;
if let Some(index) = self.stepper.char_index() {
if index > 0 {
bytecode_count -= 1;
}
}
if bytecode_count > 0 {
self.bc.place(Bytecode::DeleteElements(bytecode_count));
}
for _ in 0..count {
self.stepper.next();
}
}
fn DeleteElements(&mut self, count: usize) {
self.bc.place(Bytecode::DeleteElements(count));
for _ in 0..count {
self.stepper.next();
}
}
fn InsertDocString(&mut self, docstring: DocString, styles: S::CharsProperties) {
self.bc
.place(Bytecode::InsertDocString(docstring.clone(), json!(styles)));
self.writer.place(&DocText(styles, docstring));
}
fn UnwrapSelf(&mut self) {
self.flush();
self.bc.place(Bytecode::UnwrapSelf);
self.stepper.exit();
self.writer.unwrap_self();
}
fn WrapPrevious(&mut self, count: usize, attrs: S::GroupProperties) {
self.bc.place(Bytecode::WrapPrevious(count, json!(attrs)));
self.writer.wrap_previous(count, attrs);
}
fn skip(&mut self, count: usize) {
let last_index = self.stepper.head_index();
self.stepper.skip(count);
let new_index = self.stepper.head_index();
self.writer
.place_all(&self.stepper.current().1[last_index..new_index].to_owned());
if new_index - last_index > 0 {
self.AdvanceElements(new_index - last_index);
} else {
if let Some(ref cursor) = &self.stepper.char_cursor.clone() {
if let Some(left) = cursor.left() {
if left.char_len() == count {
self.bc.place(Bytecode::DeleteElements(1));
if let Some(&DocText(ref styles, _)) = self.stepper.head_raw() {
self.InsertDocString(left.clone(), styles.to_owned());
} else {
unreachable!();
}
return;
} else {
let mut text = left.clone();
unsafe {
text.seek_start_forward(count);
}
if let Some(&DocText(ref styles, _)) = self.stepper.head_raw() {
self.InsertDocString(text, styles.clone());
} else {
unreachable!();
}
}
}
}
}
}
}