Skip to content

Commit 3de5c89

Browse files
committed
for #362
1 parent 9ee68ae commit 3de5c89

File tree

5 files changed

+114
-48
lines changed

5 files changed

+114
-48
lines changed

autoload/easycomplete.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2805,8 +2805,8 @@ function! easycomplete#BackToOriginalBuffer()
28052805
call easycomplete#action#reference#back()
28062806
endfunction
28072807

2808-
function! easycomplete#CmdlineEnter()
2809-
call easycomplete#cmdline#enter()
2808+
function! easycomplete#CmdlineEnter(...)
2809+
return call('easycomplete#cmdline#enter', a:000)
28102810
endfunction
28112811

28122812
function! easycomplete#CmdlineLeave()

autoload/easycomplete/cmdline.vim

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ function! easycomplete#cmdline#LazyComplete()
1717
endfunction
1818

1919
function! easycomplete#cmdline#enter()
20-
" call s:console("cmdline enter")
21-
let g:easycomplete_cmdline_typing = 1
20+
return
2221
endfunction
2322

2423
function! easycomplete#cmdline#leave()
25-
let g:easycomplete_cmdline_typing = 0
26-
call easycomplete#pum#close()
24+
return
2725
endfunction
2826

2927
function! easycomplete#cmdline#typing()

autoload/easycomplete/pum.vim

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ function! s:OpenPum(startcol, lines)
190190
call s:InitBuffer(a:lines)
191191
let buffer_size = s:GetBufSize(a:lines)
192192
let pum_pos = s:ComputePumPos(a:startcol, buffer_size)
193-
if easycomplete#cmdline#typing()
193+
if exists("g:easycomplete_cmdline_typing") && g:easycomplete_cmdline_typing == 1
194194
let pum_pos.row = &window
195195
endif
196196
let pum_opts = deepcopy(s:default_pum_pot)
@@ -373,10 +373,16 @@ endfunction
373373

374374
function! easycomplete#pum#next()
375375
call s:SelectNext()
376+
if exists("g:easycomplete_cmdline_typing") && g:easycomplete_cmdline_typing == 1
377+
redraw
378+
endif
376379
endfunction
377380

378381
function! easycomplete#pum#prev()
379382
call s:SelectPrev()
383+
if exists("g:easycomplete_cmdline_typing") && g:easycomplete_cmdline_typing == 1
384+
redraw
385+
endif
380386
endfunction
381387

382388
function! easycomplete#pum#CompleteCursored()

lua/easycomplete/cmdline.lua

Lines changed: 91 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,10 @@ local console = Util.console
44
local normal_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRST0123456789#$_"
55
local M = {}
66

7-
function pum_complete()
8-
print(get_typing_word())
9-
local word = get_typing_word()
10-
local t = {
11-
{
12-
word = "s",
13-
abbr = word,
14-
kind = "x",
15-
menu = "x"
16-
},
17-
{
18-
word = "abcd",
19-
abbr = "abcd",
20-
kind = "x",
21-
menu = "x"
22-
}
23-
}
7+
function pum_complete(menu_items, typing_word)
8+
local word = typing_word
249
local start_col = vim.fn.getcmdpos() - calculate_sign_and_linenr_width() - #word
25-
vim.fn["easycomplete#pum#complete"](start_col, t)
10+
vim.fn["easycomplete#pum#complete"](start_col, menu_items)
2611
end
2712

2813
function pum_close()
@@ -61,37 +46,102 @@ function calculate_sign_and_linenr_width()
6146
return width
6247
end
6348

