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
//! The core `EditorCommand` trait implemented by all editor commands.
use crate;
use CommandResult;
/// Trait for editor commands that can be executed and undone
///
/// All commands implement this trait to provide a consistent interface
/// for execution, undo/redo, and introspection.
///
/// # Examples
///
/// Creating a custom command:
///
/// ```
/// use ass_editor::{EditorCommand, EditorDocument, CommandResult, Result, Position, Range};
///
/// #[derive(Debug)]
/// struct UppercaseCommand {
/// description: String,
/// }
///
/// impl UppercaseCommand {
/// fn new() -> Self {
/// Self {
/// description: "Convert to uppercase".to_string(),
/// }
/// }
/// }
///
/// impl EditorCommand for UppercaseCommand {
/// fn execute(&self, document: &mut EditorDocument) -> Result<CommandResult> {
/// let text = document.text().to_uppercase();
/// let range = Range::new(Position::new(0), Position::new(document.len()));
/// document.replace(range, &text)?;
/// Ok(CommandResult::success().with_message("Text converted to uppercase".to_string()))
/// }
///
/// fn description(&self) -> &str {
/// &self.description
/// }
/// }
/// ```