From 950eed6de21fa197c4f90c3bc0c768689c81c737 Mon Sep 17 00:00:00 2001 From: "blink-so[bot]" <211532188+blink-so[bot]@users.noreply.github.com> Date: Sun, 8 Jun 2025 18:27:02 +0000 Subject: [PATCH 1/5] refactor: improve terminal toggle logic From 4378c70c89e0660bd4923915d305261ce47333d1 Mon Sep 17 00:00:00 2001 From: "blink-so[bot]" <211532188+blink-so[bot]@users.noreply.github.com> Date: Sun, 8 Jun 2025 18:30:22 +0000 Subject: [PATCH 2/5] fix: improve terminal toggle logic --- lua/claudecode/terminal.lua | 85 +++++-------------------------------- 1 file changed, 11 insertions(+), 74 deletions(-) diff --git a/lua/claudecode/terminal.lua b/lua/claudecode/terminal.lua index 635ad23..04cb00e 100644 --- a/lua/claudecode/terminal.lua +++ b/lua/claudecode/terminal.lua @@ -129,31 +129,17 @@ end -- Cleans up state if invalid. -- @local -- @return boolean True if valid, false otherwise. -local function is_fallback_terminal_valid() - -- First check if we have a valid buffer - if not managed_fallback_terminal_bufnr or not vim.api.nvim_buf_is_valid(managed_fallback_terminal_bufnr) then - cleanup_fallback_terminal_state() +local function is_fallback_terminal_window_valid() + if not managed_fallback_terminal_winid or not vim.api.nvim_win_is_valid(managed_fallback_terminal_winid) then return false end + return true +end - -- If buffer is valid but window is invalid, try to find a window displaying this buffer - if not managed_fallback_terminal_winid or not vim.api.nvim_win_is_valid(managed_fallback_terminal_winid) then - -- Search all windows for our terminal buffer - local windows = vim.api.nvim_list_wins() - for _, win in ipairs(windows) do - if vim.api.nvim_win_get_buf(win) == managed_fallback_terminal_bufnr then - -- Found a window displaying our terminal buffer, update the tracked window ID - managed_fallback_terminal_winid = win - require("claudecode.logger").debug("terminal", "Recovered terminal window ID:", win) - return true - end - end - -- Buffer exists but no window displays it - cleanup_fallback_terminal_state() +local function is_fallback_terminal_buffer_valid() + if not managed_fallback_terminal_bufnr or not vim.api.nvim_buf_is_valid(managed_fallback_terminal_bufnr) then return false end - - -- Both buffer and window are valid return true end @@ -481,7 +467,7 @@ function M.toggle(opts_override) end end elseif provider == "native" then - if is_fallback_terminal_valid() then + if is_fallback_terminal_window_valid() then local claude_term_neovim_win_id = managed_fallback_terminal_winid local current_neovim_win_id = vim.api.nvim_get_current_win() @@ -491,14 +477,11 @@ function M.toggle(opts_override) vim.api.nvim_set_current_win(claude_term_neovim_win_id) vim.cmd("startinsert") end + elseif is_fallback_terminal_buffer_valid() then + open_window_for_buffer(managed_fallback_terminal_bufnr, effective_config) else - local existing_buf = find_existing_terminal_buffer_by_name() - if existing_buf then - open_window_for_buffer(existing_buf, effective_config) - else - if not open_fallback_terminal(cmd_string, claude_env_table, effective_config) then - vim.notify("Failed to open Claude terminal using native fallback (toggle).", vim.log.levels.ERROR) - end + if not open_fallback_terminal(cmd_string, claude_env_table, effective_config) then + vim.notify("Failed to open Claude terminal using native fallback (toggle).", vim.log.levels.ERROR) end end end @@ -529,52 +512,6 @@ function M.get_active_terminal_bufnr() return nil end ---- Opens a window for an existing buffer. --- @local --- @param bufnr number The buffer number to open. --- @param effective_term_config table Configuration for split_side and split_width_percentage. -local function open_window_for_buffer(bufnr, effective_term_config) - local original_win = vim.api.nvim_get_current_win() - - local width = math.floor(vim.o.columns * effective_term_config.split_width_percentage) - local full_height = vim.o.lines - local placement_modifier - - if effective_term_config.split_side == "left" then - placement_modifier = "topleft " - else - placement_modifier = "botright " - end - - vim.cmd(placement_modifier .. width .. "vsplit") - - local new_winid = vim.api.nvim_get_current_win() - - vim.api.nvim_win_set_height(new_winid, full_height) - - vim.api.nvim_win_set_buf(new_winid, bufnr) - - managed_fallback_terminal_winid = new_winid - managed_fallback_terminal_bufnr = bufnr - - vim.api.nvim_set_current_win(managed_fallback_terminal_winid) - vim.cmd("startinsert") -end return M ---- Finds the existing Claude terminal buffer, even if it's not in a window. --- @local --- @return number|nil The buffer number if found, otherwise nil. -local function find_existing_terminal_buffer_by_name() - local buffers = vim.api.nvim_list_bufs() - for _, buf in ipairs(buffers) do - if vim.api.nvim_buf_is_valid(buf) and vim.bo[buf].buftype == 'terminal' then - local buf_name = vim.api.nvim_buf_get_name(buf) - if buf_name:match("claude") then - return buf - end - end - end - return nil -end From a39327d7b67ab27d0aef1019f40271d6d5bf85e3 Mon Sep 17 00:00:00 2001 From: "blink-so[bot]" <211532188+blink-so[bot]@users.noreply.github.com> Date: Sun, 8 Jun 2025 18:32:41 +0000 Subject: [PATCH 3/5] fix: address luacheck warnings --- lua/claudecode/terminal.lua | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lua/claudecode/terminal.lua b/lua/claudecode/terminal.lua index 04cb00e..70dc8e9 100644 --- a/lua/claudecode/terminal.lua +++ b/lua/claudecode/terminal.lua @@ -150,7 +150,7 @@ end -- @param effective_term_config table Configuration for split_side and split_width_percentage. -- @return boolean True if successful, false otherwise. local function open_fallback_terminal(cmd_string, env_table, effective_term_config) - if is_fallback_terminal_valid() then -- Should not happen if called correctly, but as a safeguard + if is_fallback_terminal_window_valid() then -- Should not happen if called correctly, but as a safeguard vim.api.nvim_set_current_win(managed_fallback_terminal_winid) vim.cmd("startinsert") return true @@ -240,7 +240,7 @@ end --- Closes the managed fallback terminal if it's open and valid. -- @local local function close_fallback_terminal() - if is_fallback_terminal_valid() then + if is_fallback_terminal_window_valid() then -- Closing the window should trigger on_exit of the job if the process is still running, -- which then calls cleanup_fallback_terminal_state. -- If the job already exited, on_exit would have cleaned up. @@ -253,7 +253,7 @@ end --- Focuses the managed fallback terminal if it's open and valid. -- @local local function focus_fallback_terminal() - if is_fallback_terminal_valid() then + if is_fallback_terminal_window_valid() then vim.api.nvim_set_current_win(managed_fallback_terminal_winid) vim.cmd("startinsert") end @@ -410,7 +410,9 @@ function M.close() -- managed_snacks_terminal will be set to nil by the on_close callback end elseif provider == "native" then - close_fallback_terminal() + if is_fallback_terminal_window_valid() then + close_fallback_terminal() + end end end @@ -505,7 +507,7 @@ function M.get_active_terminal_bufnr() end end - if is_fallback_terminal_valid() then + if is_fallback_terminal_buffer_valid() then return managed_fallback_terminal_bufnr end From 377b1ee1d8297e90da840d7ae8dd1f41d2c68f54 Mon Sep 17 00:00:00 2001 From: "blink-so[bot]" <211532188+blink-so[bot]@users.noreply.github.com> Date: Sun, 8 Jun 2025 18:34:46 +0000 Subject: [PATCH 4/5] fix: restore deleted helper functions --- lua/claudecode/terminal.lua | 48 +++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/lua/claudecode/terminal.lua b/lua/claudecode/terminal.lua index 70dc8e9..b454ff0 100644 --- a/lua/claudecode/terminal.lua +++ b/lua/claudecode/terminal.lua @@ -517,3 +517,51 @@ end return M +--- Finds the existing Claude terminal buffer, even if it's not in a window. +-- @local +-- @return number|nil The buffer number if found, otherwise nil. +local function find_existing_terminal_buffer_by_name() + local buffers = vim.api.nvim_list_bufs() + for _, buf in ipairs(buffers) do + if vim.api.nvim_buf_is_valid(buf) and vim.bo[buf].buftype == 'terminal' then + local buf_name = vim.api.nvim_buf_get_name(buf) + if buf_name:match("claude") then + return buf + end + end + end + return nil +end + +--- Opens a window for an existing buffer. +-- @local +-- @param bufnr number The buffer number to open. +-- @param effective_term_config table Configuration for split_side and split_width_percentage. +local function open_window_for_buffer(bufnr, effective_term_config) + local original_win = vim.api.nvim_get_current_win() + + local width = math.floor(vim.o.columns * effective_term_config.split_width_percentage) + local full_height = vim.o.lines + local placement_modifier + + if effective_term_config.split_side == "left" then + placement_modifier = "topleft " + else + placement_modifier = "botright " + end + + vim.cmd(placement_modifier .. width .. "vsplit") + + local new_winid = vim.api.nvim_get_current_win() + + vim.api.nvim_win_set_height(new_winid, full_height) + + vim.api.nvim_win_set_buf(new_winid, bufnr) + + managed_fallback_terminal_winid = new_winid + managed_fallback_terminal_bufnr = bufnr + + vim.api.nvim_set_current_win(managed_fallback_terminal_winid) + vim.cmd("startinsert") +end + From 2eaa1f64ffa72f3e2925f19717edf5374392614c Mon Sep 17 00:00:00 2001 From: "blink-so[bot]" <211532188+blink-so[bot]@users.noreply.github.com> Date: Sun, 8 Jun 2025 18:35:58 +0000 Subject: [PATCH 5/5] fix: syntax error in terminal.lua --- lua/claudecode/terminal.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/claudecode/terminal.lua b/lua/claudecode/terminal.lua index b454ff0..3e5cb31 100644 --- a/lua/claudecode/terminal.lua +++ b/lua/claudecode/terminal.lua @@ -515,7 +515,7 @@ function M.get_active_terminal_bufnr() end -return M + --- Finds the existing Claude terminal buffer, even if it's not in a window. -- @local