Skip to content

Commit

Permalink
feat: allow pressing apply_all mapping anywhere in sidebar (yetone#528)…
Browse files Browse the repository at this point in the history
… (yetone#712)

- make apply and apply_all mappings configurable
- fixed bug where apply mapping was not unbound in unbind_apply_key
- allow apply_all mapping to be pressed anywhere in the sidebar

fixes yetone#528
  • Loading branch information
b0o authored Oct 12, 2024
1 parent e010c55 commit 347d9be
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ _See [config.lua#L9](./lua/avante/config.lua) for the full config_
insert = "<C-s>",
},
sidebar = {
apply_all = "A",
apply_cursor = "a",
switch_windows = "<Tab>",
reverse_switch_windows = "<S-Tab>",
},
Expand Down
2 changes: 2 additions & 0 deletions lua/avante/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ Respect and use existing conventions, libraries, etc that are already present in
suggestion = "<leader>as",
},
sidebar = {
apply_all = "A",
apply_cursor = "a",
switch_windows = "<Tab>",
reverse_switch_windows = "<S-Tab>",
},
Expand Down
40 changes: 26 additions & 14 deletions lua/avante/sidebar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,16 @@ function Sidebar:on_mount(opts)

current_apply_extmark_id =
api.nvim_buf_set_extmark(self.result.bufnr, CODEBLOCK_KEYBINDING_NAMESPACE, block.start_line, -1, {
virt_text = { { " [<a>: apply this, <A>: apply all] ", "AvanteInlineHint" } },
virt_text = {
{
string.format(
" [<%s>: apply this, <%s>: apply all] ",
Config.mappings.sidebar.apply_cursor,
Config.mappings.sidebar.apply_all
),
"AvanteInlineHint",
},
},
virt_text_pos = "right_align",
hl_group = "AvanteInlineHint",
priority = PRIORITY,
Expand All @@ -692,19 +701,15 @@ function Sidebar:on_mount(opts)
local function bind_apply_key()
vim.keymap.set(
"n",
"a",
Config.mappings.sidebar.apply_cursor,
function() self:apply(true) end,
{ buffer = self.result.bufnr, noremap = true, silent = true }
)
vim.keymap.set(
"n",
"A",
function() self:apply(false) end,
{ buffer = self.result.bufnr, noremap = true, silent = true }
)
end

local function unbind_apply_key() pcall(vim.keymap.del, "n", "A", { buffer = self.result.bufnr }) end
local function unbind_apply_key()
pcall(vim.keymap.del, "n", Config.mappings.sidebar.apply_cursor, { buffer = self.result.bufnr })
end

---@type AvanteCodeblock[]
local codeblocks = {}
Expand Down Expand Up @@ -739,7 +744,13 @@ function Sidebar:on_mount(opts)
end
end

local function bind_jump_keys()
local function bind_sidebar_keys()
vim.keymap.set(
"n",
Config.mappings.sidebar.apply_all,
function() self:apply(false) end,
{ buffer = self.result.bufnr, noremap = true, silent = true }
)
vim.keymap.set(
"n",
Config.mappings.jump.next,
Expand All @@ -754,8 +765,9 @@ function Sidebar:on_mount(opts)
)
end

local function unbind_jump_keys()
local function unbind_sidebar_keys()
if self.result and self.result.bufnr and api.nvim_buf_is_valid(self.result.bufnr) then
pcall(vim.keymap.del, "n", Config.mappings.sidebar.apply_all, { buffer = self.result.bufnr })
pcall(vim.keymap.del, "n", Config.mappings.jump.next, { buffer = self.result.bufnr })
pcall(vim.keymap.del, "n", Config.mappings.jump.prev, { buffer = self.result.bufnr })
end
Expand All @@ -780,7 +792,7 @@ function Sidebar:on_mount(opts)
buffer = self.result.bufnr,
callback = function(ev)
codeblocks = parse_codeblocks(ev.buf)
bind_jump_keys()
bind_sidebar_keys()
end,
})

Expand All @@ -789,13 +801,13 @@ function Sidebar:on_mount(opts)
callback = function()
if not self.result or not self.result.bufnr or not api.nvim_buf_is_valid(self.result.bufnr) then return end
codeblocks = parse_codeblocks(self.result.bufnr)
bind_jump_keys()
bind_sidebar_keys()
end,
})

api.nvim_create_autocmd("BufLeave", {
buffer = self.result.bufnr,
callback = function() unbind_jump_keys() end,
callback = function() unbind_sidebar_keys() end,
})

self:render_result()
Expand Down

0 comments on commit 347d9be

Please sign in to comment.