freya_hooks/
editor_history.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
use std::time::{
    Duration,
    Instant,
};

use ropey::Rope;

#[derive(Clone, Debug, PartialEq)]
pub enum HistoryChange {
    InsertChar {
        idx: usize,
        len: usize,
        ch: char,
    },
    InsertText {
        idx: usize,
        len: usize,
        text: String,
    },
    Remove {
        idx: usize,
        len: usize,
        text: String,
    },
}

#[derive(Clone, Debug, PartialEq)]
pub struct HistoryTransaction {
    pub timestamp: Instant,
    pub changes: Vec<HistoryChange>,
}

#[derive(Clone)]
pub struct EditorHistory {
    pub transactions: Vec<HistoryTransaction>,
    pub current_transaction: usize,
    // Incremental counter for every transaction.
    pub version: usize,
    /// After how many seconds since the last transaction a change should be grouped with the last transaction.
    transaction_treshold_groping: Duration,
}

impl EditorHistory {
    pub fn new(transaction_treshold_groping: Duration) -> Self {
        Self {
            transactions: Vec::default(),
            current_transaction: 0,
            version: 0,
            transaction_treshold_groping,
        }
    }

    pub fn push_change(&mut self, change: HistoryChange) {
        if self.can_redo() {
            self.transactions.drain(self.current_transaction..);
        }

        let last_transaction = self
            .transactions
            .get_mut(self.current_transaction.saturating_sub(1));
        if let Some(last_transaction) = last_transaction {
            if last_transaction.timestamp.elapsed() <= self.transaction_treshold_groping {
                last_transaction.changes.push(change);
                last_transaction.timestamp = Instant::now();
                return;
            }
        }

        self.transactions.push(HistoryTransaction {
            timestamp: Instant::now(),
            changes: vec![change],
        });

        self.current_transaction = self.transactions.len();
        self.version += 1;
    }

    pub fn current_change(&self) -> usize {
        self.current_transaction
    }

    pub fn any_pending_changes(&self) -> usize {
        self.transactions.len() - self.current_transaction
    }

    pub fn can_undo(&self) -> bool {
        self.current_transaction > 0
    }

    pub fn can_redo(&self) -> bool {
        self.current_transaction < self.transactions.len()
    }

    pub fn undo(&mut self, rope: &mut Rope) -> Option<usize> {
        if !self.can_undo() {
            return None;
        }

        let last_transaction = self.transactions.get(self.current_transaction - 1);
        if let Some(last_transaction) = last_transaction {
            let mut idx_end = None;
            for change in last_transaction.changes.iter().rev() {
                idx_end.replace(match change {
                    HistoryChange::Remove { idx, text, len } => {
                        let start = rope.utf16_cu_to_char(*idx);
                        rope.insert(start, text);
                        *idx + len
                    }
                    HistoryChange::InsertChar { idx, len, .. } => {
                        let start = rope.utf16_cu_to_char(*idx);
                        let end = rope.utf16_cu_to_char(*idx + len);
                        rope.remove(start..end);
                        *idx
                    }
                    HistoryChange::InsertText { idx, len, .. } => {
                        let start = rope.utf16_cu_to_char(*idx);
                        let end = rope.utf16_cu_to_char(*idx + len);
                        rope.remove(start..end);
                        *idx
                    }
                });
            }

            self.current_transaction -= 1;
            self.version += 1;
            idx_end
        } else {
            None
        }
    }

    pub fn redo(&mut self, rope: &mut Rope) -> Option<usize> {
        if !self.can_redo() {
            return None;
        }

        let last_transaction = self.transactions.get(self.current_transaction);
        if let Some(last_transaction) = last_transaction {
            let mut idx_end = None;
            for change in &last_transaction.changes {
                idx_end.replace(match change {
                    HistoryChange::Remove { idx, len, .. } => {
                        let start = rope.utf16_cu_to_char(*idx);
                        let end = rope.utf16_cu_to_char(*idx + len);
                        rope.remove(start..end);
                        *idx
                    }
                    HistoryChange::InsertChar { idx, ch, len } => {
                        let start = rope.utf16_cu_to_char(*idx);
                        rope.insert_char(start, *ch);
                        *idx + len
                    }
                    HistoryChange::InsertText { idx, text, len } => {
                        let start = rope.utf16_cu_to_char(*idx);
                        rope.insert(start, text);
                        *idx + len
                    }
                });
            }
            self.current_transaction += 1;
            self.version += 1;
            idx_end
        } else {
            None
        }
    }

