Skip to main content

ass_editor/events/
event_builders.rs

1//! Convenience constructors for common [`DocumentEvent`] variants.
2//!
3//! Factory helpers that build text, cursor, save, and validation events with
4//! derived fields (e.g. inserted text length) computed automatically.
5
6use super::DocumentEvent;
7use crate::core::{Position, Range};
8
9#[cfg(not(feature = "std"))]
10use alloc::string::String;
11
12/// Convenience functions for creating common events
13impl DocumentEvent {
14    /// Create a text insertion event
15    pub fn text_inserted(position: Position, text: String) -> Self {
16        let length = text.len();
17        Self::TextInserted {
18            position,
19            text,
20            length,
21        }
22    }
23
24    /// Create a text deletion event
25    pub fn text_deleted(range: Range, deleted_text: String) -> Self {
26        Self::TextDeleted {
27            range,
28            deleted_text,
29        }
30    }
31
32    /// Create a text replacement event
33    pub fn text_replaced(range: Range, old_text: String, new_text: String) -> Self {
34        Self::TextReplaced {
35            range,
36            old_text,
37            new_text,
38        }
39    }
40
41    /// Create a cursor moved event
42    pub fn cursor_moved(old_position: Position, new_position: Position) -> Self {
43        Self::CursorMoved {
44            old_position,
45            new_position,
46        }
47    }
48
49    /// Create a document saved event
50    pub fn document_saved(file_path: String, save_as: bool) -> Self {
51        Self::DocumentSaved { file_path, save_as }
52    }
53
54    /// Create a validation completed event
55    pub fn validation_completed(
56        issues_count: usize,
57        error_count: usize,
58        warning_count: usize,
59        validation_time_ms: u64,
60    ) -> Self {
61        Self::ValidationCompleted {
62            issues_count,
63            error_count,
64            warning_count,
65            validation_time_ms,
66        }
67    }
68}