feat: open tree on directory (#288)

This commit is contained in:
Kiyan
2021-08-28 11:39:10 +02:00
committed by GitHub
parent dc630d199a
commit 3bac5633e1
2 changed files with 74 additions and 18 deletions

View File

@@ -88,12 +88,6 @@ local function find_rogue_buffer()
return nil
end
---Check if the tree buffer is valid and loaded.
---@return boolean
local function is_buf_valid()
return a.nvim_buf_is_valid(M.View.bufnr) and a.nvim_buf_is_loaded(M.View.bufnr)
end
---Find pre-existing NvimTree buffer, delete its windows then wipe it.
---@private
function M._wipe_rogue_buffer()
@@ -118,12 +112,16 @@ local function warn_wrong_mapping()
require'nvim-tree.utils'.echo_warning(warn_str)
end
local HAS_LOADED = false
-- set user options and create tree buffer (should never be wiped)
function M.setup()
M.View.side = vim.g.nvim_tree_side or M.View.side
M.View.width = vim.g.nvim_tree_width or M.View.width
M.View.bufnr = a.nvim_create_buf(false, false)
if not HAS_LOADED then
M.View.bufnr = a.nvim_create_buf(false, false)
HAS_LOADED = true
end
if not pcall(a.nvim_buf_set_name, M.View.bufnr, 'NvimTree') then
M._wipe_rogue_buffer()
@@ -134,7 +132,8 @@ function M.setup()
vim.bo[M.View.bufnr][opt.name] = opt.val
end
vim.cmd "au! BufWinEnter * lua require'nvim-tree.view'._prevent_buffer_override()"
vim.cmd "au! BufWinEnter,BufWinLeave * lua require'nvim-tree.view'._prevent_buffer_override()"
vim.cmd "au! BufEnter,BufNewFile * lua require'nvim-tree'.open_on_directory()"
if vim.g.nvim_tree_disable_keybindings == 1 then
return
end
@@ -180,9 +179,20 @@ 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.get_winnr() or curbuf == M.View.bufnr then return end
vim.cmd("buffer "..M.View.bufnr)
if curwin ~= M.get_winnr() or curbuf == M.View.bufnr then
return
end
if a.nvim_buf_is_loaded(M.View.bufnr) and a.nvim_buf_is_valid(M.View.bufnr) then
vim.cmd("buffer "..M.View.bufnr)
end
local bufname = a.nvim_buf_get_name(curbuf)
local isdir = vim.fn.isdirectory(bufname) == 1
if isdir or not bufname or bufname == "" then
return
end
if #vim.api.nvim_list_wins() < 2 then
vim.cmd("vsplit")
@@ -264,20 +274,31 @@ local function set_local(opt, value)
vim.cmd(cmd)
end
function M.open(options)
options = options or { focus_tree = true }
if not is_buf_valid() then
M.setup()
end
a.nvim_command("vsp")
function M.replace_window()
local move_to = move_tbl[M.View.side]
a.nvim_command("wincmd "..move_to)
a.nvim_command("vertical resize "..get_width())
end
local function open_window()
a.nvim_command("vsp")
M.replace_window()
local winnr = a.nvim_get_current_win()
local tabpage = a.nvim_get_current_tabpage()
M.View.tabpages[tabpage] = vim.tbl_extend("force", M.View.tabpages[tabpage] or {help = false}, {winnr = winnr})
end
function M.open(options)
options = options or { focus_tree = true }
if not HAS_LOADED then
M.setup()
HAS_LOADED = true
end
if not M.win_open() then
open_window()
end
vim.cmd("buffer "..M.View.bufnr)
for k, v in pairs(M.View.winopts) do
set_local(k, v)