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
use crate::buffer::Buffer;
/// Search engine for finding text in the buffer
pub struct SearchEngine {
/// The search query string
query: String,
/// The last match position (row, col)
last_match: Option<(usize, usize)>,
/// Whether the search is case-sensitive
case_sensitive: bool,
}
impl SearchEngine {
/// Create a new search engine with the given query
pub fn new(query: String, case_sensitive: bool) -> Self {
Self {
query,
last_match: None,
case_sensitive,
}
}
/// Find the next occurrence of the query starting from the cursor position
///
/// Returns Some((row, col)) if a match is found, None otherwise
pub fn find_next(&mut self, buffer: &Buffer) -> Option<(usize, usize)> {
let cursor = buffer.cursor();
let lines = buffer.lines();
if self.query.is_empty() {
return None;
}
// Prepare query for comparison
let query = if self.case_sensitive {
self.query.clone()
} else {
self.query.to_lowercase()
};
// Start searching from current cursor position
let start_row = cursor.row;
let start_col = cursor.col;
// First, search the rest of the current line
if start_row < lines.len() {
let line = &lines[start_row];
let search_from = start_col;
let line_to_search = if self.case_sensitive {
line[search_from..].to_string()
} else {
line[search_from..].to_lowercase()
};
if let Some(pos) = line_to_search.find(&query) {
let match_pos = (start_row, search_from + pos);
self.last_match = Some(match_pos);
return Some(match_pos);
}
}
// Search subsequent lines
for row in (start_row + 1)..lines.len() {
let line = &lines[row];
let line_to_search = if self.case_sensitive {
line.clone()
} else {
line.to_lowercase()
};
if let Some(col) = line_to_search.find(&query) {
let match_pos = (row, col);
self.last_match = Some(match_pos);
return Some(match_pos);
}
}
// Wrap around: search from beginning to cursor position
for row in 0..=start_row {
let line = &lines[row];
let search_until = if row == start_row { start_col } else { line.len() };
let line_to_search = if self.case_sensitive {
line[..search_until].to_string()
} else {
line[..search_until].to_lowercase()
};
if let Some(col) = line_to_search.find(&query) {
let match_pos = (row, col);
self.last_match = Some(match_pos);
return Some(match_pos);
}
}
None
}
/// Find the previous occurrence of the query starting from the cursor position
///
/// Returns Some((row, col)) if a match is found, None otherwise
pub fn find_prev(&mut self, buffer: &Buffer) -> Option<(usize, usize)> {
let cursor = buffer.cursor();
let lines = buffer.lines();
if self.query.is_empty() {
return None;
}
// Prepare query for comparison
let query = if self.case_sensitive {
self.query.clone()
} else {
self.query.to_lowercase()
};
let start_row = cursor.row;
let start_col = cursor.col;
// First, search backwards in the current line before cursor
if start_row < lines.len() && start_col > 0 {
let line = &lines[start_row];
let search_in = &line[..start_col];
let line_to_search = if self.case_sensitive {
search_in.to_string()
} else {
search_in.to_lowercase()
};
if let Some(pos) = line_to_search.rfind(&query) {
let match_pos = (start_row, pos);
self.last_match = Some(match_pos);
return Some(match_pos);
}
}
// Search previous lines backwards
if start_row > 0 {
for row in (0..start_row).rev() {
let line = &lines[row];
let line_to_search = if self.case_sensitive {
line.clone()
} else {
line.to_lowercase()
};
if let Some(col) = line_to_search.rfind(&query) {
let match_pos = (row, col);
self.last_match = Some(match_pos);
return Some(match_pos);
}
}
}
// Wrap around: search from end backwards to cursor position
for row in (start_row..lines.len()).rev() {
let line = &lines[row];
let search_from = if row == start_row { start_col } else { 0 };
let line_to_search = if self.case_sensitive {
line[search_from..].to_string()
} else {
line[search_from..].to_lowercase()
};
if let Some(pos) = line_to_search.rfind(&query) {
let match_pos = (row, search_from + pos);
self.last_match = Some(match_pos);
return Some(match_pos);
}
}
None
}
/// Get the query string
pub fn query(&self) -> &str {
&self.query
}
/// Get the last match position
pub fn last_match(&self) -> Option<(usize, usize)> {
self.last_match
}
}