text-document-common 1.4.0

Shared entities, database, events, and undo/redo infrastructure for text-document
Documentation
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
// Generated by Qleany v1.5.1 from undo_redo_tests.tera
#![cfg(test)]
#![allow(dead_code)]
#![allow(unused_imports)]

use anyhow::Result;
use std::any::Any;
use std::sync::{Arc, Mutex};
use text_document_common::undo_redo::{CompositeCommand, UndoRedoCommand, UndoRedoManager};

// A simple test command that increments or decrements a counter
struct TestCommand {
    counter: Arc<Mutex<i32>>,
    value: i32,
}

impl TestCommand {
    fn new(counter: Arc<Mutex<i32>>, value: i32) -> Self {
        TestCommand { counter, value }
    }
}

impl UndoRedoCommand for TestCommand {
    fn undo(&mut self) -> Result<()> {
        let mut counter = self.counter.lock().unwrap();
        *counter -= self.value;
        Ok(())
    }

    fn redo(&mut self) -> Result<()> {
        let mut counter = self.counter.lock().unwrap();
        *counter += self.value;
        Ok(())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

// A mergeable test command that can be combined with other commands of the same type
struct MergeableCommand {
    counter: Arc<Mutex<i32>>,
    value: i32,
}

impl MergeableCommand {
    fn new(counter: Arc<Mutex<i32>>, value: i32) -> Self {
        MergeableCommand { counter, value }
    }
}

impl UndoRedoCommand for MergeableCommand {
    fn undo(&mut self) -> Result<()> {
        let mut counter = self.counter.lock().unwrap();
        *counter -= self.value;
        Ok(())
    }

    fn redo(&mut self) -> Result<()> {
        let mut counter = self.counter.lock().unwrap();
        *counter += self.value;
        Ok(())
    }

    fn can_merge(&self, other: &dyn UndoRedoCommand) -> bool {
        other.as_any().downcast_ref::<Self>().is_some()
    }

    fn merge(&mut self, other: &dyn UndoRedoCommand) -> bool {
        if let Some(other_cmd) = other.as_any().downcast_ref::<Self>() {
            self.value += other_cmd.value;
            return true;
        }
        false
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

#[test]
fn test_new() {
    let manager = UndoRedoManager::new();
    assert!(!manager.can_undo(None));
    assert!(!manager.can_redo(None));
}

#[test]
fn test_add_command() {
    let counter = Arc::new(Mutex::new(0));
    let mut manager = UndoRedoManager::new();

    // Add a command
    manager.add_command(Box::new(TestCommand::new(counter.clone(), 5)));

    assert!(manager.can_undo(None));
    assert!(!manager.can_redo(None));
    assert_eq!(*counter.lock().unwrap(), 0); // Command should not be executed automatically
}

#[test]
fn test_undo() {
    let counter = Arc::new(Mutex::new(0));
    let mut manager = UndoRedoManager::new();

    // Add a command and execute it manually
    {
        let mut cmd = TestCommand::new(counter.clone(), 5);
        cmd.redo().unwrap();
        manager.add_command(Box::new(cmd));
    }

    assert_eq!(*counter.lock().unwrap(), 5);

    // Undo the command
    manager.undo(None).unwrap();

    assert_eq!(*counter.lock().unwrap(), 0);
    assert!(!manager.can_undo(None));
    assert!(manager.can_redo(None));
}

#[test]
fn test_redo() {
    let counter = Arc::new(Mutex::new(0));
    let mut manager = UndoRedoManager::new();

    // Add a command and execute it manually
    {
        let mut cmd = TestCommand::new(counter.clone(), 5);
        cmd.redo().unwrap();
        manager.add_command(Box::new(cmd));
    }

    // Undo and then redo
    manager.undo(None).unwrap();
    assert_eq!(*counter.lock().unwrap(), 0);

    manager.redo(None).unwrap();
    assert_eq!(*counter.lock().unwrap(), 5);

    assert!(manager.can_undo(None));
    assert!(!manager.can_redo(None));
}

#[test]
fn test_undo_redo_multiple() {
    let counter = Arc::new(Mutex::new(0));
    let mut manager = UndoRedoManager::new();

    // Add multiple commands
    {
        let mut cmd1 = TestCommand::new(counter.clone(), 5);
        cmd1.redo().unwrap();
        manager.add_command(Box::new(cmd1));

        let mut cmd2 = TestCommand::new(counter.clone(), 3);
        cmd2.redo().unwrap();
        manager.add_command(Box::new(cmd2));
    }

    assert_eq!(*counter.lock().unwrap(), 8);

    // Undo both commands
    manager.undo(None).unwrap(); // Undo cmd2
    assert_eq!(*counter.lock().unwrap(), 5);

    manager.undo(None).unwrap(); // Undo cmd1
    assert_eq!(*counter.lock().unwrap(), 0);

    // Redo both commands
    manager.redo(None).unwrap(); // Redo cmd1
    assert_eq!(*counter.lock().unwrap(), 5);

    manager.redo(None).unwrap(); // Redo cmd2
    assert_eq!(*counter.lock().unwrap(), 8);
}

#[test]
fn test_composite_command() {
    let counter = Arc::new(Mutex::new(0));
    let mut manager = UndoRedoManager::new();

    // Create a composite command
    manager.begin_composite(None);

    // Add commands to the composite
    {
        let mut cmd1 = TestCommand::new(counter.clone(), 5);
        cmd1.redo().unwrap();
        manager.add_command(Box::new(cmd1));

        let mut cmd2 = TestCommand::new(counter.clone(), 3);
        cmd2.redo().unwrap();
        manager.add_command(Box::new(cmd2));
    }

    manager.end_composite();

    assert_eq!(*counter.lock().unwrap(), 8);

    // Undo the composite (should undo both commands at once)
    manager.undo(None).unwrap();
    assert_eq!(*counter.lock().unwrap(), 0);

    // Redo the composite (should redo both commands at once)
    manager.redo(None).unwrap();
    assert_eq!(*counter.lock().unwrap(), 8);
}

#[test]
fn test_empty_composite() {
    let mut manager = UndoRedoManager::new();

    // Create an empty composite command
    manager.begin_composite(None);
    manager.end_composite();

    // Nothing should be added to the undo stack
    assert!(!manager.can_undo(None));
}

#[test]
fn test_nested_composite() {
    let counter = Arc::new(Mutex::new(0));
    let mut manager = UndoRedoManager::new();

    // Start outer composite
    manager.begin_composite(None);

    // Add a command
    {
        let mut cmd1 = TestCommand::new(counter.clone(), 5);
        cmd1.redo().unwrap();
        manager.add_command(Box::new(cmd1));
    }

    // Start inner composite (should be ignored and continue with outer composite)
    manager.begin_composite(None);

    // Add another command
    {
        let mut cmd2 = TestCommand::new(counter.clone(), 3);
        cmd2.redo().unwrap();
        manager.add_command(Box::new(cmd2));
    }

    // End inner composite (should have no effect)
    manager.end_composite();

    // Add a third command
    {
        let mut cmd3 = TestCommand::new(counter.clone(), 2);
        cmd3.redo().unwrap();
        manager.add_command(Box::new(cmd3));
    }

    // End outer composite
    manager.end_composite();

    assert_eq!(*counter.lock().unwrap(), 10);

    // Undo the composite (should undo all three commands)
    manager.undo(None).unwrap();
    assert_eq!(*counter.lock().unwrap(), 0);
}

#[test]
fn test_command_merging() {
    let counter = Arc::new(Mutex::new(0));
    let mut manager = UndoRedoManager::new();

    // Add a mergeable command
    {
        let mut cmd1 = MergeableCommand::new(counter.clone(), 5);
        cmd1.redo().unwrap();
        manager.add_command(Box::new(cmd1));
    }

    // Add another mergeable command that should be merged with the first
    {
        let mut cmd2 = MergeableCommand::new(counter.clone(), 3);
        cmd2.redo().unwrap();
        manager.add_command(Box::new(cmd2));
    }

    assert_eq!(*counter.lock().unwrap(), 8);

    // Undo should undo both commands at once since they were merged
    manager.undo(None).unwrap();
    assert_eq!(*counter.lock().unwrap(), 0);

    // Redo should redo both commands at once
    manager.redo(None).unwrap();
    assert_eq!(*counter.lock().unwrap(), 8);
}

#[test]
fn test_redo_stack_cleared_on_new_command() {
    let counter = Arc::new(Mutex::new(0));
    let mut manager = UndoRedoManager::new();

    // Add and execute a command
    {
        let mut cmd1 = TestCommand::new(counter.clone(), 5);
        cmd1.redo().unwrap();
        manager.add_command(Box::new(cmd1));
    }

    // Undo the command
    manager.undo(None).unwrap();
    assert!(manager.can_redo(None));

    // Add a new command
    {
        let mut cmd2 = TestCommand::new(counter.clone(), 3);
        cmd2.redo().unwrap();
        manager.add_command(Box::new(cmd2));
    }

    // Redo stack should be cleared
    assert!(!manager.can_redo(None));
    assert_eq!(*counter.lock().unwrap(), 3);
}

#[test]
fn test_undo_with_empty_stack() {
    let mut manager = UndoRedoManager::new();

    // Undo with empty stack should not fail
    let result = manager.undo(None);
    assert!(result.is_ok());
}

#[test]
fn test_redo_with_empty_stack() {
    let mut manager = UndoRedoManager::new();

    // Redo with empty stack should not fail
    let result = manager.redo(None);
    assert!(result.is_ok());
}

#[test]
fn test_clear() {
    let counter = Arc::new(Mutex::new(0));
    let mut manager = UndoRedoManager::new();

    // Add and execute commands
    {
        let mut cmd1 = TestCommand::new(counter.clone(), 5);
        cmd1.redo().unwrap();
        manager.add_command(Box::new(cmd1));

        let mut cmd2 = TestCommand::new(counter.clone(), 3);
        cmd2.redo().unwrap();
        manager.add_command(Box::new(cmd2));
    }

    assert_eq!(*counter.lock().unwrap(), 8);
    assert!(manager.can_undo(None));
    assert!(!manager.can_redo(None));

    // Undo one command to populate the redo stack
    manager.undo(None).unwrap();
    assert_eq!(*counter.lock().unwrap(), 5);
    assert!(manager.can_undo(None));
    assert!(manager.can_redo(None));

    // Clear the manager
    manager.clear_all_stacks();

    // Verify both stacks are empty
    assert!(!manager.can_undo(None));
    assert!(!manager.can_redo(None));

    // Verify we can add new commands after clearing
    {
        let mut cmd = TestCommand::new(counter.clone(), 10);
        cmd.redo().unwrap();
        manager.add_command(Box::new(cmd));
    }

    assert!(manager.can_undo(None));
    assert!(!manager.can_redo(None));

    // Verify undo works after clearing
    manager.undo(None).unwrap();
    assert_eq!(*counter.lock().unwrap(), 5); // Back to the value before the new command
    assert!(!manager.can_undo(None));
    assert!(manager.can_redo(None));
}

#[test]
fn test_clear_with_in_progress_composite() {
    let counter = Arc::new(Mutex::new(0));
    let mut manager = UndoRedoManager::new();

    // Begin a composite command
    manager.begin_composite(None);

    // Add commands to the composite
    {
        let mut cmd1 = TestCommand::new(counter.clone(), 5);
        cmd1.redo().unwrap();
        manager.add_command(Box::new(cmd1));

        let mut cmd2 = TestCommand::new(counter.clone(), 3);
        cmd2.redo().unwrap();
        manager.add_command(Box::new(cmd2));
    }

    assert_eq!(*counter.lock().unwrap(), 8);

    // Clear the manager without ending the composite
    manager.clear_all_stacks();

    // Verify both stacks are empty
    assert!(!manager.can_undo(None));
    assert!(!manager.can_redo(None));

    // Verify the composite state is reset by adding a command normally
    {
        let mut cmd = TestCommand::new(counter.clone(), 10);
        cmd.redo().unwrap();
        manager.add_command(Box::new(cmd));
    }

    // Command should be added directly to the undo stack, not to a composite
    assert!(manager.can_undo(None));

    // Verify undo works normally after clearing
    manager.undo(None).unwrap();
    assert_eq!(*counter.lock().unwrap(), 8); // Back to the value before the new command
    assert!(!manager.can_undo(None));
    assert!(manager.can_redo(None));
}