ass-editor 0.1.1

High-performance, ergonomic editor layer for ASS subtitles
Documentation
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
//! Event system for document changes and editor notifications  
//!
//! Provides `DocumentEvent` enum for representing editor changes and
//! `EventChannel` for distributing events to observers. Supports both
//! synchronous and asynchronous event handling with filtering.

#[cfg(not(feature = "std"))]
extern crate alloc;

use crate::core::{Position, Range, Result};

#[cfg(feature = "std")]
use std::collections::HashMap;

#[cfg(not(feature = "std"))]
use alloc::collections::BTreeMap as HashMap;

#[cfg(not(feature = "std"))]
use alloc::{
    boxed::Box,
    format,
    string::{String, ToString},
    vec::Vec,
};

#[cfg(feature = "multi-thread")]
use std::sync::{Arc, RwLock};

#[cfg(not(feature = "multi-thread"))]
use core::cell::RefCell;

#[cfg(all(not(feature = "multi-thread"), not(feature = "std")))]
use alloc::rc::Rc;

#[cfg(all(not(feature = "multi-thread"), feature = "std"))]
use std::rc::Rc;

#[cfg(feature = "async")]
use futures::channel::mpsc;

/// Types of events that can occur in the editor
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DocumentEvent {
    /// Text was inserted at a position
    TextInserted {
        /// Position where text was inserted
        position: Position,
        /// Text that was inserted
        text: String,
        /// Length of inserted text in bytes
        length: usize,
    },

    /// Text was deleted from a range
    TextDeleted {
        /// Range where text was deleted
        range: Range,
        /// Text that was deleted (for undo purposes)
        deleted_text: String,
    },

    /// Text was replaced in a range
    TextReplaced {
        /// Range where text was replaced
        range: Range,
        /// Old text that was replaced
        old_text: String,
        /// New text that replaced the old text
        new_text: String,
    },

    /// Selection changed
    SelectionChanged {
        /// Previous selection range (if any)
        old_selection: Option<Range>,
        /// New selection range (if any)
        new_selection: Option<Range>,
    },

    /// Cursor position changed
    CursorMoved {
        /// Previous cursor position
        old_position: Position,
        /// New cursor position
        new_position: Position,
    },

    /// Document was saved to a file
    DocumentSaved {
        /// File path where document was saved
        file_path: String,
        /// Whether this was a "save as" operation
        save_as: bool,
    },

    /// Document was loaded from a file
    DocumentLoaded {
        /// File path from which document was loaded
        file_path: String,
        /// Size of loaded document in bytes
        size: usize,
    },

    /// Undo operation was performed
    UndoPerformed {
        /// Description of the undone action
        action_description: String,
        /// Number of changes undone
        changes_count: usize,
    },

    /// Redo operation was performed  
    RedoPerformed {
        /// Description of the redone action
        action_description: String,
        /// Number of changes redone
        changes_count: usize,
    },

    /// Validation completed with results
    ValidationCompleted {
        /// Number of issues found
        issues_count: usize,
        /// Number of errors found
        error_count: usize,
        /// Number of warnings found
        warning_count: usize,
        /// Time taken for validation in milliseconds
        validation_time_ms: u64,
    },

    /// Search operation completed
    SearchCompleted {
        /// Search pattern that was used
        pattern: String,
        /// Number of matches found
        matches_count: usize,
        /// Whether the search hit the result limit
        hit_limit: bool,
        /// Time taken for search in microseconds
        search_time_us: u64,
    },

    /// Document parsing completed
    ParsingCompleted {
        /// Whether parsing was successful
        success: bool,
        /// Number of sections parsed
        sections_count: usize,
        /// Time taken for parsing in milliseconds
        parse_time_ms: u64,
        /// Any error message if parsing failed
        error_message: Option<String>,
    },

    /// Extension was loaded or unloaded
    ExtensionChanged {
        /// Name of the extension
        extension_name: String,
        /// Whether the extension was loaded (true) or unloaded (false)
        loaded: bool,
    },

    /// Configuration setting changed
    ConfigChanged {
        /// Name of the configuration key
        key: String,
        /// Old value (if any)
        old_value: Option<String>,
        /// New value
        new_value: String,
    },

    /// Generic custom event for extensions
    CustomEvent {
        /// Event type identifier
        event_type: String,
        /// Event data as key-value pairs
        data: HashMap<String, String>,
    },
}

