Add option custom_init to enable user to customize behaviour of sidebar (#2630)

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
This commit is contained in:
brook hong
2025-08-24 02:57:52 +08:00
committed by GitHub
parent 212797a2f2
commit 40af7113a2
4 changed files with 18 additions and 4 deletions

View File

@@ -110,6 +110,8 @@ end
---@field floating? boolean whether to open a floating input to enter the question
---@field new_chat? boolean whether to open a new chat
---@field without_selection? boolean whether to open a new chat without selection
---@field sidebar_pre_render? fun(sidebar: avante.Sidebar)
---@field sidebar_post_render? fun(sidebar: avante.Sidebar)
---@param opts? AskOptions
function M.ask(opts)

View File

@@ -524,6 +524,7 @@ M._defaults = {
-- NOTE: The following will be safely set by avante.nvim
ask = "<leader>aa",
new_ask = "<leader>an",
full_view_ask = "<leader>am",
edit = "<leader>ae",
refresh = "<leader>ar",
focus = "<leader>af",

View File

@@ -94,6 +94,15 @@ function H.keymaps()
function() require("avante.api").ask() end,
{ desc = "avante: ask" }
)
Utils.safe_keymap_set({ "n", "v" }, Config.mappings.full_view_ask, function()
require("avante.api").ask({
sidebar_post_render = function(sidebar)
sidebar:toggle_code_window()
vim.wo[sidebar.containers.result.winid].number = true
vim.wo[sidebar.containers.result.winid].relativenumber = true
end,
})
end, { desc = "avante: ask with full result view" })
Utils.safe_keymap_set(
{ "n", "v" },
Config.mappings.new_ask,

View File

@@ -1479,7 +1479,7 @@ function Sidebar:resize()
vim.defer_fn(function() vim.cmd("AvanteRefresh") end, 200)
end
function Sidebar:toggleCodeWindow()
function Sidebar:toggle_code_window()
local win_width = api.nvim_win_get_width(self.code.winid)
if win_width == 0 then
api.nvim_win_set_width(self.code.winid, self.code.win_width)
@@ -2734,14 +2734,14 @@ function Sidebar:create_input_container()
self.containers.input:map(
"n",
Config.mappings.sidebar.toggle_code_window_from_input.normal,
function() self:toggleCodeWindow() end
function() self:toggle_code_window() end
)
end
if Config.mappings.sidebar.toggle_code_window_from_input.insert ~= nil then
self.containers.input:map(
"i",
Config.mappings.sidebar.toggle_code_window_from_input.insert,
function() self:toggleCodeWindow() end
function() self:toggle_code_window() end
)
end
end
@@ -2892,6 +2892,7 @@ end
---@param opts AskOptions
function Sidebar:render(opts)
self.ask_opts = opts
if opts.sidebar_pre_render then opts.sidebar_pre_render(self) end
local function get_position()
return (opts and opts.win and opts.win.position) and opts.win.position or calculate_config_window_position()
@@ -2928,7 +2929,7 @@ function Sidebar:render(opts)
end)
self.containers.result:map("n", Config.mappings.sidebar.close, function() self:shutdown() end)
self.containers.result:map("n", Config.mappings.sidebar.toggle_code_window, function() self:toggleCodeWindow() end)
self.containers.result:map("n", Config.mappings.sidebar.toggle_code_window, function() self:toggle_code_window() end)
self:create_input_container()
@@ -2958,6 +2959,7 @@ function Sidebar:render(opts)
self:setup_colors()
if opts.sidebar_post_render then vim.defer_fn(function() opts.sidebar_post_render(self) end, 100) end
return self
end