Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Oct 20, 2023
1 parent ff0bc6d commit 45b712d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 38 deletions.
55 changes: 24 additions & 31 deletions src/algos/fzf_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct FzfQuery<'a> {
impl<'a> FzfQuery<'a> {
/// TODO: docs
#[inline]
pub fn from_str(s: &'a str) -> Self {
pub fn new(s: &'a str) -> Self {
Self { raw: s }
}

Expand All @@ -37,6 +37,22 @@ impl<'a> FzfQuery<'a> {
#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq)]
pub struct FzfDistance(Distance);

impl FzfDistance {
/// TODO: docs
#[inline]
fn from_score(score: Score) -> Self {
// The higher the score the lower the distance.
Self(Distance::MAX - score)
}

/// TODO: docs
#[cfg(feature = "tests")]
pub fn into_score(self) -> Score {
// The higher the score the lower the distance.
Distance::MAX - self.0
}
}

/// TODO: docs
#[derive(Debug, Default, Clone, Copy, Ord, PartialOrd, Eq, PartialEq)]
pub enum FzfScheme {
Expand All @@ -51,20 +67,6 @@ pub enum FzfScheme {
History,
}

impl FzfDistance {
/// TODO: docs
fn from_score(score: Score) -> Self {
// The higher the score the lower the distance.
Self(Distance::MAX - score)
}

/// TODO: docs
fn into_score(self) -> Score {
// The higher the score the lower the distance.
Distance::MAX - self.0
}
}

/// TODO: docs
#[cfg_attr(docsrs, doc(any(cfg(feature = "fzf-v1", feature = "fzf-v2"))))]
#[derive(Default)]
Expand Down Expand Up @@ -133,18 +135,6 @@ impl FzfV1 {
};
self
}

/// TODO: docs
#[doc(hidden)]
pub fn score(
&self,
query: FzfQuery<'_>,
candidate: &str,
) -> Option<Score> {
self.distance(query, candidate)
.map(|m| m.distance())
.map(FzfDistance::into_score)
}
}

impl Metric for FzfV1 {
Expand Down Expand Up @@ -283,6 +273,8 @@ pub(super) fn calculate_score(
// TODO: docs
let mut consecutive = 0u32;

let range_start = range.start;

let mut prev_class = candidate[..range.start]
.chars()
.next_back()
Expand Down Expand Up @@ -324,8 +316,9 @@ pub(super) fn calculate_score(

if track_matched_ranges {
if consecutive == 0 {
let range = offset..(offset + candidate_ch.len_utf8());
matched_ranges.push(range);
let start = range_start + offset;
let end = start + candidate_ch.len_utf8();
matched_ranges.push(start..end);
} else if let Some(last_range) = matched_ranges.last_mut() {
last_range.end += candidate_ch.len_utf8();
} else {
Expand Down Expand Up @@ -429,9 +422,9 @@ fn non_ascii_char_class(ch: char, scheme: &scheme::Scheme) -> CharClass {
} else if ch.is_numeric() {
CharClass::Number
} else if ch.is_alphabetic() {
CharClass::Upper
CharClass::Letter
} else if ch.is_whitespace() {
CharClass::Delimiter
CharClass::WhiteSpace
} else if (scheme.is_delimiter)(ch) {
CharClass::Delimiter
} else {
Expand Down
7 changes: 5 additions & 2 deletions src/match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use core::ops::Range;
pub struct Match<D: Copy> {
/// TODO: docs
distance: D,

/// TODO: docs
matched_ranges: Vec<Range<usize>>,
}

impl<D: Copy> Match<D> {
Expand All @@ -14,12 +17,12 @@ impl<D: Copy> Match<D> {

/// TODO: docs
pub fn matched_ranges(&self) -> &[Range<usize>] {
&[]
&self.matched_ranges
}

/// TODO: docs
#[inline]
pub(crate) fn new(distance: D, matched_ranges: Vec<Range<usize>>) -> Self {
Self { distance }
Self { distance, matched_ranges }
}
}
14 changes: 9 additions & 5 deletions tests/fzf_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@ use norm::{CaseSensitivity, Metric};
#[test]
fn fzf_v1_empty_query() {
let fzf = FzfV1::new();
let empty = FzfQuery::from_str("");
let empty = FzfQuery::new("");
assert!(fzf.distance(empty, "foo").is_none());
}

#[test]
fn fzf_v1_score_1() {
let fzf = FzfV1::new().with_case_sensitivity(CaseSensitivity::Insensitive);
let fzf = FzfV1::new()
.with_case_sensitivity(CaseSensitivity::Insensitive)
.with_matched_ranges(true);

let query = FzfQuery::from_str("oBZ");
let query = FzfQuery::new("oBZ");

let score = fzf.score(query, "fooBarbaz").unwrap();
let mach = fzf.distance(query, "fooBarbaz").unwrap();

assert_eq!(
score,
mach.distance().into_score(),
bonus::MATCH * 3 + bonus::CAMEL_123
- penalty::GAP_START
- penalty::GAP_EXTENSION * 3
);

assert_eq!(mach.matched_ranges(), [2..4, 8..9]);
}

0 comments on commit 45b712d

Please sign in to comment.