Skip to content

Commit

Permalink
feat: show shortcut key hint (yetone#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
yetone authored Aug 24, 2024
1 parent 0e07128 commit 9ac9f82
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lua/avante/highlights.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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" })
Expand All @@ -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
Expand Down
87 changes: 87 additions & 0 deletions lua/avante/sidebar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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()
end

Expand Down

0 comments on commit 9ac9f82

Please sign in to comment.