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
use super::doc::*;

pub(crate) fn can_element_join<S: Schema>(left: &DocElement<S>, right: &DocElement<S>) -> bool {
    match (left, right) {
        (&DocText(ref _prefix, ref prefix_styles), &DocText(ref _suffix, ref suffix_styles)) => {
            if prefix_styles == suffix_styles {
                return true;
            }
        }
        _ => {}
    }
    false
}

pub(crate) fn try_element_join<S: Schema>(left: &mut DocElement<S>, right: &DocElement<S>) -> bool {
    match (left, right) {
        (
            &mut DocText(ref prefix_styles, ref mut prefix),
            &DocText(ref suffix_styles, ref suffix),
        ) => {
            if prefix_styles == suffix_styles {
                prefix.push_str(suffix.as_str());
                return true;
            }
        }
        _ => {}
    }
    false
}

pub trait DocPlaceable<S: Schema> {
    fn skip_len(&self) -> usize;
    fn place_all(&mut self, all: &[DocElement<S>]);
    fn place(&mut self, value: &DocElement<S>);
}

impl<S: Schema> DocPlaceable<S> for DocSpan<S> {
    fn place(&mut self, elem: &DocElement<S>) {
        match *elem {
            DocText(ref _styles, ref text) => {
                assert!(text.char_len() > 0);

                // If the most recent element is text, we may want to just
                // append our text to it to cut down on new elements.
                if let Some(element) = self.last_mut() {
                    if try_element_join(element, elem) {
                        return;
                    }
                }

                // Otherwise, push the new entry
                self.push(elem.clone());
            }
            DocGroup(..) => {
                self.push(elem.clone());
            }
        }
    }

    fn place_all(&mut self, all: &[DocElement<S>]) {
        for i in all {
            self.place(i);
        }
    }

    fn skip_len(&self) -> usize {
        let mut ret = 0;
        for item in self {
            ret += match *item {
                DocText(_, ref value) => value.char_len(),
                DocGroup(..) => 1,
            };
        }
        ret
    }
}

pub trait DelPlaceable<S: Schema> {
    fn place_all(&mut self, all: &[DelElement<S>]);
    fn place(&mut self, value: &DelElement<S>);
    fn skip_pre_len(&self) -> usize;
    fn skip_post_len(&self) -> usize;

    /// Optimization for depth-first code to recursively return skips up
    /// the walker.
    fn is_continuous_skip(&self) -> bool;
}

impl<S: Schema> DelPlaceable<S> for DelSpan<S> {
    fn place_all(&mut self, all: &[DelElement<S>]) {
        for i in all {
            self.place(i);
        }
    }

    fn place(&mut self, elem: &DelElement<S>) {
        match *elem {
            DelText(count) => {
                assert!(count > 0);
                if let Some(&mut DelText(ref mut value)) = self.last_mut() {
                    *value += count;
                } else {
                    self.push(DelText(count));
                }
            }
            DelStyles(count, ref styles) => {
                assert!(count > 0);
                if let Some(&mut DelStyles(ref mut prefix_count, ref prefix_styles)) =
                    self.last_mut()
                {
                    if prefix_styles == styles {
                        *prefix_count += count;
                        return;
                    }
                }

                self.push(DelStyles(count, styles.to_owned()));
            }
            DelSkip(count) => {
                assert!(count > 0);
                if let Some(&mut DelSkip(ref mut value)) = self.last_mut() {
                    *value += count;
                } else {
                    self.push(DelSkip(count));
                }
            }
            DelGroup(..) | DelWithGroup(..) => {
                self.push(elem.clone());
            } // DelGroupAll | DelObject => {
              //     unimplemented!();
              // }
              // DelMany(count) => {
              //     unimplemented!();
              // }
        }
    }

    fn skip_pre_len(&self) -> usize {
        let mut ret = 0;
        for item in self {
            ret += match *item {
                DelSkip(len) | DelText(len) | DelStyles(len, _) => len,
                DelGroup(..) | DelWithGroup(..) => 1,
                // DelMany(len) => len,
                // DelObject | DelGroupAll  => 1,
            };
        }
        ret
    }

