rat_text/
undo_buffer.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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
//! Undo functionality.

use crate::range_map::expand_range_by;
use crate::TextPosition;
use dyn_clone::DynClone;
use std::fmt::Debug;
use std::mem;
use std::ops::Range;

/// Undo buffer.
///
/// Keeps up to undo_count operations that can be undone/redone.
/// Additionally, it can provide a change-log which can be used
/// to sync other text-widgets.
///
pub trait UndoBuffer: DynClone + Debug {
    /// How many undoes are stored?
    fn undo_count(&self) -> u32;

    /// How many undoes are stored?
    fn set_undo_count(&mut self, n: u32);

    /// Begin a sequence of changes that should be undone at once.
    ///
    /// begin/end calls can be nested, but only the outer one
    /// will define the actual scope of the undo.
    ///
    /// A call to begin_seq must be matched with a call to end_seq.
    fn begin_seq(&mut self);

    /// End a sequence of changes that should be undone at once.
    fn end_seq(&mut self);

    /// Appends a new operation at the current undo-position.
    ///
    /// Redoes will be truncated by this call.
    ///
    /// This call tries merge InsertChar/RemoveChar operations,
    /// if they lie next to each other. InsertStr/RemoveStr
    /// will never be merged.
    fn append(&mut self, undo: UndoOp);

    /// Appends a new operation but doesn't fill the replay-log.
    ///
    /// Used to add to the undo-buffer during replay from another
    /// text-widget.
    fn append_from_replay(&mut self, undo: UndoEntry);

    /// Clear the undo and the replay buffer.
    fn clear(&mut self);

    /// Get the list of the next undo operations.
    fn undo(&mut self) -> Vec<&UndoOp>;

    /// Get the list of the next redo operations.
    fn redo(&mut self) -> Vec<&UndoOp>;

    /// Enable/disable replay recording.
    ///
    /// __Attention__:
    /// For this to work the widget state must be in a 'cleared' state,
    /// or you must *create a clone* of the widget-state *immediately* after
    /// activating the replay-log.
    ///
    /// Only then the replay operations obtained by recent_replay()
    /// will make sense to the clone.
    ///
    /// __Info__:
    /// How you identify the widgets that should receive the replay-log
    /// and other distribution problems are in the domain of the user
    /// of this feature.
    fn enable_replay_log(&mut self, replay: bool);

    /// Is the replay-log active?
    fn has_replay_log(&self) -> bool;

    /// Get the replay-log to sync with another textarea.
    /// This empties the replay buffer.
    fn recent_replay_log(&mut self) -> Vec<UndoEntry>;

    /// Is there undo for setting/removing styles.
    fn undo_styles_enabled(&self) -> bool;
}

/// Stores one style change.
#[derive(Debug, Default, Clone)]
pub struct StyleChange {
    pub before: Range<usize>,
    pub after: Range<usize>,
    pub style: usize,
}

/// Stores a text position change.
#[derive(Debug, Default, Clone)]
pub struct TextPositionChange {
    pub before: TextPosition,
    pub after: TextPosition,
}

/// Storage for undo.
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct UndoEntry {
    pub sequence: u32,
    pub operation: UndoOp,
}

/// Storage for undo.
#[non_exhaustive]
#[derive(Debug, Clone)]
pub enum UndoOp {
    /// Insert a single char/grapheme.
    ///
    /// This can contain a longer text, if consecutive InsertChar have
    /// been merged.
    InsertChar {
        /// byte range for the insert.
        bytes: Range<usize>,
        /// cursor position change
        cursor: TextPositionChange,
        /// anchor position change
        anchor: TextPositionChange,
        /// inserted text
        txt: String,
    },
    /// Insert a longer text.
    InsertStr {
        /// byte range for the insert.
        bytes: Range<usize>,
        /// cursor position change
        cursor: TextPositionChange,
        /// anchor position change
        anchor: TextPositionChange,
        /// inserted text
        txt: String,
    },
    /// Remove a single char/grapheme range.
    ///
    /// This can be a longer range, if consecutive RemoveChar have been
    /// merged.
    ///
    /// styles contains only styles whose range __intersects__ the
    /// removed range. Styles that lie after the bytes-range will be
    /// shifted left.
    RemoveChar {
        /// byte range for the remove.
        bytes: Range<usize>,
        /// cursor position change
        cursor: TextPositionChange,
        /// anchor position change
        anchor: TextPositionChange,
        /// removed text
        txt: String,
        /// removed styles
        styles: Vec<StyleChange>,
    },
    /// Remove longer text range.
    ///
    /// styles contains only styles whose range __intersects__ the
    /// removed range. Styles that lie after the bytes-range will be
    /// shifted left.
    RemoveStr {
        /// byte range for the remove.
        bytes: Range<usize>,
        /// cursor position change
        cursor: TextPositionChange,
        /// anchor position change
        anchor: TextPositionChange,
        /// removed text
        txt: String,
        /// removed styles
        styles: Vec<StyleChange>,
    },

