From e0759880dbdbf27edeb7cf2b20529681a028074d Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sat, 3 May 2025 08:04:06 +1000 Subject: [PATCH] refactor(#2826): retain necessary view globals --- lua/nvim-tree.lua | 1 + lua/nvim-tree/actions/fs/clipboard.lua | 2 + lua/nvim-tree/explorer/filters.lua | 2 + lua/nvim-tree/explorer/init.lua | 10 ++++- lua/nvim-tree/explorer/live-filter.lua | 2 + lua/nvim-tree/explorer/sorter.lua | 2 + lua/nvim-tree/explorer/view.lua | 54 +++++++++++++++----------- lua/nvim-tree/log.lua | 2 +- lua/nvim-tree/marks/init.lua | 2 + lua/nvim-tree/renderer/init.lua | 2 + 10 files changed, 55 insertions(+), 24 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index b5df82fb..095b5618 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -490,6 +490,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS dev = false, diagnostics = false, git = false, + lifecycle = true, profile = false, watcher = false, }, diff --git a/lua/nvim-tree/actions/fs/clipboard.lua b/lua/nvim-tree/actions/fs/clipboard.lua index 3f3ed37c..a8c6e15e 100644 --- a/lua/nvim-tree/actions/fs/clipboard.lua +++ b/lua/nvim-tree/actions/fs/clipboard.lua @@ -31,6 +31,8 @@ local Clipboard = Class:extend() ---@protected ---@param args ClipboardArgs function Clipboard:new(args) + args.explorer:log_lifecycle("Clipboard:new") + self.explorer = args.explorer self.data = { diff --git a/lua/nvim-tree/explorer/filters.lua b/lua/nvim-tree/explorer/filters.lua index 62230687..c6640fb4 100644 --- a/lua/nvim-tree/explorer/filters.lua +++ b/lua/nvim-tree/explorer/filters.lua @@ -23,6 +23,8 @@ local Filters = Class:extend() ---@protected ---@param args FiltersArgs function Filters:new(args) + args.explorer:log_lifecycle("Filters:new") + self.explorer = args.explorer self.ignore_list = {} self.exclude_list = self.explorer.opts.filters.exclude diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index 5073652c..71b7b4e1 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -56,6 +56,8 @@ function Explorer:new(args) self.uid_explorer = vim.loop.hrtime() self.augroup_id = vim.api.nvim_create_augroup("NvimTree_Explorer_" .. self.uid_explorer, {}) + self:log_lifecycle("Explorer:new") + self.open = true self.opts = config @@ -73,7 +75,7 @@ function Explorer:new(args) end function Explorer:destroy() - log.line("dev", "Explorer:destroy") + self:log_lifecycle("Explorer:des") vim.api.nvim_del_augroup_by_id(self.augroup_id) @@ -587,6 +589,12 @@ function Explorer:get_nodes() return self:clone() end +---Log a lifecycle message with uid_explorer and absolute_path +---@param msg string? +function Explorer:log_lifecycle(msg) + log.line("lifecycle", "%-15s %d %s", msg, self.uid_explorer, self.absolute_path) +end + function Explorer:setup(opts) config = opts end diff --git a/lua/nvim-tree/explorer/live-filter.lua b/lua/nvim-tree/explorer/live-filter.lua index 3a7c469c..704945e1 100644 --- a/lua/nvim-tree/explorer/live-filter.lua +++ b/lua/nvim-tree/explorer/live-filter.lua @@ -20,6 +20,8 @@ local LiveFilter = Class:extend() ---@protected ---@param args LiveFilterArgs function LiveFilter:new(args) + args.explorer:log_lifecycle("LiveFilter:new") + self.explorer = args.explorer self.prefix = self.explorer.opts.live_filter.prefix self.always_show_folders = self.explorer.opts.live_filter.always_show_folders diff --git a/lua/nvim-tree/explorer/sorter.lua b/lua/nvim-tree/explorer/sorter.lua index 799cfa48..3fd475e3 100644 --- a/lua/nvim-tree/explorer/sorter.lua +++ b/lua/nvim-tree/explorer/sorter.lua @@ -19,6 +19,8 @@ local Sorter = Class:extend() ---@protected ---@param args SorterArgs function Sorter:new(args) + args.explorer:log_lifecycle("Sorter:new") + self.explorer = args.explorer end diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index 05d4fa5c..60e21bbe 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -10,10 +10,18 @@ local Class = require("nvim-tree.classic") ---@field resize boolean|nil default true ---@field winid number|nil 0 or nil for current +local M = {} + local DEFAULT_MIN_WIDTH = 30 local DEFAULT_MAX_WIDTH = -1 local DEFAULT_PADDING = 1 +-- TODO global, rework for multiinstance explorer +-- M.View retained for simpler change history +M.View = { + tabpages = {} +} + ---@class (exact) View: Class ---@field live_filter table ---@field side string @@ -21,7 +29,6 @@ local DEFAULT_PADDING = 1 ---@field private explorer Explorer ---@field private adaptive_size boolean ---@field private centralize_selection boolean ----@field private tabpages table ---@field private cursors table as per vim.api.nvim_win_get_cursor ---@field private hide_root_folder boolean ---@field private winopts table @@ -31,7 +38,6 @@ local DEFAULT_PADDING = 1 ---@field private width (fun():integer)|integer|string ---@field private max_width integer ---@field private padding integer ----@field private bufnr_per_tab table local View = Class:extend() ---@class View @@ -43,9 +49,10 @@ local View = Class:extend() ---@protected ---@param args ViewArgs function View:new(args) + args.explorer:log_lifecycle("View:new") + self.explorer = args.explorer self.adaptive_size = false - self.bufnr_per_tab = {} self.centralize_selection = self.explorer.opts.view.centralize_selection self.cursors = {} self.float = self.explorer.opts.view.float @@ -53,7 +60,6 @@ function View:new(args) self.hide_root_folder = self.explorer.opts.renderer.root_folder_label == false self.preserve_window_proportions = self.explorer.opts.view.preserve_window_proportions self.side = (self.explorer.opts.view.side == "right") and "right" or "left" - self.tabpages = {} self.live_filter = { prev_focused_node = nil, } self.winopts = { @@ -100,6 +106,10 @@ local tabinitial = { winnr = nil, } +-- TODO global, rework for multiinstance explorer +---@type table +local BUFNR_PER_TAB = {} + ---@type { name: string, value: any }[] local BUFFER_OPTIONS = { { name = "bufhidden", value = "wipe" }, @@ -114,7 +124,7 @@ local BUFFER_OPTIONS = { ---@param bufnr integer ---@return boolean function View:matches_bufnr(bufnr) - for _, b in pairs(self.bufnr_per_tab) do + for _, b in pairs(BUFNR_PER_TAB) do if b == bufnr then return true end @@ -137,7 +147,7 @@ function View:create_buffer(bufnr) self:wipe_rogue_buffer() local tab = vim.api.nvim_get_current_tabpage() - self.bufnr_per_tab[tab] = bufnr or vim.api.nvim_create_buf(false, false) + BUFNR_PER_TAB[tab] = bufnr or vim.api.nvim_create_buf(false, false) vim.api.nvim_buf_set_name(self:get_bufnr(), "NvimTree_" .. tab) bufnr = self:get_bufnr() @@ -184,7 +194,7 @@ local move_tbl = { ---@param tabpage integer function View:setup_tabpage(tabpage) local winnr = vim.api.nvim_get_current_win() - self.tabpages[tabpage] = vim.tbl_extend("force", self.tabpages[tabpage] or tabinitial, { winnr = winnr }) + M.View.tabpages[tabpage] = vim.tbl_extend("force", M.View.tabpages[tabpage] or tabinitial, { winnr = winnr }) end ---@private @@ -308,7 +318,7 @@ function View:close_this_tab_only() end function View:close_all_tabs() - for tabpage, _ in pairs(self.tabpages) do + for tabpage, _ in pairs(M.View.tabpages) do self:close_internal(tabpage) end end @@ -454,7 +464,7 @@ end ---@private function View:set_current_win() local current_tab = vim.api.nvim_get_current_tabpage() - self.tabpages[current_tab].winnr = vim.api.nvim_get_current_win() + M.View.tabpages[current_tab].winnr = vim.api.nvim_get_current_win() end ---Open the tree in the a window @@ -478,17 +488,17 @@ end function View:abandon_current_window() local tab = vim.api.nvim_get_current_tabpage() - self.bufnr_per_tab[tab] = nil - if self.tabpages[tab] then - self.tabpages[tab].winnr = nil + BUFNR_PER_TAB[tab] = nil + if M.View.tabpages[tab] then + M.View.tabpages[tab].winnr = nil end end function View:abandon_all_windows() for tab, _ in pairs(vim.api.nvim_list_tabpages()) do - self.bufnr_per_tab[tab] = nil - if self.tabpages[tab] then - self.tabpages[tab].winnr = nil + BUFNR_PER_TAB[tab] = nil + if M.View.tabpages[tab] then + M.View.tabpages[tab].winnr = nil end end end @@ -497,15 +507,15 @@ end ---@return boolean function View:is_visible(opts) if opts and opts.tabpage then - if self.tabpages[opts.tabpage] == nil then + if M.View.tabpages[opts.tabpage] == nil then return false end - local winnr = self.tabpages[opts.tabpage].winnr + local winnr = M.View.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(self.tabpages) do + for _, v in pairs(M.View.tabpages) do if v.winnr and vim.api.nvim_win_is_valid(v.winnr) then return true end @@ -567,7 +577,7 @@ end ---@return number|nil function View:get_winnr(tabpage) tabpage = tabpage or vim.api.nvim_get_current_tabpage() - local tabinfo = self.tabpages[tabpage] + local tabinfo = M.View.tabpages[tabpage] if tabinfo and tabinfo.winnr and vim.api.nvim_win_is_valid(tabinfo.winnr) then return tabinfo.winnr end @@ -576,7 +586,7 @@ end --- Returns the current nvim tree bufnr ---@return number function View:get_bufnr() - return self.bufnr_per_tab[vim.api.nvim_get_current_tabpage()] + return BUFNR_PER_TAB[vim.api.nvim_get_current_tabpage()] end function View:prevent_buffer_override() @@ -593,9 +603,9 @@ function View:prevent_buffer_override() local bufname = vim.api.nvim_buf_get_name(curbuf) if not bufname:match("NvimTree") then - for i, tabpage in ipairs(self.tabpages) do + for i, tabpage in ipairs(M.View.tabpages) do if tabpage.winnr == view_winnr then - self.tabpages[i] = nil + M.View.tabpages[i] = nil break end end diff --git a/lua/nvim-tree/log.lua b/lua/nvim-tree/log.lua index 9665c133..4aa8e9e1 100644 --- a/lua/nvim-tree/log.lua +++ b/lua/nvim-tree/log.lua @@ -1,4 +1,4 @@ ----@alias LogTypes "all" | "config" | "copy_paste" | "dev" | "diagnostics" | "git" | "profile" | "watcher" +---@alias LogTypes "all" | "config" | "copy_paste" | "dev" | "diagnostics" | "git" | "lifecycle" | "profile" | "watcher" ---@type table local types = {} diff --git a/lua/nvim-tree/marks/init.lua b/lua/nvim-tree/marks/init.lua index c940f999..3771520c 100644 --- a/lua/nvim-tree/marks/init.lua +++ b/lua/nvim-tree/marks/init.lua @@ -25,6 +25,8 @@ local Marks = Class:extend() ---@protected ---@param args MarksArgs function Marks:new(args) + args.explorer:log_lifecycle("Marks:new") + self.explorer = args.explorer self.marks = {} diff --git a/lua/nvim-tree/renderer/init.lua b/lua/nvim-tree/renderer/init.lua index 66b2098c..ff66f041 100644 --- a/lua/nvim-tree/renderer/init.lua +++ b/lua/nvim-tree/renderer/init.lua @@ -25,6 +25,8 @@ local Renderer = Class:extend() ---@protected ---@param args RendererArgs function Renderer:new(args) + args.explorer:log_lifecycle("Renderer:new") + self.explorer = args.explorer end