simplify the code [no ci]

This commit is contained in:
sxyazi 2023-10-04 14:31:40 +08:00
parent 451f43fe15
commit 1e0a73c4a6
No known key found for this signature in database
4 changed files with 16 additions and 17 deletions

View file

@ -154,12 +154,11 @@ 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");
let smart_case = exec.named.contains_key("smart-case"); let case = match (exec.named.contains_key("smart"), exec.named.contains_key("insensitive"))
let ingore_case = exec.named.contains_key("ignore-case"); {
let case = match (smart_case, ingore_case) { (false, false) => FinderCase::Sensitive,
(false, false) => FinderCase::CaseSensitive, (false, true) => FinderCase::Insensitive,
(false, true) => FinderCase::CaseInsensitive, (true, _) => FinderCase::Smart, // prioritize smart over insensitive
(true, _) => FinderCase::SmartCase, // prioritize smart-case over ignore-case
}; };
cx.manager.active_mut().find(query, prev, case) cx.manager.active_mut().find(query, prev, case)
} }

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 --smart-case" }, { on = [ "/" ], exec = "find --smart" },
{ on = [ "?" ], exec = "find --previous --smart-case" }, { on = [ "?" ], exec = "find --previous --smart" },
{ 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,11 +6,11 @@ use shared::Url;
use crate::files::Files; use crate::files::Files;
#[derive(Debug, PartialEq, Eq)] #[derive(PartialEq, Eq)]
pub enum FinderCase { pub enum FinderCase {
CaseSensitive, Sensitive,
CaseInsensitive, Insensitive,
SmartCase, Smart,
} }
pub struct Finder { pub struct Finder {
@ -22,9 +22,9 @@ pub struct Finder {
impl Finder { impl Finder {
pub(super) fn new(s: &str, case: FinderCase) -> Result<Self> { pub(super) fn new(s: &str, case: FinderCase) -> Result<Self> {
let query = match case { let query = match case {
FinderCase::CaseSensitive => Regex::new(s)?, FinderCase::Sensitive => Regex::new(s)?,
FinderCase::CaseInsensitive => RegexBuilder::new(s).case_insensitive(true).build()?, FinderCase::Insensitive => RegexBuilder::new(s).case_insensitive(true).build()?,
FinderCase::SmartCase => { FinderCase::Smart => {
let uppercase = s.chars().any(|c| c.is_uppercase()); let uppercase = s.chars().any(|c| c.is_uppercase());
RegexBuilder::new(s).case_insensitive(!uppercase).build()? RegexBuilder::new(s).case_insensitive(!uppercase).build()?
} }

View file

@ -299,8 +299,8 @@ impl Tab {
emit!(Call( emit!(Call(
Exec::call("find", vec![s]) Exec::call("find", vec![s])
.with_bool("previous", prev) .with_bool("previous", prev)
.with_bool("smart-case", case == FinderCase::SmartCase) .with_bool("smart", case == FinderCase::Smart)
.with_bool("ignore-case", case == FinderCase::CaseInsensitive) .with_bool("insensitive", case == FinderCase::Insensitive)
.vec(), .vec(),
KeymapLayer::Manager KeymapLayer::Manager
)); ));