From 2c2c14a360493b4fad9086b436359949041e5ec2 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 4 Dec 2023 23:00:01 +0200 Subject: [PATCH] Fix the regex matcher --- crates/project/src/project.rs | 11 ++++++----- crates/project/src/search.rs | 32 +++++++++++++++++++++++--------- crates/project/src/worktree.rs | 3 +-- crates/util/src/paths.rs | 12 +++++++++++- 4 files changed, 41 insertions(+), 17 deletions(-) diff --git a/crates/project/src/project.rs b/crates/project/src/project.rs index fde5e71df3..b1432265cc 100644 --- a/crates/project/src/project.rs +++ b/crates/project/src/project.rs @@ -5784,11 +5784,6 @@ impl Project { while let Some(ignored_abs_path) = ignored_paths_to_process.pop_front() { - if !query.file_matches(Some(&ignored_abs_path)) - || snapshot.is_path_excluded(ignored_abs_path.clone()) - { - continue; - } if let Some(fs_metadata) = fs .metadata(&ignored_abs_path) .await @@ -5816,6 +5811,12 @@ impl Project { } } } else if !fs_metadata.is_symlink { + if !query.file_matches(Some(&ignored_abs_path)) + || snapshot + .is_path_excluded(ignored_abs_path.clone()) + { + continue; + } let matches = if let Some(file) = fs .open_sync(&ignored_abs_path) .await diff --git a/crates/project/src/search.rs b/crates/project/src/search.rs index c673440326..d664ba67ed 100644 --- a/crates/project/src/search.rs +++ b/crates/project/src/search.rs @@ -371,15 +371,29 @@ impl SearchQuery { pub fn file_matches(&self, file_path: Option<&Path>) -> bool { match file_path { Some(file_path) => { - !self - .files_to_exclude() - .iter() - .any(|exclude_glob| exclude_glob.is_match(file_path)) - && (self.files_to_include().is_empty() - || self - .files_to_include() - .iter() - .any(|include_glob| include_glob.is_match(file_path))) + let mut path = file_path.to_path_buf(); + let mut matches = false; + loop { + matches = !self + .files_to_exclude() + .iter() + .any(|exclude_glob| exclude_glob.is_match(&path)) + && (self.files_to_include().is_empty() + || self + .files_to_include() + .iter() + .any(|include_glob| include_glob.is_match(&path))); + if matches || !path.pop() { + break; + } + } + + let path_str = file_path.to_string_lossy(); + if path_str.contains("node_modules") && path_str.contains("prettier") { + dbg!(path_str, path, matches); + } + + matches } None => self.files_to_include().is_empty(), } diff --git a/crates/project/src/worktree.rs b/crates/project/src/worktree.rs index b77b8a3fba..6855d59d6a 100644 --- a/crates/project/src/worktree.rs +++ b/crates/project/src/worktree.rs @@ -2290,10 +2290,9 @@ impl LocalSnapshot { return true; } if !path.pop() { - break; + return false; } } - false } } diff --git a/crates/util/src/paths.rs b/crates/util/src/paths.rs index 21d6ff74b2..44e78b9376 100644 --- a/crates/util/src/paths.rs +++ b/crates/util/src/paths.rs @@ -223,7 +223,7 @@ impl PathMatcher { pub fn is_match>(&self, other: P) -> bool { let other_path = other.as_ref(); other_path.starts_with(&self.maybe_path) - || other_path.file_name() == Some(self.maybe_path.as_os_str()) + || other_path.ends_with(&self.maybe_path) || self.glob.is_match(other_path) || self.check_with_end_separator(other_path) } @@ -422,4 +422,14 @@ mod tests { "Path matcher {path_matcher} should match {path:?}" ); } + + #[test] + fn project_search() { + let path = Path::new("/Users/someonetoignore/work/zed/zed.dev/node_modules"); + let path_matcher = PathMatcher::new("**/node_modules/**").unwrap(); + assert!( + path_matcher.is_match(&path), + "Path matcher {path_matcher} should match {path:?}" + ); + } }