Skip to content

Commit 193e5f1

Browse files
committed
fix: resolve hidden terminal visibility issue for @ mentions
- Fix snacks provider to detect and show hidden terminals when focus=false - Add hidden terminal logic to native provider using show_hidden_terminal() - Both providers now properly handle the case where terminal buffer exists but has no window - Clean up debug logging after identifying and resolving the issue - Preserve user window context when focus=false in both providers This resolves the issue where @ mentions wouldn't show the terminal when it was hidden in the background. Change-Id: Ibdd884ce9f6b7b0b72d2a04fba611db2b8ff52e7 Signed-off-by: Thomas Kosiewski <[email protected]>
1 parent d668b49 commit 193e5f1

File tree

2 files changed

+52
-17
lines changed

2 files changed

+52
-17
lines changed

lua/claudecode/terminal/native.lua

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,18 +196,22 @@ local function hide_terminal()
196196
end
197197
end
198198

199-
local function show_hidden_terminal(effective_config)
199+
local function show_hidden_terminal(effective_config, focus)
200200
-- Show an existing hidden terminal buffer in a new window
201201
if not bufnr or not vim.api.nvim_buf_is_valid(bufnr) then
202202
return false
203203
end
204204

205205
-- Check if it's already visible
206206
if is_terminal_visible() then
207-
focus_terminal()
207+
if focus then
208+
focus_terminal()
209+
end
208210
return true
209211
end
210212

213+
local original_win = vim.api.nvim_get_current_win()
214+
211215
-- Create a new window for the existing buffer
212216
local width = math.floor(vim.o.columns * effective_config.split_width_percentage)
213217
local full_height = vim.o.lines
@@ -227,8 +231,14 @@ local function show_hidden_terminal(effective_config)
227231
vim.api.nvim_win_set_buf(new_winid, bufnr)
228232
winid = new_winid
229233

230-
vim.api.nvim_set_current_win(winid)
231-
vim.cmd("startinsert")
234+
if focus then
235+
-- Focus the terminal: switch to terminal window and enter insert mode
236+
vim.api.nvim_set_current_win(winid)
237+
vim.cmd("startinsert")
238+
else
239+
-- Preserve user context: return to the window they were in before showing terminal
240+
vim.api.nvim_set_current_win(original_win)
241+
end
232242

233243
logger.debug("terminal", "Showed hidden terminal in new window")
234244
return true
@@ -269,8 +279,15 @@ function M.open(cmd_string, env_table, effective_config, focus)
269279
focus = utils.normalize_focus(focus)
270280

271281
if is_valid() then
272-
if focus then
273-
focus_terminal()
282+
-- Check if terminal exists but is hidden (no window)
283+
if not winid or not vim.api.nvim_win_is_valid(winid) then
284+
-- Terminal is hidden, show it by calling show_hidden_terminal
285+
show_hidden_terminal(effective_config, focus)
286+
else
287+
-- Terminal is already visible
288+
if focus then
289+
focus_terminal()
290+
end
274291
end
275292
else
276293
-- Check if there's an existing Claude terminal we lost track of
@@ -313,7 +330,7 @@ function M.simple_toggle(cmd_string, env_table, effective_config)
313330
-- Terminal is not visible
314331
if has_buffer then
315332
-- Terminal process exists but is hidden, show it
316-
if show_hidden_terminal(effective_config) then
333+
if show_hidden_terminal(effective_config, true) then
317334
logger.debug("terminal", "Showing hidden terminal")
318335
else
319336
logger.error("terminal", "Failed to show hidden terminal")
@@ -360,7 +377,7 @@ function M.focus_toggle(cmd_string, env_table, effective_config)
360377
end
361378
else
362379
-- Terminal process exists but is hidden, show it
363-
if show_hidden_terminal(effective_config) then
380+
if show_hidden_terminal(effective_config, true) then
364381
logger.debug("terminal", "Showing hidden terminal")
365382
else
366383
logger.error("terminal", "Failed to show hidden terminal")

lua/claudecode/terminal/snacks.lua

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,33 @@ function M.open(cmd_string, env_table, config, focus)
8080
focus = utils.normalize_focus(focus)
8181

8282
if terminal and terminal:buf_valid() then
83-
if focus then
84-
terminal:focus()
85-
local term_buf_id = terminal.buf
86-
if term_buf_id and vim.api.nvim_buf_get_option(term_buf_id, "buftype") == "terminal" then
87-
-- Check if window is valid before calling nvim_win_call
88-
if terminal.win and vim.api.nvim_win_is_valid(terminal.win) then
89-
vim.api.nvim_win_call(terminal.win, function()
90-
vim.cmd("startinsert")
91-
end)
83+
-- Check if terminal exists but is hidden (no window)
84+
if not terminal.win or not vim.api.nvim_win_is_valid(terminal.win) then
85+
-- Terminal is hidden, show it using snacks toggle
86+
terminal:toggle()
87+
if focus then
88+
terminal:focus()
89+
local term_buf_id = terminal.buf
90+
if term_buf_id and vim.api.nvim_buf_get_option(term_buf_id, "buftype") == "terminal" then
91+
if terminal.win and vim.api.nvim_win_is_valid(terminal.win) then
92+
vim.api.nvim_win_call(terminal.win, function()
93+
vim.cmd("startinsert")
94+
end)
95+
end
96+
end
97+
end
98+
else
99+
-- Terminal is already visible
100+
if focus then
101+
terminal:focus()
102+
local term_buf_id = terminal.buf
103+
if term_buf_id and vim.api.nvim_buf_get_option(term_buf_id, "buftype") == "terminal" then
104+
-- Check if window is valid before calling nvim_win_call
105+
if terminal.win and vim.api.nvim_win_is_valid(terminal.win) then
106+
vim.api.nvim_win_call(terminal.win, function()
107+
vim.cmd("startinsert")
108+
end)
109+
end
92110
end
93111
end
94112
end

0 commit comments

Comments
 (0)