Skip to main content

ass_editor/commands/delta_commands/
event_edit.rs

1//! ASS-aware event edit command with delta tracking
2
3use super::DeltaCommand;
4use crate::core::{EditorDocument, Result};
5
6#[cfg(not(feature = "std"))]
7use alloc::{format, string::String};
8
9/// ASS-aware event edit command with delta tracking
10#[derive(Debug, Clone)]
11pub struct IncrementalEventEditCommand {
12    pub old_text: String,
13    pub new_text: String,
14}
15
16impl IncrementalEventEditCommand {
17    /// Create a new incremental event edit command
18    pub fn new(old_text: String, new_text: String) -> Self {
19        Self { old_text, new_text }
20    }
21}
22
23impl DeltaCommand for IncrementalEventEditCommand {
24    fn execute_with_delta(&self, document: &mut EditorDocument) -> Result<super::CommandResult> {
25        #[cfg(feature = "stream")]
26        {
27            if self.supports_incremental() {
28                // Use ASS-aware incremental editing
29                match document.edit_event_incremental(&self.old_text, &self.new_text) {
30                    Ok(delta) => {
31                        let result = super::CommandResult {
32                            success: true,
33                            message: Some(format!(
34                                "Edited event: '{}' → '{}'",
35                                self.old_text, self.new_text
36                            )),
37                            modified_range: None, // Would need to track position
38                            new_cursor: None,
39                            content_changed: true,
40                            script_delta: Some(delta),
41                        };
42                        return Ok(result);
43                    }
44                    Err(_) => {
45                        // Fallback to regular event edit
46                    }
47                }
48            }
49        }
50
51        // Regular event edit
52        document.edit_event_text(&self.old_text, &self.new_text)?;
53        let result = super::CommandResult {
54            success: true,
55            message: Some(format!(
56                "Edited event: '{}' → '{}'",
57                self.old_text, self.new_text
58            )),
59            modified_range: None,
60            new_cursor: None,
61            content_changed: true,
62            #[cfg(feature = "stream")]
63            script_delta: None,
64        };
65        Ok(result)
66    }
67
68    fn description(&self) -> String {
69        format!("Edit event: '{}' → '{}'", self.old_text, self.new_text)
70    }
71}