Skip to content

Commit d0772d7

Browse files
committed
fix: update file reference on keyboard window navigation
Add WinEnter autocommand to selection tracking so keyboard navigation (ctrl-h, ctrl-l, :wincmd) properly updates the file reference in Claude Code. Previously only mouse clicks triggered updates. The fix cancels pending debounce timers and uses a 10ms delay to ensure window/buffer state is settled, matching the existing mouse handler behavior. Closes coder#2
1 parent 837dbb8 commit d0772d7

2 files changed

Lines changed: 467 additions & 1 deletion

File tree

lua/claudecode/selection.lua

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function M.disable()
5757
end
5858

5959
---Creates autocommands for tracking selections.
60-
---Sets up listeners for CursorMoved, CursorMovedI, BufEnter, ModeChanged, and TextChanged events.
60+
---Sets up listeners for CursorMoved, CursorMovedI, BufEnter, WinEnter, ModeChanged, and TextChanged events.
6161
---@local
6262
function M._create_autocommands()
6363
local group = vim.api.nvim_create_augroup("ClaudeCodeSelection", { clear = true })
@@ -82,6 +82,13 @@ function M._create_autocommands()
8282
M.on_text_changed()
8383
end,
8484
})
85+
86+
vim.api.nvim_create_autocmd("WinEnter", {
87+
group = group,
88+
callback = function()
89+
M.on_win_enter()
90+
end,
91+
})
8592
end
8693

8794
---Sets up mouse event handler for capturing mouse-based selections.
@@ -149,6 +156,25 @@ function M.on_text_changed()
149156
M.debounce_update()
150157
end
151158

159+
---Handles window enter events.
160+
---Triggers an immediate update to ensure file reference is sent on keyboard navigation.
161+
---Uses a small delay similar to mouse handler to ensure state is settled.
162+
function M.on_win_enter()
163+
-- Cancel any pending debounce to avoid duplicate updates
164+
if M.state.debounce_timer then
165+
vim.loop.timer_stop(M.state.debounce_timer)
166+
M.state.debounce_timer = nil
167+
end
168+
169+
-- Use a small delay to ensure window/buffer state is settled
170+
-- This mirrors the mouse handler behavior which works reliably
171+
vim.defer_fn(function()
172+
if M.state.tracking_enabled then
173+
M.update_selection()
174+
end
175+
end, 10)
176+
end
177+
152178
---Debounces selection updates.
153179
---Ensures that `update_selection` is not called too frequently by deferring
154180
---its execution.

0 commit comments

Comments
 (0)