Skip to content

Commit 1978dc1

Browse files
committed
chore(mappings): use default mappings when on_attach not present, log legacy migration
1 parent 46010a9 commit 1978dc1

File tree

4 files changed

+30
-24
lines changed

4 files changed

+30
-24
lines changed

doc/nvim-tree-lua.txt

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -625,23 +625,22 @@ performance.
625625
*nvim-tree.on_attach*
626626
Function ran when creating the nvim-tree buffer.
627627
This can be used to attach keybindings to the tree buffer.
628-
When on_attach is "disabled", it will use the older mapping strategy, otherwise it
629-
will use the newer one.
630-
>
631-
on_attach = function(bufnr)
632-
local inject_node = require("nvim-tree.utils").inject_node
633-
634-
vim.keymap.set("n", "<leader>n", inject_node(function(node)
635-
if node then
636-
print(node.absolute_path)
637-
end
638-
end), { buffer = bufnr, noremap = true })
628+
When on_attach is not a function, |nvim-tree.view.mappings| will be used.
629+
Type: `function(bufnr)`, Default: `"disable"`
639630

640-
vim.bo[bufnr].path = "/tmp"
631+
Example: >
632+
local Api = require('nvim-tree.api')
633+
local Lib = require('nvim-tree.lib')
634+
635+
local my_on_attach = function(bufnr)
636+
vim.keymap.set('n', '?', Api.tree.toggle_help, { buffer = bufnr, noremap = true, silent = true, nowait = true, desc = 'Help' })
637+
vim.keymap.set('n', 'h', Api.tree.toggle_help, { buffer = bufnr, noremap = true, silent = true, nowait = true, desc = 'Help' })
638+
vim.keymap.set("n", "<C-P>", function()
639+
local node = Lib.get_node_at_cursor()
640+
print(node.absolute_path)
641+
end, { buffer = bufnr, noremap = true, silent = true, nowait = true, desc = "my description" })
641642
end
642643
<
643-
Type: `function(bufnr)`, Default: `"disable"`
644-
645644
*nvim-tree.remove_keymaps*
646645
This can be used to remove the default mappings in the tree.
647646
- Remove specific keys by passing a `string` table of keys

lua/nvim-tree.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,6 @@ function M.setup(conf)
736736
validate_options(conf)
737737

738738
local opts = merge_options(conf)
739-
legacy.move_mappings_to_keymap(opts)
740739

741740
local netrw_disabled = opts.disable_netrw or opts.hijack_netrw
742741
_config.root_dirs = opts.root_dirs
@@ -758,6 +757,8 @@ function M.setup(conf)
758757
log.line("config", "default config + user")
759758
log.raw("config", "%s\n", vim.inspect(opts))
760759

760+
legacy.move_mappings_to_keymap(opts)
761+
761762
require("nvim-tree.actions").setup(opts)
762763
require("nvim-tree.keymap").setup(opts)
763764
require("nvim-tree.colors").setup()

lua/nvim-tree/legacy.lua

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local utils = require "nvim-tree.utils"
22
local notify = require "nvim-tree.notify"
33
local open_file = require "nvim-tree.actions.node.open-file"
4+
local log = require "nvim-tree.log"
45

56
local DEFAULT_KEYMAPS = require("nvim-tree.keymap").DEFAULT_KEYMAPS
67

@@ -380,21 +381,25 @@ local on_attach = function(bufnr)
380381
]]
381382

382383
for _, el in pairs(call_list) do
384+
local vim_keymap_set
383385
if el.action_cb then
384-
M.on_attach_lua = string.format(
385-
'%s vim.keymap.set("n", "%s", function()\n local node = Lib.get_node_at_cursor()\n -- my code\n end, { buffer = bufnr, noremap = true, silent = true, nowait = true, desc = "my description" })\n',
386-
M.on_attach_lua,
386+
vim_keymap_set = string.format(
387+
'vim.keymap.set("n", "%s", function()\n local node = Lib.get_node_at_cursor()\n -- my code\n end, { buffer = bufnr, noremap = true, silent = true, nowait = true, desc = "my description" })',
387388
el.key
388389
)
389390
elseif el.keymap then
390-
M.on_attach_lua = string.format(
391-
"%s vim.keymap.set('n', '%s', %s, { buffer = bufnr, noremap = true, silent = true, nowait = true, desc = '%s' })\n",
392-
M.on_attach_lua,
391+
vim_keymap_set = string.format(
392+
"vim.keymap.set('n', '%s', %s, { buffer = bufnr, noremap = true, silent = true, nowait = true, desc = '%s' })",
393393
el.key,
394394
LEGACY_CALLBACKS[el.keymap.legacy_action],
395395
el.keymap.desc.short
396396
)
397397
end
398+
399+
if vim_keymap_set then
400+
log.line("config", "generated %s", vim_keymap_set)
401+
M.on_attach_lua = string.format("%s %s\n", M.on_attach_lua, vim_keymap_set)
402+
end
398403
end
399404
M.on_attach_lua = string.format("%send\n", M.on_attach_lua)
400405

@@ -417,8 +422,9 @@ local on_attach = function(bufnr)
417422
end
418423

419424
function M.move_mappings_to_keymap(opts)
420-
if opts.on_attach == "disable" and opts.view and opts.view.mappings then
425+
if type(opts.on_attach) ~= "function" and opts.view and opts.view.mappings then
421426
local custom_only, list = opts.view.mappings.custom_only, opts.view.mappings.list
427+
log.line("config", "generating on_attach for %d legacy view.mappings.list:", #list)
422428
if custom_only then
423429
opts.remove_keymaps = true
424430
opts.view.mappings.custom_only = nil
@@ -469,7 +475,7 @@ function M.generate_on_attach()
469475
io.close(file)
470476
open_file.fn("edit", name)
471477
else
472-
utils.notify.info "no custom mappings"
478+
notify.info "no custom mappings"
473479
end
474480
end
475481

lua/nvim-tree/view.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ local function create_buffer(bufnr)
9393
vim.bo[M.get_bufnr()][option] = value
9494
end
9595

96+
require("nvim-tree.keymap").set_keymaps(M.get_bufnr())
9697
if type(M.on_attach) == "function" then
97-
require("nvim-tree.keymap").set_keymaps(M.get_bufnr())
9898
M.on_attach(M.get_bufnr())
9999
end
100100
end

0 commit comments

Comments
 (0)