fix: handle new tabs properly (#313)

This commit is contained in:
Kiyan
2021-04-18 11:11:56 +02:00
committed by GitHub
parent 79a8188ecf
commit 783870cda9
4 changed files with 24 additions and 24 deletions

View File

@@ -38,15 +38,10 @@ function M.open()
end end
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() function M.tab_change()
-- we need defer_fn to make sure we close/open after we enter the tab if not view.win_open() then
vim.defer_fn(function() view.open()
if M.close() then end
M.open()
end
end, 1)
end end
local function gen_go_to(mode) local function gen_go_to(mode)

View File

@@ -90,7 +90,7 @@ local function get_line_from_node(node, find_parent)
end end
function M.get_node_at_cursor() 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] local line = cursor[1]
if line == 1 and M.Tree.cwd ~= "/" then if line == 1 and M.Tree.cwd ~= "/" then
return { name = ".." } return { name = ".." }
@@ -353,7 +353,7 @@ function M.parent_node(node, should_close)
elseif should_close then elseif should_close then
parent.open = false parent.open = false
end end
api.nvim_win_set_cursor(view.View.winnr, {line, 0}) api.nvim_win_set_cursor(view.get_winnr(), {line, 0})
end end
renderer.draw(M.Tree, true) renderer.draw(M.Tree, true)
end end

View File

@@ -320,7 +320,7 @@ function M.draw(tree, reload)
if not api.nvim_buf_is_loaded(view.View.bufnr) then return end if not api.nvim_buf_is_loaded(view.View.bufnr) then return end
local cursor local cursor
if view.win_open() then if view.win_open() then
cursor = api.nvim_win_get_cursor(view.View.winnr) cursor = api.nvim_win_get_cursor(view.get_winnr())
end end
if reload then if reload then
index = 0 index = 0
@@ -335,10 +335,10 @@ function M.draw(tree, reload)
api.nvim_buf_set_option(view.View.bufnr, 'modifiable', false) api.nvim_buf_set_option(view.View.bufnr, 'modifiable', false)
if cursor and #lines >= cursor[1] then 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 end
if cursor then 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
end end

View File

@@ -8,7 +8,7 @@ end
M.View = { M.View = {
bufnr = nil, bufnr = nil,
winnr = nil, tabpages = {},
width = 30, width = 30,
side = 'left', side = 'left',
winopts = { winopts = {
@@ -136,7 +136,7 @@ function M._prevent_buffer_override()
vim.schedule(function() vim.schedule(function()
local curwin = a.nvim_get_current_win() local curwin = a.nvim_get_current_win()
local curbuf = a.nvim_win_get_buf(curwin) 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) vim.cmd("buffer "..M.View.bufnr)
@@ -150,22 +150,22 @@ function M._prevent_buffer_override()
end end
function M.win_open() 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 end
function M.set_cursor(opts) function M.set_cursor(opts)
if M.win_open() then 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
end end
function M.focus(winnr, open_if_closed) 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 if a.nvim_win_get_tabpage(wnr) ~= a.nvim_win_get_tabpage(0) then
M.close() M.close()
M.open() M.open()
wnr = M.View.winnr wnr = M.get_winnr()
elseif open_if_closed and not M.win_open() then elseif open_if_closed and not M.win_open() then
M.open() M.open()
end end
@@ -174,11 +174,11 @@ function M.focus(winnr, open_if_closed)
end end
function M.resize() 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 return
end end
a.nvim_win_set_width(M.View.winnr, M.View.width) a.nvim_win_set_width(M.get_winnr(), M.View.width)
end end
local move_tbl = { local move_tbl = {
@@ -197,9 +197,10 @@ function M.open()
local move_to = move_tbl[M.View.side] local move_to = move_tbl[M.View.side]
a.nvim_command("wincmd "..move_to) a.nvim_command("wincmd "..move_to)
a.nvim_command("vertical resize "..M.View.width) 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 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 end
vim.cmd("buffer "..M.View.bufnr) vim.cmd("buffer "..M.View.bufnr)
@@ -217,7 +218,11 @@ function M.close()
end end
return return
end 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 end
return M return M