Fix handling of uppercase characters in fuzzy finding

This commit is contained in:
Max Brunsfeld 2021-04-26 15:04:26 -07:00
parent 870925e2ac
commit e7c594262f
4 changed files with 52 additions and 24 deletions

View file

@ -141,9 +141,9 @@ impl FileFinder {
self.worktree(tree_id, app).map(|tree| { self.worktree(tree_id, app).map(|tree| {
let prefix = if self.include_root_name { let prefix = if self.include_root_name {
tree.root_name_chars() tree.root_name()
} else { } else {
&[] ""
}; };
let path = path_match.path.clone(); let path = path_match.path.clone();
let path_string = path_match.path.to_string_lossy(); let path_string = path_match.path.to_string_lossy();
@ -169,7 +169,7 @@ impl FileFinder {
let highlight_color = ColorU::from_u32(0x304ee2ff); let highlight_color = ColorU::from_u32(0x304ee2ff);
let bold = *Properties::new().weight(Weight::BOLD); let bold = *Properties::new().weight(Weight::BOLD);
let mut full_path = prefix.iter().collect::<String>(); let mut full_path = prefix.to_string();
full_path.push_str(&path_string); full_path.push_str(&path_string);
let mut container = Container::new( let mut container = Container::new(

View file

@ -68,16 +68,16 @@ struct FileHandleState {
impl Worktree { impl Worktree {
pub fn new(path: impl Into<Arc<Path>>, ctx: &mut ModelContext<Self>) -> Self { pub fn new(path: impl Into<Arc<Path>>, ctx: &mut ModelContext<Self>) -> Self {
let abs_path = path.into(); let abs_path = path.into();
let root_name_chars = abs_path.file_name().map_or(Vec::new(), |n| { let root_name = abs_path
n.to_string_lossy().chars().chain(Some('/')).collect() .file_name()
}); .map_or(String::new(), |n| n.to_string_lossy().to_string() + "/");
let (scan_state_tx, scan_state_rx) = smol::channel::unbounded(); let (scan_state_tx, scan_state_rx) = smol::channel::unbounded();
let id = ctx.model_id(); let id = ctx.model_id();
let snapshot = Snapshot { let snapshot = Snapshot {
id, id,
scan_id: 0, scan_id: 0,
abs_path, abs_path,
root_name_chars, root_name,
ignores: Default::default(), ignores: Default::default(),
entries: Default::default(), entries: Default::default(),
}; };
@ -224,7 +224,7 @@ pub struct Snapshot {
id: usize, id: usize,
scan_id: usize, scan_id: usize,
abs_path: Arc<Path>, abs_path: Arc<Path>,
root_name_chars: Vec<char>, root_name: String,
ignores: HashMap<Arc<Path>, (Arc<Gitignore>, usize)>, ignores: HashMap<Arc<Path>, (Arc<Gitignore>, usize)>,
entries: SumTree<Entry>, entries: SumTree<Entry>,
} }
@ -261,12 +261,10 @@ impl Snapshot {
self.entry_for_path("").unwrap() self.entry_for_path("").unwrap()
} }
pub fn root_name(&self) -> Option<&OsStr> { /// Returns the filename of the snapshot's root directory,
self.abs_path.file_name() /// with a trailing slash.
} pub fn root_name(&self) -> &str {
&self.root_name
pub fn root_name_chars(&self) -> &[char] {
&self.root_name_chars
} }
fn entry_for_path(&self, path: impl AsRef<Path>) -> Option<&Entry> { fn entry_for_path(&self, path: impl AsRef<Path>) -> Option<&Entry> {
@ -613,7 +611,12 @@ impl BackgroundScanner {
notify: Sender<ScanState>, notify: Sender<ScanState>,
worktree_id: usize, worktree_id: usize,
) -> Self { ) -> Self {
let root_char_bag = CharBag::from(snapshot.lock().root_name_chars.as_slice()); let root_char_bag = snapshot
.lock()
.root_name
.chars()
.map(|c| c.to_ascii_lowercase())
.collect();
let mut scanner = Self { let mut scanner = Self {
root_char_bag, root_char_bag,
snapshot, snapshot,
@ -1069,7 +1072,11 @@ impl BackgroundScanner {
fn char_bag(&self, path: &Path) -> CharBag { fn char_bag(&self, path: &Path) -> CharBag {
let mut result = self.root_char_bag; let mut result = self.root_char_bag;
result.extend(path.to_string_lossy().chars()); result.extend(
path.to_string_lossy()
.chars()
.map(|c| c.to_ascii_lowercase()),
);
result result
} }
} }
@ -1465,7 +1472,7 @@ mod tests {
abs_path: root_dir.path().into(), abs_path: root_dir.path().into(),
entries: Default::default(), entries: Default::default(),
ignores: Default::default(), ignores: Default::default(),
root_name_chars: Default::default(), root_name: Default::default(),
})), })),
Arc::new(Mutex::new(Default::default())), Arc::new(Mutex::new(Default::default())),
notify_tx, notify_tx,
@ -1500,7 +1507,7 @@ mod tests {
abs_path: root_dir.path().into(), abs_path: root_dir.path().into(),
entries: Default::default(), entries: Default::default(),
ignores: Default::default(), ignores: Default::default(),
root_name_chars: Default::default(), root_name: Default::default(),
})), })),
Arc::new(Mutex::new(Default::default())), Arc::new(Mutex::new(Default::default())),
notify_tx, notify_tx,

View file

@ -1,3 +1,5 @@
use std::iter::FromIterator;
#[derive(Copy, Clone, Debug, Default)] #[derive(Copy, Clone, Debug, Default)]
pub struct CharBag(u64); pub struct CharBag(u64);
@ -31,6 +33,14 @@ impl Extend<char> for CharBag {
} }
} }
impl FromIterator<char> for CharBag {
fn from_iter<T: IntoIterator<Item = char>>(iter: T) -> Self {
let mut result = Self::default();
result.extend(iter);
result
}
}
impl From<&str> for CharBag { impl From<&str> for CharBag {
fn from(s: &str) -> Self { fn from(s: &str) -> Self {
let mut bag = Self(0); let mut bag = Self(0);

View file

@ -171,10 +171,16 @@ fn match_single_tree_paths<'a>(
let mut lowercase_path_chars = Vec::new(); let mut lowercase_path_chars = Vec::new();
let prefix = if include_root_name { let prefix = if include_root_name {
snapshot.root_name_chars.as_slice() snapshot.root_name()
} else { } else {
&[] ""
}; }
.chars()
.collect::<Vec<_>>();
let lowercase_prefix = prefix
.iter()
.map(|c| c.to_ascii_lowercase())
.collect::<Vec<_>>();
for path_entry in path_entries { for path_entry in path_entries {
if !path_entry.char_bag.is_superset(query_chars) { if !path_entry.char_bag.is_superset(query_chars) {
@ -190,7 +196,7 @@ fn match_single_tree_paths<'a>(
if !find_last_positions( if !find_last_positions(
last_positions, last_positions,
prefix, &lowercase_prefix,
&lowercase_path_chars, &lowercase_path_chars,
&lowercase_query[..], &lowercase_query[..],
) { ) {
@ -209,6 +215,7 @@ fn match_single_tree_paths<'a>(
&path_chars, &path_chars,
&lowercase_path_chars, &lowercase_path_chars,
&prefix, &prefix,
&lowercase_prefix,
smart_case, smart_case,
&last_positions, &last_positions,
score_matrix, score_matrix,
@ -257,6 +264,7 @@ fn score_match(
path: &[char], path: &[char],
path_cased: &[char], path_cased: &[char],
prefix: &[char], prefix: &[char],
lowercase_prefix: &[char],
smart_case: bool, smart_case: bool,
last_positions: &[usize], last_positions: &[usize],
score_matrix: &mut [Option<f64>], score_matrix: &mut [Option<f64>],
@ -270,6 +278,7 @@ fn score_match(
path, path,
path_cased, path_cased,
prefix, prefix,
lowercase_prefix,
smart_case, smart_case,
last_positions, last_positions,
score_matrix, score_matrix,
@ -300,6 +309,7 @@ fn recursive_score_match(
path: &[char], path: &[char],
path_cased: &[char], path_cased: &[char],
prefix: &[char], prefix: &[char],
lowercase_prefix: &[char],
smart_case: bool, smart_case: bool,
last_positions: &[usize], last_positions: &[usize],
score_matrix: &mut [Option<f64>], score_matrix: &mut [Option<f64>],
@ -328,7 +338,7 @@ fn recursive_score_match(
let mut last_slash = 0; let mut last_slash = 0;
for j in path_idx..=limit { for j in path_idx..=limit {
let path_char = if j < prefix.len() { let path_char = if j < prefix.len() {
prefix[j] lowercase_prefix[j]
} else { } else {
path_cased[j - prefix.len()] path_cased[j - prefix.len()]
}; };
@ -404,6 +414,7 @@ fn recursive_score_match(
path, path,
path_cased, path_cased,
prefix, prefix,
lowercase_prefix,
smart_case, smart_case,
last_positions, last_positions,
score_matrix, score_matrix,
@ -547,7 +558,7 @@ mod tests {
abs_path: PathBuf::new().into(), abs_path: PathBuf::new().into(),
ignores: Default::default(), ignores: Default::default(),
entries: Default::default(), entries: Default::default(),
root_name_chars: Vec::new(), root_name: Default::default(),
}, },
false, false,
path_entries.into_iter(), path_entries.into_iter(),