From 79f7a61a356b128664d6755cc87398fa04f748c0 Mon Sep 17 00:00:00 2001 From: paradisecy Date: Mon, 23 Feb 2026 14:17:41 +0200 Subject: [PATCH] fix: respect --resume/--continue args when terminal already exists When cmd_args (e.g. --resume, --continue) are passed to simple_toggle or focus_toggle, the existing terminal was reused and the args silently ignored. This made :ClaudeCode --resume only work on a fresh Neovim session. Add a force_new flag that propagates from terminal.lua to both the native and snacks providers. When force_new is true and a terminal already exists, close it first and open a fresh session with the supplied command args. Fixes the case where :ClaudeCode --resume or :ClaudeCode --continue has no effect because a previous Claude terminal is still alive in the session. Co-Authored-By: Claude Sonnet 4.6 --- lua/claudecode/terminal.lua | 6 ++++-- lua/claudecode/terminal/native.lua | 26 ++++++++++++++++++++++++-- lua/claudecode/terminal/snacks.lua | 22 ++++++++++++++++++++-- 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/lua/claudecode/terminal.lua b/lua/claudecode/terminal.lua index fae0b30f..28335ee4 100644 --- a/lua/claudecode/terminal.lua +++ b/lua/claudecode/terminal.lua @@ -514,8 +514,9 @@ end function M.simple_toggle(opts_override, cmd_args) local effective_config = build_config(opts_override) local cmd_string, claude_env_table = get_claude_command_and_env(cmd_args) + local force_new = cmd_args ~= nil and cmd_args ~= "" - get_provider().simple_toggle(cmd_string, claude_env_table, effective_config) + get_provider().simple_toggle(cmd_string, claude_env_table, effective_config, force_new) end ---Smart focus toggle: switches to terminal if not focused, hides if currently focused. @@ -524,8 +525,9 @@ end function M.focus_toggle(opts_override, cmd_args) local effective_config = build_config(opts_override) local cmd_string, claude_env_table = get_claude_command_and_env(cmd_args) + local force_new = cmd_args ~= nil and cmd_args ~= "" - get_provider().focus_toggle(cmd_string, claude_env_table, effective_config) + get_provider().focus_toggle(cmd_string, claude_env_table, effective_config, force_new) end ---Toggle open terminal without focus if not already visible, otherwise do nothing. diff --git a/lua/claudecode/terminal/native.lua b/lua/claudecode/terminal/native.lua index 7cd24dd5..460eaac6 100644 --- a/lua/claudecode/terminal/native.lua +++ b/lua/claudecode/terminal/native.lua @@ -322,11 +322,22 @@ end ---@param cmd_string string ---@param env_table table ---@param effective_config ClaudeCodeTerminalConfig -function M.simple_toggle(cmd_string, env_table, effective_config) +---@param force_new boolean? If true, close any existing terminal and open a new one with cmd_string +function M.simple_toggle(cmd_string, env_table, effective_config, force_new) -- Check if we have a valid terminal buffer (process running) local has_buffer = bufnr and vim.api.nvim_buf_is_valid(bufnr) local is_visible = has_buffer and is_terminal_visible() + -- If args like --resume or --continue were passed, force a new session + if force_new and has_buffer then + logger.debug("terminal", "simple_toggle: force_new=true, closing existing terminal to start new session") + M.close() + if not open_terminal(cmd_string, env_table, effective_config) then + vim.notify("Failed to open Claude terminal using native fallback (simple_toggle force_new).", vim.log.levels.ERROR) + end + return + end + if is_visible then -- Terminal is visible, hide it (but keep process running) hide_terminal() @@ -362,11 +373,22 @@ end ---@param cmd_string string ---@param env_table table ---@param effective_config ClaudeCodeTerminalConfig -function M.focus_toggle(cmd_string, env_table, effective_config) +---@param force_new boolean? If true, close any existing terminal and open a new one with cmd_string +function M.focus_toggle(cmd_string, env_table, effective_config, force_new) -- Check if we have a valid terminal buffer (process running) local has_buffer = bufnr and vim.api.nvim_buf_is_valid(bufnr) local is_visible = has_buffer and is_terminal_visible() + -- If args like --resume or --continue were passed, force a new session + if force_new and has_buffer then + logger.debug("terminal", "focus_toggle: force_new=true, closing existing terminal to start new session") + M.close() + if not open_terminal(cmd_string, env_table, effective_config) then + vim.notify("Failed to open Claude terminal using native fallback (focus_toggle force_new).", vim.log.levels.ERROR) + end + return + end + if has_buffer then -- Terminal process exists if is_visible then diff --git a/lua/claudecode/terminal/snacks.lua b/lua/claudecode/terminal/snacks.lua index 2b4c7c98..3587b850 100644 --- a/lua/claudecode/terminal/snacks.lua +++ b/lua/claudecode/terminal/snacks.lua @@ -173,7 +173,8 @@ end ---@param cmd_string string ---@param env_table table ---@param config table -function M.simple_toggle(cmd_string, env_table, config) +---@param force_new boolean? If true, close any existing terminal and open a new one with cmd_string +function M.simple_toggle(cmd_string, env_table, config, force_new) if not is_available() then vim.notify("Snacks.nvim terminal provider selected but Snacks.terminal not available.", vim.log.levels.ERROR) return @@ -181,6 +182,14 @@ function M.simple_toggle(cmd_string, env_table, config) local logger = require("claudecode.logger") + -- If args like --resume or --continue were passed, force a new session + if force_new and terminal and terminal:buf_valid() then + logger.debug("terminal", "Simple toggle: force_new=true, closing existing terminal to start new session") + M.close() + M.open(cmd_string, env_table, config) + return + end + -- Check if terminal exists and is visible if terminal and terminal:buf_valid() and terminal:win_valid() then -- Terminal is visible, hide it @@ -201,7 +210,8 @@ end ---@param cmd_string string ---@param env_table table ---@param config table -function M.focus_toggle(cmd_string, env_table, config) +---@param force_new boolean? If true, close any existing terminal and open a new one with cmd_string +function M.focus_toggle(cmd_string, env_table, config, force_new) if not is_available() then vim.notify("Snacks.nvim terminal provider selected but Snacks.terminal not available.", vim.log.levels.ERROR) return @@ -209,6 +219,14 @@ function M.focus_toggle(cmd_string, env_table, config) local logger = require("claudecode.logger") + -- If args like --resume or --continue were passed, force a new session + if force_new and terminal and terminal:buf_valid() then + logger.debug("terminal", "Focus toggle: force_new=true, closing existing terminal to start new session") + M.close() + M.open(cmd_string, env_table, config) + return + end + -- Terminal exists, is valid, but not visible if terminal and terminal:buf_valid() and not terminal:win_valid() then logger.debug("terminal", "Focus toggle: showing hidden terminal")