43 lines
1.4 KiB
Lua
43 lines
1.4 KiB
Lua
local function open_nvim_tree(data)
|
|
-- buffer is a real file on the disk
|
|
local real_file = vim.fn.filereadable(data.file) == 1
|
|
|
|
-- buffer is a [No Name]
|
|
local no_name = data.file == "" and vim.bo[data.buf].buftype == ""
|
|
|
|
if not real_file and not no_name then
|
|
return
|
|
end
|
|
|
|
-- open the tree, find the file but don't focus it
|
|
require("nvim-tree.api").tree.toggle({ focus = false, find_file = true, })
|
|
end
|
|
|
|
-- Auto open
|
|
-- vim.api.nvim_create_autocmd({ "VimEnter" }, { callback = open_nvim_tree })
|
|
|
|
-- Auto close
|
|
vim.api.nvim_create_autocmd({ "QuitPre" }, {
|
|
callback = function() vim.cmd("NvimTreeClose") end,
|
|
})
|
|
|
|
-- Go to last used hidden buffer when deleting a buffer
|
|
vim.api.nvim_create_autocmd("BufEnter", {
|
|
nested = true,
|
|
callback = function()
|
|
-- Only 1 window with nvim-tree left: we probably closed a file buffer
|
|
if #vim.api.nvim_list_wins() == 1 and require("nvim-tree.utils").is_nvim_tree_buf() then
|
|
local api = require('nvim-tree.api')
|
|
-- Required to let the close event complete. An error is thrown without this.
|
|
vim.defer_fn(function()
|
|
-- close nvim-tree: will go to the last hidden buffer used before closing
|
|
api.tree.toggle({ find_file = true, focus = true })
|
|
-- re-open nivm-tree
|
|
api.tree.toggle({ find_file = true, focus = true })
|
|
-- nvim-tree is still the active window. Go to the previous window.
|
|
vim.cmd("wincmd p")
|
|
end, 0)
|
|
end
|
|
end
|
|
})
|