From 5070f0c696803e494385cc9c23817e26b3222b54 Mon Sep 17 00:00:00 2001 From: RaspberryProgramming Date: Sat, 5 Oct 2024 21:04:22 -0400 Subject: [PATCH] Added wrap around feature to find menu --- i18n/en/cosmic_edit.ftl | 1 + src/config.rs | 2 ++ src/main.rs | 15 +++++++++++++-- src/tab.rs | 18 +++++++++++++++++- 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/i18n/en/cosmic_edit.ftl b/i18n/en/cosmic_edit.ftl index 96645ed..57e80db 100644 --- a/i18n/en/cosmic_edit.ftl +++ b/i18n/en/cosmic_edit.ftl @@ -61,6 +61,7 @@ replace = Replace replace-all = Replace all case-sensitive = Case sensitive use-regex = Use regex +wrap-around = Wrap Around # Menu diff --git a/src/config.rs b/src/config.rs index a0f8665..2766b41 100644 --- a/src/config.rs +++ b/src/config.rs @@ -41,6 +41,7 @@ pub struct Config { pub auto_indent: bool, pub find_case_sensitive: bool, pub find_use_regex: bool, + pub find_wrap_around: bool, pub font_name: String, pub font_size: u16, pub highlight_current_line: bool, @@ -59,6 +60,7 @@ impl Default for Config { auto_indent: true, find_case_sensitive: false, find_use_regex: false, + find_wrap_around: true, font_name: "Fira Mono".to_string(), font_size: 14, highlight_current_line: true, diff --git a/src/main.rs b/src/main.rs index ae1379c..0d54531 100644 --- a/src/main.rs +++ b/src/main.rs @@ -337,6 +337,7 @@ pub enum Message { FindReplaceValueChanged(String), FindSearchValueChanged(String), FindUseRegex(bool), + FindWrapAround(bool), Focus, GitProjectStatus(Vec<(String, PathBuf, Vec)>), GitStage(PathBuf, PathBuf), @@ -1708,7 +1709,7 @@ impl Application for App { //TODO: do not compile find regex on every search? match self.config.find_regex(&self.find_search_value) { Ok(regex) => { - tab.search(®ex, true); + tab.search(®ex, true, self.config.find_wrap_around); } Err(err) => { //TODO: put regex error in find box @@ -1731,7 +1732,7 @@ impl Application for App { //TODO: do not compile find regex on every search? match self.config.find_regex(&self.find_search_value) { Ok(regex) => { - tab.search(®ex, false); + tab.search(®ex, false, self.config.find_wrap_around); } Err(err) => { //TODO: put regex error in find box @@ -1812,6 +1813,10 @@ impl Application for App { self.config.find_use_regex = find_use_regex; return self.save_config(); } + Message::FindWrapAround(find_wrap_around) => { + self.config.find_wrap_around = find_wrap_around; + return self.save_config(); + } Message::GitProjectStatus(project_status) => { self.git_project_status = Some(project_status); } @@ -2855,6 +2860,12 @@ impl Application for App { Message::FindUseRegex, ) .into(), + widget::checkbox( + fl!("wrap-around"), + self.config.find_wrap_around, + Message::FindWrapAround, + ) + .into(), ]) .align_items(Alignment::Center) .padding(space_xxs) diff --git a/src/tab.rs b/src/tab.rs index aac6287..af8a553 100644 --- a/src/tab.rs +++ b/src/tab.rs @@ -245,10 +245,12 @@ impl EditorTab { } // Code adapted from cosmic-text ViEditor search - pub fn search(&self, regex: &Regex, forwards: bool) -> bool { + pub fn search(&self, regex: &Regex, forwards: bool, 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; + if forwards { while cursor.line < editor.with_buffer(|buffer| buffer.lines.len()) { if let Some(index) = editor.with_buffer(|buffer| { @@ -269,6 +271,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; + } } } else { cursor.line += 1; @@ -291,6 +300,13 @@ impl EditorTab { editor.set_cursor(cursor); return true; } + + // If we haven't wrapped yet and we've reached the first line, reset cursor line to the + // last line and set wrapped to true so we don't wrap again + if wrap_around && !wrapped && cursor.line == 0 { + cursor.line = editor.with_buffer(|buffer| buffer.lines.len()); + wrapped = true; + } } } false