Skip to content

Commit d668b49

Browse files
committed
docs: improve terminal focus documentation and clarify behavior
- Update build_opts() documentation to explain focus parameter behavior - Add comprehensive comments explaining window restoration logic in native provider - Clarify that focus=false preserves user context in all terminal scenarios: - Existing terminal: stays in current window - Recovered terminal: stays in current window - New terminal: returns to original window - Improve maintainer understanding of focus control architecture Addresses Copilot feedback about documentation and behavior clarity. Change-Id: I06e785d5e2378b68fce41eb64018067d2754225d Signed-off-by: Thomas Kosiewski <[email protected]>
1 parent 8dfb564 commit d668b49

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

lua/claudecode/terminal/native.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ local function open_terminal(cmd_string, env_table, effective_config, focus)
5151

5252
if is_valid() then -- Should not happen if called correctly, but as a safeguard
5353
if focus then
54+
-- Focus existing terminal: switch to terminal window and enter insert mode
5455
vim.api.nvim_set_current_win(winid)
5556
vim.cmd("startinsert")
5657
end
58+
-- If focus=false, preserve user context by staying in current window
5759
return true
5860
end
5961

@@ -127,10 +129,11 @@ local function open_terminal(cmd_string, env_table, effective_config, focus)
127129
-- buftype=terminal is set by termopen
128130

129131
if focus then
132+
-- Focus the terminal: switch to terminal window and enter insert mode
130133
vim.api.nvim_set_current_win(winid)
131134
vim.cmd("startinsert")
132135
else
133-
-- Return to original window if not focusing
136+
-- Preserve user context: return to the window they were in before terminal creation
134137
vim.api.nvim_set_current_win(original_win)
135138
end
136139

@@ -279,8 +282,9 @@ function M.open(cmd_string, env_table, effective_config, focus)
279282
-- Note: We can't recover the job ID easily, but it's less critical
280283
logger.debug("terminal", "Recovered existing Claude terminal")
281284
if focus then
282-
focus_terminal()
285+
focus_terminal() -- Focus recovered terminal
283286
end
287+
-- If focus=false, preserve user context by staying in current window
284288
else
285289
if not open_terminal(cmd_string, env_table, effective_config, focus) then
286290
vim.notify("Failed to open Claude terminal using native fallback.", vim.log.levels.ERROR)

lua/claudecode/terminal/snacks.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ local function setup_terminal_events(term_instance, config)
4242
end, { buf = true })
4343
end
4444

45-
--- @param config table
46-
--- @param env_table table
47-
--- @param focus boolean|nil
48-
--- @return table
45+
--- Builds Snacks terminal options with focus control
46+
--- @param config table Terminal configuration (split_side, split_width_percentage, etc.)
47+
--- @param env_table table Environment variables to set for the terminal process
48+
--- @param focus boolean|nil Whether to focus the terminal when opened (defaults to true)
49+
--- @return table Snacks terminal options with start_insert/auto_insert controlled by focus parameter
4950
local function build_opts(config, env_table, focus)
5051
focus = utils.normalize_focus(focus)
5152
return {

0 commit comments

Comments
 (0)