Skip to content

Commit

Permalink
matchfuzzy bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
jayli committed Jan 15, 2024
1 parent 9323c58 commit 6edba2c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
29 changes: 23 additions & 6 deletions autoload/easycomplete/sources/buf.vim
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,32 @@ function! s:GetBufKeywordsList(typing)
endif
endfor
if exists("*matchfuzzy")
" lua 和 vim 的 matchfuzzy 速度对比,vim 更快
" Lua 和 VIM 的实现做性能对比:
"
" lua 和 vim 的只做 matchfuzzy 速度对比,vim 更快
" 单词数→匹配出的结果个数
" lua 53377→9748 0.028384
" vim 53377→9748 0.010808
" let keyword_list = s:lua_toolkit.matchfuzzy(tmpkeywords, a:typing)
" call easycomplete#util#StartRecord()
let keyword_list = matchfuzzy(tmpkeywords, a:typing)
let keyword_list = filter(keyword_list, 'stridx(v:val, "' . a:typing[0] . '") < 4')
" call easycomplete#util#StopRecord('matchfuzzy ' . len(tmpkeywords) . '→' . len(keyword_list))
"
" vim 中 matchfuzzy 和 filter 的速度对比,filter 比 matchfuzzy 更慢
" 单词数→匹配出的结果个数
" matchfuzzy 58422→21372 0.018895
" filter, 21372→15633 0.027022
"
" lua 和 vim 做 matchfuzzy 和 filter 一起的速度对比
" lua 中两个函数放一起不影响时间复杂度,基本上和单个 matchfuzzy 性能一致
" vim 中必须把 matchfuzzy 和 filter 分开写,多一次全局遍历,表现更慢
" 单词数→匹配出的结果个数
" lua matchfuzzy_and_filter 58422→18010 0.024128
" vim matchfuzzy_and_filter 58422→18010 0.040463
"
" 结论:优先使用 lua 做 matchfuzzy
if easycomplete#util#HasLua()
let keyword_list = s:lua_toolkit.matchfuzzy_and_filter(tmpkeywords, a:typing)
else
let keyword_list = matchfuzzy(tmpkeywords, a:typing)
let keyword_list = filter(keyword_list, 'stridx(v:val, "' . a:typing[0] . '") < 5')
endif
else
call filter(tmpkeywords, 'v:val =~ "^' . a:typing . '" && v:val !=# "' . a:typing . '"')
let keyword_list = tmpkeywords
Expand Down
7 changes: 4 additions & 3 deletions lua/easycomplete.lua
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,17 @@ function EasyComplete.fuzzy_search(needle, haystack)
-- string.find("easycomplete#context","[0-9a-z#]*z[0-9a-z#]*t[0-9a-z#_]*")
-- string.gsub("easy", "(.)", "-%1")
local middle_regx = "[0-9a-z#_]*"
local needle_ls_regx = string.gsub(needle, "(.)", middle_regx .. "%1") .. middle_regx
if string.find(haystack, needle_ls_regx) ~= nil then
local needle_ls_regx = string.gsub(needle, "(.)", "%1" .. middle_regx)
local idx = string.find(haystack, needle_ls_regx)
if idx ~= nil and idx <= 5 then
return true
else
return false
end
end

-- vim.fn.matchfuzzy 的重新实现,只返回结果,不返回分数
function EasyComplete.matchfuzzy(match_list, needle)
function EasyComplete.matchfuzzy_and_filter(match_list, needle)
local result = {}
for _, item in ipairs(match_list) do
if EasyComplete.fuzzy_search(needle, item) then
Expand Down

0 comments on commit 6edba2c

Please sign in to comment.