enya-plugin 0.1.5

Plugin system for Enya editor
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
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
//! Plugin registry for managing plugin lifecycle.

use rustc_hash::FxHashMap;

use crate::hooks::{
    CommandHook, CommandHookResult, KeyCombo, KeyEvent, KeyboardHook, KeyboardHookResult,
    LifecycleHook, PaneHook, ThemeHook,
};
use crate::theme::ThemeDefinition;
use crate::traits::{CommandConfig, KeybindingConfig, PaneConfig, Plugin, PluginCapabilities};
use crate::types::{PluginContext, Theme};
use crate::{PluginError, PluginResult};

/// Unique identifier for a registered plugin.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct PluginId(usize);

impl PluginId {
    /// Get the inner numeric value.
    pub fn value(&self) -> usize {
        self.0
    }
}

/// Runtime state of a plugin.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PluginState {
    /// Plugin is registered but not initialized
    Registered,
    /// Plugin is initialized but not active
    Inactive,
    /// Plugin is active and running
    Active,
    /// Plugin failed to initialize or activate
    Failed,
    /// Plugin is disabled by user
    Disabled,
}

/// Information about a registered plugin.
#[derive(Debug, Clone)]
pub struct PluginInfo {
    /// Plugin identifier
    pub id: PluginId,
    /// Plugin name
    pub name: String,
    /// Plugin version
    pub version: String,
    /// Plugin description
    pub description: String,
    /// Plugin capabilities
    pub capabilities: PluginCapabilities,
    /// Current state
    pub state: PluginState,
    /// Whether the plugin is enabled by default
    pub enabled_by_default: bool,
}

/// Registry entry for a plugin.
struct PluginEntry {
    /// The plugin instance
    plugin: Box<dyn Plugin>,
    /// Plugin metadata
    info: PluginInfo,
    /// Commands provided by the plugin
    commands: Vec<CommandConfig>,
    /// Pane types provided by the plugin
    pane_types: Vec<PaneConfig>,
    /// Keybindings provided by the plugin
    keybindings: Vec<KeybindingConfig>,
    /// Lifecycle hooks
    lifecycle_hook: Option<Box<dyn LifecycleHook>>,
    /// Command hooks
    command_hook: Option<Box<dyn CommandHook>>,
    /// Keyboard hooks
    keyboard_hook: Option<Box<dyn KeyboardHook>>,
    /// Theme hooks
    theme_hook: Option<Box<dyn ThemeHook>>,
    /// Pane hooks
    pane_hook: Option<Box<dyn PaneHook>>,
}

/// Central registry for managing plugins.
///
/// The registry handles plugin lifecycle:
/// - Registration: Adding plugins to the system
/// - Initialization: Setting up plugins with context
/// - Activation/Deactivation: Enabling/disabling plugins
/// - Hook dispatch: Routing events to interested plugins
pub struct PluginRegistry {
    /// Registered plugins by ID
    plugins: FxHashMap<PluginId, PluginEntry>,
    /// Plugin name to ID mapping
    name_to_id: FxHashMap<String, PluginId>,
    /// Command name/alias to plugin ID mapping for O(1) command lookup
    command_to_plugin: FxHashMap<String, PluginId>,
    /// Next plugin ID
    next_id: usize,
    /// Plugin context (shared with all plugins)
    context: Option<PluginContext>,
    /// Plugins enabled by user configuration
    enabled_plugins: FxHashMap<String, bool>,
}

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

impl PluginRegistry {
    /// Create a new empty plugin registry.
    pub fn new() -> Self {
        Self {
            plugins: FxHashMap::default(),
            name_to_id: FxHashMap::default(),
            command_to_plugin: FxHashMap::default(),
            next_id: 0,
            context: None,
            enabled_plugins: FxHashMap::default(),
        }
    }

    /// Initialize the registry with the plugin context.
    pub fn init(&mut self, context: PluginContext) {
        self.context = Some(context);
    }

    /// Get a reference to the plugin context.
    pub fn context(&self) -> Option<&PluginContext> {
        self.context.as_ref()
    }

    /// Set the enabled state for a plugin by name.
    pub fn set_plugin_enabled(&mut self, name: &str, enabled: bool) {
        self.enabled_plugins.insert(name.to_string(), enabled);
    }

    /// Check if a plugin is enabled.
    pub fn is_plugin_enabled(&self, name: &str) -> bool {
        self.enabled_plugins.get(name).copied().unwrap_or(true)
    }