impl DocumentEvent {
    /// Get a human-readable description of the event
    pub fn description(&self) -> String {
        match self {
            Self::TextInserted { length, .. } => format!("Inserted {length} bytes of text"),
            Self::TextDeleted { range, .. } => format!("Deleted text from {range}"),
            Self::TextReplaced { range, .. } => format!("Replaced text in {range}"),
            Self::SelectionChanged { .. } => "Selection changed".to_string(),
            Self::CursorMoved { .. } => "Cursor moved".to_string(),
            Self::DocumentSaved { file_path, save_as } => {
                if *save_as {
                    format!("Saved document as '{file_path}'")
                } else {
                    format!("Saved document to '{file_path}'")
                }
            }
            Self::DocumentLoaded { file_path, size } => {
                format!("Loaded document '{file_path}' ({size} bytes)")
            }
            Self::UndoPerformed {
                action_description,
                changes_count,
            } => {
                format!("Undid '{action_description}' ({changes_count} changes)")
            }
            Self::RedoPerformed {
                action_description,
                changes_count,
            } => {
                format!("Redid '{action_description}' ({changes_count} changes)")
            }
            Self::ValidationCompleted {
                issues_count,
                validation_time_ms,
                ..
            } => {
                format!(
                    "Validation completed: {issues_count} issues found in {validation_time_ms}ms"
                )
            }
            Self::SearchCompleted {
                pattern,
                matches_count,
                search_time_us,
                ..
            } => {
                format!(
                    "Search for '{pattern}' found {matches_count} matches in {search_time_us}μs"
                )
            }
            Self::ParsingCompleted {
                success,
                sections_count,
                parse_time_ms,
                ..
            } => {
                if *success {
                    format!("Parsed {sections_count} sections in {parse_time_ms}ms")
                } else {
                    format!("Parsing failed after {parse_time_ms}ms")
                }
            }
            Self::ExtensionChanged {
                extension_name,
                loaded,
            } => {
                if *loaded {
                    format!("Loaded extension '{extension_name}'")
                } else {
                    format!("Unloaded extension '{extension_name}'")
                }
            }
            Self::ConfigChanged { key, .. } => format!("Configuration '{key}' changed"),
            Self::CustomEvent { event_type, .. } => format!("Custom event: {event_type}"),
        }
    }

    /// Check if this event represents a document modification
    pub fn is_modification(&self) -> bool {
        matches!(
            self,
            Self::TextInserted { .. }
                | Self::TextDeleted { .. }
                | Self::TextReplaced { .. }
                | Self::UndoPerformed { .. }
                | Self::RedoPerformed { .. }
        )
    }

    /// Check if this event affects the document's text content
    pub fn affects_text(&self) -> bool {
        matches!(
            self,
            Self::TextInserted { .. }
                | Self::TextDeleted { .. }
                | Self::TextReplaced { .. }
                | Self::UndoPerformed { .. }
                | Self::RedoPerformed { .. }
                | Self::DocumentLoaded { .. }
        )
    }

    /// Get the affected range for events that modify text
    pub fn affected_range(&self) -> Option<Range> {
        match self {
            Self::TextInserted {
                position, length, ..
            } => Some(Range::new(*position, position.advance(*length))),
            Self::TextDeleted { range, .. } | Self::TextReplaced { range, .. } => Some(*range),
            _ => None,
        }
    }
}

/// Event filter for selective event handling
#[derive(Debug, Clone)]
pub struct EventFilter {
    /// Event types to include (empty means all types)
    include_types: Vec<String>,
    /// Event types to exclude
    exclude_types: Vec<String>,
    /// Whether to include modification events
    include_modifications: Option<bool>,
    /// Whether to include cursor/selection events  
    include_cursor_events: Option<bool>,
}

impl EventFilter {
    /// Create a new event filter that accepts all events
    pub fn new() -> Self {
        Self {
            include_types: Vec::new(),
            exclude_types: Vec::new(),
            include_modifications: None,
            include_cursor_events: None,
        }
    }

    /// Only include specific event types
    pub fn include_types(mut self, types: Vec<String>) -> Self {
        self.include_types = types;
        self
    }

    /// Exclude specific event types
    pub fn exclude_types(mut self, types: Vec<String>) -> Self {
        self.exclude_types = types;
        self
    }

