Add case sensitive and use regex toggles for find, fixes #144

This commit is contained in:
Jeremy Soller 2024-03-25 10:18:12 -06:00
parent abfb5d845f
commit 59933e6a03
No known key found for this signature in database
GPG key ID: D02FD439211AF56F
6 changed files with 133 additions and 33 deletions

View file

@ -317,12 +317,14 @@ pub enum Message {
DefaultFontSize(usize),
DialogMessage(DialogMessage),
Find(Option<bool>),
FindCaseSensitive(bool),
FindNext,
FindPrevious,
FindReplace,
FindReplaceAll,
FindReplaceValueChanged(String),
FindSearchValueChanged(String),
FindUseRegex(bool),
GitProjectStatus(Vec<(String, PathBuf, Vec<GitStatus>)>),
Key(Modifiers, keyboard::Key),
LaunchUrl(String),
@ -1556,10 +1558,27 @@ impl Application for App {
// Focus correct input
return self.update_focus();
}
Message::FindCaseSensitive(find_case_sensitive) => {
self.config.find_case_sensitive = find_case_sensitive;
return self.save_config();
}
Message::FindNext => {
if !self.find_search_value.is_empty() {
if let Some(Tab::Editor(tab)) = self.active_tab() {
tab.search(&self.find_search_value, true);
//TODO: do not compile find regex on every search?
match self.config.find_regex(&self.find_search_value) {
Ok(regex) => {
tab.search(&regex, true);
}
Err(err) => {
//TODO: put regex error in find box
log::warn!(
"failed to compile regex {:?}: {}",
self.find_search_value,
err
);
}
}
}
}
@ -1569,7 +1588,20 @@ impl Application for App {
Message::FindPrevious => {
if !self.find_search_value.is_empty() {
if let Some(Tab::Editor(tab)) = self.active_tab() {
tab.search(&self.find_search_value, false);
//TODO: do not compile find regex on every search?
match self.config.find_regex(&self.find_search_value) {
Ok(regex) => {
tab.search(&regex, false);
}
Err(err) => {
//TODO: put regex error in find box
log::warn!(
"failed to compile regex {:?}: {}",
self.find_search_value,
err
);
}
}
}
}
@ -1579,7 +1611,21 @@ impl Application for App {
Message::FindReplace => {
if !self.find_search_value.is_empty() {
if let Some(Tab::Editor(tab)) = self.active_tab() {
tab.replace(&self.find_search_value, &self.find_replace_value);
//TODO: do not compile find regex on every search?
match self.config.find_regex(&self.find_search_value) {
Ok(regex) => {
//TODO: support captures
tab.replace(&regex, &self.find_replace_value);
}
Err(err) => {
//TODO: put regex error in find box
log::warn!(
"failed to compile regex {:?}: {}",
self.find_search_value,
err
);
}
}
}
}
@ -1589,11 +1635,25 @@ impl Application for App {
Message::FindReplaceAll => {
if !self.find_search_value.is_empty() {
if let Some(Tab::Editor(tab)) = self.active_tab() {
{
let mut editor = tab.editor.lock().unwrap();
editor.set_cursor(cosmic_text::Cursor::new(0, 0));
//TODO: do not compile find regex on every search?
match self.config.find_regex(&self.find_search_value) {
Ok(regex) => {
//TODO: support captures
{
let mut editor = tab.editor.lock().unwrap();
editor.set_cursor(cosmic_text::Cursor::new(0, 0));
}
while tab.replace(&regex, &self.find_replace_value) {}
}
Err(err) => {
//TODO: put regex error in find box
log::warn!(
"failed to compile regex {:?}: {}",
self.find_search_value,
err
);
}
}
while tab.replace(&self.find_search_value, &self.find_replace_value) {}
}
}
@ -1606,6 +1666,10 @@ impl Application for App {
Message::FindSearchValueChanged(value) => {
self.find_search_value = value;
}
Message::FindUseRegex(find_use_regex) => {
self.config.find_use_regex = find_use_regex;
return self.save_config();
}
Message::GitProjectStatus(project_status) => {
self.git_project_status = Some(project_status);
}
@ -2480,7 +2544,7 @@ impl Application for App {
.padding(space_xxs)
.spacing(space_xxs);
let mut column = widget::column::with_capacity(2).push(find_widget);
let mut column = widget::column::with_capacity(3).push(find_widget);
if *replace {
let replace_input = widget::text_input::text_input(
fl!("replace-placeholder"),
@ -2524,6 +2588,26 @@ impl Application for App {
column = column.push(replace_widget);
}
column = column.push(
widget::row::with_children(vec![
widget::checkbox(
fl!("case-sensitive"),
self.config.find_case_sensitive,
Message::FindCaseSensitive,
)
.into(),
widget::checkbox(
fl!("use-regex"),
self.config.find_use_regex,
Message::FindUseRegex,
)
.into(),
])
.align_items(Alignment::Center)
.padding(space_xxs)
.spacing(space_xxs),
);
tab_column = tab_column
.push(widget::layer_container(column).layer(cosmic_theme::Layer::Primary));
}