From 7567478754bfbc4b103b692c6b1574f80a8b453f Mon Sep 17 00:00:00 2001 From: sigoden Date: Thu, 23 Jan 2025 09:46:25 +0800 Subject: refactor: sort repl autocompletion items by fuzzy-match score (#1119) --- src/utils/mod.rs | 25 +++---------------------- 1 file changed, 3 insertions(+), 22 deletions(-) (limited to 'src/utils/mod.rs') diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 8d66470..f4b98c1 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -24,6 +24,7 @@ pub use self::variables::*; use anyhow::{Context, Result}; use fancy_regex::Regex; +use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher}; use is_terminal::IsTerminal; use std::{env, path::PathBuf, process}; use unicode_segmentation::UnicodeSegmentation; @@ -118,21 +119,8 @@ pub fn convert_option_string(value: &str) -> Option { } } -pub fn fuzzy_match(text: &str, pattern: &str) -> bool { - let text_chars: Vec = text.chars().collect(); - let pattern_chars: Vec = pattern.chars().collect(); - - let mut pattern_index = 0; - let mut text_index = 0; - - while pattern_index < pattern_chars.len() && text_index < text_chars.len() { - if pattern_chars[pattern_index] == text_chars[text_index] { - pattern_index += 1; - } - text_index += 1; - } - - pattern_index == pattern_chars.len() +pub fn fuzzy_match(choice: &str, pattern: &str) -> Option { + SkimMatcherV2::default().fuzzy_match(choice, pattern) } pub fn pretty_error(err: &anyhow::Error) -> String { @@ -234,13 +222,6 @@ pub fn set_proxy( mod tests { use super::*; - #[test] - fn test_fuzzy_match() { - assert!(fuzzy_match("openai:gpt-4-turbo", "gpt4")); - assert!(fuzzy_match("openai:gpt-4-turbo", "oai4")); - assert!(!fuzzy_match("openai:gpt-4-turbo", "4gpt")); - } - #[test] #[cfg(not(target_os = "windows"))] fn test_safe_join_path() { -- cgit v1.2.3