From f9ee60f1b7617547390d7f98a6fd12b5dd8d2cdb Mon Sep 17 00:00:00 2001 From: RaspberryProgramming Date: Sat, 5 Oct 2024 21:22:55 -0400 Subject: [PATCH] Added Wrap around functionality to Replace menu --- src/main.rs | 4 ++-- src/tab.rs | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0d54531..0ab9659 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1756,7 +1756,7 @@ impl Application for App { match self.config.find_regex(&self.find_search_value) { Ok(regex) => { //TODO: support captures - tab.replace(®ex, &self.find_replace_value); + tab.replace(®ex, &self.find_replace_value, self.config.find_wrap_around);; return self.update(Message::TabChanged(self.tab_model.active())); } Err(err) => { @@ -1785,7 +1785,7 @@ impl Application for App { let mut editor = tab.editor.lock().unwrap(); editor.set_cursor(cosmic_text::Cursor::new(0, 0)); } - while tab.replace(®ex, &self.find_replace_value) {} + while tab.replace(®ex, &self.find_replace_value, false) {} return self.update(Message::TabChanged(self.tab_model.active())); } Err(err) => { diff --git a/src/tab.rs b/src/tab.rs index af8a553..4d8c569 100644 --- a/src/tab.rs +++ b/src/tab.rs @@ -209,9 +209,10 @@ impl EditorTab { } } - pub fn replace(&self, regex: &Regex, replace: &str) -> bool { + pub fn replace(&self, regex: &Regex, replace: &str, wrap_around: bool) -> bool { let mut editor = self.editor.lock().unwrap(); let mut cursor = editor.cursor(); + let mut wrapped = false; // Keeps track of whether the search has wrapped around yet. let start_line = cursor.line; while cursor.line < editor.with_buffer(|buffer| buffer.lines.len()) { if let Some((index, len)) = editor.with_buffer(|buffer| { @@ -240,6 +241,13 @@ impl EditorTab { } cursor.line += 1; + + // If we haven't wrapped yet and we've reached the last line, reset cursor line to 0 and + // set wrapped to true so we don't wrap again + if wrap_around && !wrapped && cursor.line == editor.with_buffer(|buffer| buffer.lines.len()) { + cursor.line = 0; + wrapped = true; + } } false }