Skip to content

Commit

Permalink
feat(ui): Normallize strings when doing fuzzy matching.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hywan committed Jul 27, 2023
1 parent 63ca82c commit 3e93bdb
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 23 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/matrix-sdk-ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ serde_json = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true, features = ["attributes"] }
unicode-normalization = "0.1.22"

[dev-dependencies]
anyhow = { workspace = true }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,46 @@
pub use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher as _};
use matrix_sdk::{Client, RoomListEntry};

use super::normalize_string;

struct FuzzyMatcher {
matcher: SkimMatcherV2,
pattern: Option<String>,
}

impl FuzzyMatcher {
fn new() -> Self {
Self { matcher: SkimMatcherV2::default().smart_case().use_cache(true) }
Self { matcher: SkimMatcherV2::default().smart_case().use_cache(true), pattern: None }
}

fn fuzzy_match(&self, subject: &str, pattern: &str) -> bool {
self.matcher.fuzzy_match(subject, pattern).is_some()
fn with_pattern(mut self, pattern: &str) -> Self {
self.pattern = Some(normalize_string(pattern));

self
}

fn fuzzy_match(&self, subject: &str) -> bool {
// No pattern means there is a match.
let Some(pattern) = self.pattern.as_ref() else { return true };

self.matcher.fuzzy_match(&normalize_string(subject), pattern).is_some()
}
}

pub fn new_filter(
client: &Client,
pattern: String,
pattern: &str,
) -> impl Fn(&RoomListEntry) -> bool + Send + Sync + 'static {
let searcher = FuzzyMatcher::new();
let searcher = FuzzyMatcher::new().with_pattern(pattern);

let client = client.clone();

move |room_list_entry| -> bool {
let Some(room_id) = room_list_entry.as_room_id() else { return false };
let Some(room) = client.get_room(room_id) else { return false };
let Some(room_name) = room.name() else { return false };

searcher.fuzzy_match(&room_name, &pattern)
searcher.fuzzy_match(&room_name)
}
}

Expand All @@ -37,42 +50,61 @@ mod tests {

use super::*;

#[test]
fn test_no_pattern() {
let matcher = FuzzyMatcher::new();

assert!(matcher.fuzzy_match("hello"));
}

#[test]
fn test_literal() {
let matcher = FuzzyMatcher::new();

assert!(matcher.fuzzy_match("matrix", "mtx"));
assert!(matcher.fuzzy_match("matrix", "mxt").not());
let matcher = matcher.with_pattern("mtx");
assert!(matcher.fuzzy_match("matrix"));

let matcher = matcher.with_pattern("mxt");
assert!(matcher.fuzzy_match("matrix").not());
}

#[test]
fn test_ignore_case() {
let matcher = FuzzyMatcher::new();

assert!(matcher.fuzzy_match("MaTrIX", "mtx"));
assert!(matcher.fuzzy_match("MaTrIX", "mxt").not());
let matcher = matcher.with_pattern("mtx");
assert!(matcher.fuzzy_match("MaTrIX"));

let matcher = matcher.with_pattern("mxt");
assert!(matcher.fuzzy_match("MaTrIX").not());
}

#[test]
fn test_smart_case() {
let matcher = FuzzyMatcher::new();

assert!(matcher.fuzzy_match("Matrix", "mtx"));
assert!(matcher.fuzzy_match("Matrix", "mtx"));
assert!(matcher.fuzzy_match("MatriX", "Mtx").not());
let matcher = matcher.with_pattern("mtx");
assert!(matcher.fuzzy_match("Matrix"));
assert!(matcher.fuzzy_match("Matrix"));

let matcher = matcher.with_pattern("Mtx");
assert!(matcher.fuzzy_match("MatriX").not());
}

// This is not supported yet.
/*
#[test]
fn test_transliteration_and_normalization() {
fn test_normalization() {
let matcher = FuzzyMatcher::new();

assert!(matcher.fuzzy_match("un bel été", "été"));
assert!(matcher.fuzzy_match("un bel été", "ete"));
assert!(matcher.fuzzy_match("un bel été", "éte"));
assert!(matcher.fuzzy_match("un bel été", "étè").not());
assert!(matcher.fuzzy_match("Ștefan", "stef"));
let matcher = matcher.with_pattern("ubété");

// First, assert that the pattern has been normalized.
assert_eq!(matcher.pattern, Some("ubete".to_string()));

// Second, assert that the subject is normalized too.
assert!(matcher.fuzzy_match("un bel été"));

// Another concrete test.
let matcher = matcher.with_pattern("stf");
assert!(matcher.fuzzy_match("Ștefan"));
}
*/
}
18 changes: 18 additions & 0 deletions crates/matrix-sdk-ui/src/room_list_service/filters/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
mod fuzzy_match_room_name;

pub use fuzzy_match_room_name::new_filter as new_filter_fuzzy_match_room_name;
use unicode_normalization::{char::is_combining_mark, UnicodeNormalization};

fn normalize_string(str: &str) -> String {
str.nfd().filter(|c| !is_combining_mark(*c)).collect::<String>()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_normalize_string() {
assert_eq!(&normalize_string("abc"), "abc");
assert_eq!(&normalize_string("Ștefan Été"), "Stefan Ete");
assert_eq!(&normalize_string("Ç ṩ ḋ Å"), "C s d A");
assert_eq!(&normalize_string("هند"), "هند");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,7 @@ async fn test_entries_stream_with_updated_filter() -> Result<(), Error> {
};

let (previous_entries, entries_stream) =
all_rooms.entries_filtered(new_filter_fuzzy_match_room_name(&client, "mat ba".to_string()));
all_rooms.entries_filtered(new_filter_fuzzy_match_room_name(&client, "mat ba"));
pin_mut!(entries_stream);

sync_then_assert_request_and_fake_response! {
Expand Down

0 comments on commit 3e93bdb

Please sign in to comment.