Skip to content

Commit 0c4fe46

Browse files
committed
add code for cmdline support
1 parent 0d3a337 commit 0c4fe46

File tree

2 files changed

+76
-29
lines changed

2 files changed

+76
-29
lines changed

lua/easycomplete/cmdline.lua

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ function this.select_prev()
6868
return new_whole_word
6969
end
7070

71+
-- Tab 切换 pum 选项的动作
7172
function 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")
173174
end
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+
175199
function 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
189231
end
190232

233+
-- 获得所有command list
191234
function 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
215258
end
216259

217-
function this.trim_before(str)
218-
if str == "" then return "" end
219-
return string.gsub(str, "^%s*(.-)$", "%1")
220-
end
221-
222260
function 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
226264
end
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()
236276
end
237277

238278
-- 判断当前输入命令字符串是否完全匹配rgx
279+
-- 只获取光标前的字符串
239280
function 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
249290
end
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-
261292
this.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

lua/easycomplete/util.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,22 @@ function util.async_run(func, args, timeout)
252252
return async_timer_counter + 1
253253
end
254254

255+
function util.trim_before(str)
256+
if str == "" then return "" end
257+
return string.gsub(str, "^%s*(.-)$", "%1")
258+
end
259+
260+
-- 判断一个list中是否包含某个字符串元素
261+
function util.has_item(tb, it)
262+
if #tb == 0 then return false end
263+
local idx = vim.fn.index(tb, it)
264+
if idx == -1 then
265+
return false
266+
else
267+
return true
268+
end
269+
end
270+
255271
function util.stop_async_run()
256272
async_timer:stop()
257273
async_timer_counter = 0

0 commit comments

Comments
 (0)