diff --git a/autoload/easycomplete.vim b/autoload/easycomplete.vim index 426394a..a47d22b 100644 --- a/autoload/easycomplete.vim +++ b/autoload/easycomplete.vim @@ -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 是包含关系 diff --git a/autoload/easycomplete/sources/buf.vim b/autoload/easycomplete/sources/buf.vim index c56badc..fccf990 100644 --- a/autoload/easycomplete/sources/buf.vim +++ b/autoload/easycomplete/sources/buf.vim @@ -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 diff --git a/autoload/easycomplete/util.vim b/autoload/easycomplete/util.vim index 04dd0e8..f3ea883 100644 --- a/autoload/easycomplete/util.vim +++ b/autoload/easycomplete/util.vim @@ -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) diff --git a/lua/easycomplete.lua b/lua/easycomplete.lua index ac4baad..69ba788 100644 --- a/lua/easycomplete.lua +++ b/lua/easycomplete.lua @@ -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 @@ -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