refactor(sidebar): move handle_submit() from create_input_container

handle_submit() is not directly related to the input container, and may
be called from other places. Make it a method of Sidebar class and
adjust the callers.
This commit is contained in:
Dmitry Torokhov
2025-10-07 17:24:19 -07:00
parent d0f0580d64
commit f092bb3ec0

View File

@@ -867,7 +867,7 @@ end
function Sidebar:retry_user_request() function Sidebar:retry_user_request()
local block = self:get_current_user_request_block() local block = self:get_current_user_request_block()
if not block then return end if not block then return end
self.handle_submit(block.content) self:handle_submit(block.content)
end end
function Sidebar:handle_expand_message(message_uuid, expanded) function Sidebar:handle_expand_message(message_uuid, expanded)
@@ -2623,27 +2623,15 @@ function Sidebar:get_generate_prompts_options(request, cb)
if cb then cb(prompts_opts) end if cb then cb(prompts_opts) end
end end
function Sidebar:initialize_token_count()
if Config.behaviour.enable_token_counting then self:get_generate_prompts_options("") end
end
function Sidebar:create_input_container()
if self.containers.input then self.containers.input:unmount() end
if not self.code.bufnr or not api.nvim_buf_is_valid(self.code.bufnr) then return end
if self.chat_history == nil then self:reload_chat_history() end
---@param request string ---@param request string
local function handle_submit(request) function Sidebar:handle_submit(request)
if Config.prompt_logger.enabled then PromptLogger.log_prompt(request) end if Config.prompt_logger.enabled then PromptLogger.log_prompt(request) end
if self.is_generating then if self.is_generating then
self:add_history_messages({ self:add_history_messages({ History.Message:new("user", request) })
History.Message:new("user", request),
})
return return
end end
if request:match("@codebase") and not vim.fn.expand("%:e") then if request:match("@codebase") and not vim.fn.expand("%:e") then
self:update_content("Please open a file first before using @codebase", { focus = false, scroll = false }) self:update_content("Please open a file first before using @codebase", { focus = false, scroll = false })
return return
@@ -2683,14 +2671,12 @@ function Sidebar:create_input_container()
local selected_filepaths = self.file_selector:get_selected_filepaths() local selected_filepaths = self.file_selector:get_selected_filepaths()
---@type AvanteSelectedCode | nil ---@type AvanteSelectedCode | nil
local selected_code = nil local selected_code = self.code.selection
if self.code.selection ~= nil then and {
selected_code = {
path = self.code.selection.filepath, path = self.code.selection.filepath,
file_type = self.code.selection.filetype, file_type = self.code.selection.filetype,
content = self.code.selection.content, content = self.code.selection.content,
} }
end
--- HACK: we need to set focus to true and scroll to false to --- HACK: we need to set focus to true and scroll to false to
--- prevent the cursor from jumping to the bottom of the --- prevent the cursor from jumping to the bottom of the
@@ -2797,9 +2783,7 @@ function Sidebar:create_input_container()
}) })
vim.defer_fn(function() vim.defer_fn(function()
if if Utils.is_valid_container(self.containers.result, true) and Config.behaviour.jump_result_buffer_on_finish then
Utils.is_valid_container(self.containers.result, true) and Config.behaviour.jump_result_buffer_on_finish
then
api.nvim_set_current_win(self.containers.result.winid) api.nvim_set_current_win(self.containers.result.winid)
end end
if Config.behaviour.auto_apply_diff_after_generation then self:apply(false) end if Config.behaviour.auto_apply_diff_after_generation then self:apply(false) end
@@ -2878,6 +2862,17 @@ function Sidebar:create_input_container()
end) end)
end end
function Sidebar:initialize_token_count()
if Config.behaviour.enable_token_counting then self:get_generate_prompts_options("") end
end
function Sidebar:create_input_container()
if self.containers.input then self.containers.input:unmount() end
if not self.code.bufnr or not api.nvim_buf_is_valid(self.code.bufnr) then return end
if self.chat_history == nil then self:reload_chat_history() end
local function get_position() local function get_position()
if self:get_layout() == "vertical" then return "bottom" end if self:get_layout() == "vertical" then return "bottom" end
return "right" return "right"
@@ -2922,11 +2917,9 @@ function Sidebar:create_input_container()
if request == "" then return end if request == "" then return end
api.nvim_buf_set_lines(self.containers.input.bufnr, 0, -1, false, {}) api.nvim_buf_set_lines(self.containers.input.bufnr, 0, -1, false, {})
api.nvim_win_set_cursor(self.containers.input.winid, { 1, 0 }) api.nvim_win_set_cursor(self.containers.input.winid, { 1, 0 })
handle_submit(request) self:handle_submit(request)
end end
self.handle_submit = handle_submit
self.containers.input:mount() self.containers.input:mount()
PromptLogger.init() PromptLogger.init()
@@ -3058,14 +3051,6 @@ function Sidebar:create_input_container()
end end
end, end,
}) })
api.nvim_create_autocmd("User", {
group = self.augroup,
pattern = "AvanteInputSubmitted",
callback = function(ev)
if ev.data and ev.data.request then handle_submit(ev.data.request) end
end,
})
end end
-- FIXME: this is used by external plugin users -- FIXME: this is used by external plugin users
@@ -3227,6 +3212,14 @@ function Sidebar:render(opts)
self:update_content_with_history() self:update_content_with_history()
end end
api.nvim_create_autocmd("User", {
group = self.augroup,
pattern = "AvanteInputSubmitted",
callback = function(ev)
if ev.data and ev.data.request then self:handle_submit(ev.data.request) end
end,
})
return self return self
end end