From 783870cda9cad2bff618af43d39f798a6ea76a69 Mon Sep 17 00:00:00 2001 From: Kiyan Date: Sun, 18 Apr 2021 11:11:56 +0200 Subject: [PATCH] fix: handle new tabs properly (#313) --- lua/nvim-tree.lua | 11 +++-------- lua/nvim-tree/lib.lua | 4 ++-- lua/nvim-tree/renderer.lua | 6 +++--- lua/nvim-tree/view.lua | 27 ++++++++++++++++----------- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 85032ff7..fd8ced4f 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -38,15 +38,10 @@ function M.open() end end --- this is completely broken, but i'm not sure why --- this is definitely upstream related, but i could find a workaround function M.tab_change() - -- we need defer_fn to make sure we close/open after we enter the tab - vim.defer_fn(function() - if M.close() then - M.open() - end - end, 1) + if not view.win_open() then + view.open() + end end local function gen_go_to(mode) diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index e9a5d9df..8ad11095 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -90,7 +90,7 @@ local function get_line_from_node(node, find_parent) end function M.get_node_at_cursor() - local cursor = api.nvim_win_get_cursor(view.View.winnr) + local cursor = api.nvim_win_get_cursor(view.get_winnr()) local line = cursor[1] if line == 1 and M.Tree.cwd ~= "/" then return { name = ".." } @@ -353,7 +353,7 @@ function M.parent_node(node, should_close) elseif should_close then parent.open = false end - api.nvim_win_set_cursor(view.View.winnr, {line, 0}) + api.nvim_win_set_cursor(view.get_winnr(), {line, 0}) end renderer.draw(M.Tree, true) end diff --git a/lua/nvim-tree/renderer.lua b/lua/nvim-tree/renderer.lua index c8a2b015..8f327c65 100644 --- a/lua/nvim-tree/renderer.lua +++ b/lua/nvim-tree/renderer.lua @@ -320,7 +320,7 @@ function M.draw(tree, reload) if not api.nvim_buf_is_loaded(view.View.bufnr) then return end local cursor if view.win_open() then - cursor = api.nvim_win_get_cursor(view.View.winnr) + cursor = api.nvim_win_get_cursor(view.get_winnr()) end if reload then index = 0 @@ -335,10 +335,10 @@ function M.draw(tree, reload) api.nvim_buf_set_option(view.View.bufnr, 'modifiable', false) if cursor and #lines >= cursor[1] then - api.nvim_win_set_cursor(view.View.winnr, cursor) + api.nvim_win_set_cursor(view.get_winnr(), cursor) end if cursor then - api.nvim_win_set_option(view.View.winnr, 'wrap', false) + api.nvim_win_set_option(view.get_winnr(), 'wrap', false) end end diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index 6e583b15..4e54ed54 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -8,7 +8,7 @@ end M.View = { bufnr = nil, - winnr = nil, + tabpages = {}, width = 30, side = 'left', winopts = { @@ -136,7 +136,7 @@ function M._prevent_buffer_override() vim.schedule(function() local curwin = a.nvim_get_current_win() local curbuf = a.nvim_win_get_buf(curwin) - if curwin ~= M.View.winnr or curbuf == M.View.bufnr then return end + if curwin ~= M.get_winnr() or curbuf == M.View.bufnr then return end vim.cmd("buffer "..M.View.bufnr) @@ -150,22 +150,22 @@ function M._prevent_buffer_override() end function M.win_open() - return M.View.winnr ~= nil and a.nvim_win_is_valid(M.View.winnr) + return M.get_winnr() ~= nil and a.nvim_win_is_valid(M.get_winnr()) end function M.set_cursor(opts) if M.win_open() then - pcall(a.nvim_win_set_cursor, M.View.winnr, opts) + pcall(a.nvim_win_set_cursor, M.get_winnr(), opts) end end function M.focus(winnr, open_if_closed) - local wnr = winnr or M.View.winnr + local wnr = winnr or M.get_winnr() if a.nvim_win_get_tabpage(wnr) ~= a.nvim_win_get_tabpage(0) then M.close() M.open() - wnr = M.View.winnr + wnr = M.get_winnr() elseif open_if_closed and not M.win_open() then M.open() end @@ -174,11 +174,11 @@ function M.focus(winnr, open_if_closed) end function M.resize() - if not a.nvim_win_is_valid(M.View.winnr) then + if not a.nvim_win_is_valid(M.get_winnr()) then return end - a.nvim_win_set_width(M.View.winnr, M.View.width) + a.nvim_win_set_width(M.get_winnr(), M.View.width) end local move_tbl = { @@ -197,9 +197,10 @@ function M.open() local move_to = move_tbl[M.View.side] a.nvim_command("wincmd "..move_to) a.nvim_command("vertical resize "..M.View.width) - M.View.winnr = a.nvim_get_current_win() + local winnr = a.nvim_get_current_win() + M.View.tabpages[a.nvim_get_current_tabpage()] = winnr for k, v in pairs(M.View.winopts) do - a.nvim_win_set_option(M.View.winnr, k, v) + a.nvim_win_set_option(winnr, k, v) end vim.cmd("buffer "..M.View.bufnr) @@ -217,7 +218,11 @@ function M.close() end return end - a.nvim_win_hide(M.View.winnr) + a.nvim_win_hide(M.get_winnr()) +end + +function M.get_winnr() + return M.View.tabpages[a.nvim_get_current_tabpage()] end return M