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
//! Batch command that runs multiple commands as a single atomic operation.
use crate::core::{EditorDocument, Position, Range, Result};
use super::{CommandResult, EditorCommand};
#[cfg(not(feature = "std"))]
use alloc::{boxed::Box, format, string::String, vec::Vec};
/// Batch command that executes multiple commands as a single atomic operation
#[derive(Debug)]
pub struct BatchCommand {
/// Commands to execute in order
pub commands: Vec<Box<dyn EditorCommand>>,
/// Description of the batch operation
pub description: String,
}
impl BatchCommand {
/// Create a new batch command
///
/// # Examples
///
/// ```
/// use ass_editor::{BatchCommand, InsertTextCommand, DeleteTextCommand, Position, Range, EditorDocument, EditorCommand};
///
/// let mut doc = EditorDocument::from_content("Hello World").unwrap();
///
/// let batch = BatchCommand::new("Multiple operations".to_string())
/// .add_command(Box::new(InsertTextCommand::new(Position::new(5), " beautiful".to_string())))
/// .add_command(Box::new(DeleteTextCommand::new(Range::new(Position::new(15), Position::new(21)))));
///
/// let result = batch.execute(&mut doc).unwrap();
/// assert!(result.success);
/// assert_eq!(doc.text(), "Hello beautiful");
/// ```
pub fn new(description: String) -> Self {
Self {
commands: Vec::new(),
description,
}
}
/// Add a command to the batch
pub fn add_command(mut self, command: Box<dyn EditorCommand>) -> Self {
self.commands.push(command);
self
}
/// Add multiple commands to the batch
pub fn add_commands(mut self, commands: Vec<Box<dyn EditorCommand>>) -> Self {
self.commands.extend(commands);
self
}
}
impl EditorCommand for BatchCommand {
fn execute(&self, document: &mut EditorDocument) -> Result<CommandResult> {
let mut overall_result = CommandResult::success();
let mut first_range: Option<Range> = None;
let mut last_cursor: Option<Position> = None;
for command in &self.commands {
let result = command.execute(document)?;
if !result.success {
return Ok(CommandResult::failure(format!(
"Batch command failed at: {}",
command.description()
)));
}
// Track the overall range of changes
if let Some(range) = result.modified_range {
first_range = Some(match first_range {
Some(existing) => existing.union(&range),
None => range,
});
}
if result.new_cursor.is_some() {
last_cursor = result.new_cursor;
}
if result.content_changed {
overall_result.content_changed = true;
}
}
overall_result.modified_range = first_range;
overall_result.new_cursor = last_cursor;
Ok(overall_result)
}
fn description(&self) -> &str {
&self.description
}
fn memory_usage(&self) -> usize {
core::mem::size_of::<Self>()
+ self.description.len()
+ self
.commands
.iter()
.map(|c| c.memory_usage())
.sum::<usize>()
}
}