Skip to content

Commit

Permalink
replace matchfuzzy from vim to lua implement
Browse files Browse the repository at this point in the history
  • Loading branch information
jayli committed Jan 15, 2024
1 parent dfb98b5 commit 3f04895
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 30 deletions.
2 changes: 1 addition & 1 deletion autoload/easycomplete.vim
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ endif
let g:easycomplete_script_loaded = 1

function! easycomplete#LogStart()
call s:console()
" call s:console()
endfunction

" 全局 Complete 注册插件,其中 plugin 和 LSP Server 是包含关系
Expand Down
34 changes: 6 additions & 28 deletions autoload/easycomplete/sources/buf.vim
Original file line number Diff line number Diff line change
Expand Up @@ -110,38 +110,16 @@ function! s:GetBufKeywordsList(typing)
let tmpkeywords += local_kwlist
endif
endfor
if exists("*matchfuzzy")
" Lua 和 VIM 的实现做性能对比:
"
" lua 和 vim 的只做 matchfuzzy 速度对比,vim 更快
" 单词数→匹配出的结果个数
" lua 53377→9748 0.028384
" vim 53377→9748 0.010808
"
" 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
" call easycomplete#util#StartRecord()
if easycomplete#util#HasLua()
" 58424 → 3623 0.010739
let keyword_list = s:lua_toolkit.filter(tmpkeywords, a:typing)
else
" 58424 → 3623 0.082437
call filter(tmpkeywords, 'v:val =~ "^' . a:typing . '" && v:val !=# "' . a:typing . '"')
let keyword_list = tmpkeywords
endif

" call easycomplete#util#StopRecord('filter ' . len(tmpkeywords) . "→" . len(keyword_list))
return keyword_list
endfunction

Expand Down
21 changes: 21 additions & 0 deletions autoload/easycomplete/util.vim
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,27 @@ function! easycomplete#util#FuzzySearch(needle, haystack)
endfunction

function! s:FuzzySearchRegx(needle, haystack)
" Lua 和 VIM 的实现 matchfuzzy 做性能对比:
"
" lua 和 vim 的只做 matchfuzzy 速度对比,vim 更快
" 单词数→匹配出的结果个数
" lua 53377→9748 0.028384
" vim 53377→9748 0.010808
"
" 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
"
" 结论:如果需要同时使用 filter 和 matchfuzzy 的时候优先使用 lua 做 matchfuzzy
"
" if easycomplete#util#HasLua()
" let s:lua_toolkit = v:lua.require("easycomplete")
" return s:lua_toolkit.fuzzy_search(a:needle, a:haystack)
Expand Down
16 changes: 15 additions & 1 deletion lua/easycomplete.lua
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ function EasyComplete.fuzzy_search(needle, haystack)
local middle_regx = "[0-9a-z#_]*"
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
if idx ~= nil and idx <= 2 then
return true
else
return false
Expand All @@ -182,4 +182,18 @@ function EasyComplete.matchfuzzy_and_filter(match_list, needle)
return result
end

function EasyComplete.filter(match_list, needle)
local result = {}
for _, item in ipairs(match_list) do
if #item < #needle then
-- pass
elseif item == needle then
-- pass
elseif string.find(string.lower(item), "^" .. string.lower(needle)) ~= nil then
table.insert(result, item)
end
end
return result
end

return EasyComplete

0 comments on commit 3f04895

Please sign in to comment.