49+
function flush()
50+
vim.g.easycomplete_cmdline_pattern = ""
51+
vim.g.easycomplete_cmdline_typing = 0
52+
pum_close()
53+
end
54+
55+
function pum_next()
56+
vim.fn['easycomplete#CleverTab']()
57+
return ""
58+
end
59+
60+
function pum_prev()
61+
vim.fn['easycomplete#pum#prev']()
62+
return ""
63+
end
64+
6465
local function bind_cmdline_event()
66+
local augroup = vim.api.nvim_create_augroup('CustomCmdlineComplete', { clear = true })
67+
68+
vim.api.nvim_create_autocmd("CmdlineEnter", {
69+
group = augroup,
70+
pattern = ":",
71+
callback = function()
72+
vim.g.easycomplete_cmdline_pattern = ":"
73+
end,
74+
})
75+
vim.api.nvim_create_autocmd("CmdlineEnter", {
76+
group = augroup,
77+
pattern = "/",
78+
callback = function()
79+
vim.g.easycomplete_cmdline_pattern = "/"
80+
end,
81+
})
82+
vim.cmd [[
83+
cnoremap <expr> <Tab> easycomplete#CleverTab()
84+
cnoremap <expr> <S-Tab> easycomplete#CleverShiftTab()
85+
]]
86+
vim.api.nvim_create_autocmd("CmdlineLeave", {
87+
group = augroup,
88+
callback = function()
89+
flush()
90+
end
91+
})
6592
vim.on_key(function(keys, _)
66-
-- 这里不论是否是插入模式,都会触发,需要过滤掉
6793
if vim.api.nvim_get_mode().mode ~= "c" then
6894
return
6995
end
70-
-- 将按键字节序列转换为字符串
7196
local key_str = vim.api.nvim_replace_termcodes(keys, true, false, true)
72-
97+
vim.g.easycomplete_cmdline_typing = 1
7398
vim.defer_fn(function()
74-
pum_complete()
75-
console(string.byte(key_str))
76-
if string.byte(key_str) == 9 then
77-
console("Tab 键被按下")
78-
elseif string.byte(key_str) == 32 then
79-
console("空格键被按下")
80-
pum_close()
81-
elseif string.byte(key_str) == 8 or string.byte(key_str) == 128 then
82-
console("退格键被按下")
83-
elseif string.byte(key_str) == 13 then
84-
console("回车键被按下")
85-
else
86-
console("其他键被按下: " .. keys)
87-
end
88-
-- console(key_str)
89-
vim.cmd("redraw")
90-
-- vim.fn["easycomplete#CmdlineChanged"](key_str)
99+
vim.schedule(function()
100+
cmdline_handler(keys, key_str)
101+
end)
91102
end, 10)
92103
end)
93104
end
94105

106+
function normalize_list(arr)
107+
if #arr == 0 then return arr end
108+
local ret = {}
109+
for index, value in ipairs(arr) do
110+
table.insert(ret, {
111+
word = arr[index],
112+
abbr = arr[index],
113+
kind = vim.g.easycomplete_kindflag_cmdline,
114+
menu = vim.g.easycomplete_menuflag_cmdline
115+
})
116+
end
117+
return ret
118+
end
119+
120+
function cmdline_handler(keys, key_str)
121+
if vim.g.easycomplete_cmdline_pattern == "" then
122+
return
123+
end
124+
local cmdline = vim.fn.getcmdline()
125+
local typing_word = get_typing_word()
126+
local menu_items = vim.fn.getcompletion(typing_word, "cmdline")
127+
if string.byte(key_str) == 9 then
128+
console("Tab 键被按下")
129+
elseif string.byte(key_str) == 32 then
130+
console("空格键被按下")
131+
pum_close()
132+
elseif string.byte(key_str) == 8 or string.byte(key_str) == 128 then
133+
console("退格键被按下")
134+
pum_close()
135+
elseif string.byte(key_str) == 13 then
136+
console("回车键被按下")
137+
pum_close()
138+
else
139+
console("其他键被按下: " .. keys)
140+
pum_complete(normalize_list(menu_items), typing_word)
141+
end
142+
vim.cmd("redraw")
143+
end
144+
95145

96146
function M.init_once()
97147
-- TODO here -----------------------------
@@ -101,6 +151,9 @@ function M.init_once()
101151
return
102152
end
103153
vim.g.easycomplete_cmdline_loaded = 1
154+
155+
vim.g.easycomplete_cmdline_pattern = ""
156+
vim.g.easycomplete_cmdline_typing = 0
104157
bind_cmdline_event()
105158
end
106159