    /// Cursor/anchor changed.
    ///
    /// This will be merged with a cursor-change immediately before.
    /// And it will merge with both removes and inserts.
    Cursor {
        /// cursor position change
        cursor: TextPositionChange,
        /// anchor position change
        anchor: TextPositionChange,
    },

    /// Set of styles was replaced.
    SetStyles {
        /// old styles
        styles_before: Vec<(Range<usize>, usize)>,
        /// new styles
        styles_after: Vec<(Range<usize>, usize)>,
    },
    /// Add one style.
    AddStyle {
        /// style range
        range: Range<usize>,
        /// style
        style: usize,
    },
    /// Remove one style.
    RemoveStyle {
        /// style range
        range: Range<usize>,
        /// style
        style: usize,
    },

    /// For replay only. Complete content was replaced.
    SetText {
        /// New text
        txt: String,
    },
    /// For replay only. Undo one operation.
    Undo,
    /// For replay only. Redo one operation.
    Redo,
}

/// Standard implementation for undo.
#[derive(Debug, Clone)]
pub struct UndoVec {
    undo_styles: bool,
    track_replay: bool,
    undo_count: u32,

    begin: u8,
    sequence: u32,
    buf: Vec<UndoEntry>,
    replay: Vec<UndoEntry>,

    // undo/redo split
    idx: usize,
}

impl Default for UndoVec {
    fn default() -> Self {
        Self {
            undo_styles: false,
            track_replay: false,
            undo_count: 99,
            begin: 0,
            sequence: 0,
            buf: Vec::default(),
            replay: Vec::default(),
            idx: 0,
        }
    }
}

impl UndoVec {
    /// New undo.
    pub fn new(undo_count: u32) -> Self {
        Self {
            undo_count,
            ..Default::default()
        }
    }

    /// Enable undo for style changes.
    ///
    /// Usually not what you want.
    /// Unless you allow your users to set styles manually.
    /// If your styling is done by a parser, don't activate this.
    ///
    /// Changes to the range of styles and removal of styles
    /// caused by text edits *will* be undone anyway.
    ///
    /// Recording those operations for *replay* will not be affected
    /// by this setting.
    pub fn enable_undo_styles(&mut self, undo_styles: bool) {
        self.undo_styles = undo_styles;
    }

    /// Undo for styles are enabled.
    pub fn undo_styles(&self) -> bool {
        self.undo_styles
    }

