Skip to content

Commit

Permalink
fzf: rename with_case_sensitivity to set_case_sensitivity and add…
Browse files Browse the repository at this point in the history
… docs to it
  • Loading branch information
noib3 committed Nov 27, 2023
1 parent c4a0145 commit 900b9e4
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 30 deletions.
4 changes: 2 additions & 2 deletions benches/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub trait Metric {
ranges: &mut Vec<Range<usize>>,
);

fn with_case_sensitivity(
fn set_case_sensitivity(
&mut self,
case_sensitivity: CaseSensitivity,
) -> &mut Self;
Expand Down Expand Up @@ -76,7 +76,7 @@ fn for_all_cases_and_ranges<M, F>(
CaseSensitivity::Smart,
] {
for with_ranges in [true, false] {
metric.with_case_sensitivity(case);
metric.set_case_sensitivity(case);
let param = param(case, with_ranges, suffix);
let mut ranges = with_ranges.then(Vec::new);
fun(
Expand Down
4 changes: 2 additions & 2 deletions benches/fzf_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ impl bench::Metric for FzfV1 {
) {
self.distance_and_ranges(query, candidate, ranges);
}
fn with_case_sensitivity(
fn set_case_sensitivity(
&mut self,
case_sensitivity: CaseSensitivity,
) -> &mut Self {
self.with_case_sensitivity(case_sensitivity)
self.set_case_sensitivity(case_sensitivity)
}
}

Expand Down
4 changes: 2 additions & 2 deletions benches/fzf_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ impl bench::Metric for FzfV2 {
) {
self.distance_and_ranges(query, candidate, ranges);
}
fn with_case_sensitivity(
fn set_case_sensitivity(
&mut self,
case_sensitivity: CaseSensitivity,
) -> &mut Self {
self.with_case_sensitivity(case_sensitivity)
self.set_case_sensitivity(case_sensitivity)
}
}

Expand Down
4 changes: 2 additions & 2 deletions fuzz/fuzz_targets/fzf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fuzz_target!(|data: (Query, Candidate)| {

with_opts(|case_sensitivity, normalization, scheme| {
let _ = fzf_v1
.with_case_sensitivity(case_sensitivity)
.set_case_sensitivity(case_sensitivity)
.set_normalization(normalization)
.with_scoring_scheme(scheme)
.distance_and_ranges(query, candidate, &mut ranges);
Expand All @@ -55,7 +55,7 @@ fuzz_target!(|data: (Query, Candidate)| {
}

let _ = fzf_v2
.with_case_sensitivity(case_sensitivity)
.set_case_sensitivity(case_sensitivity)
.set_normalization(normalization)
.with_scoring_scheme(scheme)
.distance_and_ranges(query, candidate, &mut ranges);
Expand Down
22 changes: 20 additions & 2 deletions src/algos/fzf/fzf_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,27 @@ impl FzfV1 {
&self.scheme
}

/// TODO: docs
/// Sets the case sensitivity to use when comparing the characters of the
/// query and the candidate. The default is [`CaseSensitivity::Smart`].
///
/// # Example
///
/// ```rust
/// # use norm::fzf::{FzfV1, FzfParser};
/// # use norm::{Metric, CaseSensitivity};
/// let mut fzf = FzfV1::new();
/// let mut parser = FzfParser::new();
///
/// // FzfV1 uses smart case sensitivity by default.
/// assert!(fzf.distance(parser.parse("abc"), "ABC").is_some());
///
/// fzf.set_case_sensitivity(CaseSensitivity::Sensitive);
///
/// // Now it's case sensitive, so the query won't match the candidate.
/// assert!(fzf.distance(parser.parse("abc"), "ABC").is_none());
/// ```
#[inline(always)]
pub fn with_case_sensitivity(
pub fn set_case_sensitivity(
&mut self,
case_sensitivity: CaseSensitivity,
) -> &mut Self {
Expand Down
22 changes: 20 additions & 2 deletions src/algos/fzf/fzf_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,27 @@ impl FzfV2 {
&self.scheme
}

/// TODO: docs
/// Sets the case sensitivity to use when comparing the characters of the
/// query and the candidate. The default is [`CaseSensitivity::Smart`].
///
/// # Example
///
/// ```rust
/// # use norm::fzf::{FzfV1, FzfParser};
/// # use norm::{Metric, CaseSensitivity};
/// let mut fzf = FzfV2::new();
/// let mut parser = FzfParser::new();
///
/// // FzfV2 uses smart case sensitivity by default.
/// assert!(fzf.distance(parser.parse("abc"), "ABC").is_some());
///
/// fzf.set_case_sensitivity(CaseSensitivity::Sensitive);
///
/// // Now it's case sensitive, so the query won't match the candidate.
/// assert!(fzf.distance(parser.parse("abc"), "ABC").is_none());
/// ```
#[inline(always)]
pub fn with_case_sensitivity(
pub fn set_case_sensitivity(
&mut self,
case_sensitivity: CaseSensitivity,
) -> &mut Self {
Expand Down
12 changes: 6 additions & 6 deletions tests/fzf_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ mod utils {
Default
+ for<'a> Metric<Query<'a> = FzfQuery<'a>, Distance = FzfDistance>
{
fn with_case_sensitivity(
fn set_case_sensitivity(
&mut self,
case_sensitivity: CaseSensitivity,
) -> &mut Self;
Expand All @@ -571,11 +571,11 @@ mod utils {
}

impl Fzf for FzfV1 {
fn with_case_sensitivity(
fn set_case_sensitivity(
&mut self,
case_sensitivity: CaseSensitivity,
) -> &mut Self {
self.with_case_sensitivity(case_sensitivity)
self.set_case_sensitivity(case_sensitivity)
}

fn scheme(&self) -> &norm::fzf::Scheme {
Expand All @@ -592,11 +592,11 @@ mod utils {
}

impl Fzf for FzfV2 {
fn with_case_sensitivity(
fn set_case_sensitivity(
&mut self,
case_sensitivity: CaseSensitivity,
) -> &mut Self {
self.with_case_sensitivity(case_sensitivity)
self.set_case_sensitivity(case_sensitivity)
}

fn scheme(&self) -> &norm::fzf::Scheme {
Expand All @@ -619,7 +619,7 @@ mod utils {
) -> (F, Option<FzfMatch>) {
let mut fzf = F::default();

fzf.with_case_sensitivity(case_sensitivity);
fzf.set_case_sensitivity(case_sensitivity);

let mut parser = FzfParser::new();

Expand Down
12 changes: 6 additions & 6 deletions tests/fzf_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ fn fzf_v1_score_1() {
let mut ranges = Vec::new();

let _ = fzf
.with_case_sensitivity(CaseSensitivity::Sensitive)
.set_case_sensitivity(CaseSensitivity::Sensitive)
.distance_and_ranges(parser.parse("ZZ"), "ӥZZZ", &mut ranges)
.unwrap();

Expand All @@ -241,7 +241,7 @@ fn fzf_v1_score_2() {
let query = parser.parse("^\\$ ]]%]]'\0\0\0\0\0\0");

let mach = fzf
.with_case_sensitivity(CaseSensitivity::Sensitive)
.set_case_sensitivity(CaseSensitivity::Sensitive)
.distance(query, "\0");

assert!(mach.is_none());
Expand All @@ -256,7 +256,7 @@ fn fzf_v1_score_3() {
let query = parser.parse("^\\$");

let mach = fzf
.with_case_sensitivity(CaseSensitivity::Sensitive)
.set_case_sensitivity(CaseSensitivity::Sensitive)
.distance(query, " ");

assert!(mach.is_none());
Expand All @@ -275,7 +275,7 @@ fn fzf_v1_score_4() {
let candidate = "ZZ\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\u{65e}\nZ\u{65e}";

let _ = fzf
.with_case_sensitivity(CaseSensitivity::Insensitive)
.set_case_sensitivity(CaseSensitivity::Insensitive)
.distance_and_ranges(query, candidate, &mut ranges)
.unwrap();

Expand All @@ -291,7 +291,7 @@ fn fzf_v1_score_5() {
let mut ranges = Vec::new();

let _ = fzf
.with_case_sensitivity(CaseSensitivity::Sensitive)
.set_case_sensitivity(CaseSensitivity::Sensitive)
.set_normalization(true)
.distance_and_ranges(
parser.parse("e !"),
Expand All @@ -314,7 +314,7 @@ fn fzf_v1_score_6() {
let query = parser.parse("^e");

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

Expand Down
12 changes: 6 additions & 6 deletions tests/fzf_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ fn fzf_v2_score_1() {
let mut ranges = Vec::new();

let distance = fzf
.with_case_sensitivity(CaseSensitivity::Sensitive)
.set_case_sensitivity(CaseSensitivity::Sensitive)
.distance_and_ranges(parser.parse("jelly"), "jellyfish", &mut ranges)
.unwrap();

Expand All @@ -247,7 +247,7 @@ fn fzf_v2_score_2() {
let mut parser = FzfParser::new();

let distance = fzf
.with_case_sensitivity(CaseSensitivity::Sensitive)
.set_case_sensitivity(CaseSensitivity::Sensitive)
.distance(parser.parse("!$"), "$$2");

assert!(distance.is_none());
Expand All @@ -262,7 +262,7 @@ fn fzf_v2_score_3() {
let mut ranges = Vec::new();

let _ = fzf
.with_case_sensitivity(CaseSensitivity::Sensitive)
.set_case_sensitivity(CaseSensitivity::Sensitive)
.distance_and_ranges(
parser.parse("\0\0"),
"\0#B\0\u{364}\0\0",
Expand All @@ -282,7 +282,7 @@ fn fzf_v2_score_4() {
let mut ranges = Vec::new();

let _ = fzf
.with_case_sensitivity(CaseSensitivity::Sensitive)
.set_case_sensitivity(CaseSensitivity::Sensitive)
.set_normalization(true)
.distance_and_ranges(
parser.parse("e !"),
Expand All @@ -303,7 +303,7 @@ fn fzf_v2_score_5() {
let mut ranges = Vec::new();

let _ = fzf
.with_case_sensitivity(CaseSensitivity::Insensitive)
.set_case_sensitivity(CaseSensitivity::Insensitive)
.set_normalization(true)
.distance_and_ranges(parser.parse("E"), "\u{364}E", &mut ranges)
.unwrap();
Expand All @@ -322,7 +322,7 @@ fn fzf_v2_score_6() {
let query = parser.parse("!2\t\0\0\0WWHHWHWWWWWWWZ !I");

let distance = fzf
.with_case_sensitivity(CaseSensitivity::Insensitive)
.set_case_sensitivity(CaseSensitivity::Insensitive)
.set_normalization(true)
.distance_and_ranges(
query,
Expand Down

0 comments on commit 900b9e4

Please sign in to comment.