plugin/easycomplete.vim

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ if g:easycomplete_nerd_font == 1
4646
\ "tabnine": {
4747
\ "kind":get(kind_icons, "tabnine", "󰕃"),
4848
\ "menu": g:easycomplete_menu_abbr ? "𝘛𝘕" : "tabnine"
49+
\ },
50+
\ "cmdline": {
51+
\ "kind":get(kind_icons, "cmdline", ""),
52+
\ "menu": g:easycomplete_menu_abbr ? "CMD" : "cmdline"
4953
\ }
5054
\ }
5155
let g:easycomplete_sign_text = {
@@ -68,7 +72,7 @@ if g:easycomplete_nerd_font == 1
6872
\ 'const': get(kind_icons, "const", ""), 'alias': get(kind_icons, 'alias', ""),
6973
\ 'let': get(kind_icons, "let", ""), 'parameter': get(kind_icons, 'parameter', "󰏗"),
7074
\ 'operator': get(kind_icons, 'operator', "󱧕"), 'property': get(kind_icons, 'property', "󰙅"),
71-
\ 'local': get(kind_icons, 'local', ""),
75+
\ 'local': get(kind_icons, 'local', ""), 'cmdline': get(kind_icons, 'cmdline', ""),
7276
\ 'r':'', 't':'',
7377
\ 'f':'f', 'c':'',
7478
\ 'u':'𝘶', 'e':'𝘦',
@@ -96,6 +100,8 @@ let g:easycomplete_kindflag_snip = empty( easycomplete#util#get(g:easycomplete
96100
\ "s" : easycomplete#util#get(g:easycomplete_menu_skin, "snip", "kind")
97101
let g:easycomplete_kindflag_tabnine = empty(easycomplete#util#get(g:easycomplete_menu_skin, "tabnine", "kind")) ?
98102
\ "t" : easycomplete#util#get(g:easycomplete_menu_skin, "tabnine", "kind")
103+
let g:easycomplete_kindflag_cmdline = empty(easycomplete#util#get(g:easycomplete_menu_skin, "cmdline", "kind")) ?
104+
\ "c" : easycomplete#util#get(g:easycomplete_menu_skin, "cmdline", "kind")
99105
if g:easycomplete_menu_abbr
100106
let g:easycomplete_menuflag_buf = empty( easycomplete#util#get(g:easycomplete_menu_skin, "buf", "menu")) ?
101107
\ "[B]" : easycomplete#util#get(g:easycomplete_menu_skin, "buf", "menu")
@@ -105,13 +111,16 @@ if g:easycomplete_menu_abbr
105111
\ "[S]" : easycomplete#util#get(g:easycomplete_menu_skin, "snip", "menu")
106112
let g:easycomplete_menuflag_tabnine = empty(easycomplete#util#get(g:easycomplete_menu_skin, "tabnine", "menu")) ?
107113
\ "[TN]": easycomplete#util#get(g:easycomplete_menu_skin, "tabnine", "menu")
114+
let g:easycomplete_menuflag_cmdline = empty(easycomplete#util#get(g:easycomplete_menu_skin, "cmdline", "menu")) ?
115+
\ "[CMD]": easycomplete#util#get(g:easycomplete_menu_skin, "cmdline", "menu")
108116
let g:easycomplete_kindflag_tabnine = empty(easycomplete#util#get(g:easycomplete_menu_skin, "tabnine", "kind")) ?
109117
\ "" : easycomplete#util#get(g:easycomplete_menu_skin, "tabnine", "kind")
110118
else
111119
let g:easycomplete_menuflag_buf = "text"
112120
let g:easycomplete_menuflag_dict = "dict"
113121
let g:easycomplete_menuflag_snip = "snippet"
114122
let g:easycomplete_menuflag_tabnine = "tabnine"
123+
let g:easycomplete_menuflag_cmdline = "cmdline"
115124
endif
116125

117126
if !exists("g:easycomplete_fuzzymatch_hlgroup")
@@ -582,8 +591,8 @@ augroup easycomplete#NormalBinding
582591
autocmd CursorHold * call easycomplete#CursorHold()
583592
autocmd CursorHoldI * call easycomplete#CursorHoldI()
584593
autocmd CursorMovedI * call easycomplete#CursorMovedI()
585-
autocmd CmdlineEnter * noa call easycomplete#CmdlineEnter()
586-
autocmd CmdlineLeave * noa call easycomplete#CmdlineLeave()
594+
" autocmd CmdlineEnter * noa call easycomplete#CmdlineEnter()
595+
" autocmd CmdlineLeave * noa call easycomplete#CmdlineLeave()
587596
autocmd BufLeave * noa call easycomplete#BufLeave()
588597
if has("nvim")
589598
autocmd WinScrolled * noa call easycomplete#WinScrolled()

0 commit comments

Comments
 (0)