@@ -68,6 +68,7 @@ function this.select_prev()
6868 return new_whole_word
6969end
7070
71+ -- Tab 切换 pum 选项的动作
7172function this .get_tab_returing_opword ()
7273 local backing_count = vim .fn .getcmdpos () - cmdline_start_cmdpos
7374 local oprator_str = string.rep (" \b " , backing_count )
@@ -172,22 +173,64 @@ function this.cmdline_handler(keys, key_str)
172173 vim .cmd (" redraw" )
173174end
174175
176+ this .REG_CMP_HANDLER = {
177+ {
178+ -- 空
179+ pattern = " ^%s*$" ,
180+ get_cmd_items = function ()
181+ return {}
182+ end
183+ },
184+ {
185+ -- 正在输入命令
186+ pattern = " ^[a-zA-Z0-9_]+$" ,
187+ get_cmp_items = function ()
188+ return this .get_all_commands ()
189+ end
190+ },
191+ {
192+ -- 命令输入完毕,并敲击空格
193+ pattern = " ^[a-zA-Z0-9_]+%s" ,
194+ get_cmp_items = function ()
195+ end
196+ }
197+ }
198+
175199function this .do_complete ()
176200 local word = this .get_typing_word ()
177- if word == " " then
178- this .pum_close ()
179- else
180- local start_col = vim .fn .getcmdpos () - this .calculate_sign_and_linenr_width () - # word
181- cmdline_start_cmdpos = vim .fn .getcmdpos () - # word
182- local menu_items = this .get_cmp_items ()
183- if menu_items == nil or # menu_items == 0 then
184- this .pum_close ()
185- else
186- this .pum_complete (start_col , this .normalize_list (menu_items , word ))
201+ for index , item in ipairs (this .REG_CMP_HANDLER ) do
202+ if this .cmd_match (item .pattern ) then
203+ local start_col = vim .fn .getcmdpos () - this .calculate_sign_and_linenr_width () - # word
204+ cmdline_start_cmdpos = vim .fn .getcmdpos () - # word
205+ local ok , menu_items = pcall (item .get_cmp_items )
206+ if not ok then
207+ this .pum_close ()
208+ elseif menu_items == nil or # menu_items == 0 then
209+ this .pum_close ()
210+ else
211+ this .pum_complete (start_col , this .normalize_list (menu_items , word ))
212+ end
213+ break
187214 end
188215 end
216+ do return end
217+ ---- ---------------------------------------
218+ -- local word = this.get_typing_word()
219+ -- if word == "" then
220+ -- this.pum_close()
221+ -- else
222+ -- local start_col = vim.fn.getcmdpos() - this.calculate_sign_and_linenr_width() - #word
223+ -- cmdline_start_cmdpos = vim.fn.getcmdpos() - #word
224+ -- local menu_items = this.get_cmp_items()
225+ -- if menu_items == nil or #menu_items == 0 then
226+ -- this.pum_close()
227+ -- else
228+ -- this.pum_complete(start_col, this.normalize_list(menu_items, word))
229+ -- end
230+ -- end
189231end
190232
233+ -- 获得所有command list
191234function this .get_all_commands ()
192235 local all_commands = {}
193236 local tmp_items = vim .fn .getcompletion (" " , " command" )
@@ -214,18 +257,15 @@ function this.get_cmp_items()
214257 end
215258end
216259
217- function this .trim_before (str )
218- if str == " " then return " " end
219- return string.gsub (str , " ^%s*(.-)$" , " %1" )
220- end
221-
222260function this .cmdline_before_cursor ()
223261 local cmdline_all = vim .fn .getcmdline ()
224- local cmdline_typed = this .trim_before (string.sub (cmdline_all , 1 , vim .fn .getcmdpos ()))
262+ local cmdline_typed = util .trim_before (string.sub (cmdline_all , 1 , vim .fn .getcmdpos ()))
225263 return cmdline_typed
226264end
227265
228- function this .get_cmd_name ()
266+ -- 获得 cmdline 中的命令
267+ -- 不管有没有输入完整,都返回
268+ function this .get_guide_cmd ()
229269 local cmdline_typed = this .cmdline_before_cursor ()
230270 local cmdline_tb = vim .split (cmdline_typed , " %s+" )
231271 if # cmdline_tb == 0 then
@@ -236,6 +276,7 @@ function this.get_cmd_name()
236276end
237277
238278-- 判断当前输入命令字符串是否完全匹配rgx
279+ -- 只获取光标前的字符串
239280function this .cmd_match (rgx )
240281 local cmdline_typed = this .cmdline_before_cursor ()
241282 local ret = string.find (cmdline_typed , rgx )
@@ -248,26 +289,16 @@ function this.cmd_match(rgx)
248289 end
249290end
250291
251- function this .has_item (tb , it )
252- if # tb == 0 then return false end
253- local idx = vim .fn .index (tb , it )
254- if idx == - 1 then
255- return false
256- else
257- return true
258- end
259- end
260-
261292this .typing = {
262293 -- 正在输入命令
263294 cmd = function ()
264295 return this .cmd_match (" ^[a-zA-Z0-9_]+$" )
265296 end ,
266297 -- 正在输入路径
267298 file = function ()
268- local cmd_name = this .get_cmd_name ()
299+ local cmd_name = this .get_guide_cmd ()
269300 if this .cmd_match (" ^[a-zA-Z0-9_]+%s" ) and
270- this .has_item (this .cmd_type .file , cmd_name ) then
301+ util .has_item (this .cmd_type .file , cmd_name ) then
271302 return true
272303 else
273304 return false
0 commit comments