Skip to main content

ass_editor/events/
event_query.rs

1//! Query and inspection methods for [`DocumentEvent`].
2//!
3//! Provides human-readable descriptions, modification/text-effect predicates,
4//! affected-range computation, and the event type name used by filtering.
5
6use super::DocumentEvent;
7use crate::core::Range;
8
9#[cfg(not(feature = "std"))]
10use alloc::{
11    format,
12    string::{String, ToString},
13};
14
15impl DocumentEvent {
16    /// Get a human-readable description of the event
17    pub fn description(&self) -> String {
18        match self {
19            Self::TextInserted { length, .. } => format!("Inserted {length} bytes of text"),
20            Self::TextDeleted { range, .. } => format!("Deleted text from {range}"),
21            Self::TextReplaced { range, .. } => format!("Replaced text in {range}"),
22            Self::SelectionChanged { .. } => "Selection changed".to_string(),
23            Self::CursorMoved { .. } => "Cursor moved".to_string(),
24            Self::DocumentSaved { file_path, save_as } => {
25                if *save_as {
26                    format!("Saved document as '{file_path}'")
27                } else {
28                    format!("Saved document to '{file_path}'")
29                }
30            }
31            Self::DocumentLoaded { file_path, size } => {
32                format!("Loaded document '{file_path}' ({size} bytes)")
33            }
34            Self::UndoPerformed {
35                action_description,
36                changes_count,
37            } => {
38                format!("Undid '{action_description}' ({changes_count} changes)")
39            }
40            Self::RedoPerformed {
41                action_description,
42                changes_count,
43            } => {
44                format!("Redid '{action_description}' ({changes_count} changes)")
45            }
46            Self::ValidationCompleted {
47                issues_count,
48                validation_time_ms,
49                ..
50            } => {
51                format!(
52                    "Validation completed: {issues_count} issues found in {validation_time_ms}ms"
53                )
54            }
55            Self::SearchCompleted {
56                pattern,
57                matches_count,
58                search_time_us,
59                ..
60            } => {
61                format!(
62                    "Search for '{pattern}' found {matches_count} matches in {search_time_us}μs"
63                )
64            }
65            Self::ParsingCompleted {
66                success,
67                sections_count,
68                parse_time_ms,
69                ..
70            } => {
71                if *success {
72                    format!("Parsed {sections_count} sections in {parse_time_ms}ms")
73                } else {
74                    format!("Parsing failed after {parse_time_ms}ms")
75                }
76            }
77            Self::ExtensionChanged {
78                extension_name,
79                loaded,
80            } => {
81                if *loaded {
82                    format!("Loaded extension '{extension_name}'")
83                } else {
84                    format!("Unloaded extension '{extension_name}'")
85                }
86            }
87            Self::ConfigChanged { key, .. } => format!("Configuration '{key}' changed"),
88            Self::CustomEvent { event_type, .. } => format!("Custom event: {event_type}"),
89        }
90    }
91
92    /// Check if this event represents a document modification
93    pub fn is_modification(&self) -> bool {
94        matches!(
95            self,
96            Self::TextInserted { .. }
97                | Self::TextDeleted { .. }
98                | Self::TextReplaced { .. }
99                | Self::UndoPerformed { .. }
100                | Self::RedoPerformed { .. }
101        )
102    }
103
104    /// Check if this event affects the document's text content
105    pub fn affects_text(&self) -> bool {
106        matches!(
107            self,
108            Self::TextInserted { .. }
109                | Self::TextDeleted { .. }
110                | Self::TextReplaced { .. }
111                | Self::UndoPerformed { .. }
112                | Self::RedoPerformed { .. }
113                | Self::DocumentLoaded { .. }
114        )
115    }
116
117    /// Get the affected range for events that modify text
118    pub fn affected_range(&self) -> Option<Range> {
119        match self {
120            Self::TextInserted {
121                position, length, ..
122            } => Some(Range::new(*position, position.advance(*length))),
123            Self::TextDeleted { range, .. } | Self::TextReplaced { range, .. } => Some(*range),
124            _ => None,
125        }
126    }
127}
128
129impl DocumentEvent {
130    /// Get the event type as a string for filtering
131    pub(super) fn event_type_name(&self) -> String {
132        match self {
133            Self::TextInserted { .. } => "TextInserted".to_string(),
134            Self::TextDeleted { .. } => "TextDeleted".to_string(),
135            Self::TextReplaced { .. } => "TextReplaced".to_string(),
136            Self::SelectionChanged { .. } => "SelectionChanged".to_string(),
137            Self::CursorMoved { .. } => "CursorMoved".to_string(),
138            Self::DocumentSaved { .. } => "DocumentSaved".to_string(),
139            Self::DocumentLoaded { .. } => "DocumentLoaded".to_string(),
140            Self::UndoPerformed { .. } => "UndoPerformed".to_string(),
141            Self::RedoPerformed { .. } => "RedoPerformed".to_string(),
142            Self::ValidationCompleted { .. } => "ValidationCompleted".to_string(),
143            Self::SearchCompleted { .. } => "SearchCompleted".to_string(),
144            Self::ParsingCompleted { .. } => "ParsingCompleted".to_string(),
145            Self::ExtensionChanged { .. } => "ExtensionChanged".to_string(),
146            Self::ConfigChanged { .. } => "ConfigChanged".to_string(),
147            Self::CustomEvent { event_type, .. } => event_type.clone(),
148        }
149    }
150}