Skip to content

Commit 6cb31c5

Browse files
fix(commands_menu): πŸ› Preserve buffer context in command picker
πŸ” Capture original buffer before creating the picker menu πŸ› οΈ Wrap command implementations to restore proper buffer context πŸ”„ Ensure subcommands maintain correct buffer references This fixes an issue where commands executed from the picker would operate on the picker's buffer rather than the original editing buffer where the menu was invoked. The implementation now properly preserves the user's context when selecting and executing commands from the menu.
1 parent 43aeb8c commit 6cb31c5

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

Diff for: β€Žlua/nvim_aider/commands_menu.lua

+43-4
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,41 @@ end
7878
function M._menu()
7979
local items = {}
8080
local longest_cmd = 0
81+
-- Capture the original buffer before creating the picker
82+
-- This is critical because the picker will change the active buffer context
83+
local original_buf = vim.api.nvim_get_current_buf()
8184

82-
-- Build picker items and calculate longest command name
85+
-- Create wrapped command table to preserve buffer context
86+
-- This wrapping is necessary because:
87+
-- 1. The picker interface creates its own buffer
88+
-- 2. Commands executed from the picker would otherwise use the picker's buffer
89+
-- 3. File operations need to reference the user's original editing buffer
90+
local wrapped_commands = {}
8391
for name, cmd in pairs(commands) do
92+
local new_cmd = vim.deepcopy(cmd)
93+
-- Wrap the command implementation to restore original buffer context
94+
new_cmd.impl = function()
95+
-- Switch back to the original buffer before execution
96+
vim.api.nvim_set_current_buf(original_buf)
97+
-- Execute the original command implementation
98+
cmd.impl()
99+
end
100+
101+
-- Wrap subcommands using the same buffer preservation logic
102+
if new_cmd.subcommands then
103+
for subname, subcmd in pairs(new_cmd.subcommands) do
104+
subcmd.impl = function()
105+
vim.api.nvim_set_current_buf(original_buf)
106+
cmd.subcommands[subname].impl()
107+
end
108+
end
109+
end
110+
111+
wrapped_commands[name] = new_cmd
112+
end
113+
114+
-- Build picker items and calculate longest command name
115+
for name, cmd in pairs(wrapped_commands) do
84116
-- For commands with subcommands, show them as parent items
85117
table.insert(items, {
86118
text = name,
@@ -99,7 +131,8 @@ function M._menu()
99131
text = full_name,
100132
description = subcmd.doc,
101133
category = "command",
102-
name = full_name,
134+
name = name,
135+
subname = subname,
103136
parent = name,
104137
})
105138
longest_cmd = math.max(longest_cmd, #full_name)
@@ -125,8 +158,14 @@ function M._menu()
125158
end,
126159
prompt = "Aider Commands > ",
127160
confirm = function(picker_instance, item)
128-
if item and commands[item.text] then
129-
commands[item.text].impl()
161+
if item then
162+
if item.subname then
163+
-- Execute subcommand
164+
wrapped_commands[item.name].subcommands[item.subname].impl()
165+
elseif wrapped_commands[item.name] then
166+
-- Execute main command
167+
wrapped_commands[item.name].impl()
168+
end
130169
end
131170
picker_instance:close()
132171
end,

Diff for: β€Žtests/api_methods_spec.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ describe("API Methods", function()
9797
-- Create a new modifiable buffer for each test
9898
local buf = vim.api.nvim_create_buf(true, true)
9999
vim.api.nvim_set_current_buf(buf)
100-
vim.api.nvim_buf_set_option(buf, "modifiable", true)
100+
vim.bo[buf].modifiable = true
101101

102102
utils_mock.get_absolute_path.returns("/project/file.lua")
103103
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "line1", "line2" })

0 commit comments

Comments
Β (0)