@@ -103,9 +103,7 @@ fn app(cli: Cli, bookmarks_file: PathBuf) -> AppResult<Option<String>> {
103103
104104 let bookmarks = read_bookmarks ( & bookmarks_file) ?;
105105
106- if let Some ( ( best, _score) ) =
107- best_bookmark_match ( & path, bookmarks. iter ( ) . map ( |s| s. as_str ( ) ) , 100 )
108- {
106+ if let Some ( best) = best_bookmark_match ( & path, bookmarks. iter ( ) . map ( |s| s. as_str ( ) ) ) {
109107 return Ok ( Some ( best. into ( ) ) ) ;
110108 }
111109
@@ -143,17 +141,22 @@ fn app(cli: Cli, bookmarks_file: PathBuf) -> AppResult<Option<String>> {
143141fn best_bookmark_match < ' a > (
144142 query : & str ,
145143 bookmarks : impl IntoIterator < Item = & ' a str > ,
146- min_score : u32 ,
147- ) -> Option < ( & ' a str , u32 ) > {
144+ ) -> Option < & ' a str > {
145+ let min_score = 100 ;
148146 let mut matcher = Matcher :: new ( Config :: DEFAULT . match_paths ( ) ) ;
149147
150- let results = Pattern :: parse ( query, CaseMatching :: Ignore , Normalization :: Smart )
148+ let results = Pattern :: parse ( query, CaseMatching :: Smart , Normalization :: Smart )
151149 . match_list ( bookmarks, & mut matcher) ;
152150
153151 results
154152 . into_iter ( )
155- . max_by_key ( |( _, score) | * score)
156153 . filter ( |( _, score) | * score >= min_score)
154+ . max_by ( |( a_str, a_score) , ( b_str, b_score) | {
155+ a_score
156+ . cmp ( b_score)
157+ . then_with ( || b_str. len ( ) . cmp ( & a_str. len ( ) ) )
158+ } )
159+ . map ( |( s, _) | s)
157160}
158161
159162fn find_case_insensitive ( name : & str ) -> Option < PathBuf > {
@@ -219,3 +222,21 @@ fn pick_one(bookmarks: &[String]) -> AppResult<Option<String>> {
219222 }
220223 Ok ( picker. pick ( ) ?. map ( |bookmark| bookmark. to_string ( ) ) )
221224}
225+
226+ #[ cfg( test) ]
227+ mod tests {
228+ use super :: * ;
229+
230+ #[ test]
231+ fn best_with_same_score ( ) {
232+ let paths = [
233+ "/path/with/many/sub/directories" ,
234+ "/path/with/" ,
235+ "/path/with/many/sub/" ,
236+ ] ;
237+
238+ let best = best_bookmark_match ( "pathwith" , paths) . unwrap ( ) ;
239+
240+ assert_eq ! ( best, paths[ 1 ] ) ;
241+ }
242+ }
0 commit comments