Skip to content

Commit b3802be

Browse files
committed
Extract rhs to restore buffer keymaps
* Avoid using mapset due to its requirement that the intended buffer is the current buffer.
1 parent 3f72c46 commit b3802be

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

lua/diffview/vcs/file.lua

+15-8
Original file line numberDiff line numberDiff line change
@@ -373,12 +373,25 @@ function File:attach_buffer(force, opt)
373373
local result = string.find(buf_km_dict["lhs"], lhs_pat)
374374

375375
if result ~= nil and result ~= "" then
376+
local rhs = ""
377+
if buf_km_dict.rhs ~= nil then
378+
rhs = buf_km_dict.rhs
379+
else
380+
rhs = buf_km_dict.callback
381+
end
376382
-- save buffer keymap
377383
if buf_km_dict.buffer == self.bufnr then
378384
local obj = {
379385
bufnr = self.bufnr,
380386
mode = mode,
381-
km_dict = buf_km_dict,
387+
lhs = name_lhs,
388+
rhs = rhs,
389+
opts = {
390+
buffer = self.bufnr,
391+
desc = buf_km_dict["desc"],
392+
silent = buf_km_dict["silent"],
393+
noremap = buf_km_dict["noremap"],
394+
},
382395
}
383396
table.insert(R, obj)
384397
end
@@ -425,19 +438,13 @@ function File:detach_buffer()
425438
end
426439
end
427440

428-
local orig_bufnr = vim.fn.bufnr()
429441
-- restore buffer keymaps
430442
for _, dict in pairs(R) do
431443
if dict.bufnr == self.bufnr then
432-
-- switch to required buffer to restore keymap to
433-
pcall(api.nvim_set_current_buf, self.bufnr)
434-
pcall(vim.fn.mapset, dict.mode, 0, dict.km_dict)
444+
vim.keymap.set(dict.mode, dict.lhs, dict.rhs, dict.opts)
435445
end
436446
end
437447

438-
-- switch back to original buffer
439-
pcall(api.nvim_set_current_buf, orig_bufnr)
440-
441448
-- Diagnostics
442449
if state.disable_diagnostics then
443450
if HAS_NVIM_0_10 then

0 commit comments

Comments
 (0)