Skip to content

Commit 090cc16

Browse files
committed
feat: restore buffer keymaps
1 parent 4516612 commit 090cc16

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

lua/diffview/vcs/file.lua

Lines changed: 55 additions & 0 deletions
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,53 @@ 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+
-- force focus buffer
370+
-- vim.api.nvim_set_current_buf(self.bufnr)
371+
--
372+
-- if not focussed on corresponding buffer
373+
-- this function does not return correct buffer keymaps
374+
local buf_mappings = vim.api.nvim_buf_get_keymap(self.bufnr, mode)
375+
376+
for _, buf_km_dict in pairs(buf_mappings) do
377+
if buf_km_dict["lhs"] ~= nil then
378+
local result = string.find(buf_km_dict["lhs"], lhs_pat)
379+
380+
if result ~= nil and result ~= "" then
381+
-- get keymap associated with buffer
382+
local dict = vim.fn.maparg(name_lhs, mode, 0, 1)
383+
if dict ~= nil then
384+
-- save buffer keymap
385+
if dict.buffer == 1 then
386+
local obj = {
387+
bufnr = self.bufnr,
388+
mode = mode,
389+
abbr = 0,
390+
km_dict = dict,
391+
}
392+
table.insert(R, obj)
393+
end
394+
end
395+
-- found buffer keymap, so stop searching
396+
do
397+
break
398+
end
399+
end
400+
end
401+
end
402+
355403
vim.keymap.set(mapping[1], mapping[2], mapping[3], map_opt)
356404
end
357405

@@ -387,6 +435,13 @@ function File:detach_buffer()
387435
end
388436
end
389437

438+
-- restore buffer keymaps
439+
for _, dict in pairs(R) do
440+
if dict.bufnr == self.bufnr then
441+
vim.fn.mapset(dict.mode, 0, dict.km_dict)
442+
end
443+
end
444+
390445
-- Diagnostics
391446
if state.disable_diagnostics then
392447
if HAS_NVIM_0_10 then

0 commit comments

Comments
 (0)