11local util = require " easycomplete.util"
22local console = util .console
3+ local errlog = util .errlog
34-- cmdline_start_cmdpos 是不带偏移量的,偏移量只给 pum 定位用
45local cmdline_start_cmdpos = 0
5- local completeopt = vim .o . completeopt
6+ local pum_noselect = vim .g . easycomplete_pum_noselect
67local this = {}
78
89function this .pum_complete (start_col , menu_items )
9- vim .opt . completeopt : append ( " noselect " )
10+ vim .g . easycomplete_pum_noselect = 1
1011 vim .fn [" easycomplete#pum#complete" ](start_col , menu_items )
1112end
1213
1314function this .pum_close ()
1415 vim .fn [" easycomplete#pum#close" ]()
15- vim .opt . completeopt = completeopt
16+ vim .g . easycomplete_pum_noselect = pum_noselect
1617end
1718
1819function this .get_typing_word ()
@@ -104,7 +105,7 @@ function this.bind_cmdline_event()
104105 group = augroup ,
105106 callback = function ()
106107 this .flush ()
107- vim .o . completeopt = completeopt
108+ vim .g . easycomplete_pum_noselect = pum_noselect
108109 end
109110 })
110111
@@ -153,7 +154,8 @@ function this.normalize_list(arr, word)
153154 })
154155 end
155156 end
156- return vim .fn [' easycomplete#util#CompleteMenuFilter' ](ret , word , 500 )
157+ local filtered_items = vim .fn [' easycomplete#util#CompleteMenuFilter' ](ret , word , 500 )
158+ return filtered_items
157159end
158160
159161function this .cmdline_handler (keys , key_str )
@@ -199,45 +201,50 @@ this.REG_CMP_HANDLER = {
199201 end
200202 },
201203 {
202- -- 命令输入完毕,并敲击空格
203- pattern = " ^[a-zA-Z0-9_]+%s$" ,
204+ pattern = {
205+ " ^[a-zA-Z0-9_]+%s$" , -- 命令输入完毕,并敲击空格
206+ " ^[a-zA-Z0-9_]+%s+%w+$" -- 命令输入完毕,敲击空格后直接输入单词
207+ },
204208 get_cmp_items = function ()
205- return this .cmp_just_after_cmd ()
206- end
207- },
208- {
209- -- 命令输入完毕,敲击空格后直接输入单词
210- pattern = " ^[a-zA-Z0-9_]+%s+%w+$ " ,
211- get_cmp_items = function ()
212- return this . cmp_just_after_cmd ()
209+ local cmd_name = this .get_guide_cmd ()
210+ local cmp_type = this . get_complition_type ( cmd_name )
211+ if cmp_type == " " then
212+ return {}
213+ else
214+ local result = vim . fn . getcompletion ( " " , cmp_type )
215+ return result
216+ end
213217 end
214218 },
215219 {
216220 -- 输入路径
217- pattern = " ^[a-zA-Z0-9_]+%s+.*/$" ,
221+ pattern = {
222+ " ^[a-zA-Z0-9_]+%s+.*/$" ,
223+ " ^[a-zA-Z0-9_]+%s+.*/[a-zA-Z0-9_]+$"
224+ },
218225 get_cmp_items = function ()
219226 local typing_path = vim .fn [' easycomplete#sources#directory#TypingAPath' ]()
220- -- TODO 这里还需要再调试
221227 if typing_path .is_path == 0 then
222228 return {}
223229 else
230+ -- 这里原来就不提供模糊匹配?
224231 local ret = vim .fn [' easycomplete#sources#directory#GetDirAndFiles' ](typing_path , typing_path .fname )
232+ local result = this .path_dir_normalize (ret )
225233 return ret
226234 end
227235 end
228-
229236 }
230237}
231238
232- function this .cmp_just_after_cmd ()
233- local cmd_name = this .get_guide_cmd ()
234- local cmp_type = this .get_complition_type (cmd_name )
235- if cmp_type == " " then
236- return {}
237- else
238- local result = vim .fn .getcompletion (" " , cmp_type )
239- return result
239+ -- 在路径匹配中,正文中tab匹配一个目录会自动带上'/',方便回车后继续匹配下级目录
240+ -- 在cmdline中按回车是执行的意思,所以这里保持了原生menu行为习惯,即Tab出菜单和
241+ -- 选择下一个,回车是执行,因此这里就把目录末尾的"/"去掉了,让用户去输入,以便
242+ -- 做到连续的逐级匹配
243+ function this .path_dir_normalize (ret )
244+ for index , item in ipairs (ret ) do
245+ item .word = string.gsub (item .word , " /$" , " " )
240246 end
247+ return ret
241248end
242249
243250function this .get_complition_type (cmd_name )
@@ -259,19 +266,21 @@ function this.do_complete()
259266 local word = this .get_typing_word ()
260267 local matched_pattern = false
261268 for index , item in ipairs (this .REG_CMP_HANDLER ) do
262- if this .cmd_match (item .pattern ) then
263- local start_col = vim .fn .getcmdpos () - this .calculate_sign_and_linenr_width () - # word
264- cmdline_start_cmdpos = vim .fn .getcmdpos () - # word
265- local ok , menu_items = pcall (item .get_cmp_items )
266- if not ok then
267- print (" [jayli debug] " .. menu_items )
268- this .pum_close ()
269- elseif menu_items == nil or # menu_items == 0 then
270- this .pum_close ()
271- else
272- this .pum_complete (start_col , this .normalize_list (menu_items , word ))
269+ if type (item .pattern ) == " table" then
270+ for jndex , jtem in ipairs (item .pattern ) do
271+ if this .cmd_match (jtem ) then
272+ this .cmp_regex_handler (item .get_cmp_items , word )
273+ matched_pattern = true
274+ break
275+ end
276+ end
277+ else
278+ if this .cmd_match (item .pattern ) then
279+ this .cmp_regex_handler (item .get_cmp_items , word )
280+ matched_pattern = true
273281 end
274- matched_pattern = true
282+ end
283+ if matched_pattern == true then
275284 break
276285 end
277286 end
@@ -280,6 +289,21 @@ function this.do_complete()
280289 end
281290end
282291
292+ -- 根据某个正则表达式是否匹配,来调用既定的get_cmp_items,并执行complete()
293+ function this .cmp_regex_handler (get_cmp_items , word )
294+ local start_col = vim .fn .getcmdpos () - this .calculate_sign_and_linenr_width () - # word
295+ cmdline_start_cmdpos = vim .fn .getcmdpos () - # word
296+ local ok , menu_items = pcall (get_cmp_items )
297+ if not ok then
298+ print (" [jayli debug] " .. menu_items )
299+ this .pum_close ()
300+ elseif menu_items == nil or # menu_items == 0 then
301+ this .pum_close ()
302+ else
303+ this .pum_complete (start_col , this .normalize_list (menu_items , word ))
304+ end
305+ end
306+
283307-- 获得所有command list
284308function this .get_all_commands ()
285309 local all_commands = {}
@@ -480,7 +504,7 @@ this.commands_type = {
480504function this .init_once ()
481505 -- TODO here -----------------------------
482506 do return end
483- -- console (1)
507+ -- errlog (1)
484508 -- TODO here -----------------------------
485509 vim .g .easycomplete_cmdline_pattern = " "
486510 vim .g .easycomplete_cmdline_typing = 0
0 commit comments