Skip to content

Commit

Permalink
chore(keymaps): add toggle options (yetone#204)
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
  • Loading branch information
aarnphm authored Aug 25, 2024
1 parent 46ec0a5 commit 305d972
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 7 deletions.
5 changes: 4 additions & 1 deletion lua/avante/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ M.defaults = {
normal = "<CR>",
insert = "<C-s>",
},
toggle = {
debug = "<leader>ad",
hint = "<leader>ah",
},
},
windows = {
wrap = true, -- similar to vim.o.wrap
Expand All @@ -118,7 +122,6 @@ M.defaults = {
},
--- @class AvanteConflictUserConfig
diff = {
debug = false,
autojump = true,
---@type string | fun(): any
list_opener = "copen",
Expand Down
2 changes: 1 addition & 1 deletion lua/avante/diff.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions lua/avante/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion lua/avante/selection.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
7 changes: 3 additions & 4 deletions lua/avante/sidebar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 <A> to Apply these patches] ", "Keyword" } },
virt_text = { { " [<A>: apply patch] ", "Keyword" } },
virt_text_pos = "right_align",
hl_group = "Keyword",
priority = PRIORITY,
Expand Down Expand Up @@ -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 })
Expand Down
19 changes: 19 additions & 0 deletions lua/avante/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 305d972

Please sign in to comment.