Skip to content

Commit

Permalink
fzf: rename set_normalization to set_candidate_normalization and …
Browse files Browse the repository at this point in the history
…add docs to it
  • Loading branch information
noib3 committed Nov 27, 2023
1 parent 0537a7a commit ff58791
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 35 deletions.
58 changes: 45 additions & 13 deletions src/algos/fzf/fzf_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,24 @@ pub struct FzfV1 {
case_sensitivity: CaseSensitivity,

/// TODO: docs
normalization: bool,
candidate_normalization: bool,

/// TODO: docs
scheme: Scheme,
scoring_scheme: Scheme,
}

impl core::fmt::Debug for FzfV1 {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let Some(scoring_scheme) = FzfScheme::from_inner(&self.scoring_scheme)
else {
return Ok(());
};

f.debug_struct("FzfV1")
.field("candidate_normalization", &self.candidate_normalization)
.field("case_sensitivity", &self.case_sensitivity)
.field("normalization", &self.normalization)
.field("scheme", &FzfScheme::from_inner(&self.scheme).unwrap())
.field("scoring_scheme", &scoring_scheme)
.finish_non_exhaustive()
}
}
Expand All @@ -90,7 +95,7 @@ impl FzfV1 {
/// TODO: docs
#[cfg(feature = "tests")]
pub fn scheme(&self) -> &Scheme {
&self.scheme
&self.scoring_scheme
}

/// Sets the case sensitivity to use when comparing the characters of the
Expand Down Expand Up @@ -121,17 +126,43 @@ impl FzfV1 {
self
}

/// TODO: docs
/// Sets whether multi-byte latin characters in the candidate should be
/// normalized to ASCII before comparing them to the query. The default is
/// `false`.
///
/// # Example
///
/// ```rust
/// # use norm::fzf::{FzfV1, FzfParser};
/// # use norm::{Metric, CaseSensitivity};
/// let mut fzf = FzfV1::new();
/// let mut parser = FzfParser::new();
///
/// // FzfV1 doesn't normalize candidates by default.
/// assert!(fzf.distance(parser.parse("foo"), "ƒöö").is_none());
///
/// fzf.set_candidate_normalization(true);
///
/// // With normalization enabled, we get a match.
/// assert!(fzf.distance(parser.parse("foo"), "ƒöö").is_some());
///
/// // Note that normalization is only applied to the candidate, the query
/// // is left untouched.
/// assert!(fzf.distance(parser.parse("ƒöö"), "foo").is_none());
/// ```
#[inline(always)]
pub fn set_normalization(&mut self, normalization: bool) -> &mut Self {
self.normalization = normalization;
pub fn set_candidate_normalization(
&mut self,
normalization: bool,
) -> &mut Self {
self.candidate_normalization = normalization;
self
}

/// TODO: docs
#[inline(always)]
pub fn with_scoring_scheme(&mut self, scheme: FzfScheme) -> &mut Self {
self.scheme = scheme.into_inner();
self.scoring_scheme = scheme.into_inner();
self
}
}
Expand Down Expand Up @@ -176,12 +207,12 @@ impl Fzf for FzfV1 {
CaseSensitivity::Smart => pattern.has_uppercase,
};

utils::char_eq(is_sensitive, self.normalization)
utils::char_eq(is_sensitive, self.candidate_normalization)
}

#[inline(always)]
fn scheme(&self) -> &Scheme {
&self.scheme
&self.scoring_scheme
}

#[inline(always)]
Expand All @@ -202,7 +233,8 @@ impl Fzf for FzfV1 {
CaseSensitivity::Smart => pattern.has_uppercase,
};

let opts = CandidateOpts::new(is_sensitive, self.normalization);
let opts =
CandidateOpts::new(is_sensitive, self.candidate_normalization);

let end_forward = forward_pass(pattern, candidate, opts)?;

Expand All @@ -214,7 +246,7 @@ impl Fzf for FzfV1 {
candidate,
start_backward..end_forward,
opts.char_eq,
&self.scheme,
&self.scoring_scheme,
ranges,
);

