From 3155f16b93c1a63405b8fe660e3c985832cfe9b3 Mon Sep 17 00:00:00 2001
From: johnlin <linton.tw@gmail.com>
Date: Tue, 29 Jul 2025 01:07:27 +0800
Subject: [PATCH] feat: add show_terminal_on_at_mention configuration option
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- Add new config option to control terminal visibility for @ mentions
- Remove show_terminal parameter from send_at_mention function
- Update config validation to include new boolean option
- Simplify visual selection @ mention calls

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
---
 README.md                  |  1 +
 lua/claudecode/config.lua  |  3 +++
 lua/claudecode/init.lua    | 24 +++++++++++++++++++-----
 tests/unit/config_spec.lua | 27 +++++++++++++++++++++++++++
 4 files changed, 50 insertions(+), 5 deletions(-)

diff --git a/README.md b/README.md
index c7800d7..44b34c2 100644
--- a/README.md
+++ b/README.md
@@ -143,6 +143,7 @@ For deep technical details, see [ARCHITECTURE.md](./ARCHITECTURE.md).
 
     -- Selection Tracking
     track_selection = true,
+    show_terminal_on_at_mention = true, -- Whether to show terminal when sending @ mentions
     visual_demotion_delay_ms = 50,
 
     -- Terminal Configuration
diff --git a/lua/claudecode/config.lua b/lua/claudecode/config.lua
index 88bdee8..01b3e70 100644
--- a/lua/claudecode/config.lua
+++ b/lua/claudecode/config.lua
@@ -9,6 +9,7 @@ M.defaults = {
   env = {}, -- Custom environment variables for Claude terminal
   log_level = "info",
   track_selection = true,
+  show_terminal_on_at_mention = true, -- Whether to show terminal when sending @ mentions
   visual_demotion_delay_ms = 50, -- Milliseconds to wait before demoting a visual selection
   connection_wait_delay = 200, -- Milliseconds to wait after connection before sending queued @ mentions
   connection_timeout = 10000, -- Maximum time to wait for Claude Code to connect (milliseconds)
@@ -56,6 +57,8 @@ function M.validate(config)
 
   assert(type(config.track_selection) == "boolean", "track_selection must be a boolean")
 
+  assert(config.show_terminal_on_at_mention == nil or type(config.show_terminal_on_at_mention) == "boolean", "show_terminal_on_at_mention must be a boolean")
+
   assert(
     type(config.visual_demotion_delay_ms) == "number" and config.visual_demotion_delay_ms >= 0,
     "visual_demotion_delay_ms must be a non-negative number"
diff --git a/lua/claudecode/init.lua b/lua/claudecode/init.lua
index d69abc9..5c358eb 100644
--- a/lua/claudecode/init.lua
+++ b/lua/claudecode/init.lua
@@ -39,6 +39,7 @@ M.version = {
 --- @field env table<string,string> Custom environment variables for Claude terminal.
 --- @field log_level "trace"|"debug"|"info"|"warn"|"error" Log level.
 --- @field track_selection boolean Enable sending selection updates to Claude.
+--- @field show_terminal_on_at_mention boolean Whether to show terminal when sending @ mentions.
 --- @field visual_demotion_delay_ms number Milliseconds to wait before demoting a visual selection.
 --- @field connection_wait_delay number Milliseconds to wait after connection before sending queued @ mentions.
 --- @field connection_timeout number Maximum time to wait for Claude Code to connect (milliseconds).
@@ -316,6 +317,7 @@ end
 ---@return string|nil error Error message if failed
 function M.send_at_mention(file_path, start_line, end_line, context)
   context = context or "command"
+  local show_terminal = M.state.config.show_terminal_on_at_mention
 
   if not M.state.server then
     logger.error(context, "Claude Code integration is not running")
@@ -324,9 +326,9 @@ function M.send_at_mention(file_path, start_line, end_line, context)
 
   -- Check if Claude Code is connected
   if M.is_claude_connected() then
-    -- Claude is connected, send immediately and ensure terminal is visible
+    -- Claude is connected, send immediately and optionally ensure terminal is visible
     local success, error_msg = M._broadcast_at_mention(file_path, start_line, end_line)
-    if success then
+    if success and show_terminal then
       local terminal = require("claudecode.terminal")
       terminal.ensure_visible()
     end
@@ -335,9 +337,21 @@ function M.send_at_mention(file_path, start_line, end_line, context)
     -- Claude not connected, queue the mention and launch terminal
     queue_mention(file_path, start_line, end_line)
 
-    -- Launch terminal with Claude Code
-    local terminal = require("claudecode.terminal")
-    terminal.open()
+    -- Claude not connected, queue the mention and optionally launch terminal
+    local mention_data = {
+      file_path = file_path,
+      start_line = start_line,
+      end_line = end_line,
+      context = context,
+    }
+
+    queue_at_mention(mention_data)
+
+    if show_terminal then
+      -- Launch terminal with Claude Code
+      local terminal = require("claudecode.terminal")
+      terminal.open()
+    end
 
     logger.debug(context, "Queued @ mention and launched Claude Code: " .. file_path)
 
diff --git a/tests/unit/config_spec.lua b/tests/unit/config_spec.lua
index 82f801c..38e17f3 100644
--- a/tests/unit/config_spec.lua
+++ b/tests/unit/config_spec.lua
@@ -121,6 +121,32 @@ describe("Configuration", function()
     expect(success).to_be_false()
   end)
 
+  it("should reject invalid show_terminal_on_at_mention type", function()
+    local invalid_config = {
+      port_range = { min = 10000, max = 65535 },
+      auto_start = true,
+      log_level = "debug",
+      track_selection = false,
+      show_terminal_on_at_mention = "invalid", -- Should be boolean
+      visual_demotion_delay_ms = 50,
+      diff_opts = {
+        auto_close_on_accept = true,
+        show_diff_stats = true,
+        vertical_split = true,
+        open_in_current_tab = true,
+      },
+      models = {
+        { name = "Test Model", value = "test-model" },
+      },
+    }
+
+    local success, _ = pcall(function()
+      config.validate(invalid_config)
+    end)
+
+    expect(success).to_be_false()
+  end)
+
   it("should merge user config with defaults", function()
     local user_config = {
       auto_start = true,
@@ -133,6 +159,7 @@ describe("Configuration", function()
     expect(merged_config.log_level).to_be("debug")
     expect(merged_config.port_range.min).to_be(config.defaults.port_range.min)
     expect(merged_config.track_selection).to_be(config.defaults.track_selection)
+    expect(merged_config.show_terminal_on_at_mention).to_be(config.defaults.show_terminal_on_at_mention)
     expect(merged_config.models).to_be_table()
   end)
 