    fn merge_undo(mut last: UndoOp, mut curr: UndoOp) -> (Option<UndoOp>, Option<UndoOp>) {
        match &mut curr {
            UndoOp::InsertChar {
                bytes: curr_bytes,
                cursor: curr_cursor,
                anchor: curr_anchor,
                txt: curr_txt,
            } => match &mut last {
                UndoOp::InsertChar {
                    bytes: last_bytes,
                    cursor: last_cursor,
                    anchor: last_anchor,
                    txt: last_txt,
                } => {
                    if last_bytes.end == curr_bytes.start {
                        let mut last_txt = mem::take(last_txt);
                        last_txt.push_str(curr_txt);
                        (
                            Some(UndoOp::InsertChar {
                                bytes: last_bytes.start..curr_bytes.end,
                                cursor: TextPositionChange {
                                    before: last_cursor.before,
                                    after: curr_cursor.after,
                                },
                                anchor: TextPositionChange {
                                    before: last_anchor.before,
                                    after: curr_anchor.after,
                                },
                                txt: last_txt,
                            }),
                            None,
                        )
                    } else {
                        (Some(last), Some(curr))
                    }
                }
                _ => (Some(last), Some(curr)),
            },
            UndoOp::RemoveChar {
                bytes: curr_bytes,
                cursor: curr_cursor,
                anchor: curr_anchor,
                txt: curr_txt,
                styles: curr_styles,
            } => match &mut last {
                UndoOp::RemoveChar {
                    bytes: last_bytes,
                    cursor: last_cursor,
                    anchor: last_anchor,
                    txt: last_txt,
                    styles: last_styles,
                } => {
                    if curr_bytes.end == last_bytes.start {
                        // backspace
                        let mut txt = mem::take(curr_txt);
                        txt.push_str(last_txt);

                        // merge into last_styles
                        let mut styles = mem::take(last_styles);
                        Self::merge_remove_style(last_bytes.clone(), &mut styles, curr_styles);

                        (
                            Some(UndoOp::RemoveChar {
                                bytes: curr_bytes.start..last_bytes.end,
                                cursor: TextPositionChange {
                                    before: last_cursor.before,
                                    after: curr_cursor.after,
                                },
                                anchor: TextPositionChange {
                                    before: last_anchor.before,
                                    after: curr_anchor.after,
                                },
                                txt,
                                styles,
                            }),
                            None,
                        )
                    } else if curr_bytes.start == last_bytes.start {
                        // delete
                        let mut txt = mem::take(last_txt);
                        txt.push_str(curr_txt);

                        let curr_byte_len = curr_bytes.end - curr_bytes.start;

                        // merge into last_styles
                        let mut styles = mem::take(last_styles);
                        Self::merge_remove_style(last_bytes.clone(), &mut styles, curr_styles);

                        (
                            Some(UndoOp::RemoveChar {
                                bytes: last_bytes.start..last_bytes.end + curr_byte_len,
                                cursor: TextPositionChange {
                                    before: last_cursor.before,
                                    after: curr_cursor.after,
                                },
                                anchor: TextPositionChange {
                                    before: last_anchor.before,
                                    after: curr_anchor.after,
                                },
                                txt,
                                styles,
                            }),
                            None,
                        )
                    } else {
                        (Some(last), Some(curr))
                    }
                }
                _ => (Some(last), Some(curr)),
            },

            UndoOp::Cursor {
                cursor: curr_cursor,
                anchor: curr_anchor,
            } => match &mut last {
                UndoOp::InsertChar {
                    bytes: last_bytes,
                    cursor: last_cursor,
                    anchor: last_anchor,
                    txt: last_txt,
                } => (
                    Some(UndoOp::InsertChar {
                        bytes: mem::take(last_bytes),
                        cursor: TextPositionChange {
                            before: last_cursor.before,
                            after: curr_cursor.after,
                        },
                        anchor: TextPositionChange {
                            before: last_anchor.before,
                            after: curr_anchor.after,
                        },
                        txt: mem::take(last_txt),
                    }),
                    None,
                ),
                UndoOp::InsertStr {
                    bytes: last_bytes,
                    cursor: last_cursor,
                    anchor: last_anchor,
                    txt: last_txt,
                } => (
                    Some(UndoOp::InsertStr {
                        bytes: mem::take(last_bytes),
                        cursor: TextPositionChange {
                            before: last_cursor.before,
                            after: curr_cursor.after,
                        },
                        anchor: TextPositionChange {
                            before: last_anchor.before,
                            after: curr_anchor.after,
                        },
                        txt: mem::take(last_txt),
                    }),
                    None,
                ),
                UndoOp::RemoveChar {
                    bytes: last_bytes,
                    cursor: last_cursor,
                    anchor: last_anchor,
                    txt: last_txt,
                    styles: last_styles,
                } => (
                    Some(UndoOp::RemoveChar {
                        bytes: mem::take(last_bytes),
                        cursor: TextPositionChange {
                            before: last_cursor.before,
                            after: curr_cursor.after,
                        },
                        anchor: TextPositionChange {
                            before: last_anchor.before,
                            after: curr_anchor.after,
                        },
                        txt: mem::take(last_txt),
                        styles: mem::take(last_styles),
                    }),
                    None,
                ),
                UndoOp::RemoveStr {
                    bytes: last_bytes,
                    cursor: last_cursor,
                    anchor: last_anchor,
                    txt: last_txt,
                    styles: last_styles,
                } => (
                    Some(UndoOp::RemoveChar {
                        bytes: mem::take(last_bytes),
                        cursor: TextPositionChange {
                            before: last_cursor.before,
                            after: curr_cursor.after,
                        },
                        anchor: TextPositionChange {
                            before: last_anchor.before,
                            after: curr_anchor.after,
                        },
                        txt: mem::take(last_txt),
                        styles: mem::take(last_styles),
                    }),
                    None,
                ),
                UndoOp::Cursor {
                    cursor: last_cursor,
                    anchor: last_anchor,
                } => (
                    Some(UndoOp::Cursor {
                        cursor: TextPositionChange {
                            before: last_cursor.before,
                            after: curr_cursor.after,
                        },
                        anchor: TextPositionChange {
                            before: last_anchor.before,
                            after: curr_anchor.after,
                        },
                    }),
                    None,
                ),
                _ => (Some(last), Some(curr)),
            },
            _ => (Some(last), Some(curr)),
        }
    }

