From c8ba3d36cc7457f2ffaf414b795b82735fc82827 Mon Sep 17 00:00:00 2001 From: Yang Jun Date: Mon, 6 Jan 2025 16:51:35 +0800 Subject: [PATCH] feat: focus to input after /clear or /reset be execuated (#1044) --- lua/avante/sidebar.lua | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/lua/avante/sidebar.lua b/lua/avante/sidebar.lua index 5fbaed7..007e5b6 100644 --- a/lua/avante/sidebar.lua +++ b/lua/avante/sidebar.lua @@ -133,6 +133,13 @@ function Sidebar:focus() return false end +function Sidebar:focus_input() + if self.input_container and self.input_container.winid and api.nvim_win_is_valid(self.input_container.winid) then + api.nvim_set_current_win(self.input_container.winid) + api.nvim_feedkeys("i", "n", false) + end +end + function Sidebar:is_open() return self.result_container and self.result_container.bufnr @@ -1381,10 +1388,16 @@ function Sidebar:clear_history(args, cb) if next(chat_history) ~= nil then chat_history = {} Path.history.save(self.code.bufnr, chat_history) - self:update_content("Chat history cleared", { focus = false, scroll = false }) + self:update_content( + "Chat history cleared", + { focus = false, scroll = false, callback = function() self:focus_input() end } + ) if cb then cb(args) end else - self:update_content("Chat history is already empty", { focus = false, scroll = false }) + self:update_content( + "Chat history is already empty", + { focus = false, scroll = false, callback = function() self:focus_input() end } + ) end end @@ -1404,10 +1417,17 @@ function Sidebar:reset_memory(args, cb) }) Path.history.save(self.code.bufnr, chat_history) local history_content = self:render_history_content(chat_history) - self:update_content(history_content, { focus = false, scroll = true }) + self:update_content(history_content, { + focus = false, + scroll = true, + callback = function() self:focus_input() end, + }) if cb then cb(args) end else - self:update_content("Chat history is already empty", { focus = false, scroll = false }) + self:update_content( + "Chat history is already empty", + { focus = false, scroll = false, callback = function() self:focus_input() end } + ) end end