feat(lib): Improved tabnew behavior. (#395)

This commit is contained in:
Sindre T. Strøm 2021-05-25 19:53:43 +02:00 committed by GitHub
parent d6ab59fd5a
commit 5e7e5f2949
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 9 deletions

View File

@ -177,7 +177,7 @@ Default is {}
|g:nvim_tree_quit_on_open| *g:nvim_tree_quit_on_open* |g:nvim_tree_quit_on_open| *g:nvim_tree_quit_on_open*
Can be `0` or `1`. When `1`, will close the tree when a file is opened. Can be `0` or `1`. When `1`, will close the tree when a file is opened.
Applies to: `preview`, `edit`, `edit_vsplit`, `edit_split`, `edit_tab`. Applies to: `edit`, `vsplit`, `split`, `tabnew`.
Default is 0 Default is 0
|g:nvim_tree_disable_keybindings| *g:nvim_tree_disable_keybindings* |g:nvim_tree_disable_keybindings| *g:nvim_tree_disable_keybindings*

View File

@ -283,14 +283,7 @@ end
function M.open_file(mode, filename) function M.open_file(mode, filename)
if mode == "tabnew" then if mode == "tabnew" then
-- Switch window first to ensure new window doesn't inherit settings from M.open_file_in_tab(filename)
-- NvimTree
if M.Tree.target_winid > 0 and api.nvim_win_is_valid(M.Tree.target_winid) then
api.nvim_set_current_win(M.Tree.target_winid)
else
vim.cmd("wincmd p")
end
vim.cmd("tabe " .. vim.fn.fnameescape(filename))
return return
end end
@ -368,6 +361,40 @@ function M.open_file(mode, filename)
renderer.draw(M.Tree, true) renderer.draw(M.Tree, true)
end end
function M.open_file_in_tab(filename)
local close = vim.g.nvim_tree_quit_on_open == 1
if close then
view.close()
else
-- Switch window first to ensure new window doesn't inherit settings from
-- NvimTree
if M.Tree.target_winid > 0 and api.nvim_win_is_valid(M.Tree.target_winid) then
api.nvim_set_current_win(M.Tree.target_winid)
else
vim.cmd("wincmd p")
end
end
-- This sequence of commands are here to ensure a number of things: the new
-- buffer must be opened in the current tabpage first so that focus can be
-- brought back to the tree if it wasn't quit_on_open. It also ensures that
-- when we open the new tabpage with the file, its window doesn't inherit
-- settings from NvimTree, as it was already loaded.
vim.cmd("edit " .. vim.fn.fnameescape(filename))
local alt_bufid = vim.fn.bufnr("#")
if alt_bufid ~= -1 then
api.nvim_set_current_buf(alt_bufid)
end
if not close then
vim.cmd("wincmd p")
end
vim.cmd("tabe " .. vim.fn.fnameescape(filename))
end
function M.change_dir(foldername) function M.change_dir(foldername)
if vim.fn.expand(foldername) == M.Tree.cwd then if vim.fn.expand(foldername) == M.Tree.cwd then
return return