Skip to content

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

Closed
@bernardassan

Description

@bernardassan

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions