fix: diff cleanup refocus terminal - #308
Open
perrin4869 wants to merge 3 commits into
Open
Conversation
_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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_statecloses the diff's windows (new_window, andtarget_windowif 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 = truecleanup path, which does explicitly restore the original tab and callsterminal.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 samefind_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_focusoption (defaultfalse), 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
:ClaudeCodeDiffAccept/:ClaudeCodeDiffDeny(the explicit user commands) don't call_cleanup_diff_stateat all today. It also extendskeep_terminal_focusto that resolve path. This PR covers the separate:w→close_tab→_cleanup_diff_statepath (used when Claude itself resolves the diff), which fix(diff): user-initiated accept/reject should clean up windows and buffers #241 doesn't touch — no line overlap between the two.Testing
Verified manually: with
keep_terminal_focus = trueand the default same-tab diff layout, propose a change, run:wto 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.luacovering:keep_terminal_focusis enabledluac -pandluacheck(0 warnings acrosslua/andtests/, 114 files) pass. I wasn't able to run the fullbustedsuite in my environment (mise/bustednot installed there) — please run it in CI before merging.Also updated the
keep_terminal_focusdescription inREADME.mdandCLAUDE.mdto reflect the widened scope.