Skip to content

Commit bca6ed8

Browse files
committed
vim: nvim-tree: track bufnr via buffer-update channel
1 parent 3f13193 commit bca6ed8

File tree

5 files changed

+53
-12
lines changed

5 files changed

+53
-12
lines changed

lua/nvim-tree.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ function M.purge_all_state()
668668
local explorer = core.get_explorer()
669669
if explorer then
670670
explorer.view:close_all_tabs()
671-
explorer.view:abandon_all_windows()
671+
explorer.view:abandon_all_windows("purge_all_state")
672672
require("nvim-tree.git").purge_state()
673673
explorer:destroy()
674674
core.reset_explorer()

lua/nvim-tree/actions/node/open-file.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ local function edit_in_current_buf(filename)
389389
local explorer = core.get_explorer()
390390

391391
if explorer then
392-
explorer.view:abandon_current_window()
392+
explorer.view:abandon_current_window("open-file.edit_in_current_buf")
393393
end
394394

395395
if M.relative_path then

lua/nvim-tree/explorer/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ function Explorer:create_autocmds()
176176
if self.opts.actions.open_file.eject then
177177
self.view:prevent_buffer_override()
178178
else
179-
self.view:abandon_current_window()
179+
self.view:abandon_current_window("BufWipeout")
180180
end
181181
end,
182182
})

lua/nvim-tree/explorer/view.lua

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,18 @@ function View:create_buffer(bufnr)
129129

130130
local tab = vim.api.nvim_get_current_tabpage()
131131
globals.BUFNR_PER_TAB[tab] = bufnr or vim.api.nvim_create_buf(false, false)
132-
self.bufnr_by_tab[tab] = globals.BUFNR_PER_TAB[tab]
132+
133+
if self.explorer.opts.experimental.multi_instance then
134+
local tabid = tab
135+
self.bufnr_by_tab[tabid] = globals.BUFNR_PER_TAB[tabid]
136+
log.line("dev", "buffer channel attaching t%d b%d", tabid, self.bufnr_by_tab[tabid])
137+
vim.api.nvim_buf_attach(self.bufnr_by_tab[tabid], true, {
138+
on_detach = function(op, bnr)
139+
log.line("dev", "buffer channel %s t%d b%d", op, tabid, bnr)
140+
end,
141+
})
142+
end
143+
133144
vim.api.nvim_buf_set_name(self:get_bufnr("View:create_buffer1"), "NvimTree_" .. tab)
134145

135146
bufnr = self:get_bufnr("View:create_buffer2")
@@ -447,6 +458,7 @@ function View:reposition_window()
447458
end
448459

449460
---@private
461+
---@param callsite string
450462
function View:set_current_win(callsite)
451463
local current_tab = vim.api.nvim_get_current_tabpage()
452464
local current_win = vim.api.nvim_get_current_win()
@@ -483,19 +495,44 @@ function View:open_in_win(opts)
483495
events._dispatch_on_tree_open()
484496
end
485497

486-
function View:abandon_current_window()
498+
---@param callsite string
499+
function View:abandon_current_window(callsite)
487500
local tab = vim.api.nvim_get_current_tabpage()
501+
502+
if self.explorer.opts.experimental.multi_instance then
503+
log.line("dev", "View:abandon_current_window(%-20.20s) t%d w%s b%s member b%s %s",
504+
callsite,
505+
tab,
506+
globals.TABPAGES and globals.TABPAGES.winnr or nil,
507+
globals.BUFNR_PER_TAB[tab],
508+
self.bufnr_by_tab[tab],
509+
(globals.BUFNR_PER_TAB[tab] == self.bufnr_by_tab[tab]) and "" or "MISMATCH")
510+
end
511+
512+
-- TODO multi-instance kill the buffer instead of retaining
513+
488514
globals.BUFNR_PER_TAB[tab] = nil
489-
self.bufnr_by_tab[tab] = nil
490515
if globals.TABPAGES[tab] then
491516
globals.TABPAGES[tab].winnr = nil
492517
end
493518
end
494519

495-
function View:abandon_all_windows()
520+
---@param callsite string
521+
function View:abandon_all_windows(callsite)
496522
for tab, _ in pairs(vim.api.nvim_list_tabpages()) do
523+
if self.explorer.opts.experimental.multi_instance then
524+
log.line("dev", "View:abandon_all_windows(%-20.20s) t%d w%s b%s member b%s %s",
525+
callsite,
526+
tab,
527+
globals.TABPAGES and globals.TABPAGES.winnr or nil,
528+
globals.BUFNR_PER_TAB[tab],
529+
self.bufnr_by_tab[tab],
530+
(globals.BUFNR_PER_TAB[tab] == self.bufnr_by_tab[tab]) and "" or "MISMATCH")
531+
end
532+
533+
-- TODO multi-instance kill the buffer instead of retaining
534+
497535
globals.BUFNR_PER_TAB[tab] = nil
498-
self.bufnr_by_tab[tab] = nil
499536
if globals.TABPAGES[tab] then
500537
globals.TABPAGES[tab].winnr = nil
501538
end
@@ -573,7 +610,7 @@ end
573610

574611
--- Returns the window number for nvim-tree within the tabpage specified
575612
---@param tabpage number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage.
576-
---@param callsite string for logging purposes
613+
---@param callsite string
577614
---@return number|nil
578615
function View:get_winnr(tabpage, callsite)
579616
if self.explorer.opts.experimental.multi_instance then
@@ -605,7 +642,11 @@ function View:get_winnr(tabpage, callsite)
605642
end
606643

607644
if ret ~= winid_from_bufnr then
608-
msg = string.format("%s winid_from_bufnr w%s MISMATCH", msg, winid_from_bufnr)
645+
if ret then
646+
msg = string.format("%s winid_from_bufnr w%s MISMATCH", msg, winid_from_bufnr)
647+
else
648+
msg = string.format("%s winid_from_bufnr w%s STALE", msg, winid_from_bufnr)
649+
end
609650
end
610651

611652
log.line("dev", "%s", msg)

lua/nvim-tree/multi-instance-debug.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function M.tab_line()
3636
-- winnr, if present
3737
local tp = globals.TABPAGES[tab_id]
3838
if tp then
39-
tl = tl .. " w" .. tp.winnr
39+
tl = tl .. " w" .. (tp.winnr or "nil")
4040
else
4141
tl = tl .. " "
4242
end
@@ -80,7 +80,7 @@ function M.tab_line()
8080

8181
-- maybe winnr
8282
if orphan.winnr then
83-
tl = tl .. " w" .. orphan.winnr
83+
tl = tl .. " w" .. (orphan.winnr or "nil")
8484
else
8585
tl = tl .. " "
8686
end

0 commit comments

Comments
 (0)