Skip to main content

fresh/app/
undo_actions.rs

1//! Undo and redo action handlers.
2
3use super::Editor;
4use rust_i18n::t;
5
6impl Editor {
7    /// Handle Undo action - revert the last edit operation.
8    pub fn handle_undo(&mut self) {
9        if self.is_editing_disabled() {
10            self.set_status_message(t!("buffer.editing_disabled").to_string());
11            return;
12        }
13
14        let event_log = self.active_event_log_mut();
15        let before_idx = event_log.current_index();
16        let can_undo = event_log.can_undo();
17        let events = event_log.undo();
18        let after_idx = self.active_event_log().current_index();
19
20        tracing::debug!(
21            "Undo: before_idx={}, after_idx={}, can_undo={}, events_count={}",
22            before_idx,
23            after_idx,
24            can_undo,
25            events.len()
26        );
27
28        // Apply all inverse events collected during undo
29        for event in &events {
30            tracing::debug!("Undo applying event: {:?}", event);
31            self.apply_event_to_active_buffer(event);
32        }
33
34        // Update modified status based on event log position
35        self.update_modified_from_event_log();
36    }
37
38    /// Handle Redo action - reapply an undone edit operation.
39    pub fn handle_redo(&mut self) {
40        if self.is_editing_disabled() {
41            self.set_status_message(t!("buffer.editing_disabled").to_string());
42            return;
43        }
44
45        let events = self.active_event_log_mut().redo();
46
47        // Apply all events collected during redo
48        for event in events {
49            self.apply_event_to_active_buffer(&event);
50        }
51
52        // Update modified status based on event log position
53        self.update_modified_from_event_log();
54    }
55}