Skip to content

fix: handle new tabs properly #313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lua/nvim-tree/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ".." }
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lua/nvim-tree/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
27 changes: 16 additions & 11 deletions lua/nvim-tree/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ end

M.View = {
bufnr = nil,
winnr = nil,
tabpages = {},
width = 30,
side = 'left',
winopts = {
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand All @@ -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 = {
Expand All @@ -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)
Expand All @@ -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