Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

api.tree.toggle doesn't work when setup is called right before it in a mapping #2050

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

Closed
Ultra-Code opened this issue Mar 12, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@Ultra-Code
Copy link

Ultra-Code commented Mar 12, 2023

Description

I am trying to manually lazy load nvim-tree on a mapping <leader>n, so I setup config like normally would but moved setup to be called in the mapping function. But I realized that when setup is called, followed by api.tree.toggle in the map, it always opens the tree window and never closes it. NOTE: but if I call the setup function outside of the mapping function .ie vim.keymap.set everything works normal . But because I am trying to lazy load nvim-tree, calling setup outside of the map isn't an option. In addition, the api.tree.open and api.tree.close work perfectly when mapped (.ie setup function then api.tree.*) only toggle isn't working for me.

I think this issue is manifest in view.is_visible() in the toggle fn

Neovim version

NVIM v0.8.3
Build type: Release
LuaJIT 2.1.0-beta3
Compilado por builduser

Features: +acl +iconv +tui
See ":help feature-compile"

     archivo "vimrc" del sistema: "$VIM/sysinit.vim"
            predefinido para $VIM: "/usr/share/nvim"

Run :checkhealth for more info

Operating system and version

Archlinux, 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 version

bbb6d48

Minimal config

vim.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

  1. nvim -nu /tmp/nvt-min.lua
  2. while in normal mode in editor, press ,n
  3. NvimTree opens
  4. press ,n again to toggle the tree. The NvimTree doesn't close but refreshes/opens again.

Expected behavior

I expected that the ,n mapping toggles the window .ie it opens it first and closes it afterward.

Actual behavior

The tree opens as expected but the next ,n press doesn't close/toggle the tree

@Ultra-Code Ultra-Code added the bug Something isn't working label Mar 12, 2023
@alex-courtis
Copy link
Member

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.
The subsequent calls are quite expensive, as they must tear down the world first.

Short answer: don't do this. Call setup once in your init.lua rather than your mapping.

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 :help nvim_tree_events_startup for two mechanisms you can use to determine whether nvim-tree has been setup, to conditionally call setup in your mapping.

@nvim-tree nvim-tree locked and limited conversation to collaborators Mar 13, 2023
@alex-courtis alex-courtis converted this issue into discussion #2052 Mar 13, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants