Skip to content

Commit 175550a

Browse files
committed
conding for #362
1 parent 00ccbaf commit 175550a

File tree

2 files changed

+264
-105
lines changed

2 files changed

+264
-105
lines changed

lua/easycomplete/cmdline.lua

Lines changed: 208 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
1-
local Util = require "easycomplete.util"
2-
local log = Util.log
3-
local console = Util.console
1+
local util = require "easycomplete.util"
2+
local log = util.log
3+
local console = util.console
44
local normal_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRST0123456789#$_"
55
-- cmdline_start_cmdpos 是不带偏移量的,偏移量只给 pum 定位用
66
local cmdline_start_cmdpos = 0
77
local zizz_flag = 0
88
local zizz_timer = vim.loop.new_timer()
9-
local M = {}
9+
local this = {}
1010

11-
function pum_complete(start_col, menu_items)
11+
function this.pum_complete(start_col, menu_items)
1212
vim.fn["easycomplete#pum#complete"](start_col, menu_items)
1313
end
1414

15-
function pum_close()
15+
function this.pum_close()
1616
vim.fn["easycomplete#pum#close"]()
1717
end
1818

19-
function get_typing_word()
19+
function this.get_typing_word()
2020
return vim.fn['easycomplete#util#GetTypingWord']()
21-
-- -- 获取当前行的文本
22-
-- local current_line = string.sub(vim.fn.getcmdline(),1,vim.fn.getcmdpos())
23-
-- -- 使用 gmatch 迭代所有单词(假设单词由空格分隔)
24-
-- local last_word = ""
25-
-- for word in string.gmatch(current_line, "[%w%-]+") do
26-
-- last_word = word
27-
-- end
28-
-- return last_word
2921
end
3022

31-
function calculate_sign_and_linenr_width()
23+
function this.calculate_sign_and_linenr_width()
3224
local width = 0
3325

3426
-- 检查是否有 sign 列
@@ -47,41 +39,41 @@ function calculate_sign_and_linenr_width()
4739
return width
4840
end
4941

50-
function flush()
42+
function this.flush()
5143
vim.g.easycomplete_cmdline_pattern = ""
5244
vim.g.easycomplete_cmdline_typing = 0
5345
cmdline_start_cmdpos = 0
54-
pum_close()
46+
this.pum_close()
5547
end
5648

57-
function pum_selected()
49+
function this.pum_selected()
5850
return vim.fn['easycomplete#pum#CompleteCursored']()
5951
end
6052

61-
function pum_selected_item()
53+
function this.pum_selected_item()
6254
return vim.fn['easycomplete#pum#CursoredItem']()
6355
end
6456

65-
function M.select_next()
57+
function this.select_next()
6658
vim.fn['easycomplete#pum#next']()
67-
zizz()
68-
local new_whole_word = get_tab_returing_opword()
59+
this.zizz()
60+
local new_whole_word = this.get_tab_returing_opword()
6961
return new_whole_word
7062
end
7163

72-
function M.select_prev()
64+
function this.select_prev()
7365
vim.fn['easycomplete#pum#prev']()
74-
zizz()
75-
local new_whole_word = get_tab_returing_opword()
66+
this.zizz()
67+
local new_whole_word = this.get_tab_returing_opword()
7668
return new_whole_word
7769
end
7870

79-
function get_tab_returing_opword()
71+
function this.get_tab_returing_opword()
8072
local backing_count = vim.fn.getcmdpos() - cmdline_start_cmdpos
8173
local oprator_str = string.rep("\b", backing_count)
8274
local new_whole_word = ""
83-
if pum_selected() then
84-
local item = pum_selected_item()
75+
if this.pum_selected() then
76+
local item = this.pum_selected_item()
8577
local word = item.word
8678
new_whole_word = oprator_str .. word
8779
else
@@ -90,7 +82,7 @@ function get_tab_returing_opword()
9082
return new_whole_word
9183
end
9284

93-
local function bind_cmdline_event()
85+
function this.bind_cmdline_event()
9486
local augroup = vim.api.nvim_create_augroup('CustomCmdlineComplete', { clear = true })
9587

9688
vim.api.nvim_create_autocmd("CmdlineEnter", {
@@ -109,17 +101,17 @@ local function bind_cmdline_event()
109101
})
110102

111103
vim.keymap.set("c", "<Tab>", function()
112-
return M.select_next()
104+
return this.select_next()
113105
end, { expr = true, noremap = true })
114106

115107
vim.keymap.set("c", "<S-Tab>", function()
116-
return M.select_prev()
108+
return this.select_prev()
117109
end, { expr = true, noremap = true })
118110

119111
vim.api.nvim_create_autocmd("CmdlineLeave", {
120112
group = augroup,
121113
callback = function()
122-
flush()
114+
this.flush()
123115
end
124116
})
125117
vim.on_key(function(keys, _)
@@ -130,7 +122,7 @@ local function bind_cmdline_event()
130122
vim.g.easycomplete_cmdline_typing = 1
131123
vim.defer_fn(function()
132124
vim.schedule(function()
133-
local ok, ret = pcall(cmdline_handler, keys, key_str)
125+
local ok, ret = pcall(this.cmdline_handler, keys, key_str)
134126
if not ok then
135127
print(ret)
136128
end
@@ -139,7 +131,7 @@ local function bind_cmdline_event()
139131
end)
140132
end
141133

142-
function normalize_list(arr, word)
134+
function this.normalize_list(arr, word)
143135
if #arr == 0 then return arr end
144136
local ret = {}
145137
for index, value in ipairs(arr) do
@@ -153,36 +145,97 @@ function normalize_list(arr, word)
153145
return vim.fn['easycomplete#util#CompleteMenuFilter'](ret, word, 500)
154146
end
155147

156-
function cmdline_handler(keys, key_str)
148+
function this.cmdline_handler(keys, key_str)
157149
if vim.g.easycomplete_cmdline_pattern == "" then
158150
return
159151
end
160-
if zizzing() then return end
152+
if this.zizzing() then return end
161153
local cmdline = vim.fn.getcmdline()
162154
cmdline_start_cmdpos = 0
163155
if string.byte(key_str) == 9 then
164156
-- console("Tab 键被按下")
165157
elseif string.byte(key_str) == 32 then
166158
-- console("空格键被按下")
167-
pum_close()
159+
this.pum_close()
168160
elseif string.byte(key_str) == 8 or string.byte(key_str) == 128 then
169161
-- console("退格键被按下")
170-
pum_close()
162+
this.pum_close()
171163
elseif string.byte(key_str) == 13 then
172164
-- console("回车键被按下")
173-
pum_close()
165+
this.pum_close()
174166
else
175167
-- console("其他键被按下: " .. keys)
176-
local word = get_typing_word()
177-
local menu_items = vim.fn.getcompletion(word, "function")
178-
local start_col = vim.fn.getcmdpos() - calculate_sign_and_linenr_width() - #word
168+
local word = this.get_typing_word()
169+
local start_col = vim.fn.getcmdpos() - this.calculate_sign_and_linenr_width() - #word
179170
cmdline_start_cmdpos = vim.fn.getcmdpos() - #word
180-
pum_complete(start_col, normalize_list(menu_items, word))
171+
local menu_items = this.get_cmp_items()
172+
if menu_items == nil or #menu_items == 0 then
173+
this.pum_close()
174+
else
175+
this.pum_complete(start_col, this.normalize_list(menu_items, word))
176+
end
181177
end
182178
vim.cmd("redraw")
183179
end
184180

185-
function zizz()
181+
function this.get_all_commands()
182+
local all_commands = {}
183+
local tmp_items = vim.fn.getcompletion("", "command")
184+
for index, value in ipairs(tmp_items) do
185+
if string.match(value, "^[_a-zA-Z0-9]") then
186+
table.insert(all_commands, value)
187+
end
188+
end
189+
return all_commands
190+
end
191+
192+
-- 路由
193+
function this.get_cmp_items()
194+
if this.typing_cmd() then
195+
return this.get_all_commands()
196+
end
197+
end
198+
199+
function this.trim_before(str)
200+
if str == "" then return "" end
201+
return string.gsub(str, "^%s*(.-)$", "%1")
202+
end
203+
204+
-- 正在输入命令
205+
function this.typing_cmd()
206+
local cmdline_all = vim.fn.getcmdline()
207+
local cmdline_typed = this.trim_before(string.sub(cmdline_all, 1, vim.fn.getcmdpos()))
208+
if string.find(cmdline_typed, "%s") then
209+
return false
210+
else
211+
return true
212+
end
213+
end
214+
215+
-- 正在输入buffer
216+
-- 正在输入路径
217+
-- 正在输入函数
218+
-- 正在输入文本
219+
-- 正在输入color
220+
-- 正在输入...
221+
222+
function this.get_completion_type()
223+
local cmdline = vim.fn.getcmdline()
224+
local cmd_name = string.match(cmdline, "([^%s]+)")
225+
if cmd_name == nil then
226+
return nil
227+
end
228+
local cmd_type = ""
229+
for key, value in pairs(this.commands_type) do
230+
if cmd_name == key then
231+
cmd_type = value
232+
break
233+
end
234+
end
235+
return cmd_type
236+
end
237+
238+
function this.zizz()
186239
if zizz_flag > 0 then
187240
zizz_timer:stop()
188241
zizz_flag = 0
@@ -193,24 +246,130 @@ function zizz()
193246
zizz_flag = 1
194247
end
195248

196-
function zizzing()
249+
function this.zizzing()
197250
if zizz_flag == 1 then
198251
return true
199252
else
200253
return false
201254
end
202255
end
203256

257+
this.commands_type = {
258+
-- File completion
259+
edit = 'file',
260+
read = 'file',
261+
write = 'file',
262+
saveas = 'file',
263+
source = 'file',
264+
split = 'file',
265+
vsplit = 'file',
266+
tabedit = 'file',
267+
diffsplit = 'file',
268+
diffpatch = 'file',
269+
explore = 'file',
270+
lexplore = 'file',
271+
sexplore = 'file',
272+
vexplore = 'file',
273+
find = 'file_in_path',
274+
sfind = 'file_in_path',
275+
tabfind = 'file_in_path',
276+
-- Directory completion
277+
cd = 'dir',
278+
lcd = 'dir',
279+
tcd = 'dir',
280+
chdir = 'dir',
281+
-- Buffer completion
282+
buffer = 'buffer',
283+
bdelete = 'buffer',
284+
bwipeout = 'buffer',
285+
bnext = 'buffer',
286+
bprevious = 'buffer',
287+
bfirst = 'buffer',
288+
blast = 'buffer',
289+
sbuffer = 'buffer',
290+
sball = 'buffer',
291+
diffthis = 'diff_buffer',
292+
diffoff = 'diff_buffer',
293+
diffupdate = 'diff_buffer',
294+
-- Command completion
295+
command = 'command',
296+
delcommand = 'command',
297+
-- Option completion
298+
set = 'option',
299+
setlocal = 'option',
300+
setglobal = 'option',
301+
-- Help completion
302+
help = 'help',
303+
-- Expression completion
304+
substitute = 'expression',
305+
global = 'expression',
306+
vglobal = 'expression',
307+
let = 'expression',
308+
echo = 'expression',
309+
-- Tag completion
310+
tag = 'tag',
311+
stag = 'tag',
312+
tselect = 'tag',
313+
tjump = 'tag',
314+
tlast = 'tag',
315+
tnext = 'tag',
316+
tprev = 'tag',
317+
tunmenu = 'tag',
318+
-- Argument completion
319+
args = 'arglist',
320+
argadd = 'file',
321+
argdelete = 'file',
322+
argdo = 'file',
323+
-- User completion (for user-defined functions/commands)
324+
['function'] = 'function',
325+
delfunction = 'function',
326+
-- Mapping completion
327+
map = 'mapping',
328+
noremap = 'mapping',
329+
unmap = 'mapping',
330+
nmap = 'mapping',
331+
vmap = 'mapping',
332+
imap = 'mapping',
333+
cmap = 'mapping',
334+
nunmap = 'mapping',
335+
vunmap = 'mapping',
336+
iunmap = 'mapping',
337+
cunmap = 'mapping',
338+
-- Autocmd completion
339+
autocmd = 'event',
340+
augroup = 'augroup',
341+
doautocmd = 'event',
342+
doautoall = 'event',
343+
-- Shell command completion
344+
terminal = 'shellcmd',
345+
['!'] = 'shellcmd',
346+
-- Misc
347+
['='] = 'lua',
348+
colorscheme = 'color',
349+
compiler = 'compiler',
350+
filetype = 'filetype',
351+
highlight = 'highlight',
352+
history = 'history',
353+
lua = 'lua',
354+
messages = 'messages',
355+
packadd = 'packadd',
356+
register = 'register',
357+
runtime = 'runtime',
358+
sign = 'sign',
359+
syntax = 'syntax',
360+
user = 'user',
361+
}
362+
204363

205-
function M.init_once()
364+
function this.init_once()
206365
-- TODO here -----------------------------
207366
if true then return end
208367
console(1)
209368
-- TODO here -----------------------------
210369
vim.g.easycomplete_cmdline_pattern = ""
211370
vim.g.easycomplete_cmdline_typing = 0
212-
bind_cmdline_event()
371+
this.bind_cmdline_event()
213372
end
214373

215374

216-
return M
375+
return this

0 commit comments

Comments
 (0)