Skip to content

Commit d72f85f

Browse files
committed
refactor(#2826): more consistency checking
1 parent e875f15 commit d72f85f

File tree

2 files changed

+87
-33
lines changed

2 files changed

+87
-33
lines changed

lua/nvim-tree/explorer/init.lua

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@ function Explorer:destroy()
8585
self.sorters:destroy()
8686
self.view:destroy()
8787

88-
-- TODO existing buffer is retained by global and re-used. Delete it or retain it.
89-
-- see wipe_rogue_buffer
90-
9188
vim.api.nvim_del_augroup_by_id(self.augroup_id)
9289

9390
RootNode.destroy(self)

lua/nvim-tree/explorer/view.lua

Lines changed: 87 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ local View = Class:extend()
3232
function View:new(args)
3333
args.explorer:log_new("View")
3434

35-
self.explorer = args.explorer
36-
self.adaptive_size = false
37-
self.side = (self.explorer.opts.view.side == "right") and "right" or "left"
38-
self.live_filter = { prev_focused_node = nil, }
39-
self.bufnr_by_tabid = {}
35+
self.explorer = args.explorer
36+
self.adaptive_size = false
37+
self.side = (self.explorer.opts.view.side == "right") and "right" or "left"
38+
self.live_filter = { prev_focused_node = nil, }
39+
self.bufnr_by_tabid = {}
4040

41-
self.winopts = {
41+
self.winopts = {
4242
relativenumber = self.explorer.opts.view.relativenumber,
4343
number = self.explorer.opts.view.number,
4444
list = false,
@@ -246,10 +246,13 @@ end
246246
---@private
247247
---@param tabid integer
248248
function View:close_internal(tabid)
249+
--- BEGIN multi-instance FF
249250
if self.explorer.opts.experimental.multi_instance then
250251
log.line("dev", "View:close_internal(t%s)", tabid)
251252
end
252-
if not self:is_visible({ tabpage = tabid }) then
253+
--- END multi-instance FF
254+
255+
if not self:is_visible({ tabpage = tabid }, "View:close_internal") then
253256
return
254257
end
255258
self:save_tab_state(tabid)
@@ -263,9 +266,12 @@ function View:close_internal(tabid)
263266
vim.api.nvim_set_current_win(vim.fn.win_getid(prev_win))
264267
end
265268
if vim.api.nvim_win_is_valid(tree_win or 0) then
269+
--- BEGIN multi-instance FF
266270
if self.explorer.opts.experimental.multi_instance then
267271
log.line("dev", "View:close_internal(t%s) w%s", tabid, tree_win)
268272
end
273+
--- END multi-instance FF
274+
---
269275
local success, error = pcall(vim.api.nvim_win_close, tree_win or 0, true)
270276
if not success then
271277
notify.debug("Failed to close window: " .. error)
@@ -278,29 +284,37 @@ function View:close_internal(tabid)
278284
end
279285

280286
function View:close_this_tab_only()
287+
--- BEGIN multi-instance FF
281288
if self.explorer.opts.experimental.multi_instance then
282289
log.line("dev", "View:close_this_tab_only()")
283290
end
291+
--- END multi-instance FF
292+
284293
self:close_internal(vim.api.nvim_get_current_tabpage())
285294
end
286295

287296
-- TODO this is broken at 1.13.0 - current tab does not close when tab.sync.close is set
288297
function View:close_all_tabs()
289298
log.line("dev", "View:close_all_tabs() globals.WINID_BY_TABID=%s", vim.inspect(globals.WINID_BY_TABID))
290299
for tabid, _ in pairs(globals.WINID_BY_TABID) do
300+
--- BEGIN multi-instance FF
291301
if self.explorer.opts.experimental.multi_instance then
292302
log.line("dev", "View:close_all_tabs()")
293303
end
304+
--- END multi-instance FF
305+
294306
self:close_internal(tabid)
295307
end
296308
end
297309

298310
---@param tabid integer|nil
299311
---@param callsite string
300312
function View:close(tabid, callsite)
313+
--- BEGIN multi-instance FF
301314
if self.explorer.opts.experimental.multi_instance then
302315
log.line("dev", "View:close(t%s, %s)", tabid, callsite)
303316
end
317+
--- END multi-instance FF
304318

305319
if self.explorer.opts.tab.sync.close then
306320
self:close_all_tabs()
@@ -313,7 +327,7 @@ end
313327

314328
---@param options table|nil
315329
function View:open(options)
316-
if self:is_visible() then
330+
if self:is_visible(nil, "View:open") then
317331
return
318332
end
319333

@@ -405,7 +419,7 @@ function View:resize(size)
405419
self.width = size
406420
end
407421

408-
if not self:is_visible() then
422+
if not self:is_visible(nil, "View:resize") then
409423
return
410424
end
411425

@@ -463,6 +477,7 @@ end
463477
function View:abandon_current_window()
464478
local tab = vim.api.nvim_get_current_tabpage()
465479

480+
--- BEGIN multi-instance FF
466481
if self.explorer.opts.experimental.multi_instance then
467482
log.line("dev", "View:abandon_current_window() t%d w%s b%s member b%s %s",
468483
tab,
@@ -471,8 +486,7 @@ function View:abandon_current_window()
471486
self.bufnr_by_tabid[tab],
472487
(globals.BUFNR_BY_TABID[tab] == self.bufnr_by_tabid[tab]) and "" or "MISMATCH")
473488
end
474-
475-
-- TODO multi-instance maybe kill the buffer instead of retaining
489+
--- END multi-instance FF
476490

477491
-- reset both bufnr registries
478492
globals.BUFNR_BY_TABID[tab] = nil
@@ -482,18 +496,42 @@ function View:abandon_current_window()
482496
end
483497

484498
function View:abandon_all_windows()
485-
-- TODO multi-instance kill the buffer instead of retaining
486499
for tab, _ in pairs(vim.api.nvim_list_tabpages()) do
487500
globals.BUFNR_BY_TABID[tab] = nil
488501
globals.WINID_BY_TABID[tab] = nil
489502
end
490503
end
491504

492505
---@param opts table|nil
506+
---@param callsite string
493507
---@return boolean
494-
function View:is_visible(opts)
495-
-- TODO multi-instance rewrite and consistency check
508+
function View:is_visible(opts, callsite)
509+
local msg
510+
511+
--- BEGIN multi-instance FF
512+
if self.explorer.opts.experimental.multi_instance then
513+
msg = string.format("View:is_visible(%s, %-20.20s)", vim.inspect(opts, { newline = "" }), callsite)
514+
end
515+
--- END multi-instance FF
516+
496517
if opts and opts.tabpage then
518+
--- BEGIN multi-instance FF
519+
if self.explorer.opts.experimental.multi_instance then
520+
local winid = self:winid(opts.tabpage)
521+
local winid_by_tabid = opts.tabpage and globals.WINID_BY_TABID[opts.tabpage] or nil
522+
msg = string.format("%s globals.WINID_BY_TABID[%s]=w%s view.winid(%s)=w%s",
523+
msg,
524+
opts.tabpage, winid_by_tabid,
525+
opts.tabpage, winid
526+
)
527+
if winid ~= winid_by_tabid then
528+
msg = string.format("%s MISMATCH", msg)
529+
notify.error(msg)
530+
end
531+
log.line("dev", "%s", msg)
532+
end
533+
--- END multi-instance FF
534+
497535
if not globals.WINID_BY_TABID[opts.tabpage] then
498536
return false
499537
end
@@ -502,8 +540,24 @@ function View:is_visible(opts)
502540
end
503541

504542
if opts and opts.any_tabpage then
505-
for _, winid in pairs(globals.WINID_BY_TABID) do
506-
if winid and vim.api.nvim_win_is_valid(winid) then
543+
for tabid, winid_by_tabid in pairs(globals.WINID_BY_TABID) do
544+
--- BEGIN multi-instance FF
545+
if self.explorer.opts.experimental.multi_instance then
546+
local winid = self:winid(tabid)
547+
msg = string.format("%s globals.WINID_BY_TABID[%s]=w%s view.winid(%s)=w%s",
548+
msg,
549+
tabid, winid_by_tabid,
550+
tabid, winid
551+
)
552+
if winid ~= winid_by_tabid then
553+
msg = string.format("%s MISMATCH", msg)
554+
notify.error(msg)
555+
end
556+
log.line("dev", "%s", msg)
557+
end
558+
--- END multi-instance FF
559+
560+
if winid_by_tabid and vim.api.nvim_win_is_valid(winid_by_tabid) then
507561
return true
508562
end
509563
end
@@ -516,7 +570,7 @@ end
516570

517571
---@param opts table|nil
518572
function View:set_cursor(opts)
519-
if self:is_visible() then
573+
if self:is_visible(nil, "View:set_cursor") then
520574
pcall(vim.api.nvim_win_set_cursor, self:get_winid(nil, "View:set_cursor"), opts)
521575
end
522576
end
@@ -530,7 +584,7 @@ function View:focus(winid, open_if_closed)
530584
self:close(nil, "View:focus")
531585
self:open()
532586
wid = self:get_winid(nil, "View:focus2")
533-
elseif open_if_closed and not self:is_visible() then
587+
elseif open_if_closed and not self:is_visible(nil, "View:focus") then
534588
self:open()
535589
end
536590

@@ -547,7 +601,7 @@ function View:api_winid(opts)
547601
if tabpage == 0 then
548602
tabpage = vim.api.nvim_get_current_tabpage()
549603
end
550-
if self:is_visible({ tabpage = tabpage }) then
604+
if self:is_visible({ tabpage = tabpage }, "View:api_winid") then
551605
return self:get_winid(tabpage, "View:winid")
552606
else
553607
return nil
@@ -559,8 +613,6 @@ function View:restore_tab_state()
559613
self:set_cursor(globals.CURSORS[vim.api.nvim_get_current_tabpage()])
560614
end
561615

562-
--- TODO multi-instance remove comment
563-
--- not legacy codepath
564616
--- winid containing the buffer
565617
---@param tabid number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage.
566618
---@return integer? winid
@@ -583,42 +635,44 @@ end
583635
function View:get_winid(tabid, callsite)
584636
local tabid_param = tabid
585637
tabid = tabid or vim.api.nvim_get_current_tabpage()
586-
local tabinfo_winid = nil
638+
local global_winid = nil
587639

640+
--- BEGIN multi-instance FF
588641
if self.explorer.opts.experimental.multi_instance then
589642
local msg_fault = ""
590643
if not globals.WINID_BY_TABID[tabid] then
591644
msg_fault = "no WINID_BY_TABID"
592645
elseif not vim.api.nvim_win_is_valid(globals.WINID_BY_TABID[tabid]) then
593646
msg_fault = string.format("invalid globals.WINID_BY_TABID[tabid] %d", globals.WINID_BY_TABID[tabid])
594647
else
595-
tabinfo_winid = globals.WINID_BY_TABID[tabid]
648+
global_winid = globals.WINID_BY_TABID[tabid]
596649
end
597650

598651
local winid = self:winid(tabid)
599652

600-
if winid ~= tabinfo_winid then
653+
if winid ~= global_winid then
601654
msg_fault = "MISMATCH"
602655
end
603656

604-
local msg = string.format("View:get_winid(%3s, %-20.20s) globals.TABPAGES[%s]=w%s view.winid(%s)=w%s %s",
657+
local msg = string.format("View:get_winid(%3s, %-20.20s) globals.WINID_BY_TABID[%s]=w%s view.winid(%s)=w%s %s",
605658
tabid_param,
606659
callsite,
607-
tabid, tabinfo_winid,
660+
tabid, global_winid,
608661
tabid, winid,
609662
msg_fault
610663
)
611664

612665
log.line("dev", "%s", msg)
613666

614-
if winid ~= tabinfo_winid then
667+
if winid ~= global_winid then
615668
notify.error(msg)
616669
end
617670
end
671+
--- END multi-instance FF
618672

619673
-- legacy codepath
620-
if tabinfo_winid and vim.api.nvim_win_is_valid(tabinfo_winid) then
621-
return tabinfo_winid
674+
if global_winid and vim.api.nvim_win_is_valid(global_winid) then
675+
return global_winid
622676
end
623677
end
624678

@@ -627,6 +681,8 @@ end
627681
---@return number
628682
function View:get_bufnr(callsite)
629683
local tab = vim.api.nvim_get_current_tabpage()
684+
685+
--- BEGIN multi-instance FF
630686
if self.explorer.opts.experimental.multi_instance then
631687
local msg = string.format("View:get_bufnr(%-20.20s) globals.BUFNR_BY_TABID[%s]=b%s view.bufnr_by_tab[%s]=b%s %s",
632688
callsite,
@@ -641,6 +697,8 @@ function View:get_bufnr(callsite)
641697

642698
log.line("dev", msg)
643699
end
700+
--- END multi-instance FF
701+
644702
return globals.BUFNR_BY_TABID[tab]
645703
end
646704

@@ -657,7 +715,6 @@ function View:prevent_buffer_override()
657715
local curbuf = vim.api.nvim_win_get_buf(curwin)
658716
local bufname = vim.api.nvim_buf_get_name(curbuf)
659717

660-
--- TODO multi-instance this can be removed as winid() will handle it
661718
if not bufname:match("NvimTree") then
662719
for i, winid in ipairs(globals.WINID_BY_TABID) do
663720
if winid == view_winid then

0 commit comments

Comments
 (0)