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:
@@ -867,7 +867,7 @@ end
|
||||
function Sidebar:retry_user_request()
|
||||
local block = self:get_current_user_request_block()
|
||||
if not block then return end
|
||||
self.handle_submit(block.content)
|
||||
self:handle_submit(block.content)
|
||||
end
|
||||
|
||||
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
|
||||
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
|
||||
local function handle_submit(request)
|
||||
function Sidebar:handle_submit(request)
|
||||
if Config.prompt_logger.enabled then PromptLogger.log_prompt(request) end
|
||||
|
||||
if self.is_generating then
|
||||
self:add_history_messages({
|
||||
History.Message:new("user", request),
|
||||
})
|
||||
self:add_history_messages({ History.Message:new("user", request) })
|
||||
return
|
||||
end
|
||||
|
||||
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 })
|
||||
return
|
||||
@@ -2683,14 +2671,12 @@ function Sidebar:create_input_container()
|
||||
local selected_filepaths = self.file_selector:get_selected_filepaths()
|
||||
|
||||
---@type AvanteSelectedCode | nil
|
||||
local selected_code = nil
|
||||
if self.code.selection ~= nil then
|
||||
selected_code = {
|
||||
local selected_code = self.code.selection
|
||||
and {
|
||||
path = self.code.selection.filepath,
|
||||
file_type = self.code.selection.filetype,
|
||||
content = self.code.selection.content,
|
||||
}
|
||||
end
|
||||
|
||||
--- HACK: we need to set focus to true and scroll to false to
|
||||
--- prevent the cursor from jumping to the bottom of the
|
||||
@@ -2797,9 +2783,7 @@ function Sidebar:create_input_container()
|
||||
})
|
||||
|
||||
vim.defer_fn(function()
|
||||
if
|
||||
Utils.is_valid_container(self.containers.result, true) and Config.behaviour.jump_result_buffer_on_finish
|
||||
then
|
||||
if 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)
|
||||
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
|
||||
|
||||
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()
|
||||
if self:get_layout() == "vertical" then return "bottom" end
|
||||
return "right"
|
||||
@@ -2922,11 +2917,9 @@ function Sidebar:create_input_container()
|
||||
if request == "" then return end
|
||||
api.nvim_buf_set_lines(self.containers.input.bufnr, 0, -1, false, {})
|
||||
api.nvim_win_set_cursor(self.containers.input.winid, { 1, 0 })
|
||||
handle_submit(request)
|
||||
self:handle_submit(request)
|
||||
end
|
||||
|
||||
self.handle_submit = handle_submit
|
||||
|
||||
self.containers.input:mount()
|
||||
PromptLogger.init()
|
||||
|
||||
@@ -3058,14 +3051,6 @@ function Sidebar:create_input_container()
|
||||
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
|
||||
|
||||
-- FIXME: this is used by external plugin users
|
||||
@@ -3227,6 +3212,14 @@ function Sidebar:render(opts)
|
||||
self:update_content_with_history()
|
||||
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
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user