From 6edba2c38ff67c2864147d8970e17e440893dcec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8B=94=E8=B5=A4?= Date: Mon, 15 Jan 2024 12:28:04 +0800 Subject: [PATCH] matchfuzzy bugfix --- autoload/easycomplete/sources/buf.vim | 29 +++++++++++++++++++++------ lua/easycomplete.lua | 7 ++++--- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/autoload/easycomplete/sources/buf.vim b/autoload/easycomplete/sources/buf.vim index 7e5b2b3..c56badc 100644 --- a/autoload/easycomplete/sources/buf.vim +++ b/autoload/easycomplete/sources/buf.vim @@ -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 diff --git a/lua/easycomplete.lua b/lua/easycomplete.lua index 079e8f8..ac4baad 100644 --- a/lua/easycomplete.lua +++ b/lua/easycomplete.lua @@ -162,8 +162,9 @@ 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 @@ -171,7 +172,7 @@ function EasyComplete.fuzzy_search(needle, haystack) 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