Skip to content

Commit 6e4d601

Browse files
committed
for #362
1 parent 6f5a1dc commit 6e4d601

File tree

3 files changed

+50
-28
lines changed

3 files changed

+50
-28
lines changed

autoload/easycomplete/sources/buf.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ function! easycomplete#sources#buf#completor(opt, ctx)
3434
return v:true
3535
endfunction
3636

37+
function! easycomplete#sources#buf#GetKeywords(typing)
38+
return s:GetKeywords(a:typing)
39+
endfunction
40+
3741
" 读取缓冲区词表和字典词表,两者合并输出大词表
3842
function! s:GetKeywords(typing)
3943
" 性能测试,3万个单词量级

autoload/easycomplete/util.vim

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,8 +679,13 @@ endfunction " }}}
679679

680680
" GetTypingWord {{{
681681
function! easycomplete#util#GetTypingWord()
682-
let start = col('.') - 1
683-
let line = getline('.')
682+
if exists("g:easycomplete_cmdline_typing") && g:easycomplete_cmdline_typing == 1
683+
let start = getcmdpos() - 1
684+
let line = getcmdline()
685+
else
686+
let start = col('.') - 1
687+
let line = getline('.')
688+
endif
684689
let width = 0
685690
" 正常情况这里取普通单词逻辑不应当变化
686691
" 如果不同语言对单词组成字符界定不一,在主流程中处理

lua/easycomplete/cmdline.lua

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@ function pum_close()
1717
end
1818

1919
function get_typing_word()
20-
-- 获取当前行的文本
21-
local current_line = string.sub(vim.fn.getcmdline(),1,vim.fn.getcmdpos())
22-
23-
-- 使用 gmatch 迭代所有单词(假设单词由空格分隔)
24-
local last_word = ""
25-
for word in string.gmatch(current_line, "[%w%-]+") do
26-
last_word = word
27-
end
28-
29-
return last_word
20+
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
3029
end
3130

3231
function calculate_sign_and_linenr_width()
@@ -66,6 +65,18 @@ end
6665
function M.select_next()
6766
vim.fn['easycomplete#pum#next']()
6867
zizz()
68+
local new_whole_word = get_tab_returing_opword()
69+
return new_whole_word
70+
end
71+
72+
function M.select_prev()
73+
vim.fn['easycomplete#pum#prev']()
74+
zizz()
75+
local new_whole_word = get_tab_returing_opword()
76+
return new_whole_word
77+
end
78+
79+
function get_tab_returing_opword()
6980
local backing_count = vim.fn.getcmdpos() - cmdline_start_cmdpos
7081
local oprator_str = string.rep("\b", backing_count)
7182
local new_whole_word = ""
@@ -79,11 +90,6 @@ function M.select_next()
7990
return new_whole_word
8091
end
8192

82-
function M.select_prev()
83-
vim.fn['easycomplete#pum#prev']()
84-
zizz()
85-
end
86-
8793
local function bind_cmdline_event()
8894
local augroup = vim.api.nvim_create_augroup('CustomCmdlineComplete', { clear = true })
8995

@@ -101,10 +107,15 @@ local function bind_cmdline_event()
101107
vim.g.easycomplete_cmdline_pattern = "/"
102108
end,
103109
})
104-
vim.cmd [[
105-
cnoremap <expr> <Tab> v:lua.require("easycomplete.cmdline").select_next()
106-
cnoremap <expr> <S-Tab> v:lua.require("easycomplete.cmdline").select_prev()
107-
]]
110+
111+
vim.keymap.set("c", "<Tab>", function()
112+
return M.select_next()
113+
end, { expr = true, noremap = true })
114+
115+
vim.keymap.set("c", "<S-Tab>", function()
116+
return M.select_prev()
117+
end, { expr = true, noremap = true })
118+
108119
vim.api.nvim_create_autocmd("CmdlineLeave", {
109120
group = augroup,
110121
callback = function()
@@ -136,7 +147,9 @@ function normalize_list(arr)
136147
word = arr[index],
137148
abbr = arr[index],
138149
kind = vim.g.easycomplete_kindflag_cmdline,
139-
menu = vim.g.easycomplete_menuflag_cmdline
150+
menu = vim.g.easycomplete_menuflag_cmdline,
151+
marked_position = {0},
152+
abbr_marked = "§" .. string.sub(arr[index], 1, 1) .. "§" .. string.sub(arr[index], 2)
140153
})
141154
end
142155
return ret
@@ -150,20 +163,20 @@ function cmdline_handler(keys, key_str)
150163
local cmdline = vim.fn.getcmdline()
151164
cmdline_start_cmdpos = 0
152165
if string.byte(key_str) == 9 then
153-
console("Tab 键被按下")
166+
-- console("Tab 键被按下")
154167
elseif string.byte(key_str) == 32 then
155-
console("空格键被按下")
168+
-- console("空格键被按下")
156169
pum_close()
157170
elseif string.byte(key_str) == 8 or string.byte(key_str) == 128 then
158-
console("退格键被按下")
171+
-- console("退格键被按下")
159172
pum_close()
160173
elseif string.byte(key_str) == 13 then
161-
console("回车键被按下")
174+
-- console("回车键被按下")
162175
pum_close()
163176
else
164-
console("其他键被按下: " .. keys)
177+
-- console("其他键被按下: " .. keys)
165178
local word = get_typing_word()
166-
local menu_items = vim.fn.getcompletion(word, "cmdline")
179+
local menu_items = vim.fn.getcompletion(word, "function")
167180
local start_col = vim.fn.getcmdpos() - calculate_sign_and_linenr_width() - #word
168181
cmdline_start_cmdpos = vim.fn.getcmdpos() - #word
169182
pum_complete(start_col, normalize_list(menu_items))

0 commit comments

Comments
 (0)