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:
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?
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 :)
Most helpful comment
@michaelb In the meantime you could implement this behavior with something like this: