Skip to content

Commit d06eb52

Browse files
committed
completeMenuFilter → lua
1 parent 1e5eaa2 commit d06eb52

File tree

2 files changed

+104
-47
lines changed

2 files changed

+104
-47
lines changed

autoload/easycomplete/util.vim

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,62 +1157,17 @@ function! easycomplete#util#CompleteMenuFilter(all_menu, word, maxlength)
11571157
let word = substitute(word, "\\.", "\\\\\\\\.", "g")
11581158
endif
11591159
if exists('*matchfuzzy')
1160-
let fullmatch_result = [] " 完全匹配
1161-
let firstchar_result = [] " 首字母匹配
1162-
let fuzzymatching = []
11631160
let all_items = a:all_menu
11641161
" TODO here vim 里针对 g: 的匹配有 hack,word 前面减了两个字符,导致abbr
11651162
" 和 word 不是一一对应的,通过word fuzzy 匹配的位置无法正确应用在 abbr 上
11661163
" 这里只 hack 了 vim,其他类型的文件未测试
11671164
let key_name = (&filetype == "vim") ? "abbr" : "word"
11681165
let matching_res = all_items->matchfuzzypos(word, {'key': key_name})
1169-
let fuzzymatching = matching_res[0]
1170-
let fuzzy_position = matching_res[1]
1171-
let fuzzy_scores = matching_res[2]
1172-
let fuzzymatch_result = []
1173-
if g:env_is_nvim && has("nvim-0.6.1")
1174-
let count_i = 0
1175-
while count_i < len(fuzzymatching)
1176-
let abbr = get(fuzzymatching[count_i], "abbr", "")
1177-
if empty(abbr)
1178-
let fuzzymatching[count_i]["abbr"] = fuzzymatching[count_i]["word"]
1179-
let abbr = fuzzymatching[count_i]["word"]
1180-
endif
1181-
let abbr = easycomplete#util#parseAbbr(abbr)
1182-
let fuzzymatching[count_i]["abbr"] = abbr
1183-
let p = fuzzy_position[count_i]
1184-
let fuzzymatching[count_i]["abbr_marked"] = s:ReplaceMent(abbr, p, "§")
1185-
let fuzzymatching[count_i]["marked_position"] = p
1186-
let fuzzymatching[count_i]["score"] = fuzzy_scores[count_i]
1187-
1188-
" 进行初步分检
1189-
if stridx(toupper(fuzzymatching[count_i]["word"]), toupper(word)) == 0
1190-
" 完全匹配,放在fullmatch_result
1191-
call add(fullmatch_result, fuzzymatching[count_i])
1192-
elseif fuzzymatching[count_i]["word"][0] == word[0]
1193-
" 首字母匹配,放在firstchar_result
1194-
call add(firstchar_result, fuzzymatching[count_i])
1195-
else
1196-
" 否则就是fuzzymatch的结果,放在 fuzzymatch_result 中
1197-
call add(fuzzymatch_result, fuzzymatching[count_i])
1198-
endif
1199-
let count_i += 1
1200-
endwhile
1201-
if has('nvim-0.7')
1202-
" 对 fuzzymatch_result 进行排序,TODO 似乎没有必要
1203-
" call sort(fuzzymatch_result, {a, b -> b['score'] - a['score'] })
1204-
endif
1205-
endif
1206-
if len(easycomplete#GetStuntMenuItems()) == 0 && g:easycomplete_first_complete_hit == 1
1207-
" 对fuzzymatch_result 进行长度排序
1208-
call sort(fuzzymatch_result, "easycomplete#util#SortTextComparatorByLength")
1209-
endif
12101166
if g:env_is_nvim
1211-
let filtered_menu = fullmatch_result + firstchar_result + fuzzymatch_result
1167+
return s:util_toolkit.complete_menu_filter(matching_res, word)
12121168
else
1213-
let filtered_menu = fuzzymatching
1169+
return s:CompleteMenuFilterVim(matching_res, word)
12141170
endif
1215-
return filtered_menu
12161171
else " for nvim(<=0.5.0)
12171172
" 完整匹配
12181173
let original_matching_menu = []
@@ -1287,6 +1242,57 @@ function! easycomplete#util#CompleteMenuFilter(all_menu, word, maxlength)
12871242
endif
12881243
endfunction
12891244

1245+
function! s:CompleteMenuFilterVim(matching_res, word)
1246+
let matching_res = a:matching_res
1247+
let word = a:word
1248+
let fullmatch_result = [] " 完全匹配
1249+
let firstchar_result = [] " 首字母匹配
1250+
let fuzzymatching = []
1251+
let fuzzymatching = matching_res[0]
1252+
let fuzzy_position = matching_res[1]
1253+
let fuzzy_scores = matching_res[2]
1254+
let fuzzymatch_result = []
1255+
if g:env_is_nvim && has("nvim-0.6.1")
1256+
let count_i = 0
1257+
while count_i < len(fuzzymatching)
1258+
let abbr = get(fuzzymatching[count_i], "abbr", "")
1259+
if empty(abbr)
1260+
let fuzzymatching[count_i]["abbr"] = fuzzymatching[count_i]["word"]
1261+
let abbr = fuzzymatching[count_i]["word"]
1262+
endif
1263+
let abbr = easycomplete#util#parseAbbr(abbr)
1264+
let fuzzymatching[count_i]["abbr"] = abbr
1265+
let p = fuzzy_position[count_i]
1266+
let fuzzymatching[count_i]["abbr_marked"] = s:ReplaceMent(abbr, p, "§")
1267+
let fuzzymatching[count_i]["marked_position"] = p
1268+
let fuzzymatching[count_i]["score"] = fuzzy_scores[count_i]
1269+
1270+
" 进行初步分检
1271+
if stridx(toupper(fuzzymatching[count_i]["word"]), toupper(word)) == 0
1272+
" 完全匹配,放在fullmatch_result
1273+
call add(fullmatch_result, fuzzymatching[count_i])
1274+
elseif fuzzymatching[count_i]["word"][0] == word[0]
1275+
" 首字母匹配,放在firstchar_result
1276+
call add(firstchar_result, fuzzymatching[count_i])
1277+
else
1278+
" 否则就是fuzzymatch的结果,放在 fuzzymatch_result 中
1279+
call add(fuzzymatch_result, fuzzymatching[count_i])
1280+
endif
1281+
let count_i += 1
1282+
endwhile
1283+
endif
1284+
if len(easycomplete#GetStuntMenuItems()) == 0 && g:easycomplete_first_complete_hit == 1
1285+
" 对fuzzymatch_result 进行长度排序
1286+
call sort(fuzzymatch_result, "easycomplete#util#SortTextComparatorByLength")
1287+
endif
1288+
if g:env_is_nvim
1289+
let filtered_menu = fullmatch_result + firstchar_result + fuzzymatch_result
1290+
else
1291+
let filtered_menu = fuzzymatching
1292+
endif
1293+
return filtered_menu
1294+
endfunction
1295+
12901296
function! easycomplete#util#ls(path)
12911297
let result_list = []
12921298
let is_win = has('win32') || has('win64')

lua/easycomplete/util.lua

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ local zizz_timer = vim.loop.new_timer()
44
local async_timer = vim.loop.new_timer()
55
local async_timer_counter = 0
66

7+
local function console(...)
8+
local args = {...}
9+
local ok, res = pcall(util.console, unpack(args))
10+
if ok then
11+
return res
12+
else
13+
print(res)
14+
end
15+
end
16+
717
function util.parse_abbr(abbr)
818
local max_length = vim.g.easycomplete_pum_maxlength
919
if max_length == 0 or #abbr <= max_length then
@@ -91,6 +101,47 @@ function util.get_server()
91101
return Server
92102
end
93103

104+
function util.complete_menu_filter(matching_res, word)
105+
local fullmatch_result = {} -- 完全匹配
106+
local firstchar_result = {} -- 首字母匹配
107+
local fuzzymatching = matching_res[1]
108+
local fuzzy_position = matching_res[2]
109+
local fuzzy_scores = matching_res[3]
110+
local fuzzymatch_result = {}
111+
112+
for i, item in ipairs(fuzzymatching) do
113+
if item["abbr"] == nil or item["abbr"] == "" then
114+
item["abbr"] = item["word"]
115+
end
116+
local abbr = item["abbr"]
117+
abbr = util.parse_abbr(abbr)
118+
item["abbr"] = abbr
119+
local p = fuzzy_position[i]
120+
item["abbr_marked"] = require("easycomplete").replacement(abbr, p, "§")
121+
item["marked_position"] = p
122+
item["score"] = fuzzy_scores[i]
123+
if vim.fn.stridx(string.lower(item["word"]), string.lower(word)) == 0 then
124+
table.insert(fullmatch_result, item)
125+
elseif string.lower(string.sub(item["word"],1,1)) == string.lower(string.sub(word,1,1)) then
126+
table.insert(firstchar_result, item)
127+
else
128+
table.insert(fuzzymatch_result, item)
129+
end
130+
end
131+
132+
if vim.fn["easycomplete#GetStuntMenuItems"]() == 0 and vim.g.easycomplete_first_complete_hit == 1 then
133+
table.sort(fuzzymatch_result, function(a, b)
134+
return #a.abbr < #b.abbr -- 按 abbr 字段的长度升序排序
135+
end)
136+
end
137+
138+
local filtered_menu = {}
139+
for _, v in ipairs(fullmatch_result) do table.insert(filtered_menu, v) end
140+
for _, v in ipairs(firstchar_result) do table.insert(filtered_menu, v) end
141+
for _, v in ipairs(fuzzymatch_result) do table.insert(filtered_menu, v) end
142+
return filtered_menu
143+
end
144+
94145
-- TODO 需要再测试一下这个函数
95146
function util.get(a, ...)
96147
local args = {...}

0 commit comments

Comments
 (0)