Skip to content

Commit 19addd2

Browse files
committed
fix: can configure builtin replace_in_file tool keymap (fixes ravitemer#159)
1 parent 9ca711e commit 19addd2

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

lua/mcphub/config.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ local defaults = {
1111
mcp_request_timeout = 60000, --Timeout for MCP requests in milliseconds, useful for long running tasks
1212
---@type table<string, NativeServerDef>
1313
native_servers = {},
14+
builtin_tools = {
15+
replace_in_file = {
16+
keymaps = {
17+
accept = "ga",
18+
reject = "gr",
19+
},
20+
},
21+
},
1422
auto_approve = false,
1523
auto_toggle_mcp_servers = true, -- Let LLMs start and stop MCP servers automatically
1624
use_bundled_binary = false, -- Whether to use bundled mcp-hub binary

lua/mcphub/native/neovim/files/replace.lua

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local Path = require("plenary.path")
2+
local State = require("mcphub.state")
23

34
-- Helper function to count diff blocks
45
local function count_diff_blocks(diff_content)
@@ -113,6 +114,37 @@ local function apply_diff_blocks(original_content, diff_content)
113114

114115
return result
115116
end
117+
-- Helper function to safely get keymap info
118+
local function get_keymap_info(mode, lhs, buffer)
119+
local maps = vim.api.nvim_buf_get_keymap(buffer, mode)
120+
for _, map in ipairs(maps) do
121+
if map.lhs == lhs then
122+
return map
123+
end
124+
end
125+
return nil
126+
end
127+
128+
-- Helper function to restore keymap
129+
local function restore_keymap(mode, lhs, buffer, original_map)
130+
if original_map then
131+
-- If there was an original mapping, restore it
132+
local opts = {
133+
buffer = buffer,
134+
desc = original_map.desc,
135+
nowait = original_map.nowait == 1,
136+
silent = original_map.silent == 1,
137+
expr = original_map.expr == 1,
138+
}
139+
140+
if original_map.callback then
141+
vim.keymap.set(mode, lhs, original_map.callback, opts)
142+
elseif original_map.rhs then
143+
vim.keymap.set(mode, lhs, original_map.rhs, opts)
144+
end
145+
end
146+
end
147+
116148
-- Core replace file logic
117149
local function handle_replace_file(req, res)
118150
if not req.params.path or not req.params.diff or req.params.diff == vim.NIL then
@@ -216,14 +248,29 @@ local function handle_replace_file(req, res)
216248
local response_sent = false
217249
local changes_diff = nil
218250

251+
local keymaps = State.config.builtin_tools.replace_in_file.keymaps
252+
or {
253+
accept = "ga",
254+
reject = "gr",
255+
}
256+
257+
-- Store original keymaps before setting temporary ones
258+
local original_accept_map = get_keymap_info("n", keymaps.accept, bufnr)
259+
local original_reject_map = get_keymap_info("n", keymaps.reject, bufnr)
260+
219261
local function cleanup_diff()
220262
vim.cmd("diffoff!")
221263
if vim.api.nvim_buf_is_valid(diff_bufnr) then
222264
vim.api.nvim_buf_delete(diff_bufnr, { force = true })
223265
end
224266
if vim.api.nvim_buf_is_valid(bufnr) then
225-
vim.keymap.del("n", "ga", { buffer = bufnr })
226-
vim.keymap.del("n", "gr", { buffer = bufnr })
267+
-- Safely delete our temporary keymaps and restore originals
268+
pcall(vim.keymap.del, "n", keymaps.accept, { buffer = bufnr })
269+
pcall(vim.keymap.del, "n", keymaps.reject, { buffer = bufnr })
270+
271+
-- Restore original keymaps if they existed
272+
restore_keymap("n", keymaps.accept, bufnr, original_accept_map)
273+
restore_keymap("n", keymaps.reject, bufnr, original_reject_map)
227274
end
228275
if not augroup_cleared then
229276
pcall(vim.api.nvim_del_augroup_by_id, augroup)
@@ -278,13 +325,13 @@ local function handle_replace_file(req, res)
278325
vim.cmd("write")
279326
end
280327

281-
-- Set up key mappings
282-
vim.keymap.set("n", "ga", function()
328+
-- Set up key mappings using configured keys
329+
vim.keymap.set("n", keymaps.accept, function()
283330
vim.cmd("write")
284331
handle_post_write()
285332
end, { buffer = bufnr, desc = "Accept changes" })
286333

287-
vim.keymap.set("n", "gr", function()
334+
vim.keymap.set("n", keymaps.reject, function()
288335
handle_reject()
289336
end, { buffer = bufnr, desc = "Reject changes" })
290337

0 commit comments

Comments
 (0)