    fn skip_post_len(&self) -> usize {
        let mut ret = 0;
        for item in self {
            ret += match *item {
                DelSkip(len) | DelStyles(len, _) => len,
                DelText(..) => 0,
                DelWithGroup(..) => 1,
                DelGroup(ref span) => span.skip_post_len(),
                // DelObject | DelMany(..) | DelGroupAll => 0,
            };
        }
        ret
    }

    fn is_continuous_skip(&self) -> bool {
        if self.len() > 1 {
            // Will never be a continuous skip
            false
        } else if self.is_empty() {
            // is []
            true
        } else if let DelSkip(_) = self[0] {
            // is [DelSkip(n)]
            true
        } else {
            // is [DelSomething(n)]
            false
        }
    }
}

pub trait AddPlaceable<S: Schema> {
    fn place_all(&mut self, all: &[AddElement<S>]);
    fn place(&mut self, value: &AddElement<S>);
    fn skip_pre_len(&self) -> usize;
    fn skip_post_len(&self) -> usize;

    /// Optimization for depth-first code to recursively return skips up
    /// the walker.
    fn is_continuous_skip(&self) -> bool;
}

impl<S: Schema> AddPlaceable<S> for AddSpan<S> {
    fn place_all(&mut self, all: &[AddElement<S>]) {
        for i in all {
            self.place(i);
        }
    }

    fn place(&mut self, elem: &AddElement<S>) {
        match *elem {
            AddText(ref styles, ref text) => {
                assert!(text.char_len() > 0);

                // If the most recent element is text, we may want to just
                // append our text to it to cut down on new elements.
                if let Some(&mut AddText(ref prefix_styles, ref mut prefix)) = self.last_mut() {
                    // Check if they're equal and we can push it directly.
                    if styles == prefix_styles {
                        prefix.push_str(text.as_str());
                        return;
                    }
                }

                // Otherwise, push the new entry
                self.push(AddText(styles.clone(), text.to_owned()));
            }
            AddStyles(count, ref styles) => {
                assert!(count > 0);
                if let Some(&mut AddStyles(ref mut prefix_count, ref prefix_styles)) =
                    self.last_mut()
                {
                    if prefix_styles == styles {
                        *prefix_count += count;
                        return;
                    }
                }

                self.push(AddStyles(count, styles.to_owned()));
            }
            AddSkip(count) => {
                assert!(count > 0);
                if let Some(&mut AddSkip(ref mut value)) = self.last_mut() {
                    *value += count;
                } else {
                    self.push(AddSkip(count));
                }
            }
            AddGroup(..) | AddWithGroup(..) => {
                self.push(elem.clone());
            }
        }
    }

    fn skip_pre_len(&self) -> usize {
        let mut ret = 0;
        for item in self {
            ret += match *item {
                AddSkip(len) | AddStyles(len, _) => len,
                AddText(ref _chars, _) => 0,
                AddGroup(_, ref span) => span.skip_pre_len(),
                AddWithGroup(..) => 1,
            };
        }
        ret
    }

    fn skip_post_len(&self) -> usize {
        let mut ret = 0;
        for item in self {
            ret += match *item {
                AddSkip(len) | AddStyles(len, _) => len,
                AddText(_, ref chars) => chars.char_len(),
                AddGroup(..) | AddWithGroup(..) => 1,
            };
        }
        ret
    }

    fn is_continuous_skip(&self) -> bool {
        if self.len() > 1 {
            // Will never be a continuous skip
            false
        } else if self.is_empty() {
            // is []
            true
        } else if let AddSkip(_) = self[0] {
            // is [DelSkip(n)]
            true
        } else {
            // is [DelSomething(n)]
            false
        }
    }
}

pub trait CurPlaceable {
    fn place_all(&mut self, all: &[CurElement]);
    fn place(&mut self, value: &CurElement);
}

impl CurPlaceable for CurSpan {
    fn place_all(&mut self, all: &[CurElement]) {
        for i in all {
            self.place(i);
        }
    }

    fn place(&mut self, elem: &CurElement) {
        match *elem {
            CurSkip(count) => {
                assert!(count > 0);
                if let Some(&mut CurSkip(ref mut value)) = self.last_mut() {
                    *value += count;
                } else {
                    self.push(CurSkip(count));
                }
            }
            CurGroup | CurChar | CurWithGroup(..) => {
                self.push(elem.clone());
            }
        }
    }
}