Skip to content

Commit 61e9f06

Browse files
Address copilot feedback: fix require restoration and extract mode detection helper
- Fix require restoration in test file to use original_require variable - Extract mode detection logic to shared helper function to reduce duplication - All quality gates pass: nix fmt, make check, make test Co-authored-by: ThomasK33 <[email protected]>
1 parent f793344 commit 61e9f06

File tree

2 files changed

+23
-28
lines changed

2 files changed

+23
-28
lines changed

lua/claudecode/visual_commands.lua

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@
44
-- @module claudecode.visual_commands
55
local M = {}
66

7+
--- Get current vim mode with fallback for test environments
8+
--- @param full_mode boolean|nil Whether to get full mode info (passed to vim.fn.mode)
9+
--- @return string current_mode The current vim mode
10+
local function get_current_mode(full_mode)
11+
local current_mode = "n" -- Default fallback
12+
13+
pcall(function()
14+
if vim.api and vim.api.nvim_get_mode then
15+
current_mode = vim.api.nvim_get_mode().mode
16+
else
17+
current_mode = vim.fn.mode(full_mode)
18+
end
19+
end)
20+
21+
return current_mode
22+
end
23+
724
-- ESC key constant matching neo-tree's implementation
825
local ESC_KEY
926
local success = pcall(function()
@@ -40,20 +57,7 @@ end
4057
--- @return boolean true if in visual mode, false otherwise
4158
--- @return string|nil error message if not in visual mode
4259
function M.validate_visual_mode()
43-
local current_mode = "n" -- Default fallback
44-
45-
-- Use pcall to handle test environments
46-
local mode_success = pcall(function()
47-
if vim.api and vim.api.nvim_get_mode then
48-
current_mode = vim.api.nvim_get_mode().mode
49-
else
50-
current_mode = vim.fn.mode(true)
51-
end
52-
end)
53-
54-
if not mode_success then
55-
return false, "Cannot determine current mode (test environment)"
56-
end
60+
local current_mode = get_current_mode(true)
5761

5862
local is_visual = current_mode == "v" or current_mode == "V" or current_mode == "\022"
5963

@@ -82,12 +86,7 @@ function M.get_visual_range()
8286
-- Use pcall to handle test environments
8387
local range_success = pcall(function()
8488
-- Check if we're currently in visual mode
85-
local current_mode
86-
if vim.api and vim.api.nvim_get_mode then
87-
current_mode = vim.api.nvim_get_mode().mode
88-
else
89-
current_mode = vim.fn.mode(true)
90-
end
89+
local current_mode = get_current_mode(true)
9190
local is_visual = current_mode == "v" or current_mode == "V" or current_mode == "\022"
9291

9392
if is_visual then
@@ -186,12 +185,7 @@ end
186185
--- @return function The wrapped command function
187186
function M.create_visual_command_wrapper(normal_handler, visual_handler)
188187
return function(...)
189-
local current_mode
190-
if vim.api and vim.api.nvim_get_mode then
191-
current_mode = vim.api.nvim_get_mode().mode
192-
else
193-
current_mode = vim.fn.mode(true)
194-
end
188+
local current_mode = get_current_mode(true)
195189

196190
if current_mode == "v" or current_mode == "V" or current_mode == "\022" then
197191
-- Use the neo-tree pattern: exit visual mode, then schedule execution

tests/unit/claudecode_send_command_spec.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ describe("ClaudeCodeSend Command Range Functionality", function()
77
local mock_server
88
local mock_terminal
99
local command_callback
10+
local original_require
1011

1112
before_each(function()
1213
-- Reset package cache
@@ -115,7 +116,7 @@ describe("ClaudeCodeSend Command Range Functionality", function()
115116
}
116117

117118
-- Setup require mocks BEFORE requiring claudecode
118-
local original_require = _G.require
119+
original_require = _G.require
119120
_G.require = function(module_name)
120121
if module_name == "claudecode.selection" then
121122
return mock_selection_module
@@ -147,7 +148,7 @@ describe("ClaudeCodeSend Command Range Functionality", function()
147148

148149
after_each(function()
149150
-- Restore original require
150-
_G.require = require
151+
_G.require = original_require
151152
end)
152153

153154
describe("ClaudeCodeSend command", function()

0 commit comments

Comments
 (0)