feat: show shortcut key hint (#193)

This commit is contained in:
yetone 2024-08-24 20:29:46 +08:00 committed by GitHub
parent 0e07128b56
commit 9ac9f827ba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 92 additions and 1 deletions

View File

@ -11,9 +11,12 @@ local M = {
} }
M.input_ns = api.nvim_create_namespace("avante_input") M.input_ns = api.nvim_create_namespace("avante_input")
M.hint_ns = api.nvim_create_namespace("avante_hint")
M.setup = function() M.setup = function()
local normal = api.nvim_get_hl(0, { name = "Normal" }) local normal = api.nvim_get_hl(0, { name = "Normal" })
local normal_float = api.nvim_get_hl(0, { name = "NormalFloat" })
api.nvim_set_hl(0, M.REVERSED_NORMAL, { fg = normal.bg }) api.nvim_set_hl(0, M.REVERSED_NORMAL, { fg = normal.bg })
api.nvim_set_hl(0, M.TITLE, { fg = "#1e222a", bg = "#98c379" }) api.nvim_set_hl(0, M.TITLE, { fg = "#1e222a", bg = "#98c379" })
api.nvim_set_hl(0, M.REVERSED_TITLE, { fg = "#98c379" }) api.nvim_set_hl(0, M.REVERSED_TITLE, { fg = "#98c379" })
@ -22,7 +25,8 @@ M.setup = function()
api.nvim_set_hl(0, M.THRIDTITLE, { fg = "#ABB2BF", bg = "#353B45" }) api.nvim_set_hl(0, M.THRIDTITLE, { fg = "#ABB2BF", bg = "#353B45" })
api.nvim_set_hl(0, M.REVERSED_THRIDTITLE, { fg = "#353B45" }) api.nvim_set_hl(0, M.REVERSED_THRIDTITLE, { fg = "#353B45" })
local normal_float = api.nvim_get_hl(0, { name = "NormalFloat" }) api.nvim_set_hl(M.hint_ns, "NormalFloat", { fg = normal_float.fg, bg = normal_float.bg })
api.nvim_set_hl(M.input_ns, "NormalFloat", { fg = normal_float.fg, bg = normal_float.bg }) api.nvim_set_hl(M.input_ns, "NormalFloat", { fg = normal_float.fg, bg = normal_float.bg })
api.nvim_set_hl(M.input_ns, "FloatBorder", { fg = normal.fg, bg = normal.bg }) api.nvim_set_hl(M.input_ns, "FloatBorder", { fg = normal.fg, bg = normal.bg })
end end

View File

@ -1344,6 +1344,93 @@ function Sidebar:create_input()
end, end,
}) })
local hint_window = nil
-- Close the floating window
local function close_hint()
if hint_window and api.nvim_win_is_valid(hint_window) then
api.nvim_win_close(hint_window, true)
hint_window = nil
end
end
-- Create a floating window as a hint
local function show_hint()
close_hint() -- Close the existing hint window
local hint_text = "Press <C-s> to submit"
if vim.fn.mode() ~= "i" then
hint_text = "Press <CR> to submit"
end
local buf = api.nvim_create_buf(false, true)
api.nvim_buf_set_lines(buf, 0, -1, false, { hint_text })
-- Get the current window size
local win_width = api.nvim_win_get_width(self.input.winid)
local width = #hint_text + 2
-- Set the floating window options
local opts = {
relative = "win",
win = self.input.winid,
width = width,
height = 1,
row = -1,
col = math.max(win_width - width, 0), -- Display in the top right corner
style = "minimal",
border = "none",
focusable = false,
zindex = 100,
}
-- Create the floating window
hint_window = api.nvim_open_win(buf, false, opts)
api.nvim_win_set_hl_ns(hint_window, Highlights.hint_ns)
end
show_hint()
self.input:on_unmount(function()
close_hint()
end)
-- Show hint in insert mode
api.nvim_create_autocmd("ModeChanged", {
group = self.augroup,
pattern = "*:i",
callback = function()
local cur_buf = api.nvim_get_current_buf()
if self.input and cur_buf == self.input.bufnr then
show_hint()
end
end,
})
-- Close hint when exiting insert mode
api.nvim_create_autocmd("ModeChanged", {
group = self.augroup,
pattern = "i:*",
callback = function()
local cur_buf = api.nvim_get_current_buf()
if self.input and cur_buf == self.input.bufnr then
show_hint()
end
end,
})
api.nvim_create_autocmd("WinEnter", {
callback = function()
local cur_win = api.nvim_get_current_win()
if self.input and cur_win == self.input.winid then
show_hint()
else
close_hint()
end
end,
})
self:refresh_winids() self:refresh_winids()
end end