Skip to content

Commit

Permalink
Added Wrap around functionality to Replace menu
Browse files Browse the repository at this point in the history
  • Loading branch information
RaspberryProgramming authored and jackpot51 committed Oct 6, 2024
1 parent 3b0acf8 commit f9ee60f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) => {
Expand Down
10 changes: 9 additions & 1 deletion src/tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit f9ee60f

Please sign in to comment.