Added wrap around feature to find menu

This commit is contained in:
RaspberryProgramming 2024-10-05 21:04:22 -04:00 • committed by Jeremy Soller
parent a23a95b609
commit 5070f0c696
4 changed files with 33 additions and 3 deletions

View file

@ -61,6 +61,7 @@ replace = Replace
replace-all = Replace all replace-all = Replace all
case-sensitive = Case sensitive case-sensitive = Case sensitive
use-regex = Use regex use-regex = Use regex
wrap-around = Wrap Around
# Menu # Menu

View file

@ -41,6 +41,7 @@ pub struct Config {
pub auto_indent: bool, pub auto_indent: bool,
pub find_case_sensitive: bool, pub find_case_sensitive: bool,
pub find_use_regex: bool, pub find_use_regex: bool,
pub find_wrap_around: bool,
pub font_name: String, pub font_name: String,
pub font_size: u16, pub font_size: u16,
pub highlight_current_line: bool, pub highlight_current_line: bool,
@ -59,6 +60,7 @@ impl Default for Config {
auto_indent: true, auto_indent: true,
find_case_sensitive: false, find_case_sensitive: false,
find_use_regex: false, find_use_regex: false,
find_wrap_around: true,
font_name: "Fira Mono".to_string(), font_name: "Fira Mono".to_string(),
font_size: 14, font_size: 14,
highlight_current_line: true, highlight_current_line: true,

View file

@ -337,6 +337,7 @@ pub enum Message {
FindReplaceValueChanged(String), FindReplaceValueChanged(String),
FindSearchValueChanged(String), FindSearchValueChanged(String),
FindUseRegex(bool), FindUseRegex(bool),
FindWrapAround(bool),
Focus, Focus,
GitProjectStatus(Vec<(String, PathBuf, Vec<GitStatus>)>), GitProjectStatus(Vec<(String, PathBuf, Vec<GitStatus>)>),
GitStage(PathBuf, PathBuf), GitStage(PathBuf, PathBuf),
@ -1708,7 +1709,7 @@ impl Application for App {
//TODO: do not compile find regex on every search? //TODO: do not compile find regex on every search?
match self.config.find_regex(&self.find_search_value) { match self.config.find_regex(&self.find_search_value) {
Ok(regex) => { Ok(regex) => {
tab.search(&regex, true); tab.search(&regex, true, self.config.find_wrap_around);
} }
Err(err) => { Err(err) => {
//TODO: put regex error in find box //TODO: put regex error in find box
@ -1731,7 +1732,7 @@ impl Application for App {
//TODO: do not compile find regex on every search? //TODO: do not compile find regex on every search?
match self.config.find_regex(&self.find_search_value) { match self.config.find_regex(&self.find_search_value) {
Ok(regex) => { Ok(regex) => {
tab.search(&regex, false); tab.search(&regex, false, self.config.find_wrap_around);
} }
Err(err) => { Err(err) => {
//TODO: put regex error in find box //TODO: put regex error in find box
@ -1812,6 +1813,10 @@ impl Application for App {
self.config.find_use_regex = find_use_regex; self.config.find_use_regex = find_use_regex;
return self.save_config(); 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) => { Message::GitProjectStatus(project_status) => {
self.git_project_status = Some(project_status); self.git_project_status = Some(project_status);
} }
@ -2855,6 +2860,12 @@ impl Application for App {
Message::FindUseRegex, Message::FindUseRegex,
) )
.into(), .into(),
widget::checkbox(
fl!("wrap-around"),
self.config.find_wrap_around,
Message::FindWrapAround,
)
.into(),
]) ])
.align_items(Alignment::Center) .align_items(Alignment::Center)
.padding(space_xxs) .padding(space_xxs)

View file

@ -245,10 +245,12 @@ impl EditorTab {
} }
// Code adapted from cosmic-text ViEditor search // 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 editor = self.editor.lock().unwrap();
let mut cursor = editor.cursor(); let mut cursor = editor.cursor();
let mut wrapped = false; // Keeps track of whether the search has wrapped around yet.
let start_line = cursor.line; let start_line = cursor.line;
if forwards { if forwards {
while cursor.line < editor.with_buffer(|buffer| buffer.lines.len()) { while cursor.line < editor.with_buffer(|buffer| buffer.lines.len()) {
if let Some(index) = editor.with_buffer(|buffer| { if let Some(index) = editor.with_buffer(|buffer| {
@ -269,6 +271,13 @@ impl EditorTab {
} }
cursor.line += 1; 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 { } else {
cursor.line += 1; cursor.line += 1;
@ -291,6 +300,13 @@ impl EditorTab {
editor.set_cursor(cursor); editor.set_cursor(cursor);
return true; 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 false