    /// Set whether to include modification events
    pub fn include_modifications(mut self, include: bool) -> Self {
        self.include_modifications = Some(include);
        self
    }

    /// Set whether to include cursor/selection events
    pub fn include_cursor_events(mut self, include: bool) -> Self {
        self.include_cursor_events = Some(include);
        self
    }

    /// Check if an event passes this filter
    pub fn matches(&self, event: &DocumentEvent) -> bool {
        let event_type = event.event_type_name();

        // Check exclude list first
        if self.exclude_types.contains(&event_type) {
            return false;
        }

        // Check include list if specified
        if !self.include_types.is_empty() && !self.include_types.contains(&event_type) {
            return false;
        }

        // Check modification filter
        if let Some(include_mods) = self.include_modifications {
            if event.is_modification() != include_mods {
                return false;
            }
        }

        // Check cursor event filter
        if let Some(include_cursor) = self.include_cursor_events {
            let is_cursor_event = matches!(
                event,
                DocumentEvent::CursorMoved { .. } | DocumentEvent::SelectionChanged { .. }
            );
            if is_cursor_event != include_cursor {
                return false;
            }
        }

        true
    }
}

impl Default for EventFilter {
    fn default() -> Self {
        Self::new()
    }
}

impl DocumentEvent {
    /// Get the event type as a string for filtering
    fn event_type_name(&self) -> String {
        match self {
            Self::TextInserted { .. } => "TextInserted".to_string(),
            Self::TextDeleted { .. } => "TextDeleted".to_string(),
            Self::TextReplaced { .. } => "TextReplaced".to_string(),
            Self::SelectionChanged { .. } => "SelectionChanged".to_string(),
            Self::CursorMoved { .. } => "CursorMoved".to_string(),
            Self::DocumentSaved { .. } => "DocumentSaved".to_string(),
            Self::DocumentLoaded { .. } => "DocumentLoaded".to_string(),
            Self::UndoPerformed { .. } => "UndoPerformed".to_string(),
            Self::RedoPerformed { .. } => "RedoPerformed".to_string(),
            Self::ValidationCompleted { .. } => "ValidationCompleted".to_string(),
            Self::SearchCompleted { .. } => "SearchCompleted".to_string(),
            Self::ParsingCompleted { .. } => "ParsingCompleted".to_string(),
            Self::ExtensionChanged { .. } => "ExtensionChanged".to_string(),
            Self::ConfigChanged { .. } => "ConfigChanged".to_string(),
            Self::CustomEvent { event_type, .. } => event_type.clone(),
        }
    }
}

/// Event handler trait for responding to document events
pub trait EventHandler: Send + Sync {
    /// Handle a document event
    fn handle_event(&mut self, event: &DocumentEvent) -> Result<()>;

    /// Get the event filter for this handler
    fn event_filter(&self) -> EventFilter {
        EventFilter::new()
    }

    /// Get handler priority (higher numbers = higher priority)
    fn priority(&self) -> i32 {
        0
    }
}

/// Statistics about event handling
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EventStats {
    /// Total number of events dispatched
    pub events_dispatched: usize,
    /// Number of handlers currently registered
    pub handlers_count: usize,
    /// Number of events dropped due to filters
    pub events_filtered: usize,
    /// Number of async events queued
    pub async_events_queued: usize,
    /// Average event processing time in microseconds
    pub avg_processing_time_us: u64,
}

/// Event channel configuration
#[derive(Debug, Clone)]
pub struct EventChannelConfig {
    /// Maximum number of handlers
    pub max_handlers: usize,
    /// Maximum size of async event queue
    pub max_async_queue_size: usize,
    /// Whether to enable event batching
    pub enable_batching: bool,
    /// Maximum batch size for event processing
    pub max_batch_size: usize,
    /// Whether to log events for debugging
    pub enable_logging: bool,
}

impl Default for EventChannelConfig {
    fn default() -> Self {
        Self {
            max_handlers: 100,
            max_async_queue_size: 1000,
            enable_batching: false,
            max_batch_size: 10,
            enable_logging: false,
        }
    }
}

/// Event channel for distributing document events to handlers
#[derive(Debug)]
pub struct EventChannel {
    /// Configuration for this channel
    config: EventChannelConfig,

    /// Registered event handlers
    #[cfg(feature = "multi-thread")]
    handlers: Arc<RwLock<Vec<HandlerInfo>>>,

