Skip to content

Commit 7f5d7bc

Browse files
committed
整理代码,等待补充更多条件和特定类型的返回
1 parent 9a70722 commit 7f5d7bc

File tree

1 file changed

+42
-18
lines changed

1 file changed

+42
-18
lines changed

lua/easycomplete/cmdline.lua

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
local util = require "easycomplete.util"
2-
local log = util.log
32
local console = util.console
43
local normal_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRST0123456789#$_"
54
-- cmdline_start_cmdpos 是不带偏移量的,偏移量只给 pum 定位用
65
local cmdline_start_cmdpos = 0
76
local zizz_flag = 0
87
local zizz_timer = vim.loop.new_timer()
9-
local this = {}
108
local completeopt = vim.o.completeopt
9+
local this = {}
1110

1211
function this.pum_complete(start_col, menu_items)
1312
vim.opt.completeopt:append("noselect")
@@ -164,13 +163,22 @@ function this.cmdline_handler(keys, key_str)
164163
this.pum_close()
165164
elseif string.byte(key_str) == 8 or string.byte(key_str) == 128 then
166165
-- console("退格键被按下")
167-
this.pum_close()
166+
this.do_complete()
168167
elseif string.byte(key_str) == 13 then
169168
-- console("回车键被按下")
170169
this.pum_close()
171170
else
172171
-- console("其他键被按下: " .. keys)
173-
local word = this.get_typing_word()
172+
this.do_complete()
173+
end
174+
vim.cmd("redraw")
175+
end
176+
177+
function this.do_complete()
178+
local word = this.get_typing_word()
179+
if word == "" then
180+
this.pum_close()
181+
else
174182
local start_col = vim.fn.getcmdpos() - this.calculate_sign_and_linenr_width() - #word
175183
cmdline_start_cmdpos = vim.fn.getcmdpos() - #word
176184
local menu_items = this.get_cmp_items()
@@ -180,7 +188,6 @@ function this.cmdline_handler(keys, key_str)
180188
this.pum_complete(start_col, this.normalize_list(menu_items, word))
181189
end
182190
end
183-
vim.cmd("redraw")
184191
end
185192

186193
function this.get_all_commands()
@@ -196,8 +203,16 @@ end
196203

197204
-- 路由
198205
function this.get_cmp_items()
199-
if this.typing_cmd() then
206+
if this.typing:cmd() then
200207
return this.get_all_commands()
208+
elseif this.typing:path() then
209+
return {}
210+
elseif this.typing:buffer() then
211+
return {}
212+
elseif this.typing:fun() then
213+
return {}
214+
else
215+
return {}
201216
end
202217
end
203218

@@ -206,16 +221,27 @@ function this.trim_before(str)
206221
return string.gsub(str, "^%s*(.-)$", "%1")
207222
end
208223

209-
-- 正在输入命令
210-
function this.typing_cmd()
211-
local cmdline_all = vim.fn.getcmdline()
212-
local cmdline_typed = this.trim_before(string.sub(cmdline_all, 1, vim.fn.getcmdpos()))
213-
if string.find(cmdline_typed, "%s") then
214-
return false
215-
else
216-
return true
224+
this.typing = {
225+
-- 正在输入命令
226+
cmd = function()
227+
local cmdline_all = vim.fn.getcmdline()
228+
local cmdline_typed = this.trim_before(string.sub(cmdline_all, 1, vim.fn.getcmdpos()))
229+
if string.find(cmdline_typed, "%s") then
230+
return false
231+
else
232+
return true
233+
end
234+
end,
235+
-- 正在输入路径
236+
path = function()
237+
end,
238+
-- 正在输入buf
239+
buffer = function()
240+
end,
241+
-- 正在输入函数
242+
fun = function ()
217243
end
218-
end
244+
}
219245

220246
-- 正在输入buffer
221247
-- 正在输入路径
@@ -365,16 +391,14 @@ this.commands_type = {
365391
user = 'user',
366392
}
367393

368-
369394
function this.init_once()
370395
-- TODO here -----------------------------
371-
if true then return end
396+
do return end
372397
console(1)
373398
-- TODO here -----------------------------
374399
vim.g.easycomplete_cmdline_pattern = ""
375400
vim.g.easycomplete_cmdline_typing = 0
376401
this.bind_cmdline_event()
377402
end
378403

379-
380404
return this

0 commit comments

Comments
 (0)