fix: do not write to invalid buffer (#112)

This commit is contained in:
yetone 2024-08-20 17:58:05 +08:00 committed by GitHub
parent fcd7a82ff7
commit 00f1e296b0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -245,7 +245,7 @@ end
---@param content string concatenated content of the buffer
---@param opts? {focus?: boolean, stream?: boolean, scroll?: boolean, callback?: fun(): nil} whether to focus the result view
function Sidebar:update_content(content, opts)
if not self.view.buf then
if not self.view.buf or not api.nvim_buf_is_valid(self.view.buf) then
return
end
opts = vim.tbl_deep_extend("force", { focus = true, scroll = true, stream = false, callback = nil }, opts or {})
@ -267,6 +267,9 @@ function Sidebar:update_content(content, opts)
end
vim.schedule(function()
if not self.view.buf or not api.nvim_buf_is_valid(self.view.buf) then
return
end
scroll_to_bottom()
local lines = vim.split(content, "\n")
api.nvim_set_option_value("modifiable", true, { buf = self.view.buf })
@ -284,7 +287,7 @@ function Sidebar:update_content(content, opts)
end)
else
vim.defer_fn(function()
if self.view.buf == nil then
if not self.view.buf or not api.nvim_buf_is_valid(self.view.buf) then
return
end
local lines = vim.split(content, "\n")