-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Adjust fuzzy matching to be more inline with other editors #3969
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,74 @@ | ||
use fuzzy_matcher::skim::SkimMatcherV2 as Matcher; | ||
use fuzzy_matcher::FuzzyMatcher; | ||
|
||
#[cfg(test)] | ||
mod test; | ||
|
||
pub struct FuzzyQuery { | ||
queries: Vec<String>, | ||
} | ||
|
||
impl FuzzyQuery { | ||
pub fn new(query: &str) -> FuzzyQuery { | ||
let mut saw_backslash = false; | ||
let queries = query | ||
.split(|c| { | ||
saw_backslash = match c { | ||
' ' if !saw_backslash => return true, | ||
'\\' => true, | ||
_ => false, | ||
}; | ||
false | ||
}) | ||
.filter_map(|query| { | ||
if query.is_empty() { | ||
None | ||
} else { | ||
Some(query.replace("\\ ", " ")) | ||
} | ||
}) | ||
.collect(); | ||
FuzzyQuery { queries } | ||
} | ||
|
||
pub fn fuzzy_match(&self, item: &str, matcher: &Matcher) -> Option<i64> { | ||
// use the rank of the first query for the rank, because merging ranks is not really possible | ||
// this behaviour matches fzf and skim | ||
let score = matcher.fuzzy_match(item, self.queries.get(0)?)?; | ||
if self | ||
.queries | ||
.iter() | ||
.any(|query| matcher.fuzzy_match(item, query).is_none()) | ||
{ | ||
return None; | ||
} | ||
Some(score) | ||
} | ||
|
||
pub fn fuzzy_indicies(&self, item: &str, matcher: &Matcher) -> Option<(i64, Vec<usize>)> { | ||
if self.queries.len() == 1 { | ||
return matcher.fuzzy_indices(item, &self.queries[0]); | ||
} | ||
|
||
// use the rank of the first query for the rank, because merging ranks is not really possible | ||
// this behaviour matches fzf and skim | ||
let (score, mut indicies) = matcher.fuzzy_indices(item, self.queries.get(0)?)?; | ||
|
||
// fast path for the common case of not using a space | ||
// during matching this branch should be free thanks to branch prediction | ||
if self.queries.len() == 1 { | ||
return Some((score, indicies)); | ||
} | ||
|
||
for query in &self.queries[1..] { | ||
let (_, matched_indicies) = matcher.fuzzy_indices(item, query)?; | ||
indicies.extend_from_slice(&matched_indicies); | ||
} | ||
|
||
// deadup and remove duplicate matches | ||
indicies.sort_unstable(); | ||
indicies.dedup(); | ||
|
||
Some((score, indicies)) | ||
} | ||
} |
This file contains hidden or 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,47 @@ | ||
use crate::ui::fuzzy_match::FuzzyQuery; | ||
use crate::ui::fuzzy_match::Matcher; | ||
|
||
fn run_test<'a>(query: &str, items: &'a [&'a str]) -> Vec<String> { | ||
let query = FuzzyQuery::new(query); | ||
let matcher = Matcher::default(); | ||
items | ||
.iter() | ||
.filter_map(|item| { | ||
let (_, indicies) = query.fuzzy_indicies(item, &matcher)?; | ||
let matched_string = indicies | ||
.iter() | ||
.map(|&pos| item.chars().nth(pos).unwrap()) | ||
.collect(); | ||
Some(matched_string) | ||
}) | ||
.collect() | ||
} | ||
|
||
#[test] | ||
fn match_single_value() { | ||
let matches = run_test("foo", &["foobar", "foo", "bar"]); | ||
assert_eq!(matches, &["foo", "foo"]) | ||
} | ||
|
||
#[test] | ||
fn match_multiple_values() { | ||
let matches = run_test( | ||
"foo bar", | ||
&["foo bar", "foo bar", "bar foo", "bar", "foo"], | ||
); | ||
assert_eq!(matches, &["foobar", "foobar", "barfoo"]) | ||
} | ||
|
||
#[test] | ||
fn space_escape() { | ||
let matches = run_test(r"foo\ bar", &["bar foo", "foo bar", "foobar"]); | ||
assert_eq!(matches, &["foo bar"]) | ||
} | ||
|
||
#[test] | ||
fn trim() { | ||
let matches = run_test(r" foo bar ", &["bar foo", "foo bar", "foobar"]); | ||
assert_eq!(matches, &["barfoo", "foobar", "foobar"]); | ||
let matches = run_test(r" foo bar\ ", &["bar foo", "foo bar", "foobar"]); | ||
assert_eq!(matches, &["bar foo"]) | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
mod completion; | ||
pub(crate) mod editor; | ||
mod fuzzy_match; | ||
mod info; | ||
pub mod lsp; | ||
mod markdown; | ||
|
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.