Expand Down
64 changes: 48 additions & 16 deletions src/algos/fzf/fzf_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ pub struct FzfV2 {
candidate_slab: CandidateSlab,

/// TODO: docs
case_sensitivity: CaseSensitivity,
candidate_normalization: bool,

/// TODO: docs
normalization: bool,
case_sensitivity: CaseSensitivity,

/// TODO: docs
scheme: Scheme,
scoring_scheme: Scheme,

/// TODO: docs
slab: V2Slab,
Expand All @@ -83,10 +83,15 @@ pub struct FzfV2 {
impl core::fmt::Debug for FzfV2 {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let Some(scoring_scheme) = FzfScheme::from_inner(&self.scoring_scheme)
else {
return Ok(());
};

f.debug_struct("FzfV2")
.field("candidate_normalization", &self.candidate_normalization)
.field("case_sensitivity", &self.case_sensitivity)
.field("normalization", &self.normalization)
.field("scheme", &FzfScheme::from_inner(&self.scheme).unwrap())
.field("scoring_scheme", &scoring_scheme)
.finish_non_exhaustive()
}
}
Expand All @@ -101,7 +106,7 @@ impl FzfV2 {
/// TODO: docs
#[cfg(feature = "tests")]
pub fn scheme(&self) -> &Scheme {
&self.scheme
&self.scoring_scheme
}

/// Sets the case sensitivity to use when comparing the characters of the
Expand Down Expand Up @@ -132,17 +137,43 @@ impl FzfV2 {
self
}

/// TODO: docs
/// Sets whether multi-byte latin characters in the candidate should be
/// normalized to ASCII before comparing them to the query. The default is
/// `false`.
///
/// # Example
///
/// ```rust
/// # use norm::fzf::{FzfV2, FzfParser};
/// # use norm::{Metric, CaseSensitivity};
/// let mut fzf = FzfV2::new();
/// let mut parser = FzfParser::new();
///
/// // FzfV2 doesn't normalize candidates by default.
/// assert!(fzf.distance(parser.parse("foo"), "ƒöö").is_none());
///
/// fzf.set_candidate_normalization(true);
///
/// // With normalization enabled, we get a match.
/// assert!(fzf.distance(parser.parse("foo"), "ƒöö").is_some());
///
/// // Note that normalization is only applied to the candidate, the query
/// // is left untouched.
/// assert!(fzf.distance(parser.parse("ƒöö"), "foo").is_none());
/// ```
#[inline(always)]
pub fn set_normalization(&mut self, normalization: bool) -> &mut Self {
self.normalization = normalization;
pub fn set_candidate_normalization(
&mut self,
normalization: bool,
) -> &mut Self {
self.candidate_normalization = normalization;
self
}

/// TODO: docs
#[inline(always)]
pub fn with_scoring_scheme(&mut self, scheme: FzfScheme) -> &mut Self {
self.scheme = scheme.into_inner();
self.scoring_scheme = scheme.into_inner();
self
}
}
Expand Down Expand Up @@ -187,12 +218,12 @@ impl Fzf for FzfV2 {
CaseSensitivity::Smart => pattern.has_uppercase,
};

utils::char_eq(is_sensitive, self.normalization)
utils::char_eq(is_sensitive, self.candidate_normalization)
}

#[inline(always)]
fn scheme(&self) -> &Scheme {
&self.scheme
&self.scoring_scheme
}

#[inline(always)]
Expand All @@ -213,7 +244,8 @@ impl Fzf for FzfV2 {
CaseSensitivity::Smart => pattern.has_uppercase,
};

let opts = CandidateOpts::new(is_sensitive, self.normalization);
let opts =
CandidateOpts::new(is_sensitive, self.candidate_normalization);

if pattern.char_len() == 1 {
return fuzzy_single_char::<RANGES>(
Expand All @@ -234,9 +266,9 @@ impl Fzf for FzfV2 {
if RANGES { candidate.to_byte_offset(first_offset) } else { 0 };

let initial_char_class = if first_offset == 0 {
self.scheme.initial_char_class
self.scoring_scheme.initial_char_class
} else {
char_class(candidate.char(first_offset - 1), &self.scheme)
char_class(candidate.char(first_offset - 1), &self.scoring_scheme)
};

let mut candidate = CandidateV2::new(
Expand All @@ -256,7 +288,7 @@ impl Fzf for FzfV2 {
pattern,
&mut candidate,
match_offsets,
&self.scheme,
&self.scoring_scheme,
);

if RANGES {
Expand Down
2 changes: 1 addition & 1 deletion src/metric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub trait Metric {
/// let mut parser = FzfParser::new();
/// let mut ranges = Vec::new();
///
/// fzf.set_normalization(true);
/// fzf.set_candidate_normalization(true);
///
/// let query = parser.parse("foo");
///
Expand Down
4 changes: 2 additions & 2 deletions tests/fzf_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ fn fzf_v1_score_5() {

let _ = fzf
.set_case_sensitivity(CaseSensitivity::Sensitive)
.set_normalization(true)
.set_candidate_normalization(true)
.distance_and_ranges(
parser.parse("e !"),
" !I\\hh+\u{364}",
Expand All @@ -315,7 +315,7 @@ fn fzf_v1_score_6() {

let _ = fzf
.set_case_sensitivity(CaseSensitivity::Insensitive)
.set_normalization(true)
.set_candidate_normalization(true)
.distance_and_ranges(query, "\u{364}", &mut ranges);

assert_eq!(ranges, [0..2]);
Expand Down
6 changes: 3 additions & 3 deletions tests/fzf_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ fn fzf_v2_score_4() {

let _ = fzf
.set_case_sensitivity(CaseSensitivity::Sensitive)
.set_normalization(true)
.set_candidate_normalization(true)
.distance_and_ranges(
parser.parse("e !"),
" !I\\hh+\u{364}",
Expand All @@ -304,7 +304,7 @@ fn fzf_v2_score_5() {

let _ = fzf
.set_case_sensitivity(CaseSensitivity::Insensitive)
.set_normalization(true)
.set_candidate_normalization(true)
.distance_and_ranges(parser.parse("E"), "\u{364}E", &mut ranges)
.unwrap();

Expand All @@ -323,7 +323,7 @@ fn fzf_v2_score_6() {

let distance = fzf
.set_case_sensitivity(CaseSensitivity::Insensitive)
.set_normalization(true)
.set_candidate_normalization(true)
.distance_and_ranges(
query,
"\u{6}\0\0 N\u{364}\u{e}\u{365}+",
Expand Down

0 comments on commit ff58791

Please sign in to comment.