diff --git a/README.md b/README.md index 81fef5e..70c79f3 100644 --- a/README.md +++ b/README.md @@ -284,6 +284,10 @@ _See [config.lua#L9](./lua/avante/config.lua) for the full config_ autojump = true, ---@type string | fun(): any list_opener = "copen", + --- Override the 'timeoutlen' setting while hovering over a diff (see :help timeoutlen). + --- Helps to avoid entering operator-pending mode with diff mappings starting with `c`. + --- Disable by setting to -1. + override_timeoutlen = 500, }, } ``` diff --git a/lua/avante/config.lua b/lua/avante/config.lua index 3a108db..69055c4 100644 --- a/lua/avante/config.lua +++ b/lua/avante/config.lua @@ -186,6 +186,10 @@ Respect and use existing conventions, libraries, etc that are already present in --- @class AvanteConflictConfig diff = { autojump = true, + --- Override the 'timeoutlen' setting while hovering over a diff (see :help timeoutlen). + --- Helps to avoid entering operator-pending mode with diff mappings starting with `c`. + --- Disable by setting to -1. + override_timeoutlen = 500, }, --- @class AvanteHintsConfig hints = { diff --git a/lua/avante/diff.lua b/lua/avante/diff.lua index 6dde475..e83d01b 100644 --- a/lua/avante/diff.lua +++ b/lua/avante/diff.lua @@ -299,15 +299,16 @@ local function register_cursor_move_events(bufnr) }) end - api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, { + api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI", "WinLeave" }, { buffer = bufnr, - callback = function() + callback = function(event) local position = get_current_position(bufnr) - - if position then + if (event.event == "CursorMoved" or event.event == "CursorMovedI") and position then show_keybinding_hint(position.current.range_start + 1) + M.override_timeoutlen(bufnr) else api.nvim_buf_clear_namespace(bufnr, KEYBINDING_NAMESPACE, 0, -1) + M.restore_timeoutlen(bufnr) end end, }) @@ -377,6 +378,24 @@ H.clear_buffer_mappings = function(bufnr) if vim.fn.hasmapto(mapping, "n") > 0 then api.nvim_buf_del_keymap(bufnr, "n", mapping) end end vim.b[bufnr].avante_conflict_mappings_set = false + M.restore_timeoutlen(bufnr) +end + +---@param bufnr integer +M.override_timeoutlen = function(bufnr) + if vim.b[bufnr].avante_original_timeoutlen then return end + if Config.diff.override_timeoutlen > 0 then + vim.b[bufnr].avante_original_timeoutlen = vim.o.timeoutlen + vim.o.timeoutlen = Config.diff.override_timeoutlen + end +end + +---@param bufnr integer +M.restore_timeoutlen = function(bufnr) + if vim.b[bufnr].avante_original_timeoutlen then + vim.o.timeoutlen = vim.b[bufnr].avante_original_timeoutlen + vim.b[bufnr].avante_original_timeoutlen = nil + end end M.augroup = api.nvim_create_augroup(AUGROUP_NAME, { clear = true })