Bug: explorer.focus_on_select = false ignored for added/untracked/deleted files in inline layout

Open Beginner friendly
#567 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
lua, neovim
Domain
tooling

Research direction

The focus leak comes from open_real_file in lua/codediff/ui/view/helpers.lua, which calls nvim_set_current_win before :edit and never restores the window. Compare with inline_view/update.lua, which saves and restores the current window in commit_update, and with inline_view/single_file.lua show_single_file, which does not. Done when selecting an added, untracked or deleted file with focus_on_select = false in inline layout leaves the cursor in the explorer; check by reproducing with the repro config in the issue.

Written by the indexing model from the issue text.

Description

Type/Bug
Environment
  • codediff.nvim v4.0.6
  • Neovim v0.12.2 (macOS)
Description

With diff.layout = "inline", explorer.focus_on_select = false and auto_open_on_cursor = true,, selecting a
modified file in the explorer correctly keeps the cursor in the explorer, but
selecting an added, untracked or deleted file moves focus into the file
pane. Focus then stays there, so further j/k navigate the file instead of the
explorer. This doesn't seem to happen on every file though. I couldn't find a clear pattern in my testing.

Side-by-side layout is not affected.

Reproduction
-- repro.lua  ->  nvim -u repro.lua
local root = vim.fn.stdpath("data") .. "/site/pack/deps/start/lazy.nvim"
if not vim.loop.fs_stat(root) then
  vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", root })
end
vim.opt.rtp:prepend(root)

require("lazy").setup({
  {
    "esmuellert/codediff.nvim",
    cmd = "CodeDiff",
    opts = {
      diff = { layout = "inline" },
      explorer = {
        auto_open_on_cursor = true,
        focus_on_select = false,
      },
    },
  },
}, { root = vim.fn.stdpath("data") .. "/repro/plugins" })
  1. In a repo with at least one file added relative to main, run :CodeDiff main.
  2. Move down the explorer with j until the cursor reaches the added file.

Expected: cursor stays in the explorer.
Actual: cursor jumps to the file pane and stays there.

Note that diff.layout = "inline" is required to reproduce. The bug does not occur
with the default side-by-side layout.

Suggested cause and fix by Claude

I don't dare create a vibe coded PR for this. This is the suggested cause and fix, that Claude created for me. The monkey-patched fix seems to work on my machine. I unfortunately don't have the time to look into this project and build a proper fix or verify this in detail. Still want to share it here, as it might be useful.

Cause

helpers.open_real_file (lua/codediff/ui/view/helpers.lua:162) calls
nvim_set_current_win(win) before :edit and never restores the previous window:

vim.api.nvim_set_current_win(win)
vim.cmd("edit " .. vim.fn.fnameescape(target))

Two callers in the inline layout:

  • inline_view/update.lua (modified files) saves saved_current_win and restores it
    in commit_update, so the leaked focus change is undone.
  • inline_view/single_file.lua:53 (added/untracked/deleted files) does not, so the
    focus change survives.

The side-by-side single-file path loads through helpers.show_real_file_buffer, which
never changes the current window — hence the layout-specific behaviour.

Suggested fix

Either save and restore the current window inside open_real_file itself, or mirror
update.lua's save/restore in inline_view/single_file.show_single_file.

Dominant language
Lua
Stars
1.6k
Forks
104
Avg merge
16d 23h
Merged PRs (30d)
1

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from esmuellert/codediff.nvim

All issues in esmuellert/codediff.nvim

Similar issues

More Lua issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.