Skip to content

feat(api): add config.mappings.current and config.mappings.default #1876

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 3 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 14 additions & 0 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,20 @@ exists.
- navigate.prev
- navigate.select

api.config.mappings.current() *nvim-tree.api.config.mappings.current()*
Retrieve a clone of the currently active mappings:
|nvim-tree-default-mappings| with |nvim-tree.view.mappings| applied.
Changing the active mappings will require a call to |nvim-tree-setup|

Return: ~
(table) as per |nvim-tree.view.mappings.list|

api.config.mappings.default() *nvim-tree.api.config.mappings.default()*
Retrieve a clone of the default mappings: |nvim-tree-default-mappings|

Return: ~
(table) as per |nvim-tree.view.mappings.list|

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A start on #1753

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

Expand Down
13 changes: 13 additions & 0 deletions lua/nvim-tree/actions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
local log = require "nvim-tree.log"
local view = require "nvim-tree.view"
local notify = require "nvim-tree.notify"
local utils = require "nvim-tree.utils"

-- BEGIN_DEFAULT_MAPPINGS
local DEFAULT_MAPPINGS = {
Expand Down Expand Up @@ -399,6 +400,18 @@ local DEFAULT_MAPPING_CONFIG = {
list = {},
}

--- clone default for the user
--- @return table
function M.default_mappings_clone()
return utils.table_deep_clone(DEFAULT_MAPPINGS)
end

--- clone active for the user
--- @return table
function M.current_mappings_clone()
return utils.table_deep_clone(M.mappings)
end

function M.setup(opts)
require("nvim-tree.actions.fs.trash").setup(opts)
require("nvim-tree.actions.node.system-open").setup(opts)
Expand Down
4 changes: 4 additions & 0 deletions lua/nvim-tree/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local Api = {
fs = { copy = {} },
git = {},
live_filter = {},
config = { mappings = {} },
}

local function inject_node(f)
Expand Down Expand Up @@ -127,4 +128,7 @@ Api.marks.navigate.next = require("nvim-tree.marks.navigation").next
Api.marks.navigate.prev = require("nvim-tree.marks.navigation").prev
Api.marks.navigate.select = require("nvim-tree.marks.navigation").select

Api.config.mappings.current = require("nvim-tree.actions").current_mappings_clone
Api.config.mappings.default = require("nvim-tree.actions").default_mappings_clone

return Api
17 changes: 17 additions & 0 deletions lua/nvim-tree/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,23 @@ function M.array_remove_nils(array)
end, array)
end

--- create a new deep cloned table
--- @param t table
--- @return table
function M.table_deep_clone(t)
local c = {}

for k, v in pairs(t) do
if type(v) == "table" then
c[k] = M.table_deep_clone(v)
else
c[k] = v
end
end

return c
end

function M.inject_node(f)
return function()
f(require("nvim-tree.lib").get_node_at_cursor())
Expand Down