diff --git a/lua/avante/sidebar.lua b/lua/avante/sidebar.lua index 196f64a..cabd26b 100644 --- a/lua/avante/sidebar.lua +++ b/lua/avante/sidebar.lua @@ -559,8 +559,7 @@ function Sidebar:apply(current_cursor) vim.defer_fn(function() for filepath, snippets in pairs(selected_snippets_map) do - local bufnr = Utils.get_opened_buffer(filepath) - if not bufnr then bufnr = Utils.create_new_buffer_with_file(filepath) end + local bufnr = Utils.get_or_create_buffer_with_filepath(filepath) insert_conflict_contents(bufnr, snippets) local winid = Utils.get_winid(bufnr) if not winid then goto continue end diff --git a/lua/avante/utils/init.lua b/lua/avante/utils/init.lua index 2bf55ec..6fc545b 100644 --- a/lua/avante/utils/init.lua +++ b/lua/avante/utils/init.lua @@ -661,23 +661,29 @@ function M.get_mentions() } end -function M.get_opened_buffer(filepath) +local function get_opened_buffer_by_filepath(filepath) for _, buf in ipairs(api.nvim_list_bufs()) do - if fn.buflisted(buf) == 1 and fn.bufname(buf) == filepath then return buf end + if fn.bufname(buf) == filepath then return buf end end return nil end -function M.create_new_buffer_with_file(filepath) +function M.get_or_create_buffer_with_filepath(filepath) + -- Check if a buffer with this filepath already exists + local existing_buf = get_opened_buffer_by_filepath(filepath) + if existing_buf then return existing_buf end + + -- Create a new buffer without setting its name local buf = api.nvim_create_buf(true, false) - api.nvim_buf_set_name(buf, filepath) - + -- Set the buffer options api.nvim_set_option_value("buftype", "", { buf = buf }) + -- Set the current buffer to the new buffer api.nvim_set_current_buf(buf) - vim.cmd("edit " .. filepath) + -- Use the edit command to load the file content and set the buffer name + vim.cmd("edit " .. vim.fn.fnameescape(filepath)) return buf end