Skip to content

Commit 127e060

Browse files
committed
refactor(#2826): add experimental.multi_instance_debug, split globals out of view, move diagnostics to its own module
1 parent 8e66733 commit 127e060

File tree

7 files changed

+149
-147
lines changed

7 files changed

+149
-147
lines changed

doc/nvim-tree-lua.txt

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ Following is the default configuration. See |nvim-tree-opts| for details. >lua
638638
},
639639
},
640640
experimental = {
641+
multi_instance_debug = false,
641642
},
642643
log = {
643644
enable = false,
@@ -649,7 +650,6 @@ Following is the default configuration. See |nvim-tree-opts| for details. >lua
649650
dev = false,
650651
diagnostics = false,
651652
git = false,
652-
lifecycle = false,
653653
profile = false,
654654
watcher = false,
655655
},
@@ -1515,11 +1515,6 @@ Configuration options for opening a file from nvim-tree.
15151515
Resizes the tree when opening a file.
15161516
Type: `boolean`, Default: `true`
15171517

1518-
*nvim-tree.experimental.actions.open_file.relative_path*
1519-
Buffers opened by nvim-tree will use with relative paths instead of
1520-
absolute.
1521-
Type: `boolean`, Default: `true`
1522-
15231518
*nvim-tree.actions.open_file.window_picker*
15241519
Window picker configuration.
15251520

@@ -3009,9 +3004,6 @@ Windows WSL and PowerShell
30093004
- Executable file detection is disabled as this is non-performant and can
30103005
freeze nvim
30113006
- Some filesystem watcher error related to permissions will not be reported
3012-
- Some users have reported unspecified issues with
3013-
|nvim-tree.experimental.actions.open_file.relative_path|. Please report any
3014-
issues or disable this feature.
30153007

30163008
==============================================================================
30173009
13. NETRW *nvim-tree-netrw*
@@ -3146,7 +3138,6 @@ highlight group is not, hard linking as follows: >
31463138
|nvim-tree.diagnostics.show_on_open_dirs|
31473139
|nvim-tree.disable_netrw|
31483140
|nvim-tree.experimental|
3149-
|nvim-tree.experimental.actions.open_file.relative_path|
31503141
|nvim-tree.filesystem_watchers.debounce_delay|
31513142
|nvim-tree.filesystem_watchers.enable|
31523143
|nvim-tree.filesystem_watchers.ignore_dirs|

lua/nvim-tree.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
485485
},
486486
},
487487
experimental = {
488+
multi_instance_debug = false,
488489
},
489490
log = {
490491
enable = false,
@@ -496,7 +497,6 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
496497
dev = false,
497498
diagnostics = false,
498499
git = false,
499-
lifecycle = false,
500500
profile = false,
501501
watcher = false,
502502
},
@@ -726,6 +726,7 @@ function M.setup(conf)
726726
require("nvim-tree.buffers").setup(opts)
727727
require("nvim-tree.help").setup(opts)
728728
require("nvim-tree.watcher").setup(opts)
729+
require("nvim-tree.multi-instance-debug").setup(opts)
729730

730731
setup_autocommands(opts)
731732

lua/nvim-tree/explorer/init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,13 +600,13 @@ end
600600
---Log a lifecycle message with uid_explorer and absolute_path
601601
---@param msg string?
602602
function Explorer:log_new(msg)
603-
log.line("lifecycle", "+ %-15s %d %s", msg, self.uid_explorer, self.absolute_path)
603+
log.line("dev", "+ %-15s %d %s", msg, self.uid_explorer, self.absolute_path)
604604
end
605605

606606
---Log a lifecycle message with uid_explorer and absolute_path
607607
---@param msg string?
608608
function Explorer:log_destroy(msg)
609-
log.line("lifecycle", "- %-15s %d %s", msg, self.uid_explorer, self.absolute_path)
609+
log.line("dev", "- %-15s %d %s", msg, self.uid_explorer, self.absolute_path)
610610
end
611611

612612
function Explorer:setup(opts)

lua/nvim-tree/explorer/view.lua

Lines changed: 21 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local events = require("nvim-tree.events")
33
local utils = require("nvim-tree.utils")
44
local log = require("nvim-tree.log")
55
local notify = require("nvim-tree.notify")
6+
local globals = require("nvim-tree.globals")
67