    #[cfg(not(feature = "multi-thread"))]
    handlers: Rc<RefCell<Vec<HandlerInfo>>>,

    /// Event statistics
    stats: EventStats,

    /// Async event sender (if async feature is enabled)
    #[cfg(feature = "async")]
    async_sender: Option<mpsc::UnboundedSender<DocumentEvent>>,

    /// Next handler ID for unique identification
    next_handler_id: usize,
}

/// Information about a registered handler
struct HandlerInfo {
    /// Unique handler ID
    id: usize,
    /// Handler implementation
    handler: Box<dyn EventHandler>,
    /// Event filter for this handler
    filter: EventFilter,
    /// Handler priority
    priority: i32,
    /// Number of events processed by this handler
    events_processed: usize,
}

impl core::fmt::Debug for HandlerInfo {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("HandlerInfo")
            .field("id", &self.id)
            .field("filter", &self.filter)
            .field("priority", &self.priority)
            .field("events_processed", &self.events_processed)
            .field("handler", &"<EventHandler>")
            .finish()
    }
}

impl EventChannel {
    /// Create a new event channel with default configuration
    pub fn new() -> Self {
        Self::with_config(EventChannelConfig::default())
    }

    /// Create a new event channel with custom configuration
    pub fn with_config(config: EventChannelConfig) -> Self {
        Self {
            config,
            #[cfg(feature = "multi-thread")]
            handlers: Arc::new(RwLock::new(Vec::new())),
            #[cfg(not(feature = "multi-thread"))]
            handlers: Rc::new(RefCell::new(Vec::new())),
            stats: EventStats {
                events_dispatched: 0,
                handlers_count: 0,
                events_filtered: 0,
                async_events_queued: 0,
                avg_processing_time_us: 0,
            },
            #[cfg(feature = "async")]
            async_sender: None,
            next_handler_id: 0,
        }
    }

    /// Register an event handler
    pub fn register_handler(&mut self, handler: Box<dyn EventHandler>) -> Result<usize> {
        let handler_id = self.next_handler_id;
        self.next_handler_id += 1;

        let filter = handler.event_filter();
        let priority = handler.priority();

        let handler_info = HandlerInfo {
            id: handler_id,
            handler,
            filter,
            priority,
            events_processed: 0,
        };

        #[cfg(feature = "multi-thread")]
        {
            let mut handlers =
                self.handlers
                    .write()
                    .map_err(|_| crate::core::EditorError::ThreadSafetyError {
                        message: "Failed to acquire write lock for handlers".to_string(),
                    })?;

            if handlers.len() >= self.config.max_handlers {
                return Err(crate::core::EditorError::CommandFailed {
                    message: format!("Handler limit reached: {}", self.config.max_handlers),
                });
            }

            handlers.push(handler_info);
            // Sort by priority (highest first)
            handlers.sort_by(|a, b| b.priority.cmp(&a.priority));
        }

        #[cfg(not(feature = "multi-thread"))]
        {
            let mut handlers = self.handlers.borrow_mut();

            if handlers.len() >= self.config.max_handlers {
                return Err(crate::core::EditorError::CommandFailed {
                    message: format!("Handler limit reached: {}", self.config.max_handlers),
                });
            }

            handlers.push(handler_info);
            // Sort by priority (highest first)
            handlers.sort_by(|a, b| b.priority.cmp(&a.priority));
        }

        self.stats.handlers_count += 1;
        Ok(handler_id)
    }

    /// Unregister an event handler by ID
    pub fn unregister_handler(&mut self, handler_id: usize) -> Result<bool> {
        #[cfg(feature = "multi-thread")]
        {
            let mut handlers =
                self.handlers
                    .write()
                    .map_err(|_| crate::core::EditorError::ThreadSafetyError {
                        message: "Failed to acquire write lock for handlers".to_string(),
                    })?;

            if let Some(pos) = handlers.iter().position(|h| h.id == handler_id) {
                handlers.remove(pos);
                self.stats.handlers_count -= 1;
                Ok(true)
            } else {
                Ok(false)
            }
        }

        #[cfg(not(feature = "multi-thread"))]
        {
            let mut handlers = self.handlers.borrow_mut();

            if let Some(pos) = handlers.iter().position(|h| h.id == handler_id) {
                handlers.remove(pos);
                self.stats.handlers_count -= 1;
                Ok(true)
            } else {
                Ok(false)
            }
        }
    }

