From 305d9728499caf63b7399588859acd2530614701 Mon Sep 17 00:00:00 2001 From: Aaron Pham Date: Sun, 25 Aug 2024 00:16:25 -0400 Subject: [PATCH] chore(keymaps): add toggle options (#204) Signed-off-by: Aaron Pham --- lua/avante/config.lua | 5 ++++- lua/avante/diff.lua | 2 +- lua/avante/init.lua | 19 +++++++++++++++++++ lua/avante/selection.lua | 2 +- lua/avante/sidebar.lua | 7 +++---- lua/avante/utils/init.lua | 19 +++++++++++++++++++ 6 files changed, 47 insertions(+), 7 deletions(-) diff --git a/lua/avante/config.lua b/lua/avante/config.lua index e2951c26d..6bbd5ef1b 100644 --- a/lua/avante/config.lua +++ b/lua/avante/config.lua @@ -107,6 +107,10 @@ M.defaults = { normal = "", insert = "", }, + toggle = { + debug = "ad", + hint = "ah", + }, }, windows = { wrap = true, -- similar to vim.o.wrap @@ -118,7 +122,6 @@ M.defaults = { }, --- @class AvanteConflictUserConfig diff = { - debug = false, autojump = true, ---@type string | fun(): any list_opener = "copen", diff --git a/lua/avante/diff.lua b/lua/avante/diff.lua index 8e4401f8f..be83037bc 100644 --- a/lua/avante/diff.lua +++ b/lua/avante/diff.lua @@ -329,7 +329,7 @@ local function register_cursor_move_events(bufnr) end local hint = string.format( - " [Press <%s> for OURS, <%s> for THEIRS, <%s> for BOTH, <%s> for PREV, <%s> for NEXT] ", + "[<%s> for OURS, <%s> for THEIRS, <%s> for BOTH, <%s> for PREV, <%s> for NEXT]", Config.diff.mappings.ours, Config.diff.mappings.theirs, Config.diff.mappings.both, diff --git a/lua/avante/init.lua b/lua/avante/init.lua index 1f5d86435..46b8f9245 100644 --- a/lua/avante/init.lua +++ b/lua/avante/init.lua @@ -43,6 +43,25 @@ end H.keymaps = function() vim.keymap.set({ "n", "v" }, Config.mappings.ask, M.toggle, { noremap = true }) vim.keymap.set("n", Config.mappings.refresh, M.refresh, { noremap = true }) + + Utils.toggle_map("n", Config.mappings.toggle.debug, { + name = "debug", + get = function() + return Config.debug + end, + set = function(state) + Config.override({ debug = state }) + end, + }) + Utils.toggle_map("n", Config.mappings.toggle.hint, { + name = "hint", + get = function() + return Config.hints.enabled + end, + set = function(state) + Config.override({ hints = { enabled = state } }) + end, + }) end H.autocmds = function() diff --git a/lua/avante/selection.lua b/lua/avante/selection.lua index c2f2ef8f4..1925aa668 100644 --- a/lua/avante/selection.lua +++ b/lua/avante/selection.lua @@ -43,7 +43,7 @@ end function Selection:show_hints_popup() self:close_hints_popup() - local hint_text = string.format(" [Ask %s] ", Config.mappings.ask) + local hint_text = string.format(" [%s: ask avante] ", Config.mappings.ask) local virt_text_line = self:get_virt_text_line() diff --git a/lua/avante/sidebar.lua b/lua/avante/sidebar.lua index 4329fe361..b6451f6e4 100644 --- a/lua/avante/sidebar.lua +++ b/lua/avante/sidebar.lua @@ -492,7 +492,7 @@ function Sidebar:on_mount() current_apply_extmark_id = api.nvim_buf_set_extmark(self.result.bufnr, CODEBLOCK_KEYBINDING_NAMESPACE, block.start_line, -1, { - virt_text = { { " [Press to Apply these patches] ", "Keyword" } }, + virt_text = { { " [: apply patch] ", "Keyword" } }, virt_text_pos = "right_align", hl_group = "Keyword", priority = PRIORITY, @@ -1366,9 +1366,8 @@ function Sidebar:create_input() local function show_hint() close_hint() -- Close the existing hint window - local hint_text = "Press " - .. (vim.fn.mode() ~= "i" and Config.mappings.submit.normal or Config.mappings.submit.insert) - .. " to submit" + local hint_text = vim.fn.mode() ~= "i" and Config.mappings.submit.normal + or Config.mappings.submit.insert .. ": submit" local buf = api.nvim_create_buf(false, true) api.nvim_buf_set_lines(buf, 0, -1, false, { hint_text }) diff --git a/lua/avante/utils/init.lua b/lua/avante/utils/init.lua index f960f03f4..6f8048f6b 100644 --- a/lua/avante/utils/init.lua +++ b/lua/avante/utils/init.lua @@ -24,6 +24,25 @@ M.has = function(plugin) return require("lazy.core.config").plugins[plugin] ~= nil end +---@alias _ToggleSet fun(state: boolean): nil +---@alias _ToggleGet fun(): boolean +--- +---@param lhs string +---@param toggle {name: string, set: _ToggleSet, get: _ToggleGet} +---@param mode? string | string[] +M.toggle_map = function(mode, lhs, toggle) + vim.keymap.set(mode or { "n" }, lhs, function() + toggle.set(not toggle.get()) + local state = toggle.get() + if state then + M.info("enabled: " .. toggle.name, { title = "Avante" }) + else + M.warn("disabled: " .. toggle.name, { title = "Avante" }) + end + return state + end, { desc = "toggle(avante): " .. toggle.name }) +end + ---@param str string ---@param opts? {suffix?: string, prefix?: string} function M.trim(str, opts)