Skip to content

Commit

Permalink
fzf: return default Match if query is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Nov 10, 2023
1 parent 995d7bf commit 4761304
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 8 deletions.
7 changes: 7 additions & 0 deletions src/algos/fzf/distance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ use super::{Distance, Score};
#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq)]
pub struct FzfDistance(Distance);

impl Default for FzfDistance {
#[inline]
fn default() -> Self {
Self::from_score(0)
}
}

impl FzfDistance {
/// TODO: docs
#[inline]
Expand Down
2 changes: 1 addition & 1 deletion src/algos/fzf/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl Metric for FzfV1 {
candidate: &str,
) -> Option<Match<Self::Distance>> {
if query.is_empty() {
return None;
return Some(Match::default());
}

let is_candidate_ascii = candidate.is_ascii();
Expand Down
2 changes: 1 addition & 1 deletion src/algos/fzf/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl Metric for FzfV2 {
candidate: &str,
) -> Option<Match<Self::Distance>> {
if query.is_empty() {
return None;
return Some(Match::default());
}

let is_candidate_ascii = candidate.is_ascii();
Expand Down
1 change: 1 addition & 0 deletions src/match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use core::ops::Range;
use crate::MatchedRanges;

/// TODO: docs
#[derive(Default)]
pub struct Match<D: Copy> {
/// TODO: docs
distance: D,
Expand Down
9 changes: 7 additions & 2 deletions tests/fzf_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ use norm::fzf::{bonus, penalty};
use norm::CaseSensitivity;
use CaseSensitivity::*;

pub fn empty_query<F: Fzf>() {
pub fn upstream_empty<F: Fzf>() {
let (_, m) = fzf::<F>(Insensitive, "", "foo");
assert!(m.is_none());

let m = m.unwrap();

assert_eq!(m.distance().into_score(), 0);

assert!(m.matched_ranges().is_empty());
}

pub fn upstream_fuzzy_1<F: Fzf>() {
Expand Down
4 changes: 2 additions & 2 deletions tests/fzf_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use fzf_common as common;
use norm::fzf::FzfV1;

#[test]
fn fzf_v1_empty_query() {
common::empty_query::<FzfV1>();
fn fzf_v1_upstream_empty() {
common::upstream_empty::<FzfV1>();
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions tests/fzf_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use norm::fzf::{bonus, FzfParser, FzfV2};
use norm::{CaseSensitivity, Metric};

#[test]
fn fzf_v2_empty_query() {
common::empty_query::<FzfV2>();
fn fzf_v2_upstream_empty_1() {
common::upstream_empty::<FzfV2>();
}

#[test]
Expand Down

0 comments on commit 4761304

Please sign in to comment.