Skip to content

Commit be65486

Browse files
committed
feat: restore buffer local keymaps
1 parent 4516612 commit be65486

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

lua/diffview/vcs/file.lua

+58
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ local pl = lazy.access(utils, "path") ---@type PathLib
1515
local api = vim.api
1616
local M = {}
1717

18+
-- keymaps to restore
19+
local R = {}
20+
1821
local HAS_NVIM_0_10 = vim.fn.has("nvim-0.10") == 1
1922

2023
---@alias git.FileDataProducer fun(kind: vcs.FileKind, path: string, pos: "left"|"right"): string[]
@@ -350,8 +353,56 @@ function File:attach_buffer(force, opt)
350353
state.keymaps = config.extend_keymaps(conf.keymaps.view, state.keymaps)
351354
local default_map_opt = { silent = true, nowait = true, buffer = self.bufnr }
352355

356+
R = {}
357+
353358
for _, mapping in ipairs(state.keymaps) do
354359
local map_opt = vim.tbl_extend("force", default_map_opt, mapping[4] or {}, { buffer = self.bufnr })
360+
361+
local mode = mapping[1] -- string
362+
local name_lhs = mapping[2] -- string
363+
364+
local lhs_pat = string.format("%s", name_lhs)
365+
-- escape special characters for search pattern
366+
lhs_pat = string.gsub(lhs_pat, "%[", "%%%[")
367+
lhs_pat = string.gsub(lhs_pat, "%]", "%%%]")
368+
369+
local buf_mappings = api.nvim_buf_get_keymap(self.bufnr, mode)
370+
371+
for _, buf_km_dict in pairs(buf_mappings) do
372+
if buf_km_dict["lhs"] ~= nil then
373+
local result = string.find(buf_km_dict["lhs"], lhs_pat)
374+
375+
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
382+
-- save buffer keymap
383+
if buf_km_dict.buffer == self.bufnr then
384+
local obj = {
385+
bufnr = self.bufnr,
386+
mode = mode,
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+
},
395+
}
396+
table.insert(R, obj)
397+
end
398+
-- found buffer keymap, so stop searching
399+
do
400+
break
401+
end
402+
end
403+
end
404+
end
405+
355406
vim.keymap.set(mapping[1], mapping[2], mapping[3], map_opt)
356407
end
357408

@@ -387,6 +438,13 @@ function File:detach_buffer()
387438
end
388439
end
389440

441+
-- restore buffer keymaps
442+
for _, dict in pairs(R) do
443+
if dict.bufnr == self.bufnr then
444+
vim.keymap.set(dict.mode, dict.lhs, dict.rhs, dict.opts)
445+
end
446+
end
447+
390448
-- Diagnostics
391449
if state.disable_diagnostics then
392450
if HAS_NVIM_0_10 then

0 commit comments

Comments
 (0)