api.tree.toggle doesn't work when setup is called right before it in a mapping #2052
-
DescriptionI am trying to manually lazy load nvim-tree on a mapping I think this issue is manifest in Neovim version
Operating system and versionArchlinux, Linux malpha 6.2.2-zen2-1-zen #1 ZEN SMP PREEMPT_DYNAMIC Wed, 08 Mar 2023 04:07:25 +0000 x86_64 GNU/Linux nvim-tree versionMinimal configvim.cmd [[set runtimepath=$VIMRUNTIME]]
vim.cmd [[set packpath=/tmp/nvt-min/site]]
local package_root = "/tmp/nvt-min/site/pack"
local install_path = package_root .. "/packer/start/packer.nvim"
local function load_plugins()
require("packer").startup {
{
"wbthomason/packer.nvim",
"nvim-tree/nvim-tree.lua",
"nvim-tree/nvim-web-devicons",
-- ADD PLUGINS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
},
config = {
package_root = package_root,
compile_path = install_path .. "/plugin/packer_compiled.lua",
display = { non_interactive = true },
},
}
end
if vim.fn.isdirectory(install_path) == 0 then
print "Installing nvim-tree and dependencies."
vim.fn.system { "git", "clone", "--depth=1", "https://github.com/wbthomason/packer.nvim", install_path }
end
load_plugins()
require("packer").sync()
vim.cmd [[autocmd User PackerComplete ++once echo "Ready!" | lua setup()]]
vim.opt.termguicolors = true
vim.opt.cursorline = true
-- MODIFY NVIM-TREE SETTINGS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
_G.setup = function()
vim.keymap.set("n", ",n", function()
require("nvim-tree").setup()
return require("nvim-tree.api").tree.toggle({ focus = false, find_file = true })
end
, { silent = true })
end Steps to reproduce
Expected behaviorI expected that the Actual behaviorThe tree opens as expected but the next |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
nvim-tree setup is intended to be called once at startup and again only if you have made configuration changes. The first call is cheap, doing nothing more than validating and applying the configuration. Nothing happens until the tree is opened. Short answer: don't do this. Call setup once in your Lazy loading: if you have a use case that absolutely requires lazy loading, please share it. I've not received a good answer other than "the cool factor". Long answer: see |
Beta Was this translation helpful? Give feedback.
-
It would be an interest feature, @alex-courtis . I'm also having this problem after migrate to lazy.nvim package manager. The idea is, only load the package (in this case the nvim-tree) if you need it. It allows a faster neovim bootstrap Following my configuration: local tree_interface = require("interfaces.tree")
local _setup = function()
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1
vim.opt.termguicolors = true
require("nvim-tree").setup({
sync_root_with_cwd = true,
respect_buf_cwd = true,
view = {
mappings = {
custom_only = true,
list = {
{ key = "d", action = "remove" },
{ key = "h", action = "parent_node" },
{ key = "H", action = "close_node" },
{ key = "R", action = "rename" },
{ key = "m", action = "bulk_move" },
{ key = "p", action = "paste" },
{ key = "P", action = "preview" },
{ key = "cf", action = "create" },
{ key = "gr", action = "refresh" },
{ key = "oh", action = "split" },
{ key = "ov", action = "vsplit" },
{ key = "ox", action = "system_open" },
{ key = "yf", action = "copy" },
{ key = "ya", action = "copy_absolute_path" },
{ key = "yp", action = "copy_name" },
{ key = "yr", action = "copy_path" },
{ key = { "oo", "l" }, action = "edit" },
{ key = "?", action = "toggle_help" },
},
},
},
renderer = {
highlight_git = true,
root_folder_label = ":t",
icons = {
show = {
git = false,
},
},
},
update_focused_file = {
enable = true,
update_root = true,
debounce_delay = 15,
},
diagnostics = {
enable = true,
show_on_open_dirs = true,
},
git = {
ignore = false,
timeout = 200,
},
trash = {
cmd = "trash",
},
})
end
tree_interface.open = function()
_setup()
print("open")
require("nvim-tree.api").tree.open()
end
tree_interface.close = function()
_setup()
print("close")
require("nvim-tree.api").tree.close()
end
tree_interface.toggle = function()
_setup()
print("toggle")
require("nvim-tree.api").tree.toggle()
end
return tree_interface |
Beta Was this translation helpful? Give feedback.
-
@nitaicharan To fix this issue, check the recommended But when using lazy.nvim you shoudn't call |
Beta Was this translation helpful? Give feedback.
@nitaicharan To fix this issue, check the recommended
nvim_tree_events_startup
help section. An example of how I used it when I was using paq.nvim can be found here.But when using lazy.nvim you shoudn't call
setup fn
outside of theconfig fn
of the plugin. check out an example of how to configure tree with lazy.nvim