From 00f1e296b0937831db42f7a6f4bac8b2a9969fd8 Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 20 Aug 2024 17:58:05 +0800 Subject: [PATCH] fix: do not write to invalid buffer (#112) --- lua/avante/sidebar.lua | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lua/avante/sidebar.lua b/lua/avante/sidebar.lua index a9cc3ac..3f572fb 100644 --- a/lua/avante/sidebar.lua +++ b/lua/avante/sidebar.lua @@ -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")