fix: handle new tabs properly (#313)
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user