-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui): Implement the “fuzzy match room name” filter
feat(ui): Implement the “fuzzy match room name” filter
- Loading branch information
Showing
8 changed files
with
191 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
crates/matrix-sdk-ui/src/room_list_service/filters/fuzzy_match_room_name.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
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), pattern: None } | ||
} | ||
|
||
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() | ||
} | ||
} | ||
|
||
/// Create a new filter that will fuzzy match a pattern on room names. | ||
/// | ||
/// Rooms are fetched from the `Client`. The pattern and the room names are | ||
/// normalized with `normalize_string`. | ||
pub fn new_filter( | ||
client: &Client, | ||
pattern: &str, | ||
) -> impl Fn(&RoomListEntry) -> bool + Send + Sync + 'static { | ||
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) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::ops::Not; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_no_pattern() { | ||
let matcher = FuzzyMatcher::new(); | ||
|
||
assert!(matcher.fuzzy_match("hello")); | ||
} | ||
|
||
#[test] | ||
fn test_literal() { | ||
let matcher = FuzzyMatcher::new(); | ||
|
||
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(); | ||
|
||
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(); | ||
|
||
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()); | ||
} | ||
|
||
#[test] | ||
fn test_normalization() { | ||
let matcher = FuzzyMatcher::new(); | ||
|
||
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")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
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}; | ||
|
||
/// Normalize a string, i.e. decompose it into NFD (Normalization Form D, i.e. a | ||
/// canonical decomposition, see http://www.unicode.org/reports/tr15/) and | ||
/// filter out the combining marks. | ||
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("هند"), "هند"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.