feat(api): add api.config.mappings.get_keymap and get_keymap_default (#2056)

* feat(api): add api.config.mappings.get_keymap and get_keymap_default

* feat(api): add api.config.mappings.get_keymap and get_keymap_default
This commit is contained in:
Alexander Courtis
2023-03-20 11:23:45 +11:00
committed by GitHub
parent 1d79a64a88
commit 4f036342f1
3 changed files with 51 additions and 0 deletions

View File

@@ -1,5 +1,24 @@
local M = {}
--- Apply mappings to a scratch buffer and return buffer local mappings
--- @param fn function(bufnr) on_attach or default_on_attach
--- @return table as per vim.api.nvim_buf_get_keymap
local function generate_keymap(fn)
-- create an unlisted scratch buffer
local scratch_bufnr = vim.api.nvim_create_buf(false, true)
-- apply mappings
fn(scratch_bufnr)
-- retrieve all
local keymap = vim.api.nvim_buf_get_keymap(scratch_bufnr, "")
-- delete the scratch buffer
vim.api.nvim_buf_delete(scratch_bufnr, { force = true })
return keymap
end
-- stylua: ignore start
function M.default_on_attach(bufnr)
local api = require('nvim-tree.api')
@@ -65,6 +84,14 @@ function M.default_on_attach(bufnr)
end
-- stylua: ignore end
function M.get_keymap()
return generate_keymap(M.on_attach)
end
function M.get_keymap_default()
return generate_keymap(M.default_on_attach)
end
function M.setup(opts)
if type(opts.on_attach) ~= "function" then
M.on_attach = M.default_on_attach