Skip to content

Commit

Permalink
fix: failed to rename buffer (yetone#730)
Browse files Browse the repository at this point in the history
  • Loading branch information
yetone authored Oct 16, 2024
1 parent 9907f05 commit a0d3845
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
3 changes: 1 addition & 2 deletions lua/avante/sidebar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 12 additions & 6 deletions lua/avante/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
local buf = api.nvim_create_buf(true, false)
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

api.nvim_buf_set_name(buf, filepath)
-- Create a new buffer without setting its name
local buf = api.nvim_create_buf(true, false)

-- 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
Expand Down

0 comments on commit a0d3845

Please sign in to comment.