78
local Class = require("nvim-tree.classic")
89

@@ -11,26 +12,17 @@ local Class = require("nvim-tree.classic")
1112
---@field resize boolean|nil default true
1213
---@field winid number|nil 0 or nil for current
1314

14-
local M = {}
15-
1615
local DEFAULT_MIN_WIDTH = 30
1716
local DEFAULT_MAX_WIDTH = -1
1817
local DEFAULT_PADDING = 1
1918

20-
-- TODO global, rework for multiinstance explorer
21-
-- M.View retained for simpler change history
22-
M.View = {
23-
tabpages = {}
24-
}
25-
2619
---@class (exact) View: Class
2720
---@field live_filter table
2821
---@field side string
2922
---@field float table
3023
---@field private explorer Explorer
3124
---@field private adaptive_size boolean
3225
---@field private centralize_selection boolean
33-
---@field private cursors table<integer, integer[]> as per vim.api.nvim_win_get_cursor
3426
---@field private hide_root_folder boolean
3527
---@field private winopts table
3628
---@field private height integer
@@ -39,7 +31,6 @@ M.View = {
3931
---@field private width (fun():integer)|integer|string
4032
---@field private max_width integer
4133
---@field private padding integer
42-
---@field tab_line fun():string
4334
local View = Class:extend()
4435

4536
---@class View
@@ -56,7 +47,6 @@ function View:new(args)
5647
self.explorer = args.explorer
5748
self.adaptive_size = false
5849
self.centralize_selection = self.explorer.opts.view.centralize_selection
59-
self.cursors = {}
6050
self.float = self.explorer.opts.view.float
6151
self.height = self.explorer.opts.view.height
6252
self.hide_root_folder = self.explorer.opts.renderer.root_folder_label == false
@@ -99,10 +89,6 @@ local tabinitial = {
9989
winnr = nil,
10090
}
10191

102-
-- TODO global, rework for multiinstance explorer
103-
---@type table<integer, integer>
104-
local BUFNR_PER_TAB = {}
105-
10692
---@type { name: string, value: any }[]
10793
local BUFFER_OPTIONS = {
10894
{ name = "bufhidden", value = "wipe" },
@@ -117,7 +103,7 @@ local BUFFER_OPTIONS = {
117103
---@param bufnr integer
118104
---@return boolean
119105
function View:matches_bufnr(bufnr)
120-
for _, b in pairs(BUFNR_PER_TAB) do
106+
for _, b in pairs(globals.BUFNR_PER_TAB) do
121107
if b == bufnr then
122108
return true
123109
end
@@ -140,7 +126,7 @@ function View:create_buffer(bufnr)
140126
self:wipe_rogue_buffer()
141127

142128
local tab = vim.api.nvim_get_current_tabpage()
143-
BUFNR_PER_TAB[tab] = bufnr or vim.api.nvim_create_buf(false, false)
129+
globals.BUFNR_PER_TAB[tab] = bufnr or vim.api.nvim_create_buf(false, false)
144130
vim.api.nvim_buf_set_name(self:get_bufnr(), "NvimTree_" .. tab)
145131

146132
bufnr = self:get_bufnr()
@@ -187,7 +173,7 @@ local move_tbl = {
187173
---@param tabpage integer
188174
function View:setup_tabpage(tabpage)
189175
local winnr = vim.api.nvim_get_current_win()
190-
M.View.tabpages[tabpage] = vim.tbl_extend("force", M.View.tabpages[tabpage] or tabinitial, { winnr = winnr })
176+
globals.TABPAGES[tabpage] = vim.tbl_extend("force", globals.TABPAGES[tabpage] or tabinitial, { winnr = winnr })
191177
end
192178

193179
---@private
@@ -275,7 +261,7 @@ end
275261
---@param tabnr integer
276262
function View:save_tab_state(tabnr)
277263
local tabpage = tabnr or vim.api.nvim_get_current_tabpage()
278-
self.cursors[tabpage] = vim.api.nvim_win_get_cursor(self:get_winnr(tabpage) or 0)
264+
globals.CURSORS[tabpage] = vim.api.nvim_win_get_cursor(self:get_winnr(tabpage) or 0)
279265
end
280266

281267
---@private
@@ -311,7 +297,7 @@ function View:close_this_tab_only()
311297
end
312298

313299
function View:close_all_tabs()
314-
for tabpage, _ in pairs(M.View.tabpages) do
300+
for tabpage, _ in pairs(globals.TABPAGES) do
315301
self:close_internal(tabpage)
316302
end
317303
end
@@ -450,7 +436,7 @@ end
450436
---@private
451437
function View:set_current_win()
452438
local current_tab = vim.api.nvim_get_current_tabpage()
453-
M.View.tabpages[current_tab].winnr = vim.api.nvim_get_current_win()
439+
globals.TABPAGES[current_tab].winnr = vim.api.nvim_get_current_win()
454440
end
455441

456442
---Open the tree in the a window
@@ -474,17 +460,17 @@ end
474460

475461
function View:abandon_current_window()
476462
local tab = vim.api.nvim_get_current_tabpage()
477-
BUFNR_PER_TAB[tab] = nil
478-
if M.View.tabpages[tab] then
479-
M.View.tabpages[tab].winnr = nil
463+
globals.BUFNR_PER_TAB[tab] = nil
464+
if globals.TABPAGES[tab] then
465+
globals.TABPAGES[tab].winnr = nil
480466
end
481467
end
482468

483469
function View:abandon_all_windows()
484470
for tab, _ in pairs(vim.api.nvim_list_tabpages()) do
485-
BUFNR_PER_TAB[tab] = nil
486-
if M.View.tabpages[tab] then
487-
M.View.tabpages[tab].winnr = nil
471+
globals.BUFNR_PER_TAB[tab] = nil
472+
if globals.TABPAGES[tab] then
473+
globals.TABPAGES[tab].winnr = nil
488474
end
489475
end
490476
end
@@ -493,15 +479,15 @@ end
493479
---@return boolean
494480
function View:is_visible(opts)
495481
if opts and opts.tabpage then
496-
if M.View.tabpages[opts.tabpage] == nil then
482+
if globals.TABPAGES[opts.tabpage] == nil then
497483
return false
498484
end
499-
local winnr = M.View.tabpages[opts.tabpage].winnr
485+
local winnr = globals.TABPAGES[opts.tabpage].winnr
500486
return winnr and vim.api.nvim_win_is_valid(winnr)
501487
end
502488

503489
if opts and opts.any_tabpage then
504-
for _, v in pairs(M.View.tabpages) do
490+
for _, v in pairs(globals.TABPAGES) do
505491
if v.winnr and vim.api.nvim_win_is_valid(v.winnr) then
506492
return true
507493
end
@@ -555,15 +541,15 @@ end
555541
--- Restores the state of a NvimTree window if it was initialized before.
556542
function View:restore_tab_state()
557543
local tabpage = vim.api.nvim_get_current_tabpage()
558-
self:set_cursor(self.cursors[tabpage])
544+
self:set_cursor(globals.CURSORS[tabpage])
559545
end
560546

561547
--- Returns the window number for nvim-tree within the tabpage specified
562548
---@param tabpage number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage.
563549
---@return number|nil
564550
function View:get_winnr(tabpage)
565551
tabpage = tabpage or vim.api.nvim_get_current_tabpage()
566-
local tabinfo = M.View.tabpages[tabpage]
552+
local tabinfo = globals.TABPAGES[tabpage]
567553
if tabinfo and tabinfo.winnr and vim.api.nvim_win_is_valid(tabinfo.winnr) then
568554
return tabinfo.winnr
569555
end
@@ -572,7 +558,7 @@ end
572558
--- Returns the current nvim tree bufnr
573559
---@return number
574560
function View:get_bufnr()
575-
return BUFNR_PER_TAB[vim.api.nvim_get_current_tabpage()]
561+
return globals.BUFNR_PER_TAB[vim.api.nvim_get_current_tabpage()]
576562
end
577563

578564
function View:prevent_buffer_override()
@@ -589,9 +575,9 @@ function View:prevent_buffer_override()
589575
local bufname = vim.api.nvim_buf_get_name(curbuf)
590576

591577
if not bufname:match("NvimTree") then
592-
for i, tabpage in ipairs(M.View.tabpages) do
578+
for i, tabpage in ipairs(globals.TABPAGES) do
593579
if tabpage.winnr == view_winnr then
594-
M.View.tabpages[i] = nil
580+
globals.TABPAGES[i] = nil
595581
break
596582
end
597583
end
@@ -665,102 +651,4 @@ function View:configure_width(width)
665651
end
666652
end
667653

668-
--- Debugging only.
669-
--- Tabs show TABPAGES winnr and BUFNR_PER_TAB bufnr for the tab.
670-
--- Orphans for inexistent tab_ids are shown at the right.
671-
--- lib.target_winid is always shown at the right next to a close button.
672-
--- Enable with:
673-
--- vim.opt.tabline = "%!v:lua.require('nvim-tree.explorer.view').tab_line()"
674-
--- vim.opt.showtabline = 2
675-
---@return string
676-
function View.tab_line()
677-
local tab_ids = vim.api.nvim_list_tabpages()
678-
local cur_tab_id = vim.api.nvim_get_current_tabpage()
679-
680-
local bufnr_per_tab = vim.deepcopy(BUFNR_PER_TAB)
681-
local tabpages = vim.deepcopy(M.View.tabpages)
682-
683-
local tl = "%#TabLine#"
684-
685-
for i, tab_id in ipairs(tab_ids) do
686-
-- click to select
687-
tl = tl .. "%" .. i .. "T"
688-
689-
-- style
690-
if tab_id == cur_tab_id then
691-
tl = tl .. "%#StatusLine#|"
692-
else
693-
tl = tl .. "|%#TabLine#"
694-
end
695-
696-
-- tab_id itself
697-
tl = tl .. " t" .. tab_id
698-
699-
-- winnr, if present
700-
local tp = M.View.tabpages[tab_id]
701-
if tp then
702-
tl = tl .. " w" .. tp.winnr
703-
else
704-
tl = tl .. " "
705-
end
706-
707-
-- bufnr, if present
708-
local bpt = BUFNR_PER_TAB[tab_id]
709-
if bpt then
710-
tl = tl .. " b" .. bpt
711-
else
712-
tl = tl .. " "
713-
end
714-
715-
tl = tl .. " "
716-
717-
-- remove actively mapped
718-
bufnr_per_tab[tab_id] = nil
719-
tabpages[tab_id] = nil
720-
end
721-
722-
-- close last and reset
723-
tl = tl .. "|%#CursorLine#%T"
724-
725-
-- collect orphans
726-
local orphans = {}
727-
for tab_id, bufnr in pairs(bufnr_per_tab) do
728-
orphans[tab_id] = orphans[tab_id] or {}
729-
orphans[tab_id].bufnr = bufnr
730-
end
731-
for tab_id, tp in pairs(tabpages) do
732-
orphans[tab_id] = orphans[tab_id] or {}
733-
orphans[tab_id].winnr = tp.winnr
734-
end
735-
736-
-- right-align
737-
tl = tl .. "%=%#TabLine#"
738-
739-
-- print orphans
740-
for tab_id, orphan in pairs(orphans) do
741-
-- inexistent tab
742-
tl = tl .. "%#error#| t" .. tab_id
743-
744-
-- maybe winnr
745-
if orphan.winnr then
746-
tl = tl .. " w" .. orphan.winnr
747-
else
748-
tl = tl .. " "
749-
end
750-
751-
-- maybe bufnr
752-
if orphan.bufnr then
753-
tl = tl .. " b" .. orphan.bufnr
754-
else
755-
tl = tl .. " "
756-
end
757-
tl = tl .. " "
758-
end
759-
760-
-- target win id and close button
761-
tl = tl .. "|%#TabLine# twi" .. (require("nvim-tree.lib").target_winid or "?") .. " %999X| X |"
762-
763-
return tl
764-
end
765-
766654
return View

lua/nvim-tree/globals.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- global state, to be refactored away during multi-instance
2+
3+
local M = {
4+
-- from View
5+
TABPAGES = {},
6+
BUFNR_PER_TAB = {},
7+
CURSORS = {},
8+
}
9+
10+
return M

0 commit comments

Comments
 (0)