From 40af7113a295b8c4f8040450076bc72f85e2c1ed Mon Sep 17 00:00:00 2001 From: brook hong Date: Sun, 24 Aug 2025 02:57:52 +0800 Subject: [PATCH] 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> --- lua/avante/api.lua | 2 ++ lua/avante/config.lua | 1 + lua/avante/init.lua | 9 +++++++++ lua/avante/sidebar.lua | 10 ++++++---- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lua/avante/api.lua b/lua/avante/api.lua index 36a0513..749107c 100644 --- a/lua/avante/api.lua +++ b/lua/avante/api.lua @@ -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) diff --git a/lua/avante/config.lua b/lua/avante/config.lua index c61cd33..e22dd29 100644 --- a/lua/avante/config.lua +++ b/lua/avante/config.lua @@ -524,6 +524,7 @@ M._defaults = { -- NOTE: The following will be safely set by avante.nvim ask = "aa", new_ask = "an", + full_view_ask = "am", edit = "ae", refresh = "ar", focus = "af", diff --git a/lua/avante/init.lua b/lua/avante/init.lua index f9ba958..342f810 100644 --- a/lua/avante/init.lua +++ b/lua/avante/init.lua @@ -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, diff --git a/lua/avante/sidebar.lua b/lua/avante/sidebar.lua index b7a94fc..fdcb7fa 100644 --- a/lua/avante/sidebar.lua +++ b/lua/avante/sidebar.lua @@ -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