From 4bc1b0fa6fd1890dba4b14571403d567db5f4d99 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 19 May 2021 23:07:16 -0700 Subject: [PATCH] Convert fuzzy match positions to byte offsets --- zed/src/worktree/fuzzy.rs | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/zed/src/worktree/fuzzy.rs b/zed/src/worktree/fuzzy.rs index 4fe81372cf..3897b2a588 100644 --- a/zed/src/worktree/fuzzy.rs +++ b/zed/src/worktree/fuzzy.rs @@ -323,9 +323,16 @@ fn score_match( let path_len = path.len() + prefix.len(); let mut cur_start = 0; + let mut byte_ix = 0; + let mut char_ix = 0; for i in 0..query.len() { - match_positions[i] = best_position_matrix[i * path_len + cur_start]; - cur_start = match_positions[i] + 1; + let match_char_ix = best_position_matrix[i * path_len + cur_start]; + while char_ix < match_char_ix { + byte_ix += path[char_ix].len_utf8(); + char_ix += 1; + } + cur_start = match_char_ix + 1; + match_positions[i] = byte_ix; } score @@ -550,6 +557,26 @@ mod tests { ); } + #[test] + fn test_match_multibyte_path_entries() { + let paths = vec!["aαbβ/cγdδ", "αβγδ/bcde", "c1️⃣2️⃣3️⃣/d4️⃣5️⃣6️⃣/e7️⃣8️⃣9️⃣/f", "/d/🆒/h"]; + assert_eq!("1️⃣".len(), 7); + assert_eq!( + match_query("bcd", false, &paths), + vec![ + ("αβγδ/bcde", vec![9, 10, 11]), + ("aαbβ/cγdδ", vec![3, 7, 10]), + ] + ); + assert_eq!( + match_query("cde", false, &paths), + vec![ + ("αβγδ/bcde", vec![10, 11, 12]), + ("c1️⃣2️⃣3️⃣/d4️⃣5️⃣6️⃣/e7️⃣8️⃣9️⃣/f", vec![0, 23, 46]), + ] + ); + } + fn match_query<'a>( query: &str, smart_case: bool,