@@ -129,31 +129,17 @@ end
129
129
-- Cleans up state if invalid.
130
130
-- @local
131
131
-- @return boolean True if valid, false otherwise.
132
- local function is_fallback_terminal_valid ()
133
- -- First check if we have a valid buffer
134
- if not managed_fallback_terminal_bufnr or not vim .api .nvim_buf_is_valid (managed_fallback_terminal_bufnr ) then
135
- cleanup_fallback_terminal_state ()
132
+ local function is_fallback_terminal_window_valid ()
133
+ if not managed_fallback_terminal_winid or not vim .api .nvim_win_is_valid (managed_fallback_terminal_winid ) then
136
134
return false
137
135
end
136
+ return true
137
+ end
138
138
139
- -- If buffer is valid but window is invalid, try to find a window displaying this buffer
140
- if not managed_fallback_terminal_winid or not vim .api .nvim_win_is_valid (managed_fallback_terminal_winid ) then
141
- -- Search all windows for our terminal buffer
142
- local windows = vim .api .nvim_list_wins ()
143
- for _ , win in ipairs (windows ) do
144
- if vim .api .nvim_win_get_buf (win ) == managed_fallback_terminal_bufnr then
145
- -- Found a window displaying our terminal buffer, update the tracked window ID
146
- managed_fallback_terminal_winid = win
147
- require (" claudecode.logger" ).debug (" terminal" , " Recovered terminal window ID:" , win )
148
- return true
149
- end
150
- end
151
- -- Buffer exists but no window displays it
152
- cleanup_fallback_terminal_state ()
139
+ local function is_fallback_terminal_buffer_valid ()
140
+ if not managed_fallback_terminal_bufnr or not vim .api .nvim_buf_is_valid (managed_fallback_terminal_bufnr ) then
153
141
return false
154
142
end
155
-
156
- -- Both buffer and window are valid
157
143
return true
158
144
end
159
145
@@ -481,7 +467,7 @@ function M.toggle(opts_override)
481
467
end
482
468
end
483
469
elseif provider == " native" then
484
- if is_fallback_terminal_valid () then
470
+ if is_fallback_terminal_window_valid () then
485
471
local claude_term_neovim_win_id = managed_fallback_terminal_winid
486
472
local current_neovim_win_id = vim .api .nvim_get_current_win ()
487
473
@@ -491,14 +477,11 @@ function M.toggle(opts_override)
491
477
vim .api .nvim_set_current_win (claude_term_neovim_win_id )
492
478
vim .cmd (" startinsert" )
493
479
end
480
+ elseif is_fallback_terminal_buffer_valid () then
481
+ open_window_for_buffer (managed_fallback_terminal_bufnr , effective_config )
494
482
else
495
- local existing_buf = find_existing_terminal_buffer_by_name ()
496
- if existing_buf then
497
- open_window_for_buffer (existing_buf , effective_config )
498
- else
499
- if not open_fallback_terminal (cmd_string , claude_env_table , effective_config ) then
500
- vim .notify (" Failed to open Claude terminal using native fallback (toggle)." , vim .log .levels .ERROR )
501
- end
483
+ if not open_fallback_terminal (cmd_string , claude_env_table , effective_config ) then
484
+ vim .notify (" Failed to open Claude terminal using native fallback (toggle)." , vim .log .levels .ERROR )
502
485
end
503
486
end
504
487
end
@@ -529,52 +512,6 @@ function M.get_active_terminal_bufnr()
529
512
return nil
530
513
end
531
514
532
- --- Opens a window for an existing buffer.
533
- -- @local
534
- -- @param bufnr number The buffer number to open.
535
- -- @param effective_term_config table Configuration for split_side and split_width_percentage.
536
- local function open_window_for_buffer (bufnr , effective_term_config )
537
- local original_win = vim .api .nvim_get_current_win ()
538
-
539
- local width = math.floor (vim .o .columns * effective_term_config .split_width_percentage )
540
- local full_height = vim .o .lines
541
- local placement_modifier
542
-
543
- if effective_term_config .split_side == " left" then
544
- placement_modifier = " topleft "
545
- else
546
- placement_modifier = " botright "
547
- end
548
-
549
- vim .cmd (placement_modifier .. width .. " vsplit" )
550
-
551
- local new_winid = vim .api .nvim_get_current_win ()
552
-
553
- vim .api .nvim_win_set_height (new_winid , full_height )
554
-
555
- vim .api .nvim_win_set_buf (new_winid , bufnr )
556
-
557
- managed_fallback_terminal_winid = new_winid
558
- managed_fallback_terminal_bufnr = bufnr
559
-
560
- vim .api .nvim_set_current_win (managed_fallback_terminal_winid )
561
- vim .cmd (" startinsert" )
562
- end
563
515
564
516
return M
565
517
566
- --- Finds the existing Claude terminal buffer, even if it's not in a window.
567
- -- @local
568
- -- @return number|nil The buffer number if found, otherwise nil.
569
- local function find_existing_terminal_buffer_by_name ()
570
- local buffers = vim .api .nvim_list_bufs ()
571
- for _ , buf in ipairs (buffers ) do
572
- if vim .api .nvim_buf_is_valid (buf ) and vim .bo [buf ].buftype == ' terminal' then
573
- local buf_name = vim .api .nvim_buf_get_name (buf )
574
- if buf_name :match (" claude" ) then
575
- return buf
576
- end
577
- end
578
- end
579
- return nil
580
- end
0 commit comments