    /// Merge styles from two deletes.
    fn merge_remove_style(
        last_range: Range<usize>,
        last: &mut Vec<StyleChange>,
        curr: &mut Vec<StyleChange>,
    ) {
        for i in (0..last.len()).rev() {
            for j in (0..curr.len()).rev() {
                if last[i].style == curr[j].style {
                    if last[i].after == curr[j].before {
                        last[i].after = curr[j].after.clone();
                        curr.remove(j);
                    }
                }
            }
        }

        // expand before and add
        for mut curr in curr.drain(..) {
            curr.before = expand_range_by(last_range.clone(), curr.before);
            last.push(curr);
        }
    }
}

impl UndoVec {
    fn filter(&self, undo: &UndoOp) -> bool {
        // only useful for tracking
        if matches!(undo, UndoOp::Undo | UndoOp::Redo | UndoOp::SetText { .. }) {
            return true;
        }

        // style changes may/may not be undone
        if !self.undo_styles {
            match &undo {
                UndoOp::SetStyles { .. } | UndoOp::AddStyle { .. } | UndoOp::RemoveStyle { .. } => {
                    return true;
                }
                _ => {}
            }
        }

        false
    }

    fn try_merge(&mut self, undo: UndoOp) -> Option<UndoOp> {
        if let Some(UndoEntry {
            sequence,
            operation: last,
        }) = self.buf.pop()
        {
            let (last, undo) = Self::merge_undo(last, undo);
            // re-add last if it survived merge
            if let Some(last) = last {
                self.buf.push(UndoEntry {
                    sequence,
                    operation: last,
                });
            }
            undo
        } else {
            Some(undo)
        }
    }

    fn trim_undo(&mut self) {
        // Dump redo.
        while self.idx < self.buf.len() {
            self.buf.pop();
        }

        // cap undo at capacity.
        // uses the sequence count instead of the size.
        let count_uniq = self
            .buf
            .iter()
            .fold((0, 0), |mut f, v| {
                if v.sequence != f.0 {
                    f.0 = v.sequence;
                    f.1 += 1;
                }
                f
            })
            .1;

        if count_uniq > self.undo_count as usize {
            // don't drop parts of current sequence at all.
            if self.buf[0].sequence != self.sequence {
                let drop_sequence = self.buf[0].sequence;
                loop {
                    if self.buf[0].sequence == drop_sequence {
                        self.buf.remove(0);
                    } else {
                        break;
                    }
                }
            }
        }
    }
}

impl UndoBuffer for UndoVec {
    fn undo_count(&self) -> u32 {
        self.undo_count
    }

    fn set_undo_count(&mut self, n: u32) {
        self.undo_count = n;
    }

    /// Begin a sequence of changes that should be undone at once.
    fn begin_seq(&mut self) {
        self.begin += 1;
        if self.begin == 1 {
            self.sequence += 1;
        }
    }

