Skip to content

Commit 783870c

Browse files
authored
fix: handle new tabs properly (#313)
1 parent 79a8188 commit 783870c

File tree

4 files changed

+24
-24
lines changed

4 files changed

+24
-24
lines changed

lua/nvim-tree.lua

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,10 @@ function M.open()
3838
end
3939
end
4040

41-
-- this is completely broken, but i'm not sure why
42-
-- this is definitely upstream related, but i could find a workaround
4341
function M.tab_change()
44-
-- we need defer_fn to make sure we close/open after we enter the tab
45-
vim.defer_fn(function()
46-
if M.close() then
47-
M.open()
48-
end
49-
end, 1)
42+
if not view.win_open() then
43+
view.open()
44+
end
5045
end
5146

5247
local function gen_go_to(mode)

lua/nvim-tree/lib.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ local function get_line_from_node(node, find_parent)
9090
end
9191

9292
function M.get_node_at_cursor()
93-
local cursor = api.nvim_win_get_cursor(view.View.winnr)
93+
local cursor = api.nvim_win_get_cursor(view.get_winnr())
9494
local line = cursor[1]
9595
if line == 1 and M.Tree.cwd ~= "/" then
9696
return { name = ".." }
@@ -353,7 +353,7 @@ function M.parent_node(node, should_close)
353353
elseif should_close then
354354
parent.open = false
355355
end
356-
api.nvim_win_set_cursor(view.View.winnr, {line, 0})
356+
api.nvim_win_set_cursor(view.get_winnr(), {line, 0})
357357
end
358358
renderer.draw(M.Tree, true)
359359
end

lua/nvim-tree/renderer.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ function M.draw(tree, reload)
320320
if not api.nvim_buf_is_loaded(view.View.bufnr) then return end
321321
local cursor
322322
if view.win_open() then
323-
cursor = api.nvim_win_get_cursor(view.View.winnr)
323+
cursor = api.nvim_win_get_cursor(view.get_winnr())
324324
end
325325
if reload then
326326
index = 0
@@ -335,10 +335,10 @@ function M.draw(tree, reload)
335335
api.nvim_buf_set_option(view.View.bufnr, 'modifiable', false)
336336

337337
if cursor and #lines >= cursor[1] then
338-
api.nvim_win_set_cursor(view.View.winnr, cursor)
338+
api.nvim_win_set_cursor(view.get_winnr(), cursor)
339339
end
340340
if cursor then
341-
api.nvim_win_set_option(view.View.winnr, 'wrap', false)
341+
api.nvim_win_set_option(view.get_winnr(), 'wrap', false)
342342
end
343343
end
344344

lua/nvim-tree/view.lua

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ end
88

99
M.View = {
1010
bufnr = nil,
11-
winnr = nil,
11+
tabpages = {},
1212
width = 30,
1313
side = 'left',
1414
winopts = {
@@ -136,7 +136,7 @@ function M._prevent_buffer_override()
136136
vim.schedule(function()
137137
local curwin = a.nvim_get_current_win()
138138
local curbuf = a.nvim_win_get_buf(curwin)
139-
if curwin ~= M.View.winnr or curbuf == M.View.bufnr then return end
139+
if curwin ~= M.get_winnr() or curbuf == M.View.bufnr then return end
140140

141141
vim.cmd("buffer "..M.View.bufnr)
142142

@@ -150,22 +150,22 @@ function M._prevent_buffer_override()
150150
end
151151

152152
function M.win_open()
153-
return M.View.winnr ~= nil and a.nvim_win_is_valid(M.View.winnr)
153+
return M.get_winnr() ~= nil and a.nvim_win_is_valid(M.get_winnr())
154154
end
155155

156156
function M.set_cursor(opts)
157157
if M.win_open() then
158-
pcall(a.nvim_win_set_cursor, M.View.winnr, opts)
158+
pcall(a.nvim_win_set_cursor, M.get_winnr(), opts)
159159
end
160160
end
161161

162162
function M.focus(winnr, open_if_closed)
163-
local wnr = winnr or M.View.winnr
163+
local wnr = winnr or M.get_winnr()
164164

165165
if a.nvim_win_get_tabpage(wnr) ~= a.nvim_win_get_tabpage(0) then
166166
M.close()
167167
M.open()
168-
wnr = M.View.winnr
168+
wnr = M.get_winnr()
169169
elseif open_if_closed and not M.win_open() then
170170
M.open()
171171
end
@@ -174,11 +174,11 @@ function M.focus(winnr, open_if_closed)
174174
end
175175

176176
function M.resize()
177-
if not a.nvim_win_is_valid(M.View.winnr) then
177+
if not a.nvim_win_is_valid(M.get_winnr()) then
178178
return
179179
end
180180

181-
a.nvim_win_set_width(M.View.winnr, M.View.width)
181+
a.nvim_win_set_width(M.get_winnr(), M.View.width)
182182
end
183183

184184
local move_tbl = {
@@ -197,9 +197,10 @@ function M.open()
197197
local move_to = move_tbl[M.View.side]
198198
a.nvim_command("wincmd "..move_to)
199199
a.nvim_command("vertical resize "..M.View.width)
200-
M.View.winnr = a.nvim_get_current_win()
200+
local winnr = a.nvim_get_current_win()
201+
M.View.tabpages[a.nvim_get_current_tabpage()] = winnr
201202
for k, v in pairs(M.View.winopts) do
202-
a.nvim_win_set_option(M.View.winnr, k, v)
203+
a.nvim_win_set_option(winnr, k, v)
203204
end
204205

205206
vim.cmd("buffer "..M.View.bufnr)
@@ -217,7 +218,11 @@ function M.close()
217218
end
218219
return
219220
end
220-
a.nvim_win_hide(M.View.winnr)
221+
a.nvim_win_hide(M.get_winnr())
222+
end
223+
224+
function M.get_winnr()
225+
return M.View.tabpages[a.nvim_get_current_tabpage()]
221226
end
222227

223228
return M

0 commit comments

Comments
 (0)