Skip to content

fix: diff cleanup refocus terminal - #308

Open
perrin4869 wants to merge 3 commits into
coder:mainfrom
perrin4869:fix/diff-cleanup-refocus-terminal
Open

fix: diff cleanup refocus terminal#308
perrin4869 wants to merge 3 commits into
coder:mainfrom
perrin4869:fix/diff-cleanup-refocus-terminal

Conversation

@perrin4869

Copy link
Copy Markdown

Problem

When a diff opens in the current tab (the default, diff_opts.open_in_new_tab = false) and you accept it with :w, _cleanup_diff_state closes the diff's windows (new_window, and target_window if the plugin created it) but never explicitly restores focus afterward. Neovim is left to pick whatever window it considers "next" after the close, which is not necessarily the Claude terminal — in practice you can land in a completely unrelated window (e.g. a file explorer split), not back in the terminal you were working from.

This is inconsistent with the open_in_new_tab = true cleanup path, which does explicitly restore the original tab and calls terminal.ensure_visible().

Fix

In the non-new-tab branch of _cleanup_diff_state, after the existing terminal-resize call, explicitly refocus the Claude terminal window if one is visible in the current tab — reusing the same find_claudecode_terminal_window() lookup already used for the resize, so there's no new logic to find it.

This is gated behind the existing diff_opts.keep_terminal_focus option (default false), consistent with how that option already governs focus behavior at diff-open time — enabling it now also returns focus to the terminal after a same-tab diff is accepted/rejected, not just when it opens. With the default (false), behavior is unchanged.

Related

Testing

Verified manually: with keep_terminal_focus = true and the default same-tab diff layout, propose a change, run :w to accept it, and confirm the cursor returns to the Claude terminal window instead of an adjacent one.

Added tests/unit/diff_cleanup_terminal_focus_spec.lua covering:

  • refocuses the terminal when keep_terminal_focus is enabled
  • leaves focus untouched when it's disabled (default) — no behavior change
  • cleanup doesn't error when no terminal window is visible

luac -p and luacheck (0 warnings across lua/ and tests/, 114 files) pass. I wasn't able to run the full busted suite in my environment (mise/busted not installed there) — please run it in CI before merging.

Also updated the keep_terminal_focus description in README.md and CLAUDE.md to reflect the widened scope.

_cleanup_diff_state's non-new-tab path closed the diff windows but
never restored focus afterward, leaving Neovim's default post-close
window selection to decide where the cursor lands -- often an
unrelated adjacent window instead of the Claude terminal. Explicitly
refocus the terminal window (already resolved for the resize call)
once the diff windows are closed, matching the terminal-visibility
behavior the open_in_new_tab path already provides.
Adds regression coverage for the previous commit: accepting a diff via
:w (BufWriteCmd -> _resolve_diff_as_saved -> close_tab ->
_cleanup_diff_state) should leave focus on the Claude terminal window,
and cleanup should not error when no terminal window is visible.

Verified with luac -p and luacheck (0 warnings across lua/ and tests/);
could not run the busted suite itself in this environment (mise/busted
unavailable), so please run it in CI.
Make the terminal refocus added in the previous commit opt-in via the
existing diff_opts.keep_terminal_focus flag, rather than unconditional,
consistent with how that option already governs focus behavior at
diff-open time. Updates the test to cover both the enabled and
default-disabled cases, and documents the widened scope in README.md
and CLAUDE.md.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant