Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions lua/codediff/ui/explorer/refresh.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,13 @@ function M.setup_auto_refresh(explorer, tabpage)
if watch_err then
return
end
-- Only refresh if this tabpage is current
if vim.api.nvim_get_current_tabpage() == tabpage and vim.api.nvim_tabpage_is_valid(tabpage) and not explorer.is_hidden then
if not vim.api.nvim_tabpage_is_valid(tabpage) or explorer.is_hidden then
return
end
if vim.api.nvim_get_current_tabpage() == tabpage then
debounced_refresh()
else
explorer._pending_refresh = true
end
end)
)
Expand All @@ -118,6 +122,17 @@ function M.setup_auto_refresh(explorer, tabpage)
callback = cleanup,
})

-- Flush pending refresh when returning to the codediff tab
vim.api.nvim_create_autocmd("TabEnter", {
group = group,
callback = function()
if explorer._pending_refresh and vim.api.nvim_get_current_tabpage() == tabpage then
explorer._pending_refresh = nil
debounced_refresh()
end
end,
})

return cleanup
end

Expand Down
2 changes: 1 addition & 1 deletion lua/codediff/ui/explorer/render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ function M.create(status_result, git_root, tabpage, width, base_revision, target
-- Same file can have different diffs (staged vs HEAD, working vs staged)
local session = lifecycle.get_session(tabpage)
if session then
local is_same_file = (session.modified_path == abs_path or (session.git_root and session.original_path == file_path))
local is_same_file = (session.modified_path == abs_path or session.modified_path == file_path or (session.git_root and session.original_path == file_path))

if is_same_file and not opts.force then
-- Check if it's the same diff comparison
Expand Down
19 changes: 15 additions & 4 deletions lua/codediff/ui/view/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ function M.is_virtual_revision(revision)
return revision ~= nil and revision ~= "WORKING"
end

-- Exact buffer name lookup (vim.fn.bufnr uses pattern matching which
-- causes prefix collisions, e.g. "Makefile" matching "Makefile.win")
local function bufnr_exact(name)
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_buf_is_valid(buf) and vim.api.nvim_buf_get_name(buf) == name then
return buf
end
end
return -1
end

-- Prepare buffer information for loading
-- Returns: { bufnr = number?, target = string?, needs_edit = boolean }
-- - If buffer already exists: { bufnr = 123, target = nil, needs_edit = false }
Expand All @@ -17,8 +28,8 @@ function M.prepare_buffer(is_virtual, git_root, revision, path)
if is_virtual then
-- Virtual file: generate URL
local virtual_url = virtual_file.create_url(git_root, revision, path)
-- Check if buffer already exists
local existing_buf = vim.fn.bufnr(virtual_url)
-- Check if buffer already exists (exact match to avoid prefix collisions)
local existing_buf = bufnr_exact(virtual_url)

-- For :0 (staged index), always force reload because index can change
-- when user runs git add/reset. For commits (immutable), we can cache.
Expand All @@ -41,8 +52,8 @@ function M.prepare_buffer(is_virtual, git_root, revision, path)
}
end
else
-- Real file: check if already loaded
local existing_buf = vim.fn.bufnr(path)
-- Real file: use exact match for buffer lookup
local existing_buf = bufnr_exact(path)
if existing_buf ~= -1 then
-- Buffer already exists, reuse it
return {
Expand Down