feat: added new options to the `find' command for smart-case/case-insensitive finding (#240)

This commit is contained in:
Nguyễn Đức Toàn 2023-10-04 13:42:53 +07:00 committed by GitHub
parent 77988d47a4
commit 6da04c7eb5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 36 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 shared::{optional_bool, Url};
@ -154,7 +154,13 @@ impl Executor {
"find" => {
let query = exec.args.get(0).map(|s| s.as_str());
let prev = exec.named.contains_key("previous");
cx.manager.active_mut().find(query, prev)
let case = match (exec.named.contains_key("smart"), exec.named.contains_key("insensitive"))
{
(true, _) => FinderCase::Smart,
(_, false) => FinderCase::Sensitive,
(_, true) => FinderCase::Insensitive,
};
cx.manager.active_mut().find(query, prev, case)
}
"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" },
# Find
{ on = [ "/" ], exec = "find" },
{ on = [ "?" ], exec = "find --previous" },
{ on = [ "/" ], exec = "find --smart" },
{ on = [ "?" ], exec = "find --previous --smart" },
{ on = [ "n" ], exec = "find_arrow" },
{ on = [ "N" ], exec = "find_arrow --previous" },

View file

@ -1,11 +1,18 @@
use std::{collections::BTreeMap, ffi::OsStr};
use anyhow::Result;
use regex::bytes::Regex;
use regex::bytes::{Regex, RegexBuilder};
use shared::Url;
use crate::files::Files;
#[derive(PartialEq, Eq)]
pub enum FinderCase {
Smart,
Sensitive,
Insensitive,
}
pub struct Finder {
query: Regex,
matched: BTreeMap<Url, u8>,
@ -13,8 +20,16 @@ pub struct Finder {
}
impl Finder {
pub(super) fn new(s: &str) -> Result<Self> {
Ok(Self { query: Regex::new(s)?, matched: Default::default(), version: 0 })
pub(super) fn new(s: &str, case: FinderCase) -> Result<Self> {
let query = match case {
FinderCase::Smart => {
let uppercase = s.chars().any(|c| c.is_uppercase());
RegexBuilder::new(s).case_insensitive(!uppercase).build()?
}
FinderCase::Sensitive => Regex::new(s)?,
FinderCase::Insensitive => RegexBuilder::new(s).case_insensitive(true).build()?,
};
Ok(Self { query, matched: Default::default(), version: 0 })
}
pub(super) fn prev(&self, files: &Files, cursor: usize, include: bool) -> Option<isize> {

View file

@ -6,7 +6,7 @@ use shared::{Debounce, Defer, InputError, Url};
use tokio::{pin, task::JoinHandle};
use tokio_stream::{wrappers::UnboundedReceiverStream, StreamExt};
use super::{Backstack, Finder, Folder, Mode, Preview, PreviewLock};
use super::{Backstack, Finder, FinderCase, Folder, Mode, Preview, PreviewLock};
use crate::{emit, external::{self, FzfOpt, ZoxideOpt}, files::{File, FilesOp, FilesSorter}, input::InputOpt, Event, Step, BLOCKER};
pub struct Tab {
@ -267,9 +267,9 @@ impl Tab {
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 {
let Ok(finder) = Finder::new(query) else {
let Ok(finder) = Finder::new(query, case) else {
return false;
};
@ -297,7 +297,11 @@ impl Tab {
while let Some(Ok(s)) | Some(Err(InputError::Typed(s))) = rx.next().await {
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 == FinderCase::Smart)
.with_bool("insensitive", case == FinderCase::Insensitive)
.vec(),
KeymapLayer::Manager
));
}