Skip to content

Commit ae9f196

Browse files
committed
rust fuzzy match done
1 parent bd5e476 commit ae9f196

File tree

6 files changed

+49
-19
lines changed

6 files changed

+49
-19
lines changed

autoload/easycomplete/util.vim

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,26 +1217,22 @@ function! easycomplete#util#CompleteMenuFilter(all_menu, word, maxlength)
12171217
endif
12181218
if exists('*matchfuzzy')
12191219
let tt = reltime()
1220-
if g:env_is_nvim
1221-
" 性能:576 个元素,n 是 550 耗时 2ms
1222-
let all_items = s:util_toolkit.trim_array_to_length(a:all_menu, a:maxlength + 130)
1223-
else
1224-
" 性能:576 个元素,n 是 550 耗时 13ms
1225-
let all_items = s:TrimArrayToLength(a:all_menu, a:maxlength + 130)
1226-
endif
1227-
" call s:console(reltimestr(reltime(tt)))
12281220
" TODO here vim 里针对 g: 的匹配有 hack,word 前面减了两个字符,导致abbr
12291221
" 和 word 不是一一对应的,通过word fuzzy 匹配的位置无法正确应用在 abbr 上
12301222
" 这里只 hack 了 vim,其他类型的文件未测试
12311223
let key_name = (&filetype == "vim") ? "abbr" : "word"
1232-
" let matching_res = all_items->matchfuzzypos(word, {'key': key_name, 'matchseq': 1, "limit": a:maxlength})
1233-
let matching_res = s:util_toolkit.matchfuzzypos(all_items, word, {'key': key_name, 'matchseq': 1, "limit": a:maxlength})
12341224
if g:env_is_nvim
1235-
" 350 个元素,7ms
1236-
let l:ret = s:util_toolkit.complete_menu_filter(matching_res, word)
1225+
" complete_menu_fuzzy_filter --------------{---
1226+
let l:ret = s:util_toolkit.complete_menu_fuzzy_filter(a:all_menu, word, key_name, a:maxlength)
12371227
return l:ret
1228+
" complete_menu_fuzzy_filter --------------}---
12381229
else
1230+
" 性能:576 个元素,n 是 550 耗时 13ms
1231+
" complete_menu_fuzzy_filter --------------{---
1232+
let all_items = s:TrimArrayToLength(a:all_menu, a:maxlength + 130)
1233+
let matching_res = all_items->matchfuzzypos(word, {'key': key_name, 'matchseq': 1, "limit": a:maxlength})
12391234
return s:CompleteMenuFilterVim(matching_res, word)
1235+
" complete_menu_fuzzy_filter --------------}---
12401236
endif
12411237
else " for nvim(<=0.5.0)
12421238
" 完整匹配

build.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env sh
22

3-
name="debug"
4-
#name="release"
3+
#name="debug"
4+
name="release"
55

66
rm -rf ./target/release
77

lua/easycomplete/lib/speed.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
410423
fn 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
}

lua/easycomplete/util.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,26 @@ function util.replacement(abbr, positions, wrap_char)
197197
end
198198
end
199199

200+
function util.complete_menu_fuzzy_filter(all_menu, word, key_name, maxlength)
201+
local tt = vim.fn.reltime()
202+
local ret = nil
203+
if util.rust_ready() then
204+
-- 应该快,但实际慢3ms
205+
ret = rust_speed.complete_menu_fuzzy_filter(all_menu, word, key_name, maxlength)
206+
else
207+
-- 应该慢,实际快3ms
208+
local all_items = util.trim_array_to_length(all_menu, maxlength + 130)
209+
local matching_res = vim.fn.matchfuzzypos(all_items, word, {
210+
key = key_name,
211+
matchseq = 1,
212+
limit = maxlength
213+
})
214+
ret = util.complete_menu_filter(matching_res, word)
215+
end
216+
-- console(vim.fn.reltimestr(vim.fn.reltime(tt)), #ret)
217+
return ret
218+
end
219+
200220
function util.complete_menu_filter(matching_res, word)
201221
-- local tt = vim.fn.reltime()
202222
local ret = {}

target/release

Lines changed: 0 additions & 1 deletion
This file was deleted.
772 KB
Binary file not shown.

0 commit comments

Comments
 (0)