Skip to content

Commit 4f03634

Browse files
authored
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
1 parent 1d79a64 commit 4f03634

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

doc/nvim-tree-lua.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,6 +1494,23 @@ api.config.mappings.default() *nvim-tree.api.config.mappings.default()*
14941494
Return: ~
14951495
(table) as per |nvim-tree.view.mappings.list|
14961496

1497+
*nvim-tree.api.config.mappings.get_keymap()*
1498+
api.config.mappings.get_keymap()
1499+
Retrieves all buffer local mappings for nvim-tree.
1500+
These are the mappings that are applied by |nvim-tree.on_attach|, which
1501+
may include default mappings.
1502+
1503+
Return: ~
1504+
(table) as per |nvim_buf_get_keymap()|
1505+
1506+
*nvim-tree.api.config.mappings.get_keymap()*
1507+
api.config.mappings.get_keymap_default()
1508+
Retrieves the buffer local mappings for nvim-tree that are applied by
1509+
|nvim-tree.api.config.mappings.default_on_attach()|
1510+
1511+
Return: ~
1512+
(table) as per |nvim_buf_get_keymap()|
1513+
14971514
==============================================================================
14981515
6. MAPPINGS *nvim-tree-mappings*
14991516

lua/nvim-tree/api.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,11 @@ Api.config.mappings.default = function()
182182
return require("nvim-tree.keymap-legacy").default_mappings_clone()
183183
end
184184

185+
Api.config.mappings.get_keymap = function()
186+
return require("nvim-tree.keymap").get_keymap()
187+
end
188+
Api.config.mappings.get_keymap_default = function()
189+
return require("nvim-tree.keymap").get_keymap_default()
190+
end
191+
185192
return Api

lua/nvim-tree/keymap.lua

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
local M = {}
22

3+
--- Apply mappings to a scratch buffer and return buffer local mappings
4+
--- @param fn function(bufnr) on_attach or default_on_attach
5+
--- @return table as per vim.api.nvim_buf_get_keymap
6+
local function generate_keymap(fn)
7+
-- create an unlisted scratch buffer
8+
local scratch_bufnr = vim.api.nvim_create_buf(false, true)
9+
10+
-- apply mappings
11+
fn(scratch_bufnr)
12+
13+
-- retrieve all
14+
local keymap = vim.api.nvim_buf_get_keymap(scratch_bufnr, "")
15+
16+
-- delete the scratch buffer
17+
vim.api.nvim_buf_delete(scratch_bufnr, { force = true })
18+
19+
return keymap
20+
end
21+
322
-- stylua: ignore start
423
function M.default_on_attach(bufnr)
524
local api = require('nvim-tree.api')
@@ -65,6 +84,14 @@ function M.default_on_attach(bufnr)
6584
end
6685
-- stylua: ignore end
6786

87+
function M.get_keymap()
88+
return generate_keymap(M.on_attach)
89+
end
90+
91+
function M.get_keymap_default()
92+
return generate_keymap(M.default_on_attach)
93+
end
94+
6895
function M.setup(opts)
6996
if type(opts.on_attach) ~= "function" then
7097
M.on_attach = M.default_on_attach

0 commit comments

Comments
 (0)