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
//! Message handling for the search/replace dialog.
use iced::Task;
use iced::widget::operation::{focus, select_all};
use crate::canvas_editor::editing::command::{
Command, CompositeCommand, ReplaceTextCommand,
};
use crate::canvas_editor::{CodeEditor, Message};
impl CodeEditor {
/// Handles opening the search dialog.
///
/// # Arguments
///
/// * `replace` - `true` to open the search-and-replace dialog, `false`
/// for search-only
///
/// # Returns
///
/// A `Task<Message>` that focuses and selects all in the search input
pub(crate) fn handle_open_search(
&mut self,
replace: bool,
) -> Task<Message> {
self.goto_line_state.close();
if replace {
self.search_state.open_replace();
} else {
self.search_state.open_search();
}
if !self.search_state.query.is_empty() {
self.search_state.update_matches(&self.buffer);
self.search_state
.select_match_near_cursor(self.cursors.primary_position());
}
self.overlay_cache.clear();
// Focus the search input and select all text if any
Task::batch([
focus(self.search_state.search_input_id.clone()),
select_all(self.search_state.search_input_id.clone()),
])
}
/// Handles closing the search dialog.
///
/// # Returns
///
/// A `Task<Message>` (currently Task::none())
pub(crate) fn handle_close_search_msg(&mut self) -> Task<Message> {
// Escape with multiple cursors and no open search: collapse to primary cursor
if self.cursors.is_multi() && !self.search_state.is_open {
self.cursors.remove_all_but_primary();
self.overlay_cache.clear();
return Task::none();
}
self.search_state.close();
self.overlay_cache.clear();
Task::none()
}
/// Handles search query text changes.
///
/// # Arguments
///
/// * `query` - The new search query
///
/// # Returns
///
/// A `Task<Message>` that scrolls to first match if any
pub(crate) fn handle_search_query_changed_msg(
&mut self,
query: &str,
) -> Task<Message> {
self.search_state.set_query(query.to_string(), &self.buffer);
self.overlay_cache.clear();
// Move cursor to first match if any
if let Some(match_pos) = self.search_state.current_match() {
self.cursors.primary_mut().position =
(match_pos.line, match_pos.col);
self.clear_selection();
return self.scroll_to_cursor();
}
Task::none()
}
/// Handles replace query text changes.
///
/// # Arguments
///
/// * `replace_text` - The new replacement text
///
/// # Returns
///
/// A `Task<Message>` (currently Task::none())
pub(crate) fn handle_replace_query_changed_msg(
&mut self,
replace_text: &str,
) -> Task<Message> {
self.search_state.set_replace_with(replace_text.to_string());
Task::none()
}
/// Handles toggling case-sensitive search.
///
/// # Returns
///
/// A `Task<Message>` that scrolls to first match if any
pub(crate) fn handle_toggle_case_sensitive_msg(&mut self) -> Task<Message> {
self.search_state.toggle_case_sensitive(&self.buffer);
self.overlay_cache.clear();
// Move cursor to first match if any
if let Some(match_pos) = self.search_state.current_match() {
self.cursors.primary_mut().position =
(match_pos.line, match_pos.col);
self.clear_selection();
return self.scroll_to_cursor();
}
Task::none()
}
/// Handles finding the next or previous match.
///
/// # Arguments
///
/// * `forward` - `true` to move to the next match, `false` for the
/// previous match
///
/// # Returns
///
/// A `Task<Message>` that scrolls to the matched position if any
pub(crate) fn handle_find_match(&mut self, forward: bool) -> Task<Message> {
if !self.search_state.matches.is_empty() {
if forward {
self.search_state.next_match();
} else {
self.search_state.previous_match();
}
if let Some(match_pos) = self.search_state.current_match() {
self.cursors.primary_mut().position =
(match_pos.line, match_pos.col);
self.clear_selection();
self.overlay_cache.clear();
return self.scroll_to_cursor();
}
}
Task::none()
}
/// Handles replacing the current match and moving to the next.
///
/// # Returns
///
/// A `Task<Message>` that scrolls to the next match if any
pub(crate) fn handle_replace_next_msg(&mut self) -> Task<Message> {
// Replace current match and move to next
if let Some(match_pos) = self.search_state.current_match() {
let query_len = self.search_state.query.chars().count();
let replace_text = self.search_state.replace_with.clone();
// Create and execute replace command
let pos = self.cursors.primary_position();
let mut cmd = ReplaceTextCommand::new(
&self.buffer,
(match_pos.line, match_pos.col),
query_len,
replace_text,
pos,
);
let mut cursor_pos = pos;
cmd.execute(&mut self.buffer, &mut cursor_pos);
self.cursors.primary_mut().position = cursor_pos;
self.history.push(Box::new(cmd));
// The replacement starts at the matched line; invalidate highlight
// from there regardless of where the cursor moved next.
self.pre_edit_line = self.pre_edit_line.min(match_pos.line);
self.pre_edit_last_line =
self.pre_edit_last_line.max(match_pos.line);
self.clear_selection();
self.finish_edit_operation();
// Move to the closest remaining match after the replacement.
if !self.search_state.matches.is_empty()
&& let Some(next_match) = self.search_state.current_match()
{
self.cursors.primary_mut().position =
(next_match.line, next_match.col);
}
return self.scroll_to_cursor();
}
Task::none()
}
/// Handles replacing all matches.
///
/// # Returns
///
/// A `Task<Message>` that scrolls to cursor after replacement
pub(crate) fn handle_replace_all_msg(&mut self) -> Task<Message> {
// Perform a fresh search to find ALL matches (ignoring the display limit)
let all_matches = super::find_matches(
&self.buffer,
&self.search_state.query,
self.search_state.case_sensitive,
None, // No limit for Replace All
);
if !all_matches.is_empty() {
let query_len = self.search_state.query.chars().count();
let replace_text = self.search_state.replace_with.clone();
// Create composite command for undo
let mut composite = CompositeCommand::new();
// Process matches in reverse order (to preserve positions)
for match_pos in all_matches.iter().rev() {
let pos = self.cursors.primary_position();
let cmd = ReplaceTextCommand::new(
&self.buffer,
(match_pos.line, match_pos.col),
query_len,
replace_text.clone(),
pos,
);
composite.add(Box::new(cmd));
}
// Execute all replacements
let mut cursor_pos = self.cursors.primary_position();
composite.execute(&mut self.buffer, &mut cursor_pos);
self.cursors.primary_mut().position = cursor_pos;
self.history.push(Box::new(composite));
// Replace All touches matches anywhere in the document, so reset
// the highlight cache entirely.
self.pre_edit_line = 0;
self.pre_edit_last_line = usize::MAX;
self.clear_selection();
self.finish_edit_operation();
self.scroll_to_cursor()
} else {
Task::none()
}
}
/// Handles Tab/Shift+Tab key in the search dialog (cycles field focus).
///
/// # Arguments
///
/// * `forward` - `true` to cycle forward (Search → Replace → Search),
/// `false` to cycle backward (Replace → Search → Replace)
///
/// # Returns
///
/// A `Task<Message>` that focuses the newly focused field
pub(crate) fn handle_search_dialog_tab(
&mut self,
forward: bool,
) -> Task<Message> {
if forward {
self.search_state.focus_next_field();
} else {
self.search_state.focus_previous_field();
}
// Focus the appropriate input based on new focused_field
match self.search_state.focused_field {
super::SearchFocusedField::Search => {
focus(self.search_state.search_input_id.clone())
}
super::SearchFocusedField::Replace => {
focus(self.search_state.replace_input_id.clone())
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_open_search_replace_opens_in_replace_mode() {
let mut editor = CodeEditor::new("hello", "txt");
let _ = editor.update(&Message::OpenSearchReplace);
assert!(editor.search_state.is_open);
assert!(editor.search_state.is_replace_mode);
}
#[test]
fn test_open_search_opens_in_search_only_mode() {
let mut editor = CodeEditor::new("hello", "txt");
let _ = editor.update(&Message::OpenSearch);
assert!(editor.search_state.is_open);
assert!(!editor.search_state.is_replace_mode);
}
#[test]
fn test_find_previous_moves_to_previous_match() {
let mut editor = CodeEditor::new("foo bar foo baz foo", "txt");
editor.search_state.open_search();
editor.search_state.set_query("foo".to_owned(), &editor.buffer);
assert_eq!(editor.search_state.current_match_index, Some(0));
let _ = editor.update(&Message::FindPrevious);
// Wraps backward from the first match to the last.
assert_eq!(editor.search_state.current_match_index, Some(2));
}
#[test]
fn test_search_dialog_shift_tab_cycles_focus_backward() {
let mut editor = CodeEditor::new("hello", "txt");
editor.search_state.open_replace();
assert_eq!(
editor.search_state.focused_field,
super::super::SearchFocusedField::Search
);
let _ = editor.update(&Message::SearchDialogShiftTab);
assert_eq!(
editor.search_state.focused_field,
super::super::SearchFocusedField::Replace
);
}
}