Added Wrap around functionality to Replace menu

This commit is contained in:
RaspberryProgramming 2024-10-05 21:22:55 -04:00 committed by Jeremy Soller
parent 3b0acf8dac
commit f9ee60f1b7
2 changed files with 11 additions and 3 deletions

View file

@ -1756,7 +1756,7 @@ impl Application for App {
match self.config.find_regex(&self.find_search_value) {
Ok(regex) => {
//TODO: support captures
tab.replace(&regex, &self.find_replace_value);
tab.replace(&regex, &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(&regex, &self.find_replace_value) {}
while tab.replace(&regex, &self.find_replace_value, false) {}
return self.update(Message::TabChanged(self.tab_model.active()));
}
Err(err) => {

View file

@ -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
}