add --smart-case and --ignore-case flags to find command, enable --smart-case by default

This commit is contained in:
Nguyen Duc Toan 2023-10-03 22:56:48 +07:00
parent 5af7c8eed9
commit 2a1ce22628
4 changed files with 35 additions and 11 deletions

View file

@ -1,4 +1,4 @@
use core::{emit, files::FilesSorter, input::InputMode}; use core::{emit, files::FilesSorter, input::InputMode, manager::FinderCase};
use config::{keymap::{Control, Exec, Key, KeymapLayer}, manager::SortBy, KEYMAP}; use config::{keymap::{Control, Exec, Key, KeymapLayer}, manager::SortBy, KEYMAP};
use shared::{optional_bool, Url}; use shared::{optional_bool, Url};
@ -154,7 +154,14 @@ impl Executor {
"find" => { "find" => {
let query = exec.args.get(0).map(|s| s.as_str()); let query = exec.args.get(0).map(|s| s.as_str());
let prev = exec.named.contains_key("previous"); let prev = exec.named.contains_key("previous");
cx.manager.active_mut().find(query, prev) let smart_case = exec.named.contains_key("smart-case");
let ingore_case = exec.named.contains_key("ignore-case");
let case = match (smart_case, ingore_case) {
(false, false) => FinderCase::CaseSensitive,
(false, true) => FinderCase::CaseInsensitive,
(true, _) => FinderCase::SmartCase, // prioritize smart-case over ignore-case
};
cx.manager.active_mut().find(query, prev, case)
} }
"find_arrow" => cx.manager.active_mut().find_arrow(exec.named.contains_key("previous")), "find_arrow" => cx.manager.active_mut().find_arrow(exec.named.contains_key("previous")),

View file

@ -73,8 +73,8 @@ keymap = [
{ on = [ "c", "n" ], exec = "copy name_without_ext", desc = "Copy the name of the file without the extension" }, { on = [ "c", "n" ], exec = "copy name_without_ext", desc = "Copy the name of the file without the extension" },
# Find # Find
{ on = [ "/" ], exec = "find" }, { on = [ "/" ], exec = "find --smart-case" },
{ on = [ "?" ], exec = "find --previous" }, { on = [ "?" ], exec = "find --previous --smart-case" },
{ on = [ "n" ], exec = "find_arrow" }, { on = [ "n" ], exec = "find_arrow" },
{ on = [ "N" ], exec = "find_arrow --previous" }, { on = [ "N" ], exec = "find_arrow --previous" },

View file

@ -6,6 +6,13 @@ use shared::Url;
use crate::files::Files; use crate::files::Files;
#[derive(Debug, PartialEq, Eq)]
pub enum FinderCase {
CaseSensitive,
CaseInsensitive,
SmartCase,
}
pub struct Finder { pub struct Finder {
query: Regex, query: Regex,
matched: BTreeMap<Url, u8>, matched: BTreeMap<Url, u8>,
@ -13,9 +20,15 @@ pub struct Finder {
} }
impl Finder { impl Finder {
pub(super) fn new(s: &str) -> Result<Self> { pub(super) fn new(s: &str, case: FinderCase) -> Result<Self> {
let query = match case {
FinderCase::CaseSensitive => Regex::new(s)?,
FinderCase::CaseInsensitive => RegexBuilder::new(s).case_insensitive(true).build()?,
FinderCase::SmartCase => {
let uppercase = s.chars().any(|c| c.is_uppercase()); let uppercase = s.chars().any(|c| c.is_uppercase());
let query = RegexBuilder::new(s).case_insensitive(!uppercase).build()?; RegexBuilder::new(s).case_insensitive(!uppercase).build()?
},
};
Ok(Self { query, matched: Default::default(), version: 0 }) Ok(Self { query, matched: Default::default(), version: 0 })
} }

View file

@ -6,7 +6,7 @@ use shared::{Debounce, Defer, InputError, Url};
use tokio::{pin, task::JoinHandle}; use tokio::{pin, task::JoinHandle};
use tokio_stream::{wrappers::UnboundedReceiverStream, StreamExt}; use tokio_stream::{wrappers::UnboundedReceiverStream, StreamExt};
use super::{Backstack, Finder, Folder, Mode, Preview, PreviewLock}; use super::{Backstack, Finder, Folder, Mode, Preview, PreviewLock, FinderCase};
use crate::{emit, external::{self, FzfOpt, ZoxideOpt}, files::{File, FilesOp, FilesSorter}, input::InputOpt, Event, Step, BLOCKER}; use crate::{emit, external::{self, FzfOpt, ZoxideOpt}, files::{File, FilesOp, FilesSorter}, input::InputOpt, Event, Step, BLOCKER};
pub struct Tab { pub struct Tab {
@ -267,9 +267,9 @@ impl Tab {
false false
} }
pub fn find(&mut self, query: Option<&str>, prev: bool) -> bool { pub fn find(&mut self, query: Option<&str>, prev: bool, case: FinderCase) -> bool {
if let Some(query) = query { if let Some(query) = query {
let Ok(finder) = Finder::new(query) else { let Ok(finder) = Finder::new(query, case) else {
return false; return false;
}; };
@ -297,7 +297,11 @@ impl Tab {
while let Some(Ok(s)) | Some(Err(InputError::Typed(s))) = rx.next().await { while let Some(Ok(s)) | Some(Err(InputError::Typed(s))) = rx.next().await {
emit!(Call( emit!(Call(
Exec::call("find", vec![s]).with_bool("previous", prev).vec(), Exec::call("find", vec![s])
.with_bool("previous", prev)
.with_bool("smart-case", case == FinderCase::SmartCase)
.with_bool("ignore-case", case == FinderCase::CaseInsensitive)
.vec(),
KeymapLayer::Manager KeymapLayer::Manager
)); ));
} }