    pub fn clear_redos(&mut self) {
        if self.can_redo() {
            self.transactions.drain(self.current_transaction..);
        }
    }

    pub fn clear(&mut self) {
        self.transactions.clear();
        self.current_transaction = 0;
        self.version = 0;
    }
}

#[cfg(test)]
mod test {
    use std::time::Duration;

    use ropey::Rope;

    use super::{
        EditorHistory,
        HistoryChange,
    };

    #[test]
    fn test() {
        let mut rope = Rope::new();
        let mut history = EditorHistory::new(Duration::ZERO);

        // Initial text
        rope.insert(0, "Hello World");

        assert!(!history.can_undo());
        assert!(!history.can_redo());

        // New change
        rope.insert(11, "\n!!!!");
        history.push_change(HistoryChange::InsertText {
            idx: 11,
            text: "\n!!!!".to_owned(),
            len: "\n!!!!".len(),
        });

        assert!(history.can_undo());
        assert!(!history.can_redo());
        assert_eq!(rope.to_string(), "Hello World\n!!!!");

        // Undo last change
        history.undo(&mut rope);

        assert!(!history.can_undo());
        assert!(history.can_redo());
        assert_eq!(rope.to_string(), "Hello World");

        // More changes
        rope.insert(11, "\n!!!!");
        history.push_change(HistoryChange::InsertText {
            idx: 11,
            text: "\n!!!!".to_owned(),
            len: "\n!!!!".len(),
        });
        rope.insert(16, "\n!!!!");
        history.push_change(HistoryChange::InsertText {
            idx: 16,
            text: "\n!!!!".to_owned(),
            len: "\n!!!!".len(),
        });
        rope.insert(21, "\n!!!!");
        history.push_change(HistoryChange::InsertText {
            idx: 21,
            text: "\n!!!!".to_owned(),
            len: "\n!!!!".len(),
        });

        assert_eq!(history.any_pending_changes(), 0);
        assert!(history.can_undo());
        assert!(!history.can_redo());
        assert_eq!(rope.to_string(), "Hello World\n!!!!\n!!!!\n!!!!");

        // Undo last changes
        history.undo(&mut rope);
        assert_eq!(history.any_pending_changes(), 1);
        assert_eq!(rope.to_string(), "Hello World\n!!!!\n!!!!");
        history.undo(&mut rope);
        assert_eq!(history.any_pending_changes(), 2);
        assert_eq!(rope.to_string(), "Hello World\n!!!!");
        history.undo(&mut rope);
        assert_eq!(history.any_pending_changes(), 3);
        assert_eq!(rope.to_string(), "Hello World");

        assert!(!history.can_undo());
        assert!(history.can_redo());

        // Redo last changes
        history.redo(&mut rope);
        assert_eq!(rope.to_string(), "Hello World\n!!!!");
        history.redo(&mut rope);
        assert_eq!(rope.to_string(), "Hello World\n!!!!\n!!!!");
        history.redo(&mut rope);
        assert_eq!(rope.to_string(), "Hello World\n!!!!\n!!!!\n!!!!");

        assert_eq!(history.any_pending_changes(), 0);
        assert!(history.can_undo());
        assert!(!history.can_redo());

        // Undo last change
        history.undo(&mut rope);
        assert_eq!(rope.to_string(), "Hello World\n!!!!\n!!!!");
        assert_eq!(history.any_pending_changes(), 1);
        history.undo(&mut rope);
        assert_eq!(rope.to_string(), "Hello World\n!!!!");
        assert_eq!(history.any_pending_changes(), 2);

        // Dischard any changes that could have been redone
        rope.insert_char(0, '.');
        history.push_change(HistoryChange::InsertChar {
            idx: 0,
            ch: '.',
            len: 1,
        });
        assert_eq!(history.any_pending_changes(), 0);
    }
}