fix(diff): keep terminal focus for floating terminals #178
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.
Fixes #150.
When
diff_opts.keep_terminal_focus = trueand the Claude terminal is a floating window (e.g. Snacksposition="float"), opening a diff no longer steals focus to the hidden diff split.Changes
find_claudecode_terminal_window()to fall back to floating windows (still preferring split terminals)Tests
make checkmake test📋 Implementation Plan
Plan: Investigate + fix #150 (keep_terminal_focus skips floating terminal windows)
Context / Why
Issue #150 reports that when
diff_opts.keep_terminal_focus = trueand the Claude terminal is displayed as a floating window (e.g. via Snackssnacks_win_opts = { position = "float" }), opening a diff steals focus to the diff split behind the float. Because focus never returns to the terminal, user input goes to the hidden diff window and the UX breaks.Goal: confirm whether this is a bug (vs expected behavior) and, if so, outline a safe fix + regression coverage.
Evidence (what was verified)
find_claudecode_terminal_window()inlua/claudecode/diff.luaas skipping floats.lua/claudecode/diff.lua:find_claudecode_terminal_window()currently returns the terminal window only if it is not floating (checkswin_config.relative).setup_new_buffer()uses this helper underdiff_opts.keep_terminal_focusto restore focus after opening a diff.lua/claudecode/config.luadocumentsdiff_opts.keep_terminal_focusas “moves focus back to terminal after diff opens”, with no caveat about floating windows.Bug assessment
Confirmed bug.
With
keep_terminal_focus = true, the plugin intends to refocus the Claude terminal after creating the diff window. When the terminal lives in a floating window,find_claudecode_terminal_window()returnsnil, so the scheduled refocus never happens. This matches the issue report and is inconsistent with the documented meaning ofkeep_terminal_focus.Implementation plan (fix + prove)
1) Add a regression test that fails on current
mainAdd a unit test that simulates a floating terminal window:
tests/unit/diff_mcp_spec.lua) or create a focused new spec (e.g.tests/unit/diff_keep_terminal_focus_float_spec.lua).diff.setup({ diff_opts = { keep_terminal_focus = true } }).package.loaded["claudecode.terminal"] = { get_active_terminal_bufnr = function() return TERM_BUF end }.vim.api.nvim_create_buf) and a floating window entry for it by settingvim._windows[TERM_WIN].config = { relative = "editor" }and ensuringTERM_WINis invim._tab_windows[current_tab].diff.open_diff_blocking(...)inside a coroutine andcoroutine.resumeonce (it should suspend after setup).resume,vim.api.nvim_get_current_win()(orvim._current_window) should equalTERM_WIN.Optional: add a second assertion for the non-floating case to ensure we don’t regress existing behavior.
2) Fix
find_claudecode_terminal_window()to support floatsIn
lua/claudecode/diff.lua, update the helper so that it can return floating windows:This preserves the original intent (prefer split terminals) while making
keep_terminal_focuswork for floats.Fix sketch (for the implementer)
buf == terminal_bufnr:win_config.relativeis empty/nil → return immediately.floating_fallback.floating_fallback.3) Ensure terminal-resize code remains sane for floats
find_claudecode_terminal_window()is also used in diff cleanup and terminal width restoration paths.nvim_win_set_widthcalls behindwin_config.relative == "".Whichever behavior is chosen, add/adjust a test so the behavior is explicit.
4) Validate locally
make test,make check.diff_opts.keep_terminal_focus = true.5) Close the loop on the issue
keep_terminal_focussupports both split and floating terminal windows.Generated with
mux• Model:openai:gpt-5.2• Thinking:xhigh