    /// Dispatch an event to all registered handlers
    pub fn dispatch(&mut self, event: DocumentEvent) -> Result<()> {
        #[cfg(feature = "std")]
        let start_time = std::time::Instant::now();

        self.stats.events_dispatched += 1;

        let mut filtered_count = 0;
        #[allow(unused_variables, unused_assignments)]
        let mut processed_count = 0;

        #[cfg(feature = "multi-thread")]
        {
            let mut handlers =
                self.handlers
                    .write()
                    .map_err(|_| crate::core::EditorError::ThreadSafetyError {
                        message: "Failed to acquire write lock for handlers".to_string(),
                    })?;

            for handler_info in handlers.iter_mut() {
                if handler_info.filter.matches(&event) {
                    handler_info.handler.handle_event(&event)?;
                    handler_info.events_processed += 1;
                    processed_count += 1;
                } else {
                    filtered_count += 1;
                }
            }
        }

        #[cfg(not(feature = "multi-thread"))]
        {
            let mut handlers = self.handlers.borrow_mut();

            for handler_info in handlers.iter_mut() {
                if handler_info.filter.matches(&event) {
                    handler_info.handler.handle_event(&event)?;
                    handler_info.events_processed += 1;
                    processed_count += 1;
                } else {
                    filtered_count += 1;
                }
            }
        }

        self.stats.events_filtered += filtered_count;

        // Update average processing time
        #[cfg(feature = "std")]
        {
            let processing_time = start_time.elapsed().as_micros() as u64;
            if self.stats.events_dispatched == 1 {
                self.stats.avg_processing_time_us = processing_time;
            } else {
                self.stats.avg_processing_time_us =
                    (self.stats.avg_processing_time_us + processing_time) / 2;
            }
        }

        if self.config.enable_logging {
            #[cfg(feature = "std")]
            eprintln!(
                "Event dispatched: {} -> {} handlers (filtered: {})",
                event.description(),
                processed_count,
                filtered_count
            );
        }

        Ok(())
    }

    /// Dispatch multiple events in a batch
    pub fn dispatch_batch(&mut self, events: Vec<DocumentEvent>) -> Result<()> {
        if self.config.enable_batching && events.len() <= self.config.max_batch_size {
            // Process as a batch
            for event in events {
                self.dispatch(event)?;
            }
        } else {
            // Process individually
            for event in events {
                self.dispatch(event)?;
            }
        }
        Ok(())
    }

    /// Get event statistics
    pub fn stats(&self) -> &EventStats {
        &self.stats
    }

    /// Clear all event handlers
    pub fn clear_handlers(&mut self) -> Result<()> {
        #[cfg(feature = "multi-thread")]
        {
            let mut handlers =
                self.handlers
                    .write()
                    .map_err(|_| crate::core::EditorError::ThreadSafetyError {
                        message: "Failed to acquire write lock for handlers".to_string(),
                    })?;
            handlers.clear();
        }

        #[cfg(not(feature = "multi-thread"))]
        {
            let mut handlers = self.handlers.borrow_mut();
            handlers.clear();
        }

        self.stats.handlers_count = 0;
        Ok(())
    }

    /// Setup async event processing (requires async feature)
    #[cfg(feature = "async")]
    pub fn setup_async(&mut self) -> mpsc::UnboundedReceiver<DocumentEvent> {
        let (sender, receiver) = mpsc::unbounded();
        self.async_sender = Some(sender);
        receiver
    }

    /// Dispatch event asynchronously (requires async feature)
    #[cfg(feature = "async")]
    pub fn dispatch_async(&mut self, event: DocumentEvent) -> Result<()> {
        if let Some(ref sender) = self.async_sender {
            sender
                .unbounded_send(event)
                .map_err(|_| crate::core::EditorError::CommandFailed {
                    message: "Failed to send async event".to_string(),
                })?;
            self.stats.async_events_queued += 1;
        } else {
            return Err(crate::core::EditorError::FeatureNotEnabled {
                feature: "async event processing".to_string(),
                required_feature: "async".to_string(),
            });
        }
        Ok(())
    }
}

impl Default for EventChannel {
    fn default() -> Self {
        Self::new()
    }
}

