From be4a866a0c42f66976c1a8806f1a5a6be2f486f5 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 4 Sep 2019 10:09:08 -0400 Subject: [PATCH] Working incremental search --- src/editor.rs | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/editor.rs b/src/editor.rs index 2dadcda..b4a3da8 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -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) { 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();