Working incremental search

This commit is contained in:
Timothy Warren 2019-09-04 10:09:08 -04:00
parent ae96a76e23
commit be4a866a0c
1 changed files with 31 additions and 2 deletions

View File

@ -49,6 +49,8 @@ pub struct Editor {
// Properties not present in C version
output_buffer: String,
quit_times: u8,
search_last_match: i32,
search_direction: i8,
}
/// Keycode mapping enum
@ -96,8 +98,11 @@ impl Default for Editor {
filename: String::new(),
status_message: String::new(),
status_message_time: Instant::now(),
output_buffer: String::new(),
quit_times: KILO_QUIT_TIMES,
search_last_match: -1,
search_direction: 1,
}
}
}
@ -903,18 +908,42 @@ impl Editor {
fn find_callback(&mut self, query: &str, key: EditorKey<char>) {
if key == Enter || key == Escape {
self.search_last_match = -1;
self.search_direction = 1;
return;
} else if key == ArrowRight || key == ArrowDown {
self.search_direction = 1;
} else if key == ArrowLeft || key == ArrowUp {
self.search_direction = -1;
} else {
self.search_last_match = -1;
self.search_direction = 1;
}
if self.search_last_match == -1 {
self.search_direction = 1;
}
if query.is_empty() {
return;
}
let mut current = self.search_last_match;
for x in 0..self.rows.len() {
match self.rows[x].render.find(query) {
current += self.search_direction as i32;
if current == -1 {
current = self.rows.len() as i32 - 1;
} else if current == self.rows.len() as i32 {
current = 0;
}
let row = &self.rows[current as usize];
match row.render.find(query) {
None => (),
Some(start) => {
self.cursor_y = x;
self.search_last_match = current;
self.cursor_y = current as usize;
self.cursor_x = self.row_rx_to_cx(x, start);
self.row_offset = self.rows.len();