refactor(#2826): remove unnecessary view setup and members
This commit is contained in:
parent
8b9c9c107f
commit
d24f959bf1
@ -2,10 +2,9 @@ local utils = require("nvim-tree.utils")
|
|||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
M.View = {
|
local BUFNR_PER_TAB = {}
|
||||||
tabpages = {},
|
local CURSORS = {}
|
||||||
cursors = {},
|
local TABPAGES = {}
|
||||||
}
|
|
||||||
|
|
||||||
-- The initial state of a tab
|
-- The initial state of a tab
|
||||||
local tabinitial = {
|
local tabinitial = {
|
||||||
@ -15,8 +14,6 @@ local tabinitial = {
|
|||||||
winnr = nil,
|
winnr = nil,
|
||||||
}
|
}
|
||||||
|
|
||||||
local BUFNR_PER_TAB = {}
|
|
||||||
|
|
||||||
---@param bufnr integer
|
---@param bufnr integer
|
||||||
---@return boolean
|
---@return boolean
|
||||||
local function matches_bufnr(bufnr)
|
local function matches_bufnr(bufnr)
|
||||||
@ -48,41 +45,41 @@ end
|
|||||||
---@param tabpage integer
|
---@param tabpage integer
|
||||||
function M.setup_tabpage(tabpage)
|
function M.setup_tabpage(tabpage)
|
||||||
local winnr = vim.api.nvim_get_current_win()
|
local winnr = vim.api.nvim_get_current_win()
|
||||||
M.View.tabpages[tabpage] = vim.tbl_extend("force", M.View.tabpages[tabpage] or tabinitial, { winnr = winnr })
|
TABPAGES[tabpage] = vim.tbl_extend("force", TABPAGES[tabpage] or tabinitial, { winnr = winnr })
|
||||||
end
|
end
|
||||||
|
|
||||||
-- save_tab_state saves any state that should be preserved across redraws.
|
-- save_tab_state saves any state that should be preserved across redraws.
|
||||||
---@param tabnr integer
|
---@param tabnr integer
|
||||||
function M.save_tab_state(tabnr)
|
function M.save_tab_state(tabnr)
|
||||||
local tabpage = tabnr or vim.api.nvim_get_current_tabpage()
|
local tabpage = tabnr or vim.api.nvim_get_current_tabpage()
|
||||||
M.View.cursors[tabpage] = vim.api.nvim_win_get_cursor(M.get_winnr(tabpage) or 0)
|
CURSORS[tabpage] = vim.api.nvim_win_get_cursor(M.get_winnr(tabpage) or 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param fn fun(tabpage: integer)
|
---@param fn fun(tabpage: integer)
|
||||||
function M.all_tabs_callback(fn)
|
function M.all_tabs_callback(fn)
|
||||||
for tabpage, _ in pairs(M.View.tabpages) do
|
for tabpage, _ in pairs(TABPAGES) do
|
||||||
fn(tabpage)
|
fn(tabpage)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.set_current_win()
|
function M.set_current_win()
|
||||||
local current_tab = vim.api.nvim_get_current_tabpage()
|
local current_tab = vim.api.nvim_get_current_tabpage()
|
||||||
M.View.tabpages[current_tab].winnr = vim.api.nvim_get_current_win()
|
TABPAGES[current_tab].winnr = vim.api.nvim_get_current_win()
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.abandon_current_window()
|
function M.abandon_current_window()
|
||||||
local tab = vim.api.nvim_get_current_tabpage()
|
local tab = vim.api.nvim_get_current_tabpage()
|
||||||
BUFNR_PER_TAB[tab] = nil
|
BUFNR_PER_TAB[tab] = nil
|
||||||
if M.View.tabpages[tab] then
|
if TABPAGES[tab] then
|
||||||
M.View.tabpages[tab].winnr = nil
|
TABPAGES[tab].winnr = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.abandon_all_windows()
|
function M.abandon_all_windows()
|
||||||
for tab, _ in pairs(vim.api.nvim_list_tabpages()) do
|
for tab, _ in pairs(vim.api.nvim_list_tabpages()) do
|
||||||
BUFNR_PER_TAB[tab] = nil
|
BUFNR_PER_TAB[tab] = nil
|
||||||
if M.View.tabpages[tab] then
|
if TABPAGES[tab] then
|
||||||
M.View.tabpages[tab].winnr = nil
|
TABPAGES[tab].winnr = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -91,15 +88,15 @@ end
|
|||||||
---@return boolean
|
---@return boolean
|
||||||
function M.is_visible(opts)
|
function M.is_visible(opts)
|
||||||
if opts and opts.tabpage then
|
if opts and opts.tabpage then
|
||||||
if M.View.tabpages[opts.tabpage] == nil then
|
if TABPAGES[opts.tabpage] == nil then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
local winnr = M.View.tabpages[opts.tabpage].winnr
|
local winnr = TABPAGES[opts.tabpage].winnr
|
||||||
return winnr and vim.api.nvim_win_is_valid(winnr)
|
return winnr and vim.api.nvim_win_is_valid(winnr)
|
||||||
end
|
end
|
||||||
|
|
||||||
if opts and opts.any_tabpage then
|
if opts and opts.any_tabpage then
|
||||||
for _, v in pairs(M.View.tabpages) do
|
for _, v in pairs(TABPAGES) do
|
||||||
if v.winnr and vim.api.nvim_win_is_valid(v.winnr) then
|
if v.winnr and vim.api.nvim_win_is_valid(v.winnr) then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
@ -135,7 +132,7 @@ end
|
|||||||
--- Restores the state of a NvimTree window if it was initialized before.
|
--- Restores the state of a NvimTree window if it was initialized before.
|
||||||
function M.restore_tab_state()
|
function M.restore_tab_state()
|
||||||
local tabpage = vim.api.nvim_get_current_tabpage()
|
local tabpage = vim.api.nvim_get_current_tabpage()
|
||||||
M.set_cursor(M.View.cursors[tabpage])
|
M.set_cursor(CURSORS[tabpage])
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Returns the window number for nvim-tree within the tabpage specified
|
--- Returns the window number for nvim-tree within the tabpage specified
|
||||||
@ -143,7 +140,7 @@ end
|
|||||||
---@return number|nil
|
---@return number|nil
|
||||||
function M.get_winnr(tabpage)
|
function M.get_winnr(tabpage)
|
||||||
tabpage = tabpage or vim.api.nvim_get_current_tabpage()
|
tabpage = tabpage or vim.api.nvim_get_current_tabpage()
|
||||||
local tabinfo = M.View.tabpages[tabpage]
|
local tabinfo = TABPAGES[tabpage]
|
||||||
if tabinfo and tabinfo.winnr and vim.api.nvim_win_is_valid(tabinfo.winnr) then
|
if tabinfo and tabinfo.winnr and vim.api.nvim_win_is_valid(tabinfo.winnr) then
|
||||||
return tabinfo.winnr
|
return tabinfo.winnr
|
||||||
end
|
end
|
||||||
@ -157,30 +154,12 @@ end
|
|||||||
|
|
||||||
---@param winnr number|nil
|
---@param winnr number|nil
|
||||||
function M.clear_tabpage(winnr)
|
function M.clear_tabpage(winnr)
|
||||||
for i, tabpage in ipairs(M.View.tabpages) do
|
for i, tabpage in ipairs(TABPAGES) do
|
||||||
if tabpage.winnr == winnr then
|
if tabpage.winnr == winnr then
|
||||||
M.View.tabpages[i] = nil
|
TABPAGES[i] = nil
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.setup(opts)
|
|
||||||
local options = opts.view or {}
|
|
||||||
M.View.centralize_selection = options.centralize_selection
|
|
||||||
M.View.side = (options.side == "right") and "right" or "left"
|
|
||||||
M.View.height = options.height
|
|
||||||
M.View.hide_root_folder = opts.renderer.root_folder_label == false
|
|
||||||
M.View.tab = opts.tab
|
|
||||||
M.View.preserve_window_proportions = options.preserve_window_proportions
|
|
||||||
M.View.winopts.cursorline = options.cursorline
|
|
||||||
M.View.winopts.number = options.number
|
|
||||||
M.View.winopts.relativenumber = options.relativenumber
|
|
||||||
M.View.winopts.signcolumn = options.signcolumn
|
|
||||||
M.View.float = options.float
|
|
||||||
M.on_attach = opts.on_attach
|
|
||||||
|
|
||||||
M.config = options
|
|
||||||
end
|
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user