From 9ac9f827bac9775c54fce007031602d521620e45 Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 24 Aug 2024 20:29:46 +0800 Subject: [PATCH] feat: show shortcut key hint (#193) --- lua/avante/highlights.lua | 6 ++- lua/avante/sidebar.lua | 87 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) diff --git a/lua/avante/highlights.lua b/lua/avante/highlights.lua index 72cf0e0..1bd2d31 100644 --- a/lua/avante/highlights.lua +++ b/lua/avante/highlights.lua @@ -11,9 +11,12 @@ local M = { } M.input_ns = api.nvim_create_namespace("avante_input") +M.hint_ns = api.nvim_create_namespace("avante_hint") M.setup = function() 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.TITLE, { fg = "#1e222a", bg = "#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.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, "FloatBorder", { fg = normal.fg, bg = normal.bg }) end diff --git a/lua/avante/sidebar.lua b/lua/avante/sidebar.lua index 3799643..425517f 100644 --- a/lua/avante/sidebar.lua +++ b/lua/avante/sidebar.lua @@ -1344,6 +1344,93 @@ function Sidebar:create_input() 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 to submit" + if vim.fn.mode() ~= "i" then + hint_text = "Press 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() end