Skip to content

Commit 96ea39b

Browse files
committed
add docs and bugfix
1 parent cfa4150 commit 96ea39b

File tree

3 files changed

+57
-26
lines changed

3 files changed

+57
-26
lines changed

autoload/easycomplete/action/completion.vim

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,12 @@ function! s:GetLspCompletionResult(server_name, data, plugin_name, word) abort
6767
let l:response = a:data['response']
6868

6969
" 这里包含了 info document 和 matches
70-
" 192 个元素
71-
" vim 用时 55 ms
72-
" lua 用时 29 ms
70+
" 性能:
71+
" 2025年5月2日:
72+
" 192 个元素
73+
" vim 用时 55 ms
74+
" lua 用时 29 ms
75+
" 2025年7月27日更新:192 个元素 lua 耗时减少到 18ms
7376
let tt = reltime()
7477
if g:env_is_nvim
7578
let l:completion_result = s:util_toolkit.get_vim_complete_items(l:response, a:plugin_name, a:word)

lua/easycomplete.lua

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,14 @@ local global_timer_counter = 1
99

1010
local function test()
1111
console(vim.inspect(Util))
12-
console(replaceCharacters("XMLDocument", {0,1,7}, "*"))
13-
14-
do
15-
return
16-
end
17-
18-
console('-------------')
19-
console(Util.get(current_lsp_ctx,'lsp'))
20-
console('-------------')
21-
console(Util.get(current_lsp_ctx, "lsp_name"))
2212
console(require'nvim-lsp-installer.servers'.get_installed_server_names())
2313
console(require'nvim-lsp-installer'.get_install_completion())
2414
end
2515

26-
-- all in all 入口
16+
-- 兼容 nvim_lsp_handler 所需的检查 nvim_lsp 是否安装的入口
2717
-- 每次进入 buf 时执行
18+
-- @param nil
19+
-- @return nil
2820
local function nvim_lsp_handler()
2921
if not Util.nvim_installer_installed() then
3022
return
@@ -45,6 +37,9 @@ local function nvim_lsp_handler()
4537
end
4638
end
4739

40+
-- 全局配置函数
41+
-- @param config, 全局配置的对象
42+
-- @return this, 以便链式调用
4843
function EasyComplete.config(config)
4944
for key, value in pairs(config) do
5045
if key == "setup" and type(value) == "function" then
@@ -56,19 +51,24 @@ function EasyComplete.config(config)
5651
return EasyComplete
5752
end
5853

54+
-- setup 函数,传入一个方法,这个方法是一个空入参的立即执行的函数
55+
-- @param func,一个立即执行的函数
56+
-- @return this, 以便链式调用
5957
function EasyComplete.setup(func)
6058
if func == nil then
6159
return
6260
end
6361
if type(func) == 'function' then
6462
pcall(func)
6563
end
64+
return EasyComplete
6665
end
6766

6867
function EasyComplete.complete_changed()
6968
console('--',Util.get(vim.v.event, "completed_item", "user_data"))
7069
end
7170