    /// Register a plugin with the registry.
    ///
    /// This does not initialize or activate the plugin - call `init_plugin`
    /// and `activate_plugin` separately.
    pub fn register<P: Plugin + 'static>(
        &mut self,
        plugin: P,
        enabled_by_default: bool,
    ) -> PluginResult<PluginId> {
        let name = plugin.name().to_string();

        if self.name_to_id.contains_key(&name) {
            return Err(PluginError::AlreadyRegistered(name));
        }

        let id = PluginId(self.next_id);
        self.next_id += 1;

        let info = PluginInfo {
            id,
            name: name.clone(),
            version: plugin.version().to_string(),
            description: plugin.description().to_string(),
            capabilities: plugin.capabilities(),
            state: PluginState::Registered,
            enabled_by_default,
        };

        let entry = PluginEntry {
            plugin: Box::new(plugin),
            info,
            commands: vec![],
            pane_types: vec![],
            keybindings: vec![],
            lifecycle_hook: None,
            command_hook: None,
            keyboard_hook: None,
            theme_hook: None,
            pane_hook: None,
        };

        self.plugins.insert(id, entry);
        self.name_to_id.insert(name, id);

        Ok(id)
    }

    /// Initialize a registered plugin.
    pub fn init_plugin(&mut self, id: PluginId) -> PluginResult<()> {
        let ctx = self
            .context
            .as_ref()
            .ok_or_else(|| PluginError::OperationFailed("Registry not initialized".to_string()))?;

        let entry = self
            .plugins
            .get_mut(&id)
            .ok_or_else(|| PluginError::NotFound(format!("Plugin ID {}", id.0)))?;

        if entry.info.state != PluginState::Registered {
            return Ok(()); // Already initialized
        }

        // Check minimum editor version
        if let Some(min_version) = entry.plugin.min_editor_version() {
            let current = ctx.editor_version();
            if !Self::check_version(current, min_version) {
                entry.info.state = PluginState::Failed;
                return Err(PluginError::IncompatibleVersion {
                    required: min_version.to_string(),
                    actual: current.to_string(),
                });
            }
        }

        // Initialize the plugin
        if let Err(e) = entry.plugin.init(ctx) {
            entry.info.state = PluginState::Failed;
            return Err(e);
        }

        // Collect plugin-provided items
        entry.commands = entry.plugin.commands();
        entry.pane_types = entry.plugin.pane_types();
        entry.keybindings = entry.plugin.keybindings();

        // Collect hooks
        entry.lifecycle_hook = entry.plugin.lifecycle_hooks();
        entry.command_hook = entry.plugin.command_hooks();
        entry.keyboard_hook = entry.plugin.keyboard_hooks();
        entry.theme_hook = entry.plugin.theme_hooks();
        entry.pane_hook = entry.plugin.pane_hooks();

        entry.info.state = PluginState::Inactive;
        Ok(())
    }

    /// Activate a plugin (must be initialized first).
    pub fn activate_plugin(&mut self, id: PluginId) -> PluginResult<()> {
        let ctx = self
            .context
            .as_ref()
            .ok_or_else(|| PluginError::OperationFailed("Registry not initialized".to_string()))?;

        let entry = self
            .plugins
            .get_mut(&id)
            .ok_or_else(|| PluginError::NotFound(format!("Plugin ID {}", id.0)))?;

        if entry.info.state != PluginState::Inactive {
            return Ok(()); // Already active or in wrong state
        }

        // Check if user has disabled this plugin
        if !self
            .enabled_plugins
            .get(&entry.info.name)
            .copied()
            .unwrap_or(entry.info.enabled_by_default)
        {
            entry.info.state = PluginState::Disabled;
            return Ok(());
        }

        if let Err(e) = entry.plugin.activate(ctx) {
            entry.info.state = PluginState::Failed;
            return Err(e);
        }

        entry.info.state = PluginState::Active;

        // Index commands for O(1) lookup
        for cmd in &entry.commands {
            self.command_to_plugin.insert(cmd.name.clone(), id);
            for alias in &cmd.aliases {
                self.command_to_plugin.insert(alias.clone(), id);
            }
        }

        Ok(())
    }

    /// Deactivate a plugin.
    pub fn deactivate_plugin(&mut self, id: PluginId) -> PluginResult<()> {
        let ctx = self
            .context
            .as_ref()
            .ok_or_else(|| PluginError::OperationFailed("Registry not initialized".to_string()))?;

        let entry = self
            .plugins
            .get_mut(&id)
            .ok_or_else(|| PluginError::NotFound(format!("Plugin ID {}", id.0)))?;

        if entry.info.state != PluginState::Active {
            return Ok(()); // Not active
        }

        // Remove commands from index
        for cmd in &entry.commands {
            self.command_to_plugin.remove(&cmd.name);
            for alias in &cmd.aliases {
                self.command_to_plugin.remove(alias);
            }
        }

        if let Err(e) = entry.plugin.deactivate(ctx) {
            log::warn!("Plugin {} deactivation error: {e}", entry.info.name);
        }

        entry.info.state = PluginState::Inactive;
        Ok(())
    }

    /// Unregister a plugin completely, removing it from the registry.
    ///
    /// This deactivates the plugin first if it's active, then removes all
    /// references to it from the registry. Use this for hot-reload scenarios
    /// where you need to replace a plugin with a new version.
    pub fn unregister_plugin(&mut self, id: PluginId) -> PluginResult<()> {
        // First deactivate if active
        let _ = self.deactivate_plugin(id);

        // Get the entry to clean up mappings
        let entry = self
            .plugins
            .remove(&id)
            .ok_or_else(|| PluginError::NotFound(format!("Plugin ID {}", id.0)))?;

        // Remove name mapping
        self.name_to_id.remove(&entry.info.name);

        // Remove commands from index (in case deactivate didn't run)
        for cmd in &entry.commands {
            self.command_to_plugin.remove(&cmd.name);
            for alias in &cmd.aliases {
                self.command_to_plugin.remove(alias);
            }
        }

        log::info!("Unregistered plugin: {}", entry.info.name);
        Ok(())
    }

    /// Unregister a plugin by name.
    pub fn unregister_plugin_by_name(&mut self, name: &str) -> PluginResult<()> {
        let id = self
            .name_to_id
            .get(name)
            .copied()
            .ok_or_else(|| PluginError::NotFound(format!("Plugin '{name}'")))?;
        self.unregister_plugin(id)
    }

    /// Get a plugin by ID.
    pub fn get(&self, id: PluginId) -> Option<&dyn Plugin> {
        self.plugins.get(&id).map(|e| e.plugin.as_ref())
    }

    /// Get a mutable plugin by ID.
    pub fn get_mut(&mut self, id: PluginId) -> Option<&mut dyn Plugin> {
        self.plugins.get_mut(&id).map(|e| e.plugin.as_mut())
    }

    /// Get a plugin by name.
    pub fn get_by_name(&self, name: &str) -> Option<&dyn Plugin> {
        self.name_to_id
            .get(name)
            .and_then(|id| self.plugins.get(id))
            .map(|e| e.plugin.as_ref())
    }

    /// Get plugin info by ID.
    pub fn info(&self, id: PluginId) -> Option<&PluginInfo> {
        self.plugins.get(&id).map(|e| &e.info)
    }

    /// Get plugin info by name.
    pub fn info_by_name(&self, name: &str) -> Option<&PluginInfo> {
        self.name_to_id
            .get(name)
            .and_then(|id| self.plugins.get(id))
            .map(|e| &e.info)
    }

    /// List all registered plugins.
    pub fn list_plugins(&self) -> Vec<&PluginInfo> {
        self.plugins.values().map(|e| &e.info).collect()
    }

    /// List active plugins.
    pub fn active_plugins(&self) -> Vec<&PluginInfo> {
        self.plugins
            .values()
            .filter(|e| e.info.state == PluginState::Active)
            .map(|e| &e.info)
            .collect()
    }

    /// Get all commands from active plugins.
    pub fn all_commands(&self) -> Vec<(&PluginInfo, &CommandConfig)> {
        self.plugins
            .values()
            .filter(|e| e.info.state == PluginState::Active)
            .flat_map(|e| e.commands.iter().map(move |c| (&e.info, c)))
            .collect()
    }

    /// Get all pane types from active plugins.
    pub fn all_pane_types(&self) -> Vec<(&PluginInfo, &PaneConfig)> {
        self.plugins
            .values()
            .filter(|e| e.info.state == PluginState::Active)
            .flat_map(|e| e.pane_types.iter().map(move |p| (&e.info, p)))
            .collect()
    }

    /// Get all keybindings from active plugins.
    pub fn all_keybindings(&self) -> Vec<(&PluginInfo, &KeybindingConfig)> {
        self.plugins
            .values()
            .filter(|e| e.info.state == PluginState::Active)
            .flat_map(|e| e.keybindings.iter().map(move |k| (&e.info, k)))
            .collect()
    }

    /// Get all custom themes from active plugins.
    pub fn all_themes(&self) -> Vec<ThemeDefinition> {
        self.plugins
            .values()
            .filter(|e| e.info.state == PluginState::Active)
            .flat_map(|e| e.plugin.themes())
            .collect()
    }

    /// Get all custom table pane configurations from active plugins.
    pub fn all_custom_table_panes(&self) -> Vec<crate::CustomTableConfig> {
        self.plugins
            .values()
            .filter(|e| e.info.state == PluginState::Active)
            .flat_map(|e| e.plugin.custom_table_panes())
            .collect()
    }

    /// Get all custom chart pane configurations from active plugins.
    pub fn all_custom_chart_panes(&self) -> Vec<crate::CustomChartConfig> {
        self.plugins
            .values()
            .filter(|e| e.info.state == PluginState::Active)
            .flat_map(|e| e.plugin.custom_chart_panes())
            .collect()
    }

    /// Get all custom stat pane configurations from active plugins.
    pub fn all_custom_stat_panes(&self) -> Vec<crate::StatPaneConfig> {
        self.plugins
            .values()
            .filter(|e| e.info.state == PluginState::Active)
            .flat_map(|e| e.plugin.custom_stat_panes())
            .collect()
    }

    /// Get all custom gauge pane configurations from active plugins.
    pub fn all_custom_gauge_panes(&self) -> Vec<crate::GaugePaneConfig> {
        self.plugins
            .values()
            .filter(|e| e.info.state == PluginState::Active)
            .flat_map(|e| e.plugin.custom_gauge_panes())
            .collect()
    }

    /// Get all pane types that support auto-refresh from active plugins.
    ///
    /// Returns a vector of (pane_type_name, refresh_interval_seconds) tuples.
    pub fn all_refreshable_pane_types(&self) -> Vec<(String, u32)> {
        self.plugins
            .values()
            .filter(|e| e.info.state == PluginState::Active)
            .flat_map(|e| {
                e.plugin
                    .refreshable_pane_types()
                    .into_iter()
                    .map(|(name, interval)| (name.to_string(), interval))
            })
            .collect()
    }

    /// Trigger a refresh for a specific pane type.
    ///
    /// Finds the plugin that owns this pane type and calls its refresh callback.
    /// Returns true if the refresh was triggered successfully.
    pub fn trigger_pane_refresh(&mut self, pane_type: &str) -> bool {
        let ctx = match &self.context {
            Some(c) => c,
            None => return false,
        };

        for entry in self.plugins.values_mut() {
            if entry.info.state != PluginState::Active {
                continue;
            }

            // Check if this plugin has this pane type as refreshable
            let has_pane_type = entry
                .plugin
                .refreshable_pane_types()
                .iter()
                .any(|(name, _)| *name == pane_type);

            if has_pane_type {
                return entry.plugin.trigger_pane_refresh(pane_type, ctx);
            }
        }

        false
    }

    /// Get commands for a specific plugin.
    pub fn commands_for_plugin(&self, id: PluginId) -> Vec<&CommandConfig> {
        self.plugins
            .get(&id)
            .map(|e| e.commands.iter().collect())
            .unwrap_or_default()
    }

    /// Get keybindings for a specific plugin.
    pub fn keybindings_for_plugin(&self, id: PluginId) -> Vec<&KeybindingConfig> {
        self.plugins
            .get(&id)
            .map(|e| e.keybindings.iter().collect())
            .unwrap_or_default()
    }

    /// Execute a plugin command.
    pub fn execute_command(&mut self, command: &str, args: &str) -> bool {
        let ctx = match &self.context {
            Some(c) => c,
            None => return false,
        };

        // O(1) lookup using command index
        let plugin_id = match self.command_to_plugin.get(command) {
            Some(&id) => id,
            None => return false,
        };

        let entry = match self.plugins.get_mut(&plugin_id) {
            Some(e) if e.info.state == PluginState::Active => e,
            _ => return false,
        };

        entry.plugin.execute_command(command, args, ctx)
    }

    // ==================== Hook Dispatch ====================

    /// Dispatch lifecycle: workspace loaded.
    pub fn on_workspace_loaded(&mut self) {
        for entry in self.plugins.values_mut() {
            if entry.info.state == PluginState::Active {
                if let Some(ref mut hook) = entry.lifecycle_hook {
                    hook.on_workspace_loaded();
                }
            }
        }
    }

    /// Dispatch lifecycle: workspace saving.
    pub fn on_workspace_saving(&mut self) {
        for entry in self.plugins.values_mut() {
            if entry.info.state == PluginState::Active {
                if let Some(ref mut hook) = entry.lifecycle_hook {
                    hook.on_workspace_saving();
                }
            }
        }
    }

    /// Dispatch lifecycle: pane added.
    pub fn on_pane_added(&mut self, pane_id: usize) {
        for entry in self.plugins.values_mut() {
            if entry.info.state == PluginState::Active {
                if let Some(ref mut hook) = entry.lifecycle_hook {
                    hook.on_pane_added(pane_id);
                }
            }
        }
    }

    /// Dispatch lifecycle: pane removing.
    pub fn on_pane_removing(&mut self, pane_id: usize) {
        for entry in self.plugins.values_mut() {
            if entry.info.state == PluginState::Active {
                if let Some(ref mut hook) = entry.lifecycle_hook {
                    hook.on_pane_removing(pane_id);
                }
            }
        }
    }

    /// Dispatch lifecycle: pane focused.
    pub fn on_pane_focused(&mut self, pane_id: usize) {
        for entry in self.plugins.values_mut() {
            if entry.info.state == PluginState::Active {
                if let Some(ref mut hook) = entry.lifecycle_hook {
                    hook.on_pane_focused(pane_id);
                }
            }
        }
    }

    /// Dispatch lifecycle: closing.
    pub fn on_closing(&mut self) {
        for entry in self.plugins.values_mut() {
            if entry.info.state == PluginState::Active {
                if let Some(ref mut hook) = entry.lifecycle_hook {
                    hook.on_closing();
                }
            }
        }
    }

    /// Dispatch lifecycle: frame update.
    pub fn on_frame(&mut self) {
        for entry in self.plugins.values_mut() {
            if entry.info.state == PluginState::Active {
                if let Some(ref mut hook) = entry.lifecycle_hook {
                    hook.on_frame();
                }
            }
        }
    }

    /// Dispatch command hook: before command.
    pub fn before_command(&mut self, command: &str, args: &str) -> CommandHookResult {
        for entry in self.plugins.values_mut() {
            if entry.info.state == PluginState::Active {
                if let Some(ref mut hook) = entry.command_hook {
                    let result = hook.before_command(command, args);
                    if result != CommandHookResult::Continue {
                        return result;
                    }
                }
            }
        }
        CommandHookResult::Continue
    }

    /// Dispatch command hook: after command.
    pub fn after_command(&mut self, command: &str, args: &str, success: bool) {
        for entry in self.plugins.values_mut() {
            if entry.info.state == PluginState::Active {
                if let Some(ref mut hook) = entry.command_hook {
                    hook.after_command(command, args, success);
                }
            }
        }
    }

    /// Dispatch keyboard hook: key pressed.
    pub fn on_key_pressed(&mut self, key: &KeyEvent) -> KeyboardHookResult {
        for entry in self.plugins.values_mut() {
            if entry.info.state == PluginState::Active {
                if let Some(ref mut hook) = entry.keyboard_hook {
                    let result = hook.on_key_pressed(key);
                    if result != KeyboardHookResult::Continue {
                        return result;
                    }
                }
            }
        }
        KeyboardHookResult::Continue
    }

    /// Dispatch keyboard hook: key combo.
    pub fn on_key_combo(&mut self, combo: &KeyCombo) -> KeyboardHookResult {
        for entry in self.plugins.values_mut() {
            if entry.info.state == PluginState::Active {
                if let Some(ref mut hook) = entry.keyboard_hook {
                    let result = hook.on_key_combo(combo);
                    if result != KeyboardHookResult::Continue {
                        return result;
                    }
                }
            }
        }
        KeyboardHookResult::Continue
    }

    /// Dispatch theme hook: theme changing.
    pub fn on_theme_changing(&mut self, old_theme: Theme, new_theme: Theme) {
        for entry in self.plugins.values_mut() {
            if entry.info.state == PluginState::Active {
                if let Some(ref mut hook) = entry.theme_hook {
                    hook.before_theme_change(old_theme, new_theme);
                }
            }
        }
    }

    /// Dispatch theme hook: theme changed.
    pub fn on_theme_changed(&mut self, theme: Theme) {
        for entry in self.plugins.values_mut() {
            if entry.info.state == PluginState::Active {
                // Notify the plugin trait method
                entry.plugin.on_theme_changed(theme);
                // Notify the theme hook
                if let Some(ref mut hook) = entry.theme_hook {
                    hook.after_theme_change(theme);
                }
            }
        }
    }

    /// Dispatch pane hook: pane created.
    pub fn on_pane_created(&mut self, pane_id: usize, pane_type: &str) {
        for entry in self.plugins.values_mut() {
            if entry.info.state == PluginState::Active {
                if let Some(ref mut hook) = entry.pane_hook {
                    hook.on_pane_created(pane_id, pane_type);
                }
            }
        }
    }

    /// Dispatch pane hook: query changed.
    pub fn on_query_changed(&mut self, pane_id: usize, query: &str) {
        for entry in self.plugins.values_mut() {
            if entry.info.state == PluginState::Active {
                if let Some(ref mut hook) = entry.pane_hook {
                    hook.on_query_changed(pane_id, query);
                }
            }
        }
    }

    /// Dispatch pane hook: data received.
    pub fn on_data_received(&mut self, pane_id: usize) {
        for entry in self.plugins.values_mut() {
            if entry.info.state == PluginState::Active {
                if let Some(ref mut hook) = entry.pane_hook {
                    hook.on_data_received(pane_id);
                }
            }
        }
    }

    /// Dispatch pane hook: pane error.
    pub fn on_pane_error(&mut self, pane_id: usize, error: &str) {
        for entry in self.plugins.values_mut() {
            if entry.info.state == PluginState::Active {
                if let Some(ref mut hook) = entry.pane_hook {
                    hook.on_pane_error(pane_id, error);
                }
            }
        }
    }

    // ==================== Private Helpers ====================

    /// Simple semver check (major.minor.patch).
    fn check_version(current: &str, required: &str) -> bool {
        let parse = |v: &str| -> (u32, u32, u32) {
            let parts: Vec<&str> = v.split('.').collect();
            (
                parts.first().and_then(|s| s.parse().ok()).unwrap_or(0),
                parts.get(1).and_then(|s| s.parse().ok()).unwrap_or(0),
                parts.get(2).and_then(|s| s.parse().ok()).unwrap_or(0),
            )
        };

        let curr = parse(current);
        let req = parse(required);

        // Current must be >= required
        curr >= req
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::{
        BoxFuture, HttpError, HttpResponse, LogLevel, NotificationLevel, PluginHost,
    };
    use std::any::Any;
    use std::sync::Arc;

    /// Mock plugin host for testing.
    struct MockPluginHost;

    impl PluginHost for MockPluginHost {
        fn notify(&self, _level: NotificationLevel, _message: &str) {}
        fn request_repaint(&self) {}
        fn log(&self, _level: LogLevel, _message: &str) {}
        fn version(&self) -> &'static str {
            "1.0.0"
        }
        fn is_wasm(&self) -> bool {
            false
        }
        fn theme(&self) -> Theme {
            Theme::Dark
        }
        fn theme_name(&self) -> &'static str {
            "dark"
        }
        fn clipboard_write(&self, _text: &str) -> bool {
            true
        }
        fn clipboard_read(&self) -> Option<String> {
            None
        }
        fn spawn(&self, _future: BoxFuture<()>) {}
        fn http_get(
            &self,
            _url: &str,
            _headers: &rustc_hash::FxHashMap<String, String>,
        ) -> Result<HttpResponse, HttpError> {
            Err(HttpError {
                message: "Not implemented".to_string(),
            })
        }
        fn http_post(
            &self,
            _url: &str,
            _body: &str,
            _headers: &rustc_hash::FxHashMap<String, String>,
        ) -> Result<HttpResponse, HttpError> {
            Err(HttpError {
                message: "Not implemented".to_string(),
            })
        }

        // Pane management (no-op for mock)
        fn add_query_pane(&self, _query: &str, _title: Option<&str>) {}
        fn add_logs_pane(&self) {}
        fn add_tracing_pane(&self, _trace_id: Option<&str>) {}
        fn add_terminal_pane(&self) {}
        fn add_sql_pane(&self) {}
        fn close_focused_pane(&self) {}
        fn focus_pane(&self, _direction: &str) {}

        // Time range (no-op for mock)
        fn set_time_range_preset(&self, _preset: &str) {}
        fn set_time_range_absolute(&self, _start_secs: f64, _end_secs: f64) {}
        fn get_time_range(&self) -> (f64, f64) {
            (0.0, 0.0)
        }

        // Custom table panes (no-op for mock)
        fn register_custom_table_pane(&self, _config: crate::CustomTableConfig) {}
        fn add_custom_table_pane(&self, _pane_type: &str) {}
        fn update_custom_table_data(&self, _pane_id: usize, _data: crate::CustomTableData) {}
        fn update_custom_table_data_by_type(
            &self,
            _pane_type: &str,
            _data: crate::CustomTableData,
        ) {
        }

        // Custom chart panes (no-op for mock)
        fn register_custom_chart_pane(&self, _config: crate::CustomChartConfig) {}
        fn add_custom_chart_pane(&self, _pane_type: &str) {}
        fn update_custom_chart_data_by_type(
            &self,
            _pane_type: &str,
            _data: crate::CustomChartData,
        ) {
        }

        // Custom stat panes (no-op for mock)
        fn register_stat_pane(&self, _config: crate::StatPaneConfig) {}
        fn add_stat_pane(&self, _pane_type: &str) {}
        fn update_stat_data_by_type(&self, _pane_type: &str, _data: crate::StatPaneData) {}

        // Custom gauge panes (no-op for mock)
        fn register_gauge_pane(&self, _config: crate::GaugePaneConfig) {}
        fn add_gauge_pane(&self, _pane_type: &str) {}
        fn update_gauge_data_by_type(&self, _pane_type: &str, _data: crate::GaugePaneData) {}

        // Focused pane info (no-op for mock)
        fn get_focused_pane_info(&self) -> Option<crate::FocusedPaneInfo> {
            None
        }
    }

    /// Simple test plugin for testing registry operations.
    struct TestPlugin {
        name: &'static str,
        version: &'static str,
        min_version: Option<&'static str>,
        commands: Vec<CommandConfig>,
        executed_commands: std::sync::atomic::AtomicUsize,
    }

    impl TestPlugin {
        fn new(name: &'static str) -> Self {
            Self {
                name,
                version: "1.0.0",
                min_version: None,
                commands: vec![],
                executed_commands: std::sync::atomic::AtomicUsize::new(0),
            }
        }

        fn with_version(mut self, version: &'static str) -> Self {
            self.version = version;
            self
        }

        fn with_min_version(mut self, min_version: &'static str) -> Self {
            self.min_version = Some(min_version);
            self
        }

        fn with_command(mut self, name: &str) -> Self {
            self.commands.push(CommandConfig {
                name: name.to_string(),
                aliases: vec![],
                description: format!("Test command: {name}"),
                accepts_args: false,
            });
            self
        }
    }

    impl crate::traits::Plugin for TestPlugin {
        fn name(&self) -> &'static str {
            self.name
        }

        fn version(&self) -> &'static str {
            self.version
        }

        fn description(&self) -> &'static str {
            "A test plugin"
        }

        fn capabilities(&self) -> PluginCapabilities {
            if self.commands.is_empty() {
                PluginCapabilities::empty()
            } else {
                PluginCapabilities::COMMANDS
            }
        }

        fn min_editor_version(&self) -> Option<&'static str> {
            self.min_version
        }

        fn init(&mut self, _ctx: &PluginContext) -> crate::PluginResult<()> {
            Ok(())
        }

        fn commands(&self) -> Vec<CommandConfig> {
            self.commands.clone()
        }

        fn execute_command(&mut self, command: &str, _args: &str, _ctx: &PluginContext) -> bool {
            if self.commands.iter().any(|c| c.name == command) {
                self.executed_commands
                    .fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                true
            } else {
                false
            }
        }

        fn as_any(&self) -> &dyn Any {
            self
        }

        fn as_any_mut(&mut self) -> &mut dyn Any {
            self
        }
    }

    fn create_test_context() -> PluginContext {
        PluginContext::new(Arc::new(MockPluginHost))
    }

    #[test]
    fn test_registry_new() {
        let registry = PluginRegistry::new();
        assert!(registry.list_plugins().is_empty());
        assert!(registry.context().is_none());
    }

    #[test]
    fn test_registry_init() {
        let mut registry = PluginRegistry::new();
        let ctx = create_test_context();
        registry.init(ctx);
        assert!(registry.context().is_some());
    }

    #[test]
    fn test_register_plugin() {
        let mut registry = PluginRegistry::new();
        let plugin = TestPlugin::new("test-plugin").with_version("2.5.0");

        let id = registry.register(plugin, true).unwrap();
        assert_eq!(id.value(), 0);
        assert_eq!(registry.list_plugins().len(), 1);

        let info = registry.info(id).unwrap();
        assert_eq!(info.name, "test-plugin");
        assert_eq!(info.version, "2.5.0");
        assert_eq!(info.state, PluginState::Registered);
    }

    #[test]
    fn test_register_duplicate_fails() {
        let mut registry = PluginRegistry::new();
        registry.register(TestPlugin::new("dupe"), true).unwrap();

        let result = registry.register(TestPlugin::new("dupe"), true);
        assert!(matches!(result, Err(PluginError::AlreadyRegistered(_))));
    }

    #[test]
    fn test_plugin_lifecycle() {
        let mut registry = PluginRegistry::new();
        registry.init(create_test_context());

        let id = registry
            .register(TestPlugin::new("lifecycle"), true)
            .unwrap();

        // After register: Registered state
        assert_eq!(registry.info(id).unwrap().state, PluginState::Registered);

        // After init: Inactive state
        registry.init_plugin(id).unwrap();
        assert_eq!(registry.info(id).unwrap().state, PluginState::Inactive);

        // After activate: Active state
        registry.activate_plugin(id).unwrap();
        assert_eq!(registry.info(id).unwrap().state, PluginState::Active);

        // After deactivate: Inactive state
        registry.deactivate_plugin(id).unwrap();
        assert_eq!(registry.info(id).unwrap().state, PluginState::Inactive);
    }

    #[test]
    fn test_active_plugins() {
        let mut registry = PluginRegistry::new();
        registry.init(create_test_context());

        let id1 = registry
            .register(TestPlugin::new("plugin-1"), true)
            .unwrap();
        let id2 = registry
            .register(TestPlugin::new("plugin-2"), true)
            .unwrap();

        // Neither active yet
        assert!(registry.active_plugins().is_empty());

        // Activate first
        registry.init_plugin(id1).unwrap();
        registry.activate_plugin(id1).unwrap();
        assert_eq!(registry.active_plugins().len(), 1);

        // Activate second
        registry.init_plugin(id2).unwrap();
        registry.activate_plugin(id2).unwrap();
        assert_eq!(registry.active_plugins().len(), 2);
    }

    #[test]
    fn test_get_by_name() {
        let mut registry = PluginRegistry::new();
        registry
            .register(TestPlugin::new("named-plugin"), true)
            .unwrap();

        assert!(registry.get_by_name("named-plugin").is_some());
        assert!(registry.get_by_name("nonexistent").is_none());

        let info = registry.info_by_name("named-plugin").unwrap();
        assert_eq!(info.name, "named-plugin");
    }

    #[test]
    fn test_version_check() {
        // Equal versions
        assert!(PluginRegistry::check_version("1.0.0", "1.0.0"));

        // Current > required
        assert!(PluginRegistry::check_version("2.0.0", "1.0.0"));
        assert!(PluginRegistry::check_version("1.1.0", "1.0.0"));
        assert!(PluginRegistry::check_version("1.0.1", "1.0.0"));

        // Current < required
        assert!(!PluginRegistry::check_version("1.0.0", "2.0.0"));
        assert!(!PluginRegistry::check_version("1.0.0", "1.1.0"));
        assert!(!PluginRegistry::check_version("1.0.0", "1.0.1"));

        // Partial versions
        assert!(PluginRegistry::check_version("1.0", "1.0.0"));
        assert!(PluginRegistry::check_version("1", "1.0.0"));
    }

    #[test]
    fn test_min_version_enforcement() {
        let mut registry = PluginRegistry::new();
        registry.init(create_test_context()); // Host version is "1.0.0"

        // Plugin requires 2.0.0 but host is 1.0.0
        let id = registry
            .register(
                TestPlugin::new("future-plugin").with_min_version("2.0.0"),
                true,
            )
            .unwrap();

        let result = registry.init_plugin(id);
        assert!(matches!(
            result,
            Err(PluginError::IncompatibleVersion { .. })
        ));
        assert_eq!(registry.info(id).unwrap().state, PluginState::Failed);
    }

    #[test]
    fn test_command_collection() {
        let mut registry = PluginRegistry::new();
        registry.init(create_test_context());

        let id = registry
            .register(
                TestPlugin::new("cmd-plugin")
                    .with_command("cmd-1")
                    .with_command("cmd-2"),
                true,
            )
            .unwrap();

        registry.init_plugin(id).unwrap();
        registry.activate_plugin(id).unwrap();

        let commands = registry.all_commands();
        assert_eq!(commands.len(), 2);

        let plugin_cmds = registry.commands_for_plugin(id);
        assert_eq!(plugin_cmds.len(), 2);
    }

    #[test]
    fn test_execute_command() {
        let mut registry = PluginRegistry::new();
        registry.init(create_test_context());

        let id = registry
            .register(TestPlugin::new("exec-plugin").with_command("my-cmd"), true)
            .unwrap();

        registry.init_plugin(id).unwrap();
        registry.activate_plugin(id).unwrap();

        // Execute existing command
        assert!(registry.execute_command("my-cmd", ""));

        // Execute non-existent command
        assert!(!registry.execute_command("nonexistent", ""));
    }

    #[test]
    fn test_disabled_plugin() {
        let mut registry = PluginRegistry::new();
        registry.init(create_test_context());

        // Disable the plugin before activation
        registry.set_plugin_enabled("disabled-plugin", false);

        let id = registry
            .register(TestPlugin::new("disabled-plugin"), true)
            .unwrap();

        registry.init_plugin(id).unwrap();
        registry.activate_plugin(id).unwrap();

        // Plugin should be in Disabled state, not Active
        assert_eq!(registry.info(id).unwrap().state, PluginState::Disabled);
        assert!(registry.active_plugins().is_empty());
    }

    #[test]
    fn test_plugin_enabled_check() {
        let mut registry = PluginRegistry::new();

        // Unknown plugin defaults to enabled
        assert!(registry.is_plugin_enabled("unknown"));

        // Explicitly disabled
        registry.set_plugin_enabled("my-plugin", false);
        assert!(!registry.is_plugin_enabled("my-plugin"));

        // Explicitly enabled
        registry.set_plugin_enabled("my-plugin", true);
        assert!(registry.is_plugin_enabled("my-plugin"));
    }
}