Skip to content

Commit

Permalink
refactor: simplify buffer handling using builtin functions
Browse files Browse the repository at this point in the history
Replace manual buffer search and creation logic with built-in Neovim functions
bufadd() and bufload(). This change makes the code more concise and reliable
by leveraging Neovim's native buffer management APIs.

The new implementation:
- Removes redundant buffer search loop
- Uses bufadd() to add new buffer
- Uses bufload() to ensure buffer is loaded

Signed-off-by: Tomas Slusny <[email protected]>
  • Loading branch information
deathbeam committed Nov 28, 2024
1 parent 1ff4819 commit f2fc7ca
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions lua/CopilotChat/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ end
---@param end_line number
---@param config CopilotChat.config.shared
local function jump_to_diff(winnr, bufnr, start_line, end_line, config)
vim.api.nvim_win_set_cursor(winnr, { start_line, 0 })
pcall(vim.api.nvim_win_set_cursor, winnr, { start_line, 0 })
pcall(vim.api.nvim_buf_set_mark, bufnr, '<', start_line, 0, {})
pcall(vim.api.nvim_buf_set_mark, bufnr, '>', end_line, 0, {})
pcall(vim.api.nvim_buf_set_mark, bufnr, '[', start_line, 0, {})
Expand Down Expand Up @@ -1062,24 +1062,15 @@ function M.setup(config)

local diff_bufnr = diff.bufnr

-- Try to find existing buffer first
-- If buffer is not found, try to load it
if not diff_bufnr then
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
if utils.filename_same(vim.api.nvim_buf_get_name(buf), diff.filename) then
diff_bufnr = buf
break
end
end
end

-- Create new empty buffer if doesn't exist
if not diff_bufnr then
diff_bufnr = vim.api.nvim_create_buf(true, false)
vim.api.nvim_buf_set_name(diff_bufnr, diff.filename)
vim.bo[diff_bufnr].filetype = diff.filetype
diff_bufnr = vim.fn.bufadd(diff.filename)
vim.fn.bufload(diff_bufnr)
end

state.source.bufnr = diff_bufnr
vim.api.nvim_win_set_buf(state.source.winnr, diff_bufnr)

jump_to_diff(
state.source.winnr,
diff_bufnr,
Expand Down

0 comments on commit f2fc7ca

Please sign in to comment.