71+
-- 执行typing的函数,已经废弃
7272
function EasyComplete.typing(...)
7373
local ctx = vim.fn['easycomplete#context']()
7474
print({
@@ -84,6 +84,8 @@ function EasyComplete.typing(...)
8484
end
8585

8686
-- 执行每个模块里的 init_once 方法
87+
-- @param mojos, 一个 table,传入需要初始化的模块名称
88+
-- @return nil
8789
function EasyComplete.load_mojo(mojos)
8890
if vim.g.easycomplete_lua_mojos_loaded == 1 then
8991
return
@@ -96,18 +98,17 @@ function EasyComplete.load_mojo(mojos)
9698
end
9799
end
98100

99-
-- 初始化入口
101+
-- 全局的 lua 初始化入口
100102
function EasyComplete.init()
101103
EasyComplete.load_mojo({"autocmd", "tabnine", "ghost_text", "luasnip", "cmdline"})
102104

103-
nvim_lsp_handler()
104-
if vim.api.nvim_get_var('easycomplete_kindflag_buf') == "" and debug == true then
105-
test()
106-
else
107-
return
108-
end
105+
-- nvim_lsp 的支持已经废弃
106+
-- nvim_lsp_handler()
109107
end
110108

109+
-- 普通的 cmp items 排序方法,只做最简单的比较
110+
-- @param items, cmp items 列表
111+
-- @reutrn 返回排序后的列表
111112
function EasyComplete.normalize_sort(items)
112113
table.sort(items, function(a1, a2)
113114
local k1 = Util.get_word(a1)
@@ -126,12 +127,16 @@ function EasyComplete.normalize_sort(items)
126127
end
127128

128129

129-
-- 字符串组成的数组
130+
-- 字符串组成的数组进行去重
131+
-- @param items, 字符串数组
132+
-- @return table, 去重后的数组
130133
function EasyComplete.distinct(items)
131134
return Util.distinct(items)
132135
end
133136

134-
-- 返回去重之后的列表
137+
-- 根据 buflines 分割出来关键词
138+
-- @param table list, 传入buflines
139+
-- @return table, 字符串数组,这里的数组没有去重
135140
function EasyComplete.get_buf_keywords(lines)
136141
local buf_keywords = {}
137142
for _, line in ipairs(lines) do
@@ -148,7 +153,10 @@ function EasyComplete.get_buf_keywords(lines)
148153
return buf_keywords
149154
end
150155

151-
-- 一个单词的 fuzzy 比对
156+
-- 一个单词的 fuzzy 比对,没有计算 score
157+
-- @param needle, 原始单词
158+
-- @param haystack, 比对单词
159+
-- @return boolean, 比对成功或者失败
152160
function EasyComplete.fuzzy_search(needle, haystack)
153161
if #needle > #haystack then
154162
return false
@@ -175,6 +183,9 @@ function EasyComplete.fuzzy_search(needle, haystack)
175183
end
176184

177185
-- vim.fn.matchfuzzy 的重新实现,只返回结果,不返回分数
186+
-- @param match_list, 待比对的数组列表
187+
-- @param needle, 比对的单词
188+
-- @return table, 返回比对成功的列表
178189
function EasyComplete.matchfuzzy_and_filter(match_list, needle)
179190
local result = {}
180191
for _, item in ipairs(match_list) do
@@ -185,6 +196,10 @@ function EasyComplete.matchfuzzy_and_filter(match_list, needle)
185196
return result
186197
end
187198

199+
-- 简单的过滤,只通过做模糊匹配来过滤,不考虑fuzzymatch 的分数
200+
-- @param match_list, 原始列表
201+
-- @param needle, 比对单词
202+
-- @return table,返回过滤后的列表
188203
function EasyComplete.filter(match_list, needle)
189204
local result = {}
190205
for _, item in ipairs(match_list) do
@@ -206,11 +221,15 @@ function EasyComplete.filter(match_list, needle)
206221
end
207222

208223
-- 假设 s:GetItemWord(item) 在 Lua 中对应 item.word
224+
-- @param item
225+
-- @return word
209226
local function get_item_word(item)
210227
return item.word or ""
211228
end
212229

213-
-- Firstcomplete 中调用
230+
-- 给 menu_list 去重,只在 Firstcomplete 中调用
231+
-- @param menu_list, 待去重的列表,列表元素是 cmp item
232+
-- @return table, 去重后的列表
214233
function EasyComplete.distinct_keywords(menu_list)
215234
if not menu_list or #menu_list == 0 then
216235
return {}
@@ -249,26 +268,32 @@ function EasyComplete.distinct_keywords(menu_list)
249268
return result_items
250269
end
251270

271+
-- 注册源
252272
function EasyComplete.register_source(tb)
253273
vim.fn["easycomplete#RegisterSource"](tb)
254274
end
255275

276+
-- 注册 lsp server
256277
function EasyComplete.register_lsp_server(opt, tb)
257278
vim.fn["easycomplete#RegisterLspServer"](opt, tb)
258279
end
259280

281+
-- 获得 lsp 命令
260282
function EasyComplete.get_command(plugin_name)
261283
return vim.fn["easycomplete#installer#GetCommand"](plugin_name)
262284
end
263285

286+
-- 得到插件根目录
264287
function EasyComplete.get_default_root_uri()
265288
return vim.fn["easycomplete#util#GetDefaultRootUri"]()
266289
end
267290

291+
-- 调用 lsp complete
268292
function EasyComplete.do_lsp_complete(opt, ctx)
269293
return vim.fn["easycomplete#DoLspComplete"](opt, ctx)
270294
end
271295

296+
-- 调用 lsp defination
272297
function EasyComplete.do_lsp_defination()
273298
return vim.fn["easycomplete#DoLspDefinition"]()
274299
end
@@ -307,6 +332,8 @@ function EasyComplete.replacement(abbr, positions, wrap_char)
307332
-- 转换为字符数组(字符串 -> 字符表)
308333
local letters = {}
309334
for i = 1, #abbr do
335+
-- lua 的 string.sub 是根据字节长度取下表对应的字符,而不是字符长度
336+
-- 因此对于 unicode 字符就取不正确,需要用 vim.fn.strcharpart 代替
310337
-- letters[i] = abbr:sub(i, i)
311338
letters[i] = vim.fn.strcharpart(abbr, i - 1, 1)
312339
end

lua/easycomplete/util.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,9 @@ function util.get_vim_complete_items(response, plugin_name, word)
252252
local l_placeholder_position = vim.fn.match(l_origin_word, l_placeholder_regex)
253253
local l_cursor_backing_steps = string.len(string.sub(l_vim_complete_item['word'], l_placeholder_position + 1))
254254
l_vim_complete_item['abbr'] = l_completion_item['label'] .. '~'
255+
local l_user_data_json = {plugin_name = plugin_name}
255256
if string.len(l_origin_word) > string.len(l_vim_complete_item['word']) then
256-
local l_user_data_json = {
257+
l_user_data_json = {
257258
expandable = 1,
258259
placeholder_position = l_placeholder_position,
259260
cursor_backing_steps = l_cursor_backing_steps

0 commit comments

Comments
 (0)