|
4 | 4 | -- @module claudecode.visual_commands
|
5 | 5 | local M = {}
|
6 | 6 |
|
| 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 | + |
7 | 24 | -- ESC key constant matching neo-tree's implementation
|
8 | 25 | local ESC_KEY
|
9 | 26 | local success = pcall(function()
|
|
40 | 57 | --- @return boolean true if in visual mode, false otherwise
|
41 | 58 | --- @return string|nil error message if not in visual mode
|
42 | 59 | 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) |
57 | 61 |
|
58 | 62 | local is_visual = current_mode == "v" or current_mode == "V" or current_mode == "\022"
|
59 | 63 |
|
@@ -82,12 +86,7 @@ function M.get_visual_range()
|
82 | 86 | -- Use pcall to handle test environments
|
83 | 87 | local range_success = pcall(function()
|
84 | 88 | -- 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) |
91 | 90 | local is_visual = current_mode == "v" or current_mode == "V" or current_mode == "\022"
|
92 | 91 |
|
93 | 92 | if is_visual then
|
|
186 | 185 | --- @return function The wrapped command function
|
187 | 186 | function M.create_visual_command_wrapper(normal_handler, visual_handler)
|
188 | 187 | 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) |
195 | 189 |
|
196 | 190 | if current_mode == "v" or current_mode == "V" or current_mode == "\022" then
|
197 | 191 | -- Use the neo-tree pattern: exit visual mode, then schedule execution
|
|
0 commit comments