@@ -406,9 +406,21 @@ fn trim_array_to_length(lua: &Lua, (arr, n): (LuaTable, i32)) -> Result<LuaTable
406406 Ok ( ret)
407407}
408408
409+ fn complete_menu_fuzzy_filter ( lua : & Lua ,
410+ ( all_menu, word, key_name, maxlength) : ( LuaTable , String , String , i32 ) ) -> Result < LuaTable , LuaError > {
411+
412+ let all_items: LuaTable = trim_array_to_length ( lua, ( all_menu, maxlength + 130 ) ) ?;
413+ let opt: LuaTable = lua. create_table ( ) ?;
414+ opt. set ( "key" , key_name. to_string ( ) ) ?;
415+ opt. set ( "limit" , maxlength) ?;
416+ let mut matching_res: LuaTable = matchfuzzypos ( lua, ( all_items, word. clone ( ) , opt) ) ?;
417+ let mut ret: LuaTable = complete_menu_filter ( lua, ( matching_res, word. clone ( ) ) ) ?;
418+ Ok ( ret)
419+ }
420+
409421// https://crates.io/crates/sublime_fuzzy
422+ // TODO maxlength: 350, 耗时 11 ~ 15 ms,比 lua 慢 2ms
410423fn matchfuzzypos ( lua : & Lua , ( list, word, opt) : ( LuaTable , String , LuaTable ) ) -> Result < LuaTable , LuaError > {
411-
412424 let mut matchfuzzy = lua. create_table ( ) ?;
413425 let mut positions = lua. create_table ( ) ?;
414426 let mut scores = lua. create_table ( ) ?;
@@ -427,7 +439,6 @@ fn matchfuzzypos(lua: &Lua, (list, word, opt): (LuaTable, String, LuaTable)) ->
427439 Some ( m) => {
428440 if m. matched_indices ( ) . len ( ) == word. chars ( ) . count ( ) {
429441 // match
430- // position = m.matched_indices();
431442 for p in m. matched_indices ( ) {
432443 position. push ( * p as i32 ) ;
433444 }
@@ -457,7 +468,6 @@ fn matchfuzzypos(lua: &Lua, (list, word, opt): (LuaTable, String, LuaTable)) ->
457468 } ) ;
458469
459470 let mut new_matchfuzzy = lua. create_table ( ) ?;
460- let max: usize = limit as usize ;
461471
462472 // 4. 写回排序后的结果
463473 for ( i, item) in matchfuzzy_vec. into_iter ( ) . enumerate ( ) {
@@ -466,7 +476,11 @@ fn matchfuzzypos(lua: &Lua, (list, word, opt): (LuaTable, String, LuaTable)) ->
466476 new_matchfuzzy. set ( i+1 , item) ?; // Lua 索引从 1 开始
467477 positions. set ( i+1 , p. clone ( ) ) ?;
468478 scores. set ( i+1 , s. clone ( ) ) ?;
469- if i > max {
479+
480+ // new_matchfuzzy.push(item)?;
481+ // positions.push(p.clone())?;
482+ // scores.push(s.clone())?;
483+ if i > limit as usize {
470484 break ;
471485 }
472486 }
@@ -497,6 +511,7 @@ fn easycomplete_rust_speed(lua: &Lua) -> LuaResult<LuaTable> {
497511 exports. set ( "complete_menu_filter" , lua. create_function ( complete_menu_filter) ?) ?;
498512 exports. set ( "trim_array_to_length" , lua. create_function ( trim_array_to_length) ?) ?;
499513 exports. set ( "matchfuzzypos" , lua. create_function ( matchfuzzypos) ?) ?;
514+ exports. set ( "complete_menu_fuzzy_filter" , lua. create_function ( complete_menu_fuzzy_filter) ?) ?;
500515
501516 Ok ( exports)
502517}
0 commit comments