Skip to content

Commit

Permalink
fzf: return a null score if pattern is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Nov 12, 2023
1 parent 792664f commit 617342f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/algos/fzf/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ pub(super) fn exact_match(
with_matched_ranges: bool,
matched_ranges: &mut MatchedRanges,
) -> Option<Score> {
if pattern.is_empty() {
return Some(0);
}

// TODO: docs
let mut best_bonus: i64 = -1;

Expand Down Expand Up @@ -209,6 +213,10 @@ pub(super) fn prefix_match(
with_matched_ranges: bool,
matched_ranges: &mut MatchedRanges,
) -> Option<Score> {
if pattern.is_empty() {
return Some(0);
}

let mut pattern_chars = pattern.chars();

let ignored_leading_spaces =
Expand Down Expand Up @@ -256,6 +264,10 @@ pub(super) fn suffix_match(
with_matched_ranges: bool,
matched_ranges: &mut MatchedRanges,
) -> Option<Score> {
if pattern.is_empty() {
return Some(0);
}

let mut pattern_chars = pattern.chars().rev();

let up_to_ignored_spaces = candidate.len()
Expand Down Expand Up @@ -305,6 +317,10 @@ pub(super) fn equal_match(
with_matched_ranges: bool,
matched_ranges: &mut MatchedRanges,
) -> Option<Score> {
if pattern.is_empty() {
return Some(0);
}

let ignored_leading_spaces =
ignored_candidate_leading_spaces(pattern, candidate)?;

Expand Down
4 changes: 4 additions & 0 deletions src/algos/fzf/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ pub(super) fn fzf_v1(
is_candidate_ascii: bool,
matched_ranges: &mut MatchedRanges,
) -> Option<Score> {
if pattern.is_empty() {
return Some(0);
}

let range_forward = forward_pass(
pattern,
candidate,
Expand Down
4 changes: 4 additions & 0 deletions src/algos/fzf/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ pub(super) fn fzf_v2(
(slab, is_candidate_ascii): (&mut V2Slab, bool),
ranges: &mut MatchedRanges,
) -> Option<Score> {
if pattern.is_empty() {
return Some(0);
}

let (matches, last_match_offset) = matches(
&mut slab.matched_indices,
pattern,
Expand Down
13 changes: 13 additions & 0 deletions tests/fzf_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,16 @@ fn fzf_v2_score_1() {

assert_eq!(mach.matched_ranges().sorted(), [0..5]);
}

#[test]
fn fzf_v2_score_2() {
let mut fzf = FzfV2::new()
.with_case_sensitivity(CaseSensitivity::Sensitive)
.with_matched_ranges(true);

let mut parser = FzfParser::new();

let mach = fzf.distance(parser.parse("!$"), "$$2");

assert!(mach.is_none());
}

0 comments on commit 617342f

Please sign in to comment.