From d7758407689da2f65ec1fd14da54c407ce57bbb2 Mon Sep 17 00:00:00 2001 From: lijing00333 Date: Sun, 14 Jan 2024 23:47:12 +0800 Subject: [PATCH] bugfix for matchfuzzy --- autoload/easycomplete/sources/buf.vim | 15 ++++++++--- autoload/easycomplete/util.vim | 6 +++++ lua/easycomplete.lua | 38 +++++++++++++++++++++++++-- 3 files changed, 54 insertions(+), 5 deletions(-) diff --git a/autoload/easycomplete/sources/buf.vim b/autoload/easycomplete/sources/buf.vim index f35bb1e..7fcdd2b 100644 --- a/autoload/easycomplete/sources/buf.vim +++ b/autoload/easycomplete/sources/buf.vim @@ -110,9 +110,18 @@ function! s:GetBufKeywordsList(typing) let tmpkeywords += local_kwlist endif endfor - " preform 0.020s for a 10222 -> 611 filter - call filter(tmpkeywords, 'v:val =~ "^' . a:typing . '" && v:val !=# "' . a:typing . '"') - let keyword_list = tmpkeywords + if exists("*matchfuzzy") + " lua 和 vim 的 matchfuzzy 速度对比,vim 更快 + " 单词数→匹配出的结果个数 + " lua 53377→9748 0.028384 + " vim 53377→9748 0.010808 + " let keyword_list = s:lua_toolkit.matchfuzzy(tmpkeywords, a:typing) + let keyword_list = matchfuzzy(tmpkeywords, a:typing, {"limit": 1000}) + else + call filter(tmpkeywords, 'v:val =~ "^' . a:typing . '" && v:val !=# "' . a:typing . '"') + let keyword_list = tmpkeywords + endif + return keyword_list endfunction diff --git a/autoload/easycomplete/util.vim b/autoload/easycomplete/util.vim index 635878f..04dd0e8 100644 --- a/autoload/easycomplete/util.vim +++ b/autoload/easycomplete/util.vim @@ -1,5 +1,6 @@ """ 常用的工具函数 scriptencoding utf-8 + " get file extention {{{ function! easycomplete#util#extention() let filename = fnameescape(fnamemodify(bufname('%'),':p')) @@ -420,6 +421,10 @@ function! easycomplete#util#FuzzySearch(needle, haystack) endfunction function! s:FuzzySearchRegx(needle, haystack) + " if easycomplete#util#HasLua() + " let s:lua_toolkit = v:lua.require("easycomplete") + " return s:lua_toolkit.fuzzy_search(a:needle, a:haystack) + " else let tlen = strlen(a:haystack) let qlen = strlen(a:needle) if qlen > tlen @@ -438,6 +443,7 @@ function! s:FuzzySearchRegx(needle, haystack) endif let matching = (a:haystack =~ needle_ls_regx) return matching ? v:true : v:false + " endif endfunction function! s:FuzzySearchSpeedUp(needle, haystack) diff --git a/lua/easycomplete.lua b/lua/easycomplete.lua index 458d912..079e8f8 100644 --- a/lua/easycomplete.lua +++ b/lua/easycomplete.lua @@ -10,8 +10,6 @@ local function test() console(vim.inspect(Util)) console(replaceCharacters("XMLDocument", {0,1,7}, "*")) - - do return end @@ -147,4 +145,40 @@ function EasyComplete.get_buf_keywords(lines) return buf_keywords end +-- 一个单词的 fuzzy 比对 +function EasyComplete.fuzzy_search(needle, haystack) + if #needle > #haystack then + return false + end + local needle = string.lower(needle) + local haystack = string.lower(haystack) + if #needle == #haystack then + if needle == haystack then + return true + else + return false + end + end + -- 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 + return true + else + return false + end +end + +-- vim.fn.matchfuzzy 的重新实现,只返回结果,不返回分数 +function EasyComplete.matchfuzzy(match_list, needle) + local result = {} + for _, item in ipairs(match_list) do + if EasyComplete.fuzzy_search(needle, item) then + table.insert(result, item) + end + end + return result +end + return EasyComplete