Skip to content

Commit e075988

Browse files
committed
refactor(#2826): retain necessary view globals
1 parent 6218f9c commit e075988

File tree

10 files changed

+55
-24
lines changed

10 files changed

+55
-24
lines changed

lua/nvim-tree.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
490490
dev = false,
491491
diagnostics = false,
492492
git = false,
493+
lifecycle = true,
493494
profile = false,
494495
watcher = false,
495496
},

lua/nvim-tree/actions/fs/clipboard.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ local Clipboard = Class:extend()
3131
---@protected
3232
---@param args ClipboardArgs
3333
function Clipboard:new(args)
34+
args.explorer:log_lifecycle("Clipboard:new")
35+
3436
self.explorer = args.explorer
3537

3638
self.data = {

lua/nvim-tree/explorer/filters.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ local Filters = Class:extend()
2323
---@protected
2424
---@param args FiltersArgs
2525
function Filters:new(args)
26+
args.explorer:log_lifecycle("Filters:new")
27+
2628
self.explorer = args.explorer
2729
self.ignore_list = {}
2830
self.exclude_list = self.explorer.opts.filters.exclude

lua/nvim-tree/explorer/init.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ function Explorer:new(args)
5656
self.uid_explorer = vim.loop.hrtime()
5757
self.augroup_id = vim.api.nvim_create_augroup("NvimTree_Explorer_" .. self.uid_explorer, {})
5858

59+
self:log_lifecycle("Explorer:new")
60+
5961
self.open = true
6062
self.opts = config
6163

@@ -73,7 +75,7 @@ function Explorer:new(args)
7375
end
7476

7577
function Explorer:destroy()
76-
log.line("dev", "Explorer:destroy")
78+
self:log_lifecycle("Explorer:des")
7779

7880
vim.api.nvim_del_augroup_by_id(self.augroup_id)
7981

@@ -587,6 +589,12 @@ function Explorer:get_nodes()
587589
return self:clone()
588590
end
589591

592+
---Log a lifecycle message with uid_explorer and absolute_path
593+
---@param msg string?
594+
function Explorer:log_lifecycle(msg)
595+
log.line("lifecycle", "%-15s %d %s", msg, self.uid_explorer, self.absolute_path)
596+
end
597+
590598
function Explorer:setup(opts)
591599
config = opts
592600
end

lua/nvim-tree/explorer/live-filter.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ local LiveFilter = Class:extend()
2020
---@protected
2121
---@param args LiveFilterArgs
2222
function LiveFilter:new(args)
23+
args.explorer:log_lifecycle("LiveFilter:new")
24+
2325
self.explorer = args.explorer
2426
self.prefix = self.explorer.opts.live_filter.prefix
2527
self.always_show_folders = self.explorer.opts.live_filter.always_show_folders

lua/nvim-tree/explorer/sorter.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ local Sorter = Class:extend()
1919
---@protected
2020
---@param args SorterArgs
2121
function Sorter:new(args)
22+
args.explorer:log_lifecycle("Sorter:new")
23+
2224
self.explorer = args.explorer
2325
end
2426

lua/nvim-tree/explorer/view.lua

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,25 @@ local Class = require("nvim-tree.classic")
1010
---@field resize boolean|nil default true
1111
---@field winid number|nil 0 or nil for current
1212

13+
local M = {}
14+
1315
local DEFAULT_MIN_WIDTH = 30
1416
local DEFAULT_MAX_WIDTH = -1
1517
local DEFAULT_PADDING = 1
1618

19+
-- TODO global, rework for multiinstance explorer
20+
-- M.View retained for simpler change history
21+
M.View = {
22+
tabpages = {}
23+
}
24+
1725
---@class (exact) View: Class
1826
---@field live_filter table
1927
---@field side string
2028
---@field float table
2129
---@field private explorer Explorer
2230
---@field private adaptive_size boolean
2331
---@field private centralize_selection boolean
24-
---@field private tabpages table
2532
---@field private cursors table<integer, integer[]> as per vim.api.nvim_win_get_cursor
2633
---@field private hide_root_folder boolean
2734
---@field private winopts table
@@ -31,7 +38,6 @@ local DEFAULT_PADDING = 1
3138
---@field private width (fun():integer)|integer|string
3239
---@field private max_width integer
3340
---@field private padding integer
34-
---@field private bufnr_per_tab table<integer, integer>
3541
local View = Class:extend()
3642

3743
---@class View
@@ -43,17 +49,17 @@ local View = Class:extend()
4349
---@protected
4450
---@param args ViewArgs
4551
function View:new(args)
52+
args.explorer:log_lifecycle("View:new")
53+
4654
self.explorer = args.explorer
4755
self.adaptive_size = false
48-
self.bufnr_per_tab = {}
4956
self.centralize_selection = self.explorer.opts.view.centralize_selection
5057
self.cursors = {}
5158
self.float = self.explorer.opts.view.float
5259
self.height = self.explorer.opts.view.height
5360
self.hide_root_folder = self.explorer.opts.renderer.root_folder_label == false
5461
self.preserve_window_proportions = self.explorer.opts.view.preserve_window_proportions
5562
self.side = (self.explorer.opts.view.side == "right") and "right" or "left"
56-
self.tabpages = {}
5763
self.live_filter = { prev_focused_node = nil, }
5864

5965
self.winopts = {
@@ -100,6 +106,10 @@ local tabinitial = {
100106
winnr = nil,
101107
}
102108

109+
-- TODO global, rework for multiinstance explorer
110+
---@type table<integer, integer>
111+
local BUFNR_PER_TAB = {}
112+
103113
---@type { name: string, value: any }[]
104114
local BUFFER_OPTIONS = {
105115
{ name = "bufhidden", value = "wipe" },
@@ -114,7 +124,7 @@ local BUFFER_OPTIONS = {
114124
---@param bufnr integer
115125
---@return boolean
116126
function View:matches_bufnr(bufnr)
117-
for _, b in pairs(self.bufnr_per_tab) do
127+
for _, b in pairs(BUFNR_PER_TAB) do
118128
if b == bufnr then
119129
return true
120130
end
@@ -137,7 +147,7 @@ function View:create_buffer(bufnr)
137147
self:wipe_rogue_buffer()
138148

139149
local tab = vim.api.nvim_get_current_tabpage()
140-
self.bufnr_per_tab[tab] = bufnr or vim.api.nvim_create_buf(false, false)
150+
BUFNR_PER_TAB[tab] = bufnr or vim.api.nvim_create_buf(false, false)
141151
vim.api.nvim_buf_set_name(self:get_bufnr(), "NvimTree_" .. tab)
142152

143153
bufnr = self:get_bufnr()
@@ -184,7 +194,7 @@ local move_tbl = {
184194
---@param tabpage integer
185195
function View:setup_tabpage(tabpage)
186196
local winnr = vim.api.nvim_get_current_win()
187-
self.tabpages[tabpage] = vim.tbl_extend("force", self.tabpages[tabpage] or tabinitial, { winnr = winnr })
197+
M.View.tabpages[tabpage] = vim.tbl_extend("force", M.View.tabpages[tabpage] or tabinitial, { winnr = winnr })
188198
end
189199

190200
---@private
@@ -308,7 +318,7 @@ function View:close_this_tab_only()
308318
end
309319

310320
function View:close_all_tabs()
311-
for tabpage, _ in pairs(self.tabpages) do
321+
for tabpage, _ in pairs(M.View.tabpages) do
312322
self:close_internal(tabpage)
313323
end
314324
end
@@ -454,7 +464,7 @@ end
454464
---@private
455465
function View:set_current_win()
456466
local current_tab = vim.api.nvim_get_current_tabpage()
457-
self.tabpages[current_tab].winnr = vim.api.nvim_get_current_win()
467+
M.View.tabpages[current_tab].winnr = vim.api.nvim_get_current_win()
458468
end
459469

460470
---Open the tree in the a window
@@ -478,17 +488,17 @@ end
478488

479489
function View:abandon_current_window()
480490
local tab = vim.api.nvim_get_current_tabpage()
481-
self.bufnr_per_tab[tab] = nil
482-
if self.tabpages[tab] then
483-
self.tabpages[tab].winnr = nil
491+
BUFNR_PER_TAB[tab] = nil
492+
if M.View.tabpages[tab] then
493+
M.View.tabpages[tab].winnr = nil
484494
end
485495
end
486496

487497
function View:abandon_all_windows()
488498
for tab, _ in pairs(vim.api.nvim_list_tabpages()) do
489-
self.bufnr_per_tab[tab] = nil
490-
if self.tabpages[tab] then
491-
self.tabpages[tab].winnr = nil
499+
BUFNR_PER_TAB[tab] = nil
500+
if M.View.tabpages[tab] then
501+
M.View.tabpages[tab].winnr = nil
492502
end
493503
end
494504
end
@@ -497,15 +507,15 @@ end
497507
---@return boolean
498508
function View:is_visible(opts)
499509
if opts and opts.tabpage then
500-
if self.tabpages[opts.tabpage] == nil then
510+
if M.View.tabpages[opts.tabpage] == nil then
501511
return false
502512
end
503-
local winnr = self.tabpages[opts.tabpage].winnr
513+
local winnr = M.View.tabpages[opts.tabpage].winnr
504514
return winnr and vim.api.nvim_win_is_valid(winnr)
505515
end
506516

507517
if opts and opts.any_tabpage then
508-
for _, v in pairs(self.tabpages) do
518+
for _, v in pairs(M.View.tabpages) do
509519
if v.winnr and vim.api.nvim_win_is_valid(v.winnr) then
510520
return true
511521
end
@@ -567,7 +577,7 @@ end
567577
---@return number|nil
568578
function View:get_winnr(tabpage)
569579
tabpage = tabpage or vim.api.nvim_get_current_tabpage()
570-
local tabinfo = self.tabpages[tabpage]
580+
local tabinfo = M.View.tabpages[tabpage]
571581
if tabinfo and tabinfo.winnr and vim.api.nvim_win_is_valid(tabinfo.winnr) then
572582
return tabinfo.winnr
573583
end
@@ -576,7 +586,7 @@ end
576586
--- Returns the current nvim tree bufnr
577587
---@return number
578588
function View:get_bufnr()
579-
return self.bufnr_per_tab[vim.api.nvim_get_current_tabpage()]
589+
return BUFNR_PER_TAB[vim.api.nvim_get_current_tabpage()]
580590
end
581591

582592
function View:prevent_buffer_override()
@@ -593,9 +603,9 @@ function View:prevent_buffer_override()
593603
local bufname = vim.api.nvim_buf_get_name(curbuf)
594604

595605
if not bufname:match("NvimTree") then
596-
for i, tabpage in ipairs(self.tabpages) do
606+
for i, tabpage in ipairs(M.View.tabpages) do
597607
if tabpage.winnr == view_winnr then
598-
self.tabpages[i] = nil
608+
M.View.tabpages[i] = nil
599609
break
600610
end
601611
end

lua/nvim-tree/log.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
---@alias LogTypes "all" | "config" | "copy_paste" | "dev" | "diagnostics" | "git" | "profile" | "watcher"
1+
---@alias LogTypes "all" | "config" | "copy_paste" | "dev" | "diagnostics" | "git" | "lifecycle" | "profile" | "watcher"
22

33
---@type table<LogTypes, boolean>
44
local types = {}

lua/nvim-tree/marks/init.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ local Marks = Class:extend()
2525
---@protected
2626
---@param args MarksArgs
2727
function Marks:new(args)
28+
args.explorer:log_lifecycle("Marks:new")
29+
2830
self.explorer = args.explorer
2931

3032
self.marks = {}

lua/nvim-tree/renderer/init.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ local Renderer = Class:extend()
2525
---@protected
2626
---@param args RendererArgs
2727
function Renderer:new(args)
28+
args.explorer:log_lifecycle("Renderer:new")
29+
2830
self.explorer = args.explorer
2931
end
3032

0 commit comments

Comments
 (0)