refactor(#2826): remove unnecessary view setup and members

This commit is contained in:
Alexander Courtis 2025-06-16 16:18:49 +10:00
parent 8b9c9c107f
commit d24f959bf1

View File

@ -2,10 +2,9 @@ local utils = require("nvim-tree.utils")
local M = {}
M.View = {
tabpages = {},
cursors = {},
}
local BUFNR_PER_TAB = {}
local CURSORS = {}
local TABPAGES = {}
-- The initial state of a tab
local tabinitial = {
@ -15,8 +14,6 @@ local tabinitial = {
winnr = nil,
}
local BUFNR_PER_TAB = {}
---@param bufnr integer
---@return boolean
local function matches_bufnr(bufnr)
@ -48,41 +45,41 @@ end
---@param tabpage integer
function M.setup_tabpage(tabpage)
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
-- save_tab_state saves any state that should be preserved across redraws.
---@param tabnr integer
function M.save_tab_state(tabnr)
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
---@param fn fun(tabpage: integer)
function M.all_tabs_callback(fn)
for tabpage, _ in pairs(M.View.tabpages) do
for tabpage, _ in pairs(TABPAGES) do
fn(tabpage)
end
end
function M.set_current_win()
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
function M.abandon_current_window()
local tab = vim.api.nvim_get_current_tabpage()
BUFNR_PER_TAB[tab] = nil
if M.View.tabpages[tab] then
M.View.tabpages[tab].winnr = nil
if TABPAGES[tab] then
TABPAGES[tab].winnr = nil
end
end
function M.abandon_all_windows()
for tab, _ in pairs(vim.api.nvim_list_tabpages()) do
BUFNR_PER_TAB[tab] = nil
if M.View.tabpages[tab] then
M.View.tabpages[tab].winnr = nil
if TABPAGES[tab] then
TABPAGES[tab].winnr = nil
end
end
end
@ -91,15 +88,15 @@ end
---@return boolean
function M.is_visible(opts)
if opts and opts.tabpage then
if M.View.tabpages[opts.tabpage] == nil then
if TABPAGES[opts.tabpage] == nil then
return false
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)
end
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
return true
end
@ -135,7 +132,7 @@ end
--- Restores the state of a NvimTree window if it was initialized before.
function M.restore_tab_state()
local tabpage = vim.api.nvim_get_current_tabpage()
M.set_cursor(M.View.cursors[tabpage])
M.set_cursor(CURSORS[tabpage])
end
--- Returns the window number for nvim-tree within the tabpage specified
@ -143,7 +140,7 @@ end
---@return number|nil
function M.get_winnr(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
return tabinfo.winnr
end
@ -157,30 +154,12 @@ end
---@param winnr number|nil
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
M.View.tabpages[i] = nil
TABPAGES[i] = nil
break
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