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
//! Undo-aware text editing operations
//!
//! Public `insert`, `delete`, and `replace` methods that run the matching
//! command, record the operation in the undo history, invalidate the
//! validation cache, and emit document events.
use super::EditorDocument;
use crate::core::errors::Result;
use crate::core::position::{Position, Range};
#[cfg(feature = "std")]
use crate::events::DocumentEvent;
#[cfg(not(feature = "std"))]
use alloc::string::ToString;
impl EditorDocument {
/// Insert text at position with undo support
///
/// Inserts text at the given position, automatically updating the underlying
/// text representation and recording the operation in the undo history.
///
/// # Examples
///
/// ```
/// use ass_editor::{EditorDocument, Position};
///
/// let mut doc = EditorDocument::from_content("Hello World").unwrap();
/// let pos = Position::new(5); // Insert after "Hello"
/// doc.insert(pos, " there").unwrap();
///
/// assert_eq!(doc.text(), "Hello there World");
///
/// // Can undo the operation
/// doc.undo().unwrap();
/// assert_eq!(doc.text(), "Hello World");
/// ```
///
/// # Errors
///
/// Returns `Err` if the position is beyond the document bounds.
pub fn insert(&mut self, pos: Position, text: &str) -> Result<()> {
use crate::commands::{EditorCommand, InsertTextCommand};
use crate::core::history::Operation;
let command = InsertTextCommand::new(pos, text.to_string());
let result = command.execute(self)?;
// Record the operation in history
let operation = Operation::Insert {
position: pos,
text: text.to_string(),
};
self.history
.record_operation(operation, command.description().to_string(), &result);
// Clear validation cache since content changed
self.validator.clear_cache();
// Emit event
#[cfg(feature = "std")]
self.emit(DocumentEvent::TextInserted {
position: pos,
text: text.to_string(),
length: text.len(),
});
Ok(())
}
/// Delete text in range with undo support
pub fn delete(&mut self, range: Range) -> Result<()> {
use crate::commands::{DeleteTextCommand, EditorCommand};
use crate::core::history::Operation;
// Capture the text that will be deleted BEFORE deletion
let deleted_text = self.text_range(range)?;
let command = DeleteTextCommand::new(range);
let result = command.execute(self)?;
// Record the operation in history
let operation = Operation::Delete {
range,
deleted_text: deleted_text.clone(),
};
self.history
.record_operation(operation, command.description().to_string(), &result);
// Clear validation cache since content changed
self.validator.clear_cache();
// Emit event
#[cfg(feature = "std")]
self.emit(DocumentEvent::TextDeleted {
range,
deleted_text,
});
Ok(())
}
/// Replace text in range with undo support
pub fn replace(&mut self, range: Range, text: &str) -> Result<()> {
use crate::commands::{EditorCommand, ReplaceTextCommand};
use crate::core::history::Operation;
// Capture the old text BEFORE replacement
let old_text = self.text_range(range)?;
let command = ReplaceTextCommand::new(range, text.to_string());
let result = command.execute(self)?;
// Record the operation in history
let operation = Operation::Replace {
range,
old_text: old_text.clone(),
new_text: text.to_string(),
};
self.history
.record_operation(operation, command.description().to_string(), &result);
// Clear validation cache since content changed
self.validator.clear_cache();
// Emit event
#[cfg(feature = "std")]
self.emit(DocumentEvent::TextReplaced {
range,
old_text,
new_text: text.to_string(),
});
Ok(())
}
}