-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fuzzy matching for moon test --package
- Loading branch information
Showing
6 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
pub fn fuzzy_match<T: AsRef<str>>( | ||
needle: T, | ||
haystack: impl IntoIterator<Item = T>, | ||
) -> Option<Vec<String>> { | ||
let mut matcher = nucleo_matcher::Matcher::new(nucleo_matcher::Config::DEFAULT.match_paths()); | ||
let matches = nucleo_matcher::pattern::Pattern::parse( | ||
needle.as_ref(), | ||
nucleo_matcher::pattern::CaseMatching::Ignore, | ||
nucleo_matcher::pattern::Normalization::Smart, | ||
) | ||
.match_list(haystack, &mut matcher); | ||
if matches.is_empty() { | ||
None | ||
} else { | ||
Some( | ||
matches | ||
.into_iter() | ||
.map(|m| m.0.as_ref().to_string()) | ||
.collect(), | ||
) | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_fuzzy() { | ||
let haystack = [ | ||
"moonbitlang/core/builtin", | ||
"moonbitlang/core/int", | ||
"moonbitlang/core/list", | ||
"moonbitlang/core/list/internal", | ||
"moonbitlang/core/hashmap", | ||
]; | ||
let result = fuzzy_match("mci", haystack); | ||
expect_test::expect![[r#" | ||
Some( | ||
[ | ||
"moonbitlang/core/int", | ||
"moonbitlang/core/list/internal", | ||
"moonbitlang/core/list", | ||
"moonbitlang/core/builtin", | ||
], | ||
) | ||
"#]] | ||
.assert_debug_eq(&result); | ||
|
||
let result = fuzzy_match("moonbitlang/core/list", haystack); | ||
expect_test::expect![[r#" | ||
Some( | ||
[ | ||
"moonbitlang/core/list", | ||
"moonbitlang/core/list/internal", | ||
], | ||
) | ||
"#]] | ||
.assert_debug_eq(&result); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters