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
//! Adapter to make `VimSearchManager` work with `StateDispatcher`
use crate::app_state_container::AppStateContainer;
use crate::buffer::{AppMode, Buffer, BufferAPI};
use crate::data::data_view::DataView;
use crate::state::{StateEvent, StateSubscriber};
use crate::ui::search::vim_search_manager::VimSearchManager;
use crate::ui::state::shadow_state::SearchType;
use crate::ui::viewport_manager::ViewportManager;
use crossterm::event::KeyCode;
use tracing::{debug, info};
/// Adapter that connects `VimSearchManager` to the state dispatcher
pub struct VimSearchAdapter {
manager: VimSearchManager,
is_active: bool,
}
impl Default for VimSearchAdapter {
fn default() -> Self {
Self::new()
}
}
impl VimSearchAdapter {
#[must_use]
pub fn new() -> Self {
Self {
manager: VimSearchManager::new(),
is_active: false,
}
}
/// Create with a specific manager (for testing or advanced use)
#[must_use]
pub fn with_manager(manager: VimSearchManager) -> Self {
Self {
manager,
is_active: false,
}
}
/// Check if vim search should handle a key based on `AppStateContainer` state
pub fn should_handle_key(&self, state: &AppStateContainer) -> bool {
// Use AppStateContainer's vim search check
let should_handle = state.vim_search_should_handle_key();
debug!(
"VimSearchAdapter: should_handle_key? mode={:?}, pattern='{}', active={}, result={}",
state.get_mode(),
state.get_search_pattern(),
self.is_active,
should_handle
);
should_handle || self.is_active
}
/// Check if vim search should handle a key based on Buffer state (legacy - for compatibility)
pub fn should_handle_key_buffer(&self, buffer: &dyn BufferAPI) -> bool {
// Check Buffer's state, not internal state
let in_search_mode = buffer.get_mode() == AppMode::Search;
let has_pattern = !buffer.get_search_pattern().is_empty();
debug!(
"VimSearchAdapter: should_handle_key_buffer? mode={:?}, pattern='{}', active={}",
buffer.get_mode(),
buffer.get_search_pattern(),
self.is_active
);
// Only handle keys if we're in search mode OR have an active pattern
in_search_mode || (self.is_active && has_pattern)
}
/// Clear the search manager when search ends
pub fn clear(&mut self) {
info!("VimSearchAdapter: Clearing vim search");
self.manager.clear();
self.is_active = false;
}
/// Get the inner manager
#[must_use]
pub fn manager(&self) -> &VimSearchManager {
&self.manager
}
/// Get mutable reference to inner manager
pub fn manager_mut(&mut self) -> &mut VimSearchManager {
&mut self.manager
}
/// Handle a key press through `AppStateContainer` (simplified interface)
/// Returns true if key was handled, false to let it fall through
pub fn handle_key(&mut self, key: KeyCode, state: &mut AppStateContainer) -> bool {
let mode = state.get_mode();
// Special handling for Escape key
if key == KeyCode::Esc {
match mode {
AppMode::Results if self.is_active || self.is_navigating() => {
// In Results mode with active search: Signal that Escape was pressed
// But DON'T handle it here - return false to let StateCoordinator handle it
info!("VimSearchAdapter: Escape detected in Results mode with active search - signaling for StateCoordinator");
// Return false so it falls through to handle_results_input
// where StateCoordinator will properly clear everything
return false;
}
AppMode::Search => {
// In Search mode: exit to Results
info!("VimSearchAdapter: Exiting search mode to Results");
state.exit_vim_search();
self.clear();
return true;
}
_ => {
// Not our concern
return false;
}
}
}
// For other keys, only handle if we should
if !self.should_handle_key(state) {
debug!("VimSearchAdapter: Not handling key - search not active");
return false;
}
// For n/N keys, let them go through the normal action system
// The TUI will map them to NextSearchMatch/PreviousSearchMatch actions
// which will then call vim_search_next()/vim_search_previous()
match key {
KeyCode::Char('n' | 'N') => {
info!(
"VimSearchAdapter: Navigation key '{}' - letting TUI handle via action system",
if key == KeyCode::Char('n') { "n" } else { "N" }
);
// Don't handle - let TUI map to NextSearchMatch/PreviousSearchMatch actions
false
}
KeyCode::Enter => {
info!("VimSearchAdapter: Confirming search - will need dataview and viewport from TUI");
// Signal that this key was handled - the TUI needs to provide dataview and viewport
true
}
_ => false,
}
}
/// Handle a key press - delegates to `VimSearchManager` if appropriate (legacy)
pub fn handle_key_legacy(
&mut self,
key: KeyCode,
dataview: &DataView,
viewport: &mut ViewportManager,
buffer: &dyn BufferAPI,
) -> bool {
// First check if we should handle keys at all
if !self.should_handle_key_buffer(buffer) {
debug!("VimSearchAdapter: Not handling key - search not active");
return false;
}
// Delegate to VimSearchManager for actual search operations
match key {
KeyCode::Char('n') => {
info!("VimSearchAdapter: Delegating 'n' (next match) to VimSearchManager");
self.manager.next_match(viewport);
true
}
KeyCode::Char('N') => {
info!("VimSearchAdapter: Delegating 'N' (previous match) to VimSearchManager");
self.manager.previous_match(viewport);
true
}
KeyCode::Enter => {
info!("VimSearchAdapter: Delegating Enter (confirm search) to VimSearchManager");
self.manager.confirm_search(dataview, viewport);
true
}
KeyCode::Esc => {
info!("VimSearchAdapter: Search cancelled");
self.clear();
false // Let TUI handle mode change
}
_ => {
// For typing characters in search mode
if self.manager.is_typing() {
if let KeyCode::Char(c) = key {
// Update pattern - this would need to be connected to Buffer's search_state
debug!("VimSearchAdapter: Character '{}' typed in search", c);
// Note: Pattern updates should go through Buffer
true
} else {
false
}
} else {
false
}
}
}
}
/// Start a new search
pub fn start_search(&mut self) {
info!("VimSearchAdapter: Starting new search");
self.is_active = true;
self.manager.start_search();
}
/// Update search pattern and find matches
pub fn update_pattern(
&mut self,
pattern: String,
dataview: &DataView,
viewport: &mut ViewportManager,
) {
debug!("VimSearchAdapter: Updating pattern to '{}'", pattern);
self.manager.update_pattern(pattern, dataview, viewport);
}
/// Confirm the current search
pub fn confirm_search(&mut self, dataview: &DataView, viewport: &mut ViewportManager) -> bool {
info!("VimSearchAdapter: Confirming search");
self.manager.confirm_search(dataview, viewport)
}
/// Check if the adapter is active (has vim search running)
#[must_use]
pub fn is_active(&self) -> bool {
self.is_active || self.manager.is_active()
}
/// Check if we're currently navigating through search results
#[must_use]
pub fn is_navigating(&self) -> bool {
self.manager.is_navigating()
}
/// Get the current search pattern
#[must_use]
pub fn get_pattern(&self) -> Option<String> {
self.manager.get_pattern()
}
/// Get match information (current, total)
#[must_use]
pub fn get_match_info(&self) -> Option<(usize, usize)> {
self.manager.get_match_info()
}
/// Cancel the current search
pub fn cancel_search(&mut self) {
info!("VimSearchAdapter: Cancelling search");
self.manager.cancel_search();
self.is_active = false;
}
/// Exit navigation mode
pub fn exit_navigation(&mut self) {
info!("VimSearchAdapter: Exiting navigation");
self.manager.exit_navigation();
}
/// Mark search as complete (after Apply/Enter)
/// Keeps matches for n/N navigation and stays active
pub fn mark_search_complete(&mut self) {
info!("VimSearchAdapter: Marking search as complete, keeping matches for navigation");
// Keep is_active = true so n/N continue to work
// The manager stays active with matches
// Only clear() or cancel_search() will deactivate
}
/// Navigate to next match
pub fn next_match(
&mut self,
viewport: &mut ViewportManager,
) -> Option<crate::ui::search::vim_search_manager::SearchMatch> {
self.manager.next_match(viewport)
}
/// Navigate to previous match
pub fn previous_match(
&mut self,
viewport: &mut ViewportManager,
) -> Option<crate::ui::search::vim_search_manager::SearchMatch> {
self.manager.previous_match(viewport)
}
/// Set search state from external source (for compatibility)
pub fn set_search_state_from_external(
&mut self,
pattern: String,
matches: Vec<(usize, usize)>,
dataview: &DataView,
) {
self.manager
.set_search_state_from_external(pattern, matches, dataview);
self.is_active = true; // Activate when search state is set externally
}
/// Resume the last search
pub fn resume_last_search(
&mut self,
dataview: &DataView,
viewport: &mut ViewportManager,
) -> bool {
let result = self.manager.resume_last_search(dataview, viewport);
if result {
self.is_active = true;
}
result
}
/// Reset to the first match
pub fn reset_to_first_match(
&mut self,
viewport: &mut ViewportManager,
) -> Option<crate::ui::search::vim_search_manager::SearchMatch> {
self.manager.reset_to_first_match(viewport)
}
}
impl StateSubscriber for VimSearchAdapter {
fn on_state_event(&mut self, event: &StateEvent, buffer: &Buffer) {
match event {
StateEvent::SearchStarted { search_type } => {
if matches!(search_type, SearchType::Vim) {
info!("VimSearchAdapter: Activating for vim search");
self.is_active = true;
self.manager.start_search();
}
}
StateEvent::SearchEnded { search_type } => {
if matches!(search_type, SearchType::Vim) {
info!("VimSearchAdapter: Search ended, clearing");
self.clear();
}
}
StateEvent::ModeChanged { from: _, to } => {
// If we exit to Results mode and search is empty, clear
if *to == AppMode::Results
&& buffer.search_state.pattern.is_empty()
&& self.is_active
{
info!("VimSearchAdapter: Mode changed to Results with empty search, clearing");
self.clear();
}
// If we enter Search mode, activate
if *to == AppMode::Search {
info!("VimSearchAdapter: Mode changed to Search, activating");
self.is_active = true;
if !self.manager.is_active() {
self.manager.start_search();
}
}
}
_ => {}
}
}
fn name(&self) -> &'static str {
"VimSearchAdapter"
}
}