diff --git a/lua/codediff/ui/explorer/refresh.lua b/lua/codediff/ui/explorer/refresh.lua index 23b5a24..0b6ae86 100644 --- a/lua/codediff/ui/explorer/refresh.lua +++ b/lua/codediff/ui/explorer/refresh.lua @@ -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) ) @@ -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 diff --git a/lua/codediff/ui/explorer/render.lua b/lua/codediff/ui/explorer/render.lua index dbb2f16..88c6aa6 100644 --- a/lua/codediff/ui/explorer/render.lua +++ b/lua/codediff/ui/explorer/render.lua @@ -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 diff --git a/lua/codediff/ui/view/helpers.lua b/lua/codediff/ui/view/helpers.lua index 48d846f..c525618 100644 --- a/lua/codediff/ui/view/helpers.lua +++ b/lua/codediff/ui/view/helpers.lua @@ -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 } @@ -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. @@ -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 {