reovim_plugin_sticky_context/
lib.rs1use std::sync::Arc;
10
11use {
12 reovim_core::{
13 event_bus::{EventBus, EventResult},
14 plugin::{Plugin, PluginContext, PluginId, PluginStateRegistry},
15 },
16 reovim_plugin_context::ViewportContextUpdated,
17};
18
19mod state;
20mod window;
21
22use {
23 state::{CachedViewportContext, StickyContextState},
24 window::StickyContextWindow,
25};
26
27pub struct StickyContextPlugin {
29 state: Arc<StickyContextState>,
30}
31
32impl StickyContextPlugin {
33 #[must_use]
35 pub fn new() -> Self {
36 Self {
37 state: Arc::new(StickyContextState::new()),
38 }
39 }
40}
41
42impl Default for StickyContextPlugin {
43 fn default() -> Self {
44 Self::new()
45 }
46}
47
48impl Plugin for StickyContextPlugin {
49 fn id(&self) -> PluginId {
50 PluginId::new("reovim:sticky-context")
51 }
52
53 fn name(&self) -> &'static str {
54 "sticky-context"
55 }
56
57 fn init_state(&self, registry: &PluginStateRegistry) {
58 registry.register(self.state.clone());
60
61 registry.register_plugin_window(Arc::new(StickyContextWindow::new(self.state.clone())));
63
64 tracing::debug!("StickyContextPlugin: initialized");
65 }
66
67 fn build(&self, _ctx: &mut PluginContext) {
68 tracing::debug!("StickyContextPlugin::build");
69 }
70
71 fn subscribe(&self, bus: &EventBus, _state: Arc<PluginStateRegistry>) {
72 Self::register_settings(bus);
74
75 let plugin_state = Arc::clone(&self.state);
77 bus.subscribe::<ViewportContextUpdated, _>(100, move |event, _ctx| {
78 plugin_state.set_cached_context(CachedViewportContext {
80 buffer_id: event.buffer_id,
81 context: event.context.clone(),
82 });
83
84 tracing::trace!(
85 window_id = event.window_id,
86 buffer_id = event.buffer_id,
87 top_line = event.top_line,
88 has_context = event.context.is_some(),
89 "StickyContextPlugin: received ViewportContextUpdated"
90 );
91
92 EventResult::Handled
93 });
94
95 tracing::debug!("StickyContextPlugin::subscribe - subscribed to ViewportContextUpdated");
96 }
97}
98
99impl StickyContextPlugin {
100 fn register_settings(bus: &EventBus) {
102 use reovim_core::option::{OptionCategory, OptionSpec, OptionValue, RegisterOption};
103
104 bus.emit(RegisterOption::new(
106 OptionSpec::new(
107 "sticky_headers_enabled",
108 "Enable sticky context headers",
109 OptionValue::Bool(true),
110 )
111 .with_category(OptionCategory::Display)
112 .with_section("Editor")
113 .with_display_order(55),
114 ));
115
116 bus.emit(RegisterOption::new(
118 OptionSpec::new(
119 "sticky_headers_max_count",
120 "Maximum sticky headers to display",
121 OptionValue::Integer(3),
122 )
123 .with_category(OptionCategory::Display)
124 .with_section("Editor")
125 .with_display_order(56),
126 ));
127
128 bus.emit(RegisterOption::new(
130 OptionSpec::new(
131 "sticky_headers_show_separator",
132 "Show separator line below sticky headers",
133 OptionValue::Bool(true),
134 )
135 .with_category(OptionCategory::Display)
136 .with_section("Editor")
137 .with_display_order(57),
138 ));
139 }
140}