diff --git a/README.md b/README.md index 3357a36..92c2f66 100644 --- a/README.md +++ b/README.md @@ -452,7 +452,10 @@ _See [config.lua#L9](./lua/avante/config.lua) for the full config_ close_from_input = nil, -- e.g., { normal = "", insert = "" } }, }, - hints = { enabled = true }, + selection = { + enabled = true, + hint_display = "delayed", + }, windows = { ---@type "right" | "left" | "top" | "bottom" position = "right", -- the position of the sidebar diff --git a/README_zh.md b/README_zh.md index e39dccc..d646fd9 100644 --- a/README_zh.md +++ b/README_zh.md @@ -405,7 +405,10 @@ _请参见 [config.lua#L9](./lua/avante/config.lua) 以获取完整配置_ close_from_input = nil, -- 例如,{ normal = "", insert = "" } }, }, - hints = { enabled = true }, + selection = { + enabled = true, + hint_display = "delayed", + }, windows = { ---@type "right" | "left" | "top" | "bottom" position = "right", -- 侧边栏的位置 diff --git a/lua/avante/config.lua b/lua/avante/config.lua index efadd4a..199b924 100644 --- a/lua/avante/config.lua +++ b/lua/avante/config.lua @@ -528,7 +528,7 @@ M._defaults = { toggle = { default = "at", debug = "ad", - hint = "ah", + selection = "aC", suggestion = "as", repomap = "aR", }, @@ -645,9 +645,14 @@ M._defaults = { --- Disable by setting to -1. override_timeoutlen = 500, }, - --- @class AvanteHintsConfig - hints = { + --- Allows selecting code or other data in a buffer and ask LLM questions about it or + --- to perform edits/transformations. + --- @class AvanteSelectionConfig + --- @field enabled boolean + --- @field hint_display "delayed" | "immediate" | "none" When to show key map hints. + selection = { enabled = true, + hint_display = "delayed", }, --- @class AvanteRepoMapConfig repo_map = { diff --git a/lua/avante/init.lua b/lua/avante/init.lua index 33b0bf5..f9ba958 100644 --- a/lua/avante/init.lua +++ b/lua/avante/init.lua @@ -75,7 +75,7 @@ function H.keymaps() vim.keymap.set("n", "(AvanteBuild)", function() require("avante.api").build() end, { noremap = true }) vim.keymap.set("n", "(AvanteToggle)", function() M.toggle() end, { noremap = true }) vim.keymap.set("n", "(AvanteToggleDebug)", function() M.toggle.debug() end) - vim.keymap.set("n", "(AvanteToggleHint)", function() M.toggle.hint() end) + vim.keymap.set("n", "(AvanteToggleSelection)", function() M.toggle.selection() end) vim.keymap.set("n", "(AvanteToggleSuggestion)", function() M.toggle.suggestion() end) vim.keymap.set({ "n", "v" }, "(AvanteConflictOurs)", function() Diff.choose("ours") end) @@ -134,9 +134,9 @@ function H.keymaps() ) Utils.safe_keymap_set( "n", - Config.mappings.toggle.hint, + Config.mappings.toggle.selection, function() M.toggle.hint() end, - { desc = "avante: toggle hint" } + { desc = "avante: toggle selection" } ) Utils.safe_keymap_set( "n", @@ -230,7 +230,7 @@ function H.autocmds() callback = function(ev) local tab = tonumber(ev.file) M._init(tab or api.nvim_get_current_tabpage()) - if Config.hints.enabled and not M.current.selection.did_setup then M.current.selection:setup_autocmds() end + if Config.selection.enabled and not M.current.selection.did_setup then M.current.selection:setup_autocmds() end end, }) @@ -287,7 +287,7 @@ function H.autocmds() vim.schedule(function() M._init(api.nvim_get_current_tabpage()) - if Config.hints.enabled then M.current.selection:setup_autocmds() end + if Config.selection.enabled then M.current.selection:setup_autocmds() end end) local function setup_colors() @@ -405,10 +405,10 @@ M.toggle.debug = H.api(Utils.toggle_wrap({ set = function(state) Config.override({ debug = state }) end, })) -M.toggle.hint = H.api(Utils.toggle_wrap({ - name = "hint", - get = function() return Config.hints.enabled end, - set = function(state) Config.override({ hints = { enabled = state } }) end, +M.toggle.selection = H.api(Utils.toggle_wrap({ + name = "selection", + get = function() return Config.selection.enabled end, + set = function(state) Config.override({ selection = { enabled = state } }) end, })) M.toggle.suggestion = H.api(Utils.toggle_wrap({ diff --git a/lua/avante/selection.lua b/lua/avante/selection.lua index 53dd13f..13cfcfa 100644 --- a/lua/avante/selection.lua +++ b/lua/avante/selection.lua @@ -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