Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,23 @@ api.config.mappings.default() *nvim-tree.api.config.mappings.default()*
Return: ~
(table) as per |nvim-tree.view.mappings.list|

*nvim-tree.api.config.mappings.get_keymap()*
api.config.mappings.get_keymap()
Retrieves all buffer local mappings for nvim-tree.
These are the mappings that are applied by |nvim-tree.on_attach|, which
may include default mappings.

Return: ~
(table) as per |nvim_buf_get_keymap()|

*nvim-tree.api.config.mappings.get_keymap()*
api.config.mappings.get_keymap_default()
Retrieves the buffer local mappings for nvim-tree that are applied by
|nvim-tree.api.config.mappings.default_on_attach()|

Return: ~
(table) as per |nvim_buf_get_keymap()|

==============================================================================
6. MAPPINGS *nvim-tree-mappings*

Expand Down
7 changes: 7 additions & 0 deletions lua/nvim-tree/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,11 @@ Api.config.mappings.default = function()
return require("nvim-tree.keymap-legacy").default_mappings_clone()
end

Api.config.mappings.get_keymap = function()
return require("nvim-tree.keymap").get_keymap()
end
Api.config.mappings.get_keymap_default = function()
return require("nvim-tree.keymap").get_keymap_default()
end

return Api
27 changes: 27 additions & 0 deletions lua/nvim-tree/keymap.lua
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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
Expand Down