/// Convenience functions for creating common events
impl DocumentEvent {
    /// Create a text insertion event
    pub fn text_inserted(position: Position, text: String) -> Self {
        let length = text.len();
        Self::TextInserted {
            position,
            text,
            length,
        }
    }

    /// Create a text deletion event
    pub fn text_deleted(range: Range, deleted_text: String) -> Self {
        Self::TextDeleted {
            range,
            deleted_text,
        }
    }

    /// Create a text replacement event
    pub fn text_replaced(range: Range, old_text: String, new_text: String) -> Self {
        Self::TextReplaced {
            range,
            old_text,
            new_text,
        }
    }

    /// Create a cursor moved event
    pub fn cursor_moved(old_position: Position, new_position: Position) -> Self {
        Self::CursorMoved {
            old_position,
            new_position,
        }
    }

    /// Create a document saved event
    pub fn document_saved(file_path: String, save_as: bool) -> Self {
        Self::DocumentSaved { file_path, save_as }
    }

    /// Create a validation completed event
    pub fn validation_completed(
        issues_count: usize,
        error_count: usize,
        warning_count: usize,
        validation_time_ms: u64,
    ) -> Self {
        Self::ValidationCompleted {
            issues_count,
            error_count,
            warning_count,
            validation_time_ms,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[cfg(not(feature = "std"))]
    use alloc::{string::ToString, vec};

    #[test]
    fn document_event_creation() {
        let event = DocumentEvent::text_inserted(Position::new(0), "Hello".to_string());

        match event {
            DocumentEvent::TextInserted {
                position,
                text,
                length,
            } => {
                assert_eq!(position.offset, 0);
                assert_eq!(text, "Hello");
                assert_eq!(length, 5);
            }
            _ => panic!("Expected TextInserted event"),
        }
    }

    #[test]
    fn document_event_description() {
        let event = DocumentEvent::text_inserted(Position::new(0), "Hello".to_string());
        assert_eq!(event.description(), "Inserted 5 bytes of text");

        let event = DocumentEvent::cursor_moved(Position::new(0), Position::new(5));
        assert_eq!(event.description(), "Cursor moved");
    }

    #[test]
    fn document_event_modification_check() {
        let insert_event = DocumentEvent::text_inserted(Position::new(0), "Hello".to_string());
        assert!(insert_event.is_modification());

        let cursor_event = DocumentEvent::cursor_moved(Position::new(0), Position::new(5));
        assert!(!cursor_event.is_modification());
    }

    #[test]
    fn document_event_affects_text() {
        let insert_event = DocumentEvent::text_inserted(Position::new(0), "Hello".to_string());
        assert!(insert_event.affects_text());

        let config_event = DocumentEvent::ConfigChanged {
            key: "font_size".to_string(),
            old_value: Some("12".to_string()),
            new_value: "14".to_string(),
        };
        assert!(!config_event.affects_text());
    }

    #[test]
    fn document_event_affected_range() {
        let insert_event = DocumentEvent::text_inserted(Position::new(10), "Hello".to_string());
        let range = insert_event.affected_range().unwrap();
        assert_eq!(range.start.offset, 10);
        assert_eq!(range.end.offset, 15);

        let cursor_event = DocumentEvent::cursor_moved(Position::new(0), Position::new(5));
        assert!(cursor_event.affected_range().is_none());
    }

    #[test]
    fn event_filter_creation() {
        let filter = EventFilter::new()
            .include_modifications(true)
            .exclude_types(vec!["CursorMoved".to_string()]);

        let insert_event = DocumentEvent::text_inserted(Position::new(0), "Hello".to_string());
        assert!(filter.matches(&insert_event));

        let cursor_event = DocumentEvent::cursor_moved(Position::new(0), Position::new(5));
        assert!(!filter.matches(&cursor_event));
    }

    #[test]
    fn event_filter_include_types() {
        let filter = EventFilter::new()
            .include_types(vec!["TextInserted".to_string(), "TextDeleted".to_string()]);

        let insert_event = DocumentEvent::text_inserted(Position::new(0), "Hello".to_string());
        assert!(filter.matches(&insert_event));

        let cursor_event = DocumentEvent::cursor_moved(Position::new(0), Position::new(5));
        assert!(!filter.matches(&cursor_event));
    }

    #[test]
    fn event_channel_creation() {
        let channel = EventChannel::new();
        assert_eq!(channel.stats().handlers_count, 0);
        assert_eq!(channel.stats().events_dispatched, 0);
    }

    #[test]
    fn event_channel_config() {
        let config = EventChannelConfig {
            max_handlers: 50,
            enable_logging: true,
            ..Default::default()
        };

        let channel = EventChannel::with_config(config);
        assert_eq!(channel.config.max_handlers, 50);
        assert!(channel.config.enable_logging);
    }

    // Mock handler for testing
    struct TestHandler {
        events_received: Vec<String>,
        filter: EventFilter,
        priority: i32,
    }

    impl TestHandler {
        fn new() -> Self {
            Self {
                events_received: Vec::new(),
                filter: EventFilter::new(),
                priority: 0,
            }
        }

        fn with_filter(filter: EventFilter) -> Self {
            Self {
                events_received: Vec::new(),
                filter,
                priority: 0,
            }
        }

        fn with_priority(priority: i32) -> Self {
            Self {
                events_received: Vec::new(),
                filter: EventFilter::new(),
                priority,
            }
        }
    }

    impl EventHandler for TestHandler {
        fn handle_event(&mut self, event: &DocumentEvent) -> Result<()> {
            self.events_received.push(event.description());
            Ok(())
        }

        fn event_filter(&self) -> EventFilter {
            self.filter.clone()
        }

        fn priority(&self) -> i32 {
            self.priority
        }
    }

    #[test]
    fn event_channel_handler_registration() {
        let mut channel = EventChannel::new();
        let handler = Box::new(TestHandler::new());

        let handler_id = channel.register_handler(handler).unwrap();
        assert_eq!(channel.stats().handlers_count, 1);

        let removed = channel.unregister_handler(handler_id).unwrap();
        assert!(removed);
        assert_eq!(channel.stats().handlers_count, 0);
    }

    #[test]
    fn event_channel_dispatch() {
        let mut channel = EventChannel::new();
        let handler = Box::new(TestHandler::new());

        channel.register_handler(handler).unwrap();

        let event = DocumentEvent::text_inserted(Position::new(0), "Hello".to_string());
        channel.dispatch(event).unwrap();

        assert_eq!(channel.stats().events_dispatched, 1);
    }

    #[test]
    fn event_channel_filtering() {
        let mut channel = EventChannel::new();
        let filter = EventFilter::new().exclude_types(vec!["CursorMoved".to_string()]);
        let handler = Box::new(TestHandler::with_filter(filter));

        channel.register_handler(handler).unwrap();

        // This should be handled
        let insert_event = DocumentEvent::text_inserted(Position::new(0), "Hello".to_string());
        channel.dispatch(insert_event).unwrap();

        // This should be filtered out
        let cursor_event = DocumentEvent::cursor_moved(Position::new(0), Position::new(5));
        channel.dispatch(cursor_event).unwrap();

        assert_eq!(channel.stats().events_dispatched, 2);
        assert_eq!(channel.stats().events_filtered, 1);
    }

    #[test]
    fn event_channel_priority_ordering() {
        let mut channel = EventChannel::new();

        let low_priority_handler = Box::new(TestHandler::with_priority(1));
        let high_priority_handler = Box::new(TestHandler::with_priority(10));

        // Register in reverse priority order
        channel.register_handler(low_priority_handler).unwrap();
        channel.register_handler(high_priority_handler).unwrap();

        // Handlers should be sorted by priority internally
        assert_eq!(channel.stats().handlers_count, 2);
    }

    #[test]
    fn event_channel_batch_dispatch() {
        let mut channel = EventChannel::new();
        let handler = Box::new(TestHandler::new());

        channel.register_handler(handler).unwrap();

        let events = vec![
            DocumentEvent::text_inserted(Position::new(0), "Hello".to_string()),
            DocumentEvent::text_inserted(Position::new(5), " World".to_string()),
        ];

        channel.dispatch_batch(events).unwrap();
        assert_eq!(channel.stats().events_dispatched, 2);
    }

    #[test]
    fn event_channel_clear_handlers() {
        let mut channel = EventChannel::new();
        let handler = Box::new(TestHandler::new());

        channel.register_handler(handler).unwrap();
        assert_eq!(channel.stats().handlers_count, 1);

        channel.clear_handlers().unwrap();
        assert_eq!(channel.stats().handlers_count, 0);
    }
}