Skip to content

Commit 9ee68ae

Browse files
committed
for #362
1 parent 0e21582 commit 9ee68ae

File tree

5 files changed

+124
-9
lines changed

5 files changed

+124
-9
lines changed

autoload/easycomplete.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2813,8 +2813,8 @@ function! easycomplete#CmdlineLeave()
28132813
call easycomplete#cmdline#leave()
28142814
endfunction
28152815

2816-
function! easycomplete#CmdlineChanged(pfx)
2817-
call easycomplete#cmdline#changed(a:pfx)
2816+
function! easycomplete#CmdlineChanged(char)
2817+
call easycomplete#cmdline#changed(a:char)
28182818
endfunction
28192819

28202820
function! easycomplete#Textchanged()

autoload/easycomplete/cmdline.vim

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11

22
let g:easycomplete_cmdline_typing = 0
33

4-
function! easycomplete#cmdline#changed(pfx)
5-
return
4+
function! easycomplete#cmdline#changed(char)
65
try
76
let item_list = s:normalize(["abc","aaa","aaaasf","asdfsefd","asssfd","aaaas","bsd","aadsfafafaf"])
87
let word = s:GetTypingWord()
9-
call s:console("x", getcmdpos(), strlen(word))
8+
let pleft = win_screenpos(win_getid())[1] - 1
109
let start_col = getcmdpos() - strlen(word)
11-
call easycomplete#pum#complete(0, item_list)
1210
catch
1311
echom v:exception
1412
endtry
13+
" call easycomplete#util#timer_start("easycomplete#cmdline#LazyComplete", [], 10)
14+
endfunction
15+
16+
function! easycomplete#cmdline#LazyComplete()
1517
endfunction
1618

1719
function! easycomplete#cmdline#enter()
20+
" call s:console("cmdline enter")
1821
let g:easycomplete_cmdline_typing = 1
1922
endfunction
2023

2124
function! easycomplete#cmdline#leave()
2225
let g:easycomplete_cmdline_typing = 0
26+
call easycomplete#pum#close()
2327
endfunction
2428

2529
function! easycomplete#cmdline#typing()
26-
return v:false
2730
if !exists("g:easycomplete_cmdline_typing")
2831
let g:easycomplete_cmdline_typing = 0
2932
endif
@@ -62,3 +65,7 @@ endfunction
6265
function! s:console(...)
6366
return call('easycomplete#log#log', a:000)
6467
endfunction
68+
69+
function! s:log(...)
70+
return call('easycomplete#util#log', a:000)
71+
endfunction

lua/easycomplete.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local AutoLoad = require "easycomplete.autoload"
55
local TabNine = require "easycomplete.tabnine"
66
local GhostText = require "easycomplete.ghost_text"
77
local LuaSnip = require "easycomplete.luasnip"
8+
local Cmdline = require "easycomplete.cmdline"
89
local console = Util.console
910
local log = Util.log
1011
local global_timer = vim.loop.new_timer()
@@ -77,6 +78,7 @@ function EasyComplete.init()
7778
TabNine.init_once()
7879
GhostText.init_once()
7980
LuaSnip.init_once()
81+
Cmdline.init_once()
8082

8183
nvim_lsp_handler()
8284
if vim.api.nvim_get_var('easycomplete_kindflag_buf') == "" and debug == true then

lua/easycomplete/cmdline.lua

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
local Util = require "easycomplete.util"
2+
local log = Util.log
3+
local console = Util.console
4+
local normal_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRST0123456789#$_"
5+
local M = {}
6+
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+
}
24+
local start_col = vim.fn.getcmdpos() - calculate_sign_and_linenr_width() - #word
25+
vim.fn["easycomplete#pum#complete"](start_col, t)
26+
end
27+
28+
function pum_close()
29+
vim.fn["easycomplete#pum#close"]()
30+
end
31+
32+
function get_typing_word()
33+
-- 获取当前行的文本
34+
local current_line = string.sub(vim.fn.getcmdline(),1,vim.fn.getcmdpos())
35+
36+
-- 使用 gmatch 迭代所有单词(假设单词由空格分隔)
37+
local last_word = ""
38+
for word in string.gmatch(current_line, "[%w%-]+") do
39+
last_word = word
40+
end
41+
42+
return last_word
43+
end
44+
45+
function calculate_sign_and_linenr_width()
46+
local width = 0
47+
48+
-- 检查是否有 sign 列
49+
local signcolumn = vim.api.nvim_win_get_option(0, "signcolumn")
50+
if signcolumn == "yes" or signcolumn == "auto" or signcolumn == "number" then
51+
width = width + 2 -- sign 列通常占据 2 个字符宽度
52+
end
53+
54+
-- 检查是否显示行号
55+
if vim.wo.number or vim.wo.relativenumber then
56+
-- 计算行号的最大宽度
57+
local max_num_width = #tostring(vim.fn.line("$"))
58+
width = width + max_num_width + 1 -- 加 1 是为了考虑空隙或者额外的字符
59+
end
60+
61+
return width
62+
end
63+
64+
local function bind_cmdline_event()
65+
vim.on_key(function(keys, _)
66+
-- 这里不论是否是插入模式,都会触发,需要过滤掉
67+
if vim.api.nvim_get_mode().mode ~= "c" then
68+
return
69+
end
70+
-- 将按键字节序列转换为字符串
71+
local key_str = vim.api.nvim_replace_termcodes(keys, true, false, true)
72+
73+
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)
91+
end, 10)
92+
end)
93+
end
94+
95+
96+
function M.init_once()
97+
-- TODO here -----------------------------
98+
if true then return end
99+
-- TODO here -----------------------------
100+
if vim.g.easycomplete_cmdline_loaded == 1 then
101+
return
102+
end
103+
vim.g.easycomplete_cmdline_loaded = 1
104+
bind_cmdline_event()
105+
end
106+
107+
108+
return M

plugin/easycomplete.vim

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,8 +584,6 @@ augroup easycomplete#NormalBinding
584584
autocmd CursorMovedI * call easycomplete#CursorMovedI()
585585
autocmd CmdlineEnter * noa call easycomplete#CmdlineEnter()
586586
autocmd CmdlineLeave * noa call easycomplete#CmdlineLeave()
587-
autocmd CmdlineChanged : noa call easycomplete#CmdlineChanged(":")
588-
autocmd CmdlineChanged @/ noa call easycomplete#CmdlineChanged("/")
589587
autocmd BufLeave * noa call easycomplete#BufLeave()
590588
if has("nvim")
591589
autocmd WinScrolled * noa call easycomplete#WinScrolled()

0 commit comments

Comments
 (0)