Skip to content

Commit

Permalink
bugfix for matchfuzzy
Browse files Browse the repository at this point in the history
  • Loading branch information
jayli committed Jan 14, 2024
1 parent b3cd984 commit d775840
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
15 changes: 12 additions & 3 deletions autoload/easycomplete/sources/buf.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions autoload/easycomplete/util.vim
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
""" 常用的工具函数
scriptencoding utf-8

" get file extention {{{
function! easycomplete#util#extention()
let filename = fnameescape(fnamemodify(bufname('%'),':p'))
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
38 changes: 36 additions & 2 deletions lua/easycomplete.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ local function test()
console(vim.inspect(Util))
console(replaceCharacters("XMLDocument", {0,1,7}, "*"))



do
return
end
Expand Down Expand Up @@ -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

0 comments on commit d775840

Please sign in to comment.