@@ -9,22 +9,14 @@ local global_timer_counter = 1
99
1010local 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 ())
2414end
2515
26- -- all in all 入口
16+ -- 兼容 nvim_lsp_handler 所需的检查 nvim_lsp 是否安装的入口
2717-- 每次进入 buf 时执行
18+ -- @param nil
19+ -- @return nil
2820local 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
4638end
4739
40+ -- 全局配置函数
41+ -- @param config, 全局配置的对象
42+ -- @return this, 以便链式调用
4843function 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
5752end
5853
54+ -- setup 函数,传入一个方法,这个方法是一个空入参的立即执行的函数
55+ -- @param func,一个立即执行的函数
56+ -- @return this, 以便链式调用
5957function 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
6665end
6766
6867function EasyComplete .complete_changed ()
6968 console (' --' ,Util .get (vim .v .event , " completed_item" , " user_data" ))
7069end
7170
71+ -- 执行typing的函数,已经废弃
7272function EasyComplete .typing (...)
7373 local ctx = vim .fn [' easycomplete#context' ]()
7474 print ({
@@ -84,6 +84,8 @@ function EasyComplete.typing(...)
8484end
8585
8686-- 执行每个模块里的 init_once 方法
87+ -- @param mojos, 一个 table,传入需要初始化的模块名称
88+ -- @return nil
8789function 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
9799end
98100
99- -- 初始化入口
101+ -- 全局的 lua 初始化入口
100102function 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()
109107end
110108
109+ -- 普通的 cmp items 排序方法,只做最简单的比较
110+ -- @param items, cmp items 列表
111+ -- @reutrn 返回排序后的列表
111112function 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)
126127end
127128
128129
129- -- 字符串组成的数组
130+ -- 字符串组成的数组进行去重
131+ -- @param items, 字符串数组
132+ -- @return table, 去重后的数组
130133function EasyComplete .distinct (items )
131134 return Util .distinct (items )
132135end
133136
134- -- 返回去重之后的列表
137+ -- 根据 buflines 分割出来关键词
138+ -- @param table list, 传入buflines
139+ -- @return table, 字符串数组,这里的数组没有去重
135140function 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
149154end
150155
151- -- 一个单词的 fuzzy 比对
156+ -- 一个单词的 fuzzy 比对,没有计算 score
157+ -- @param needle, 原始单词
158+ -- @param haystack, 比对单词
159+ -- @return boolean, 比对成功或者失败
152160function EasyComplete .fuzzy_search (needle , haystack )
153161 if # needle > # haystack then
154162 return false
@@ -175,6 +183,9 @@ function EasyComplete.fuzzy_search(needle, haystack)
175183end
176184
177185-- vim.fn.matchfuzzy 的重新实现,只返回结果,不返回分数
186+ -- @param match_list, 待比对的数组列表
187+ -- @param needle, 比对的单词
188+ -- @return table, 返回比对成功的列表
178189function 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
186197end
187198
199+ -- 简单的过滤,只通过做模糊匹配来过滤,不考虑fuzzymatch 的分数
200+ -- @param match_list, 原始列表
201+ -- @param needle, 比对单词
202+ -- @return table,返回过滤后的列表
188203function 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)
206221end
207222
208223-- 假设 s:GetItemWord(item) 在 Lua 中对应 item.word
224+ -- @param item
225+ -- @return word
209226local function get_item_word (item )
210227 return item .word or " "
211228end
212229
213- -- Firstcomplete 中调用
230+ -- 给 menu_list 去重,只在 Firstcomplete 中调用
231+ -- @param menu_list, 待去重的列表,列表元素是 cmp item
232+ -- @return table, 去重后的列表
214233function 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
250269end
251270
271+ -- 注册源
252272function EasyComplete .register_source (tb )
253273 vim .fn [" easycomplete#RegisterSource" ](tb )
254274end
255275
276+ -- 注册 lsp server
256277function EasyComplete .register_lsp_server (opt , tb )
257278 vim .fn [" easycomplete#RegisterLspServer" ](opt , tb )
258279end
259280
281+ -- 获得 lsp 命令
260282function EasyComplete .get_command (plugin_name )
261283 return vim .fn [" easycomplete#installer#GetCommand" ](plugin_name )
262284end
263285
286+ -- 得到插件根目录
264287function EasyComplete .get_default_root_uri ()
265288 return vim .fn [" easycomplete#util#GetDefaultRootUri" ]()
266289end
267290
291+ -- 调用 lsp complete
268292function EasyComplete .do_lsp_complete (opt , ctx )
269293 return vim .fn [" easycomplete#DoLspComplete" ](opt , ctx )
270294end
271295
296+ -- 调用 lsp defination
272297function EasyComplete .do_lsp_defination ()
273298 return vim .fn [" easycomplete#DoLspDefinition" ]()
274299end
@@ -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
0 commit comments