refacto: buffer management, setup fixes, autocmd fixes (#967)

This commit is contained in:
Kiyan
2022-02-14 19:16:45 +01:00
committed by GitHub
parent 3f4ed9b6c2
commit 6da7467944
11 changed files with 377 additions and 379 deletions

View File

@@ -3,17 +3,15 @@ local a = vim.api
local M = {}
M.View = {
last_focused_winnr = nil,
bufnr = nil,
tabpages = {},
hide_root_folder = false,
winopts = {
relativenumber = false,
number = false,
list = false,
foldenable = false,
winfixwidth = true,
winfixheight = true,
foldenable = false,
spell = false,
signcolumn = 'yes',
foldmethod = 'manual',
@@ -33,36 +31,281 @@ M.View = {
'NormalNC:NvimTreeNormalNC',
}, ',')
},
bufopts = {
{ name = 'swapfile', val = false },
{ name = 'buftype', val = 'nofile' },
{ name = 'modifiable', val = false },
{ name = 'filetype', val = 'NvimTree' },
{ name = 'bufhidden', val = 'hide' }
},
}
local BUFNR = nil
local LAST_FOCUSED_WIN = nil
local BUFFER_OPTIONS = {
swapfile = false,
buftype = 'nofile',
modifiable = false,
filetype = 'NvimTree',
bufhidden = 'wipe',
buflisted = false,
}
local function wipe_rogue_buffer()
for _, bn in ipairs(a.nvim_list_bufs()) do
if vim.fn.bufname(bn) == "NvimTree" then
return pcall(a.nvim_buf_delete, bn, { force = true })
for _, bufnr in ipairs(a.nvim_list_bufs()) do
if bufnr ~= BUFNR and a.nvim_buf_get_name(bufnr):match("NvimTree") ~= nil then
return pcall(a.nvim_buf_delete, bufnr, { force = true })
end
end
end
-- FIXME: setting options to buffer clears the startup screen
function M.create_buffer()
local function create_buffer(bufnr)
BUFNR = bufnr or a.nvim_create_buf(false, false)
wipe_rogue_buffer()
M.View.bufnr = a.nvim_create_buf(false, false)
a.nvim_buf_set_name(M.View.bufnr, 'NvimTree')
a.nvim_buf_set_name(BUFNR, 'NvimTree')
for _, opt in ipairs(M.View.bufopts) do
vim.bo[M.View.bufnr][opt.name] = opt.val
for option, value in pairs(BUFFER_OPTIONS) do
vim.bo[BUFNR][option] = value
end
require'nvim-tree.actions'.apply_mappings(M.View.bufnr)
require'nvim-tree.actions'.apply_mappings(BUFNR)
end
local function get_size()
local width_or_height = M.is_vertical() and 'width' or 'height'
local size = M.View[width_or_height]
if type(size) == "number" then
return size
elseif type(size) == "function" then
return size()
end
local size_as_number = tonumber(size:sub(0, -2))
local percent_as_decimal = size_as_number / 100
return math.floor(vim.o.columns * percent_as_decimal)
end
local move_tbl = {
left = 'H',
right = 'L',
bottom = 'J',
top = 'K',
}
-- TODO: remove this once they fix https://github.com/neovim/neovim/issues/14670
local function set_local(opt, value)
local cmd
if value == true then
cmd = string.format('setlocal %s', opt)
elseif value == false then
cmd = string.format('setlocal no%s', opt)
else
cmd = string.format('setlocal %s=%s', opt, value)
end
vim.cmd(cmd)
end
local function open_window()
a.nvim_command("vsp")
M.reposition_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
local function set_window_options_and_buffer()
pcall(vim.cmd, "buffer "..BUFNR)
for k, v in pairs(M.View.winopts) do
set_local(k, v)
end
end
local function get_existing_buffers()
return vim.tbl_filter(
function(buf)
return a.nvim_buf_is_valid(buf) and vim.fn.buflisted(buf) == 1
end,
a.nvim_list_bufs()
)
end
local function switch_buf_if_last_buf()
if #a.nvim_list_wins() == 1 then
if #get_existing_buffers() > 0 then
vim.cmd "sbnext"
else
vim.cmd "new"
end
end
end
function M.close()
if not M.is_visible() then return end
switch_buf_if_last_buf()
local tree_win = M.get_winnr()
local current_win = a.nvim_get_current_win()
for _, win in pairs(a.nvim_list_wins()) do
if tree_win ~= win and a.nvim_win_get_config(win).relative == "" then
a.nvim_win_close(tree_win, true)
if tree_win == current_win and LAST_FOCUSED_WIN then
a.nvim_set_current_win(LAST_FOCUSED_WIN)
end
return
end
end
end
function M.open(options)
if M.is_visible() then
return
end
LAST_FOCUSED_WIN = a.nvim_get_current_win()
create_buffer()
open_window()
set_window_options_and_buffer()
M.resize()
local opts = options or { focus_tree = true }
if not opts.focus_tree then
vim.cmd("wincmd p")
end
end
function M.resize(size)
if size then
M.View.width = size
M.View.height = size
end
if not M.is_visible() then
return
end
if M.is_vertical() then
a.nvim_win_set_width(M.get_winnr(), get_size())
else
a.nvim_win_set_height(M.get_winnr(), get_size())
end
vim.cmd ":wincmd ="
end
function M.reposition_window()
local move_to = move_tbl[M.View.side]
a.nvim_command("wincmd "..move_to)
local resize_direction = M.is_vertical() and 'vertical ' or ''
a.nvim_command(resize_direction.."resize "..get_size())
end
local function set_current_win()
local current_tab = a.nvim_get_current_tabpage()
M.View.tabpages[current_tab] = { winnr = a.nvim_get_current_win() }
end
function M.open_in_current_win()
create_buffer(a.nvim_get_current_buf())
set_current_win()
set_window_options_and_buffer()
M.reposition_window()
M.resize()
end
function M.is_visible(opts)
if opts and opts.any_tabpage then
for _, v in pairs(M.View.tabpages) do
if a.nvim_win_is_valid(v.winnr) then
return true
end
end
return false
end
return M.get_winnr() ~= nil and a.nvim_win_is_valid(M.get_winnr())
end
function M.set_cursor(opts)
if M.is_visible() then
pcall(a.nvim_win_set_cursor, M.get_winnr(), opts)
end
end
function M.focus(winnr, open_if_closed)
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.get_winnr()
elseif open_if_closed and not M.is_visible() then
M.open()
end
a.nvim_set_current_win(wnr)
end
function M.is_vertical()
return M.View.side == 'left' or M.View.side == 'right'
end
--- Returns the window number for nvim-tree within the tabpage specified
---@param tabpage number: (optional) the number of the chosen tabpage. Defaults to current tabpage.
---@return number
function M.get_winnr(tabpage)
tabpage = tabpage or a.nvim_get_current_tabpage()
local tabinfo = M.View.tabpages[tabpage]
if tabinfo ~= nil then
return tabinfo.winnr
end
end
--- Returns the current nvim tree bufnr
---@return number
function M.get_bufnr()
return BUFNR
end
--- Checks if nvim-tree is displaying the help ui within the tabpage specified
---@param tabpage number: (optional) the number of the chosen tabpage. Defaults to current tabpage.
---@return number
function M.is_help_ui(tabpage)
tabpage = tabpage or a.nvim_get_current_tabpage()
local tabinfo = M.View.tabpages[tabpage]
if tabinfo ~= nil then
return tabinfo.help
end
end
function M.toggle_help(tabpage)
tabpage = tabpage or a.nvim_get_current_tabpage()
M.View.tabpages[tabpage].help = not M.View.tabpages[tabpage].help
end
function M.is_buf_valid(bufnr)
return bufnr and a.nvim_buf_is_valid(bufnr) and a.nvim_buf_is_loaded(bufnr)
end
function M._prevent_buffer_override()
local view_winnr = M.get_winnr()
local view_bufnr = M.get_bufnr()
-- need to schedule to let the new buffer populate the window
-- because this event needs to be run on bufWipeout.
-- Otherwise the curwin/curbuf would match the view buffer and the view window.
vim.schedule(function()
local curwin = a.nvim_get_current_win()
local curbuf = a.nvim_win_get_buf(curwin)
local bufname = a.nvim_buf_get_name(curbuf)
if not bufname:match("NvimTree") then
M.View.tabpages = {}
end
if curwin ~= view_winnr or bufname == "" or curbuf == view_bufnr then
return
end
-- patch to avoid the overriding window to be fixed in size
-- might need a better patch
vim.cmd "setlocal nowinfixwidth"
vim.cmd "setlocal nowinfixheight"
M.open({ focus_tree = false })
require"nvim-tree.renderer".draw()
require"nvim-tree".find_file(false)
end)
end
local DEFAULT_CONFIG = {
width = 30,
height = 30,
@@ -85,189 +328,4 @@ function M.setup(opts)
M.View.winopts.signcolumn = options.signcolumn
end
function M.win_open(opts)
if opts and opts.any_tabpage then
for _, v in pairs(M.View.tabpages) do
if a.nvim_win_is_valid(v.winnr) then
return true
end
end
return false
else
return M.get_winnr() ~= nil and a.nvim_win_is_valid(M.get_winnr())
end
end
function M.set_cursor(opts)
if M.win_open() then
pcall(a.nvim_win_set_cursor, M.get_winnr(), opts)
end
end
function M.focus(winnr, open_if_closed)
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.get_winnr()
elseif open_if_closed and not M.win_open() then
M.open()
end
a.nvim_set_current_win(wnr)
end
function M.is_vertical()
return M.View.side == 'left' or M.View.side == 'right'
end
local function get_size()
local width_or_height = M.is_vertical() and 'width' or 'height'
local size = M.View[width_or_height]
if type(size) == "number" then
return size
elseif type(size) == "function" then
return size()
end
local size_as_number = tonumber(size:sub(0, -2))
local percent_as_decimal = size_as_number / 100
return math.floor(vim.o.columns * percent_as_decimal)
end
function M.resize()
if not M.View.auto_resize or not a.nvim_win_is_valid(M.get_winnr()) then
return
end
if M.is_vertical() then
a.nvim_win_set_width(M.get_winnr(), get_size())
else
a.nvim_win_set_height(M.get_winnr(), get_size())
end
end
local move_tbl = {
left = 'H',
right = 'L',
bottom = 'J',
top = 'K',
}
-- TODO: remove this once they fix https://github.com/neovim/neovim/issues/14670
local function set_local(opt, value)
local cmd
if value == true then
cmd = string.format('setlocal %s', opt)
elseif value == false then
cmd = string.format('setlocal no%s', opt)
else
cmd = string.format('setlocal %s=%s', opt, value)
end
vim.cmd(cmd)
end
function M.replace_window()
local move_to = move_tbl[M.View.side]
a.nvim_command("wincmd "..move_to)
local resize_direction = M.is_vertical() and 'vertical ' or ''
a.nvim_command(resize_direction.."resize "..get_size())
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.is_buf_valid(bufnr)
return bufnr and a.nvim_buf_is_valid(bufnr) and a.nvim_buf_is_loaded(bufnr)
end
function M.open(options)
M.View.last_focused_winnr = a.nvim_get_current_win()
local should_redraw = false
if not M.is_buf_valid(M.View.bufnr) then
should_redraw = true
M.create_buffer()
end
if not M.win_open() then
open_window()
end
pcall(vim.cmd, "buffer "..M.View.bufnr)
for k, v in pairs(M.View.winopts) do
set_local(k, v)
end
vim.cmd ":wincmd ="
local opts = options or { focus_tree = true }
if not opts.focus_tree then
vim.cmd("wincmd p")
end
return should_redraw
end
local function get_existing_buffers()
return vim.tbl_filter(
function(buf)
return a.nvim_buf_is_valid(buf) and vim.fn.buflisted(buf) == 1
end,
a.nvim_list_bufs()
)
end
function M.close()
if not M.win_open() then return end
if #a.nvim_list_wins() == 1 then
local existing_bufs = get_existing_buffers()
if #existing_bufs > 0 then
vim.cmd "sbnext"
else
vim.cmd "new"
end
end
local tree_win = M.get_winnr()
local current_win = a.nvim_get_current_win()
for _, win in pairs(a.nvim_list_wins()) do
if tree_win ~= win and a.nvim_win_get_config(win).relative == "" then
a.nvim_win_hide(tree_win)
if tree_win == current_win and M.View.last_focused_winnr then
a.nvim_set_current_win(M.View.last_focused_winnr)
end
return
end
end
end
--- Returns the window number for nvim-tree within the tabpage specified
---@param tabpage number: (optional) the number of the chosen tabpage. Defaults to current tabpage.
---@return number
function M.get_winnr(tabpage)
tabpage = tabpage or a.nvim_get_current_tabpage()
local tabinfo = M.View.tabpages[tabpage]
if tabinfo ~= nil then
return tabinfo.winnr
end
end
--- Checks if nvim-tree is displaying the help ui within the tabpage specified
---@param tabpage number: (optional) the number of the chosen tabpage. Defaults to current tabpage.
---@return number
function M.is_help_ui(tabpage)
tabpage = tabpage or a.nvim_get_current_tabpage()
local tabinfo = M.View.tabpages[tabpage]
if tabinfo ~= nil then
return tabinfo.help
end
end
function M.toggle_help(tabpage)
tabpage = tabpage or a.nvim_get_current_tabpage()
M.View.tabpages[tabpage].help = not M.View.tabpages[tabpage].help
end
return M