    /// End a sequence of changes.
    /// Unbalanced end_seq calls panic.
    fn end_seq(&mut self) {
        self.begin -= 1;
    }

    fn append(&mut self, undo: UndoOp) {
        let track_undo = if self.track_replay {
            Some(undo.clone())
        } else {
            None
        };

        // try merge
        let add_undo = if let Some(last) = self.buf.last() {
            // first begin starts a new sequence.
            // so this shouldn't cross that boundary.
            if last.sequence == self.sequence {
                self.try_merge(undo)
            } else {
                Some(undo)
            }
        } else {
            Some(undo)
        };

        // New separate undo.
        if add_undo.is_some() {
            // auto begin+end
            if self.begin == 0 {
                self.sequence += 1;
            }
        }

        // Store in tracking.
        // Sequence number is new if the merge failed, otherwise the
        // same as the last. This fact will be used when replaying.
        if let Some(track_undo) = track_undo {
            self.replay.push(UndoEntry {
                sequence: self.sequence,
                operation: track_undo,
            });
        }

        // Add if not merged.
        if let Some(add_undo) = add_undo {
            // Not everything is undo.
            if self.filter(&add_undo) {
                return;
            }
            // Drop redo and trim undo.
            self.trim_undo();

            // add new undo if it survived merge
            self.buf.push(UndoEntry {
                sequence: self.sequence,
                operation: add_undo,
            });

            self.idx = self.buf.len();
        }
    }

    fn append_from_replay(&mut self, undo: UndoEntry) {
        let UndoEntry {
            sequence,
            operation: undo,
        } = undo;

        // try merge
        let add_undo = if let Some(last) = self.buf.last() {
            // merges act just like sequences, so this
            // works out for both.
            if last.sequence == sequence {
                self.try_merge(undo)
            } else {
                Some(undo)
            }
        } else {
            Some(undo)
        };

        // sync sequence
        self.sequence = sequence;

        // Add if not merged.
        if let Some(add_undo) = add_undo {
            // Not everything is undo.
            if self.filter(&add_undo) {
                return;
            }
            // Drop redo and trim undo.
            self.trim_undo();

            // add new undo if it survived merge
            self.buf.push(UndoEntry {
                sequence,
                operation: add_undo,
            });

            self.idx = self.buf.len();
        }
    }

    fn clear(&mut self) {
        self.buf.clear();
        self.idx = 0;
        self.begin = 0;
        self.sequence = 0;
        self.replay.clear();
    }

    /// Get next undo
    fn undo(&mut self) -> Vec<&UndoOp> {
        if self.idx > 0 {
            let sequence = self.buf[self.idx - 1].sequence;
            let mut undo = Vec::new();
            loop {
                if self.buf[self.idx - 1].sequence == sequence {
                    undo.push(&self.buf[self.idx - 1].operation);
                    self.idx -= 1;
                } else {
                    break;
                }
                if self.idx == 0 {
                    break;
                }
            }
            undo
        } else {
            Vec::default()
        }
    }

    /// Get next redo
    fn redo(&mut self) -> Vec<&UndoOp> {
        if self.idx < self.buf.len() {
            let sequence = self.buf[self.idx].sequence;
            let mut redo = Vec::new();
            loop {
                if self.buf[self.idx].sequence == sequence {
                    redo.push(&self.buf[self.idx].operation);
                    self.idx += 1;
                } else {
                    break;
                }
                if self.idx == self.buf.len() {
                    break;
                }
            }
            redo
        } else {
            Vec::default()
        }
    }

    /// Enable replay functionality.
    ///
    /// This keeps track of all changes to a textarea.
    /// These changes can be copied to another textarea with
    /// the replay() function.
    fn enable_replay_log(&mut self, replay: bool) {
        if self.track_replay != replay {
            self.replay.clear();
        }
        self.track_replay = replay;
    }

    fn has_replay_log(&self) -> bool {
        self.track_replay
    }

    /// Get all new replay entries.
    fn recent_replay_log(&mut self) -> Vec<UndoEntry> {
        mem::take(&mut self.replay)
    }

    fn undo_styles_enabled(&self) -> bool {
        self.undo_styles
    }
}