Nvim-tree.lua: nvim-tree is too fast, following wincmd does not work right

Created on 6 Apr 2021  路  5Comments  路  Source: kyazdani42/nvim-tree.lua

While this is a 'serious' issue, the underlying cause may actually be what's in the title, or more likely async issues.

I'm on neovim latest and nvim-tree is up-to-date, and I observe the following behavior, but first, here's what i'm trying to achieve:

  • I want to toggle the tree to have a bird's eye view on my project, but not focus the nvim tree buffer. On NERDTree for example, this is achieved simply by mapping to "NERDTreeToggle | wincmd p" which returns to the previous windows after opening the tree one.

Then the issue: I couldn't reproduce this behavior with :NvimTreeToggle, or even lua require'nvim-tree'.toggle() (has not effect or errors out, but that could be my bad syntax), but then I came across this (simplified version).

function ! Nvimtreetogglecustom()
  lua require'nvim-tree'.toggle()
  " sleep 1ms
  wincmd p
endfunction

If the `sleep 1ms' is commented, calling the function toggles nvim-tree but the focus stay on the tree.

If the sleep 1ms is uncommented, calling the function toggle the tree and switches to the previous windows (so at least on 'opening') this achieve the desired behavior

What do you think of this?

Most helpful comment

@michaelb In the meantime you could implement this behavior with something like this:

function! NvimTreeToggleCustom()
lua << EOF
    local nt_lib = require'nvim-tree.lib'
    if nt_lib.win_open() then
        nt_lib.close()
    else
        nt_lib.open()
        vim.cmd("wincmd p")
    end
EOF
endfunction

All 5 comments

actually yes the nvim-tree toggle opening file is done through scheduling the opening functions, which results in failing synchronous code. Maybe a good solution would be to either not do this asynchronously or provide custom function to pass in toggle which would call once the async stuff is finished.

You be the judge, maybe documenting this behavior could help some users, especially if it affects more than wincmd.

Given the edginess of this case no one would mind if it's not fixed right away.

@michaelb In the meantime you could implement this behavior with something like this:

function! NvimTreeToggleCustom()
lua << EOF
    local nt_lib = require'nvim-tree.lib'
    if nt_lib.win_open() then
        nt_lib.close()
    else
        nt_lib.open()
        vim.cmd("wincmd p")
    end
EOF
endfunction

I figured out something similar, but this is prettier, thanks !

thanks again @sindrets :)

Was this page helpful?
0 / 5 - 0 ratings