feat!(selection): do not display hint immediately

Selection hint that is displayed immediately upon entering visual mode
([<leader>aa: ask, <leader>ae: edit]) gets old pretty quickly. Add a
config option to control when the hint is displayed:

  selection = {
    enabled = true,
    hint_display = "delayed",
  },

The "hint_display" option recognizes the following values:

- "immediate" results in the hint being shown immediately after entering
  visual mode. This is the old behavior.
- "delayed" causes the hint be displayed only if the cursor has not been
  moved for vim.o.updatetime milliseconds. This is the new default.
- "none" suppresses showing the hint completely.

Unfortunately "CursorHold" event is not emitted in visual mode so we
have to emulate it using Utils.debounce().

This is a breaking change because selection behavior was controller by
"hints" config entry which makes little sense, so the config section and
associated commands were renamed to "selection".

Additionally the "hints"/"selection" was mapped to "<leader>ah", but the
very same key combination was used to select from old Avante chat
histories, which overrode the toggle. New selection toggle keymap is
"<leader>aC".
This commit is contained in:
Dmitry Torokhov
2025-07-25 17:48:42 -07:00
committed by yetone
parent bd2079a5cc
commit fed6902c9a
5 changed files with 55 additions and 19 deletions

View File

@@ -19,6 +19,7 @@ local PRIORITY = (vim.hl or vim.highlight).priorities.user
---@field selection avante.SelectionResult | nil
---@field cursor_pos table | nil
---@field shortcuts_extmark_id integer | nil
---@field shortcuts_hint_timer? uv.uv_timer_t
---@field selected_code_extmark_id integer | nil
---@field augroup integer | nil
---@field visual_mode_augroup integer | nil
@@ -308,16 +309,34 @@ end
---Show the hints virtual line and set up autocommands to update it or stop showing it when exiting visual mode
---@param bufnr integer
function Selection:on_entering_visual_mode(bufnr)
if Config.selection.hint_display == "none" then return end
if vim.bo[bufnr].buftype == "terminal" or Utils.is_sidebar_buffer(bufnr) then return end
self:show_shortcuts_hints_popup()
self.visual_mode_augroup = api.nvim_create_augroup("avante_selection_visual_" .. self.id, { clear = true })
api.nvim_create_autocmd({ "CursorMoved" }, {
group = self.visual_mode_augroup,
buffer = bufnr,
callback = function() self:show_shortcuts_hints_popup() end,
})
if Config.selection.hint_display == "delayed" then
local deferred_show_shortcut_hints_popup = Utils.debounce(function()
self:show_shortcuts_hints_popup()
self.shortcuts_hint_timer = nil
end, vim.o.updatetime)
api.nvim_create_autocmd({ "CursorMoved" }, {
group = self.visual_mode_augroup,
buffer = bufnr,
callback = function()
self:close_shortcuts_hints_popup()
self.shortcuts_hint_timer = deferred_show_shortcut_hints_popup()
end,
})
else
self:show_shortcuts_hints_popup()
api.nvim_create_autocmd({ "CursorMoved" }, {
group = self.visual_mode_augroup,
buffer = bufnr,
callback = function() self:show_shortcuts_hints_popup() end,
})
end
api.nvim_create_autocmd({ "ModeChanged" }, {
group = self.visual_mode_augroup,
buffer = bufnr,
@@ -337,6 +356,12 @@ end
function Selection:on_exiting_visual_mode()
self:close_shortcuts_hints_popup()
if self.shortcuts_hint_timer then
self.shortcuts_hint_timer:stop()
self.shortcuts_hint_timer:close()
self.shortcuts_hint_timer = nil
end
api.nvim_del_augroup_by_id(self.visual_mode_augroup)
self.visual_mode_augroup = nil
end