refactor: open_file (#345)

This commit is contained in:
Sindre T. Strøm
2021-05-17 22:13:30 +02:00
committed by GitHub
parent f09143900b
commit 624bec7ecd
2 changed files with 51 additions and 33 deletions

View File

@@ -73,11 +73,11 @@ function M.window_options()
if vim.g.nvim_tree_side == 'right' then if vim.g.nvim_tree_side == 'right' then
opts.open_command = 'h' opts.open_command = 'h'
opts.preview_command = 'l' opts.preview_command = 'l'
opts.split_command = 'nosplitright' opts.split_command = 'aboveleft'
else else
opts.open_command = 'l' opts.open_command = 'l'
opts.preview_command = 'h' opts.preview_command = 'h'
opts.split_command = 'splitright' opts.split_command = 'belowright'
end end
return opts return opts

View File

@@ -214,50 +214,61 @@ function M.set_index_and_redraw(fname)
end end
function M.open_file(mode, filename) function M.open_file(mode, filename)
local target_winnr = vim.fn.win_id2win(M.Tree.target_winid) local tabpage = api.nvim_get_current_tabpage()
local target_bufnr = target_winnr > 0 and vim.fn.winbufnr(M.Tree.target_winid) local win_ids = api.nvim_tabpage_list_wins(tabpage)
local splitcmd = window_opts.split_command == 'splitright' and 'vsplit' or 'split' local target_winid = M.Tree.target_winid
local ecmd = target_bufnr and string.format('%dwindo %s', target_winnr, mode == 'preview' and 'edit' or mode) or (mode == 'preview' and 'edit' or mode) local do_split = mode == "split" or mode == "vsplit"
local vertical = mode ~= "split"
api.nvim_command('wincmd '..window_opts.open_command)
-- Check if filename is already open in a window
local found = false local found = false
for _, win in ipairs(api.nvim_list_wins()) do for _, id in ipairs(win_ids) do
if filename == api.nvim_buf_get_name(api.nvim_win_get_buf(win)) then if filename == api.nvim_buf_get_name(api.nvim_win_get_buf(id)) then
if mode == "preview" then return end
found = true found = true
ecmd = function() view.focus(win) end api.nvim_set_current_win(id)
break
end end
end end
if not found and (mode == 'edit' or mode == 'preview') then if not found then
if target_bufnr then if not target_winid or not vim.tbl_contains(win_ids, target_winid) then
if not vim.o.hidden and api.nvim_buf_get_option(target_bufnr, 'modified') then -- Target is invalid, or window does not exist in current tabpage: create
ecmd = string.format('%dwindo %s', target_winnr, splitcmd) -- new window
api.nvim_command(window_opts.split_command .. " vsp")
target_winid = api.nvim_get_current_win()
M.Tree.target_winid = target_winid
-- No need to split, as we created a new window.
do_split = false
elseif not vim.o.hidden then
-- If `hidden` is not enabled, check if buffer in target window is
-- modified, and create new split if it is.
local target_bufid = api.nvim_win_get_buf(target_winid)
if api.nvim_buf_get_option(target_bufid, "modified") then
do_split = true
end end
else
ecmd = splitcmd
end end
local cmd
if do_split then
cmd = string.format("%ssplit ", vertical and "vertical " or "")
else
cmd = "edit "
end
cmd = cmd .. vim.fn.fnameescape(filename)
api.nvim_set_current_win(target_winid)
api.nvim_command(cmd)
view.resize()
end end
if type(ecmd) == 'string' then if mode == "preview" then
api.nvim_command(string.format('%s %s', ecmd, vim.fn.fnameescape(filename)))
else
ecmd()
end
view.resize()
if mode == 'preview' then
if not found then M.set_target_win() end
view.focus() view.focus()
return return
end end
if found then if vim.g.nvim_tree_quit_on_open == 1 then
return
end
if vim.g.nvim_tree_quit_on_open == 1 and mode ~= 'preview' then
view.close() view.close()
end end
@@ -275,7 +286,14 @@ function M.change_dir(foldername)
end end
function M.set_target_win() function M.set_target_win()
M.Tree.target_winid = vim.fn.win_getid(vim.fn.bufwinnr(api.nvim_get_current_buf())) local id = api.nvim_get_current_win()
local tree_id = view.View.tabpages[api.nvim_get_current_tabpage()]
if tree_id and id == tree_id then
M.Tree.target_winid = 0
return
end
M.Tree.target_winid = id
end end
function M.open() function M.open()