fix: do not insert the stream results into user code (#94)

This commit is contained in:
yetone 2024-08-20 01:47:43 +08:00 committed by GitHub
parent 60e3eac77e
commit 940106d82b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -250,32 +250,47 @@ function Sidebar:update_content(content, opts)
end end
opts = vim.tbl_deep_extend("force", { focus = true, scroll = true, stream = false, callback = nil }, opts or {}) opts = vim.tbl_deep_extend("force", { focus = true, scroll = true, stream = false, callback = nil }, opts or {})
if opts.stream then if opts.stream then
local scroll_to_bottom = function()
local last_line = api.nvim_buf_line_count(self.view.buf)
local current_lines = api.nvim_buf_get_lines(self.view.buf, last_line - 1, last_line, false)
if #current_lines > 0 then
local last_line_content = current_lines[1]
local last_col = #last_line_content
xpcall(function()
api.nvim_win_set_cursor(self.view.win, { last_line, last_col })
end, function(err)
return err
end)
end
end
vim.schedule(function() vim.schedule(function()
api.nvim_set_option_value("modifiable", true, { buf = self.view.buf }) scroll_to_bottom()
local current_window = vim.api.nvim_get_current_win()
local cursor_position = vim.api.nvim_win_get_cursor(current_window)
local row, col = cursor_position[1], cursor_position[2]
local lines = vim.split(content, "\n") local lines = vim.split(content, "\n")
api.nvim_set_option_value("modifiable", true, { buf = self.view.buf })
vim.api.nvim_put(lines, "c", true, true) api.nvim_buf_call(self.view.buf, function()
api.nvim_put(lines, "c", true, true)
local num_lines = #lines end)
local last_line_length = #lines[num_lines] api.nvim_set_option_value("modifiable", false, { buf = self.view.buf })
vim.api.nvim_win_set_cursor(current_window, { row + num_lines - 1, col + last_line_length }) api.nvim_set_option_value("filetype", "Avante", { buf = self.view.buf })
if opts.scroll then
scroll_to_bottom()
end
end) end)
else else
vim.defer_fn(function() vim.defer_fn(function()
if self.view.buf == nil then if self.view.buf == nil then
return return
end end
local lines = vim.split(content, "\n")
local n_lines = #lines
local last_line_length = lines[n_lines]
api.nvim_set_option_value("modifiable", true, { buf = self.view.buf }) api.nvim_set_option_value("modifiable", true, { buf = self.view.buf })
api.nvim_buf_set_lines(self.view.buf, 0, -1, false, vim.split(content, "\n")) api.nvim_buf_set_lines(self.view.buf, 0, -1, false, lines)
api.nvim_set_option_value("modifiable", false, { buf = self.view.buf }) api.nvim_set_option_value("modifiable", false, { buf = self.view.buf })
api.nvim_set_option_value("filetype", "Avante", { buf = self.view.buf }) api.nvim_set_option_value("filetype", "Avante", { buf = self.view.buf })
if opts.callback ~= nil then
opts.callback()
end
if opts.focus and not self:is_focused() then if opts.focus and not self:is_focused() then
xpcall(function() xpcall(function()
--- set cursor to bottom of result view --- set cursor to bottom of result view
@ -287,11 +302,15 @@ function Sidebar:update_content(content, opts)
if opts.scroll then if opts.scroll then
xpcall(function() xpcall(function()
api.nvim_win_set_cursor(self.winid.result, { api.nvim_buf_line_count(self.bufnr.result), 0 }) api.nvim_win_set_cursor(self.winid.result, { n_lines, #last_line_length })
end, function(err) end, function(err)
return err return err
end) end)
end end
if opts.callback ~= nil then
opts.callback()
end
end, 0) end, 0)
end end
return self return self
@ -683,36 +702,52 @@ function Sidebar:render()
local filetype = api.nvim_get_option_value("filetype", { buf = self.code.buf }) local filetype = api.nvim_get_option_value("filetype", { buf = self.code.buf })
local is_first_chunk = true
---@type AvanteChunkParser ---@type AvanteChunkParser
local on_chunk = function(chunk) local on_chunk = function(chunk)
local append_chunk = function()
signal.is_loading = true signal.is_loading = true
full_response = full_response .. chunk full_response = full_response .. chunk
self:update_content(chunk, { stream = true, scroll = false }) self:update_content(chunk, { stream = true, scroll = true })
vim.schedule(function() vim.schedule(function()
vim.cmd("redraw") vim.cmd("redraw")
end) end)
end end
if is_first_chunk then
is_first_chunk = false
self:update_content(content_prefix, {
scroll = true,
callback = function()
vim.schedule(function()
vim.cmd("redraw")
append_chunk()
end)
end,
})
else
append_chunk()
end
end
---@type AvanteCompleteParser ---@type AvanteCompleteParser
local on_complete = function(err) local on_complete = function(err)
signal.is_loading = false signal.is_loading = false
if err ~= nil then if err ~= nil then
self:update_content(content_prefix .. full_response .. "\n\n🚨 Error: " .. vim.inspect(err)) self:update_content("\n\n🚨 Error: " .. vim.inspect(err), { stream = true, scroll = true })
return return
end end
-- Execute when the stream request is actually completed -- Execute when the stream request is actually completed
self:update_content( self:update_content("\n\n🎉🎉🎉 **Generation complete!** Please review the code suggestions above.\n\n", {
content_prefix stream = true,
.. full_response scroll = true,
.. "\n\n🎉🎉🎉 **Generation complete!** Please review the code suggestions above.\n\n",
{
callback = function() callback = function()
api.nvim_exec_autocmds("User", { pattern = VIEW_BUFFER_UPDATED_PATTERN }) api.nvim_exec_autocmds("User", { pattern = VIEW_BUFFER_UPDATED_PATTERN })
end, end,
} })
)
-- Save chat history -- Save chat history
table.insert(chat_history or {}, { table.insert(chat_history or {}, {