From 6e7ce8771bc41ac90c4f1651cf0018aa2d524e30 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Thu, 19 Jun 2025 16:25:32 +1000 Subject: [PATCH 01/17] refactor(#2826): fuller error messages --- lua/nvim-tree/explorer/view.lua | 70 ++++++++++++++++----------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index 627364a4e40..5afdbb90f87 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -638,45 +638,46 @@ end ---@param callsite string ---@return number|nil function View:get_winnr(tabpage, callsite) - if self.explorer.opts.experimental.multi_instance then - local msg = string.format("View:get_winnr(%3s, %-20.20s)", tabpage, callsite) - - tabpage = tabpage or vim.api.nvim_get_current_tabpage() - local tabinfo = globals.TABPAGES[tabpage] - - local ret = nil + local tabid = tabpage or vim.api.nvim_get_current_tabpage() + local tabinfo = globals.TABPAGES[tabid] + local tabinfo_winid = nil + if self.explorer.opts.experimental.multi_instance then + local msg_fault = "" if not tabinfo then - msg = string.format("%s t%d no tabinfo", msg, tabpage) + msg_fault = "no tabinfo" elseif not tabinfo.winnr then - msg = string.format("%s t%d no tabinfo.winnr", msg, tabpage) + msg_fault = "no tabinfo.winnr" elseif not vim.api.nvim_win_is_valid(tabinfo.winnr) then - msg = string.format("%s t%d invalid tabinfo.winnr %d", msg, tabpage, tabinfo.winnr) + msg_fault = string.format("invalid tabinfo.winnr %d", tabinfo.winnr) else - msg = string.format("%s t%d w%d", msg, tabpage, tabinfo.winnr) - ret = tabinfo.winnr + tabinfo_winid = tabinfo.winnr end - local winid = self:winid(tabpage, "View:get_winnr") - if ret ~= winid then - if ret then - msg = string.format("%s winid_from_bufnr w%s MISMATCH", msg, winid) - else - msg = string.format("%s winid_from_bufnr w%s STALE", msg, winid) - end - notify.error(string.format("View:get_winnr w%s View:winnr w%s MISMATCH", ret, winid)) + local winid = self:winid(tabid, "View:get_winnr") + + if winid ~= tabinfo_winid then + msg_fault = "MISMATCH" end + local msg = string.format("View:get_winnr(%3s, %-20.20s) globals.TABPAGES[%s].winnr=w%s view.winid(%s)=w%s %s", + tabpage, + callsite, + tabid, tabinfo_winid, + tabid, winid, + msg_fault + ) + log.line("dev", "%s", msg) - return ret - else - tabpage = tabpage or vim.api.nvim_get_current_tabpage() - local tabinfo = globals.TABPAGES[tabpage] - if tabinfo and tabinfo.winnr and vim.api.nvim_win_is_valid(tabinfo.winnr) then - return tabinfo.winnr + if winid ~= tabinfo_winid then + notify.error(msg) end end + + if tabinfo and tabinfo.winnr and vim.api.nvim_win_is_valid(tabinfo.winnr) then + return tabinfo.winnr + end end --- Returns the current nvim tree bufnr @@ -685,19 +686,18 @@ end function View:get_bufnr(callsite) local tab = vim.api.nvim_get_current_tabpage() if self.explorer.opts.experimental.multi_instance then - log.line("dev", "View:get_bufnr(%-20.20s) t%d global b%s member b%s %s", + local msg = string.format("View:get_bufnr(%-20.20s) globals.BUFNR_PER_TAB[%s]=b%s view.bufnr_by_tab[%s]=b%s MISMATCH", callsite, - tab, - globals.BUFNR_PER_TAB[tab], - self.bufnr_by_tab[tab], - (globals.BUFNR_PER_TAB[tab] == self.bufnr_by_tab[tab]) and "" or "MISMATCH") + tab, globals.BUFNR_PER_TAB[tab], + tab, self.bufnr_by_tab[tab], + (globals.BUFNR_PER_TAB[tab] == self.bufnr_by_tab[tab]) and "" or "MISMATCH" + ) if globals.BUFNR_PER_TAB[tab] ~= self.bufnr_by_tab[tab] then - notify.error(string.format("View:get_bufnr globals.BUFNR_PER_TAB[%s] b%s view.bufnr_by_tab[%s] b%s MISMATCH", - tab, globals.BUFNR_PER_TAB[tab], - tab, self.bufnr_by_tab[tab] - )) + notify.error(msg) end + + log.line("dev", msg) end return globals.BUFNR_PER_TAB[tab] end From b7e97898502c67839074443be4fe7b4e8cec8df2 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Thu, 19 Jun 2025 16:44:19 +1000 Subject: [PATCH 02/17] refactor(#2826): winnr->winid in view/globals, remove redundant get_winid and get_bufnr calls --- lua/nvim-tree/actions/node/open-file.lua | 2 +- lua/nvim-tree/explorer/init.lua | 2 +- lua/nvim-tree/explorer/view.lua | 101 ++++++++++++----------- lua/nvim-tree/lib.lua | 2 +- lua/nvim-tree/renderer/init.lua | 7 +- 5 files changed, 59 insertions(+), 55 deletions(-) diff --git a/lua/nvim-tree/actions/node/open-file.lua b/lua/nvim-tree/actions/node/open-file.lua index dedd61229e5..b3bcce75a5a 100644 --- a/lua/nvim-tree/actions/node/open-file.lua +++ b/lua/nvim-tree/actions/node/open-file.lua @@ -22,7 +22,7 @@ local function usable_win_ids() local explorer = core.get_explorer() local tabpage = vim.api.nvim_get_current_tabpage() local win_ids = vim.api.nvim_tabpage_list_wins(tabpage) - local tree_winid = explorer and explorer.view:get_winnr(tabpage, "open-file.usable_win_ids") + local tree_winid = explorer and explorer.view:get_winid(tabpage, "open-file.usable_win_ids") return vim.tbl_filter(function(id) local bufid = vim.api.nvim_win_get_buf(id) diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index ca8c6d39103..6625d5b0870 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -554,7 +554,7 @@ end ---nil on no explorer or invalid view win ---@return integer[]|nil function Explorer:get_cursor_position() - local winnr = self.view:get_winnr(nil, "Explorer:get_cursor_position") + local winnr = self.view:get_winid(nil, "Explorer:get_cursor_position") if not winnr or not vim.api.nvim_win_is_valid(winnr) then return end diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index 5afdbb90f87..f0465192322 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -88,7 +88,7 @@ local tabinitial = { -- The position of the cursor { line, column } cursor = { 0, 0 }, -- The NvimTree window number - winnr = nil, + winid = nil, } ---@type { name: string, value: any }[] @@ -134,16 +134,17 @@ function View:create_buffer(bufnr) self.bufnr_by_tab[tab] = globals.BUFNR_PER_TAB[tab] end - vim.api.nvim_buf_set_name(self:get_bufnr("View:create_buffer1"), "NvimTree_" .. tab) + bufnr = self:get_bufnr("View:create_buffer") + + vim.api.nvim_buf_set_name(bufnr, "NvimTree_" .. tab) - bufnr = self:get_bufnr("View:create_buffer2") for _, option in ipairs(BUFFER_OPTIONS) do vim.api.nvim_set_option_value(option.name, option.value, { buf = bufnr }) end - require("nvim-tree.keymap").on_attach(self:get_bufnr("View:create_buffer3")) + require("nvim-tree.keymap").on_attach(bufnr) - events._dispatch_tree_attached_post(self:get_bufnr("View:create_buffer4")) + events._dispatch_tree_attached_post(bufnr) end ---@private @@ -180,17 +181,17 @@ local move_tbl = { ---@param tabpage integer ---@param callsite string function View:setup_tabpage(tabpage, callsite) - local winnr = vim.api.nvim_get_current_win() + local winid = vim.api.nvim_get_current_win() if self.explorer.opts.experimental.multi_instance then log.line("dev", "View:setup_tabpage(%3s, %-20.20s) w%d %s", tabpage, callsite, - winnr, + winid, globals.TABPAGES[tabpage] and vim.inspect(globals.TABPAGES[tabpage], { newline = "" }) or "tabinitial") end - globals.TABPAGES[tabpage] = vim.tbl_extend("force", globals.TABPAGES[tabpage] or tabinitial, { winnr = winnr }) + globals.TABPAGES[tabpage] = vim.tbl_extend("force", globals.TABPAGES[tabpage] or tabinitial, { winid = winid }) end ---@private @@ -278,7 +279,7 @@ end ---@param tabnr integer function View:save_tab_state(tabnr) local tabpage = tabnr or vim.api.nvim_get_current_tabpage() - globals.CURSORS[tabpage] = vim.api.nvim_win_get_cursor(self:get_winnr(tabpage, "View:save_tab_state") or 0) + globals.CURSORS[tabpage] = vim.api.nvim_win_get_cursor(self:get_winid(tabpage, "View:save_tab_state") or 0) end ---@private @@ -292,7 +293,7 @@ function View:close_internal(tabpage) end self:save_tab_state(tabpage) switch_buf_if_last_buf() - local tree_win = self:get_winnr(tabpage, "View:close_internal") + local tree_win = self:get_winid(tabpage, "View:close_internal") local current_win = vim.api.nvim_get_current_win() for _, win in pairs(vim.api.nvim_tabpage_list_wins(tabpage)) do if vim.api.nvim_win_get_config(win).relative == "" then @@ -371,7 +372,7 @@ function View:grow() local padding = self:get_size(self.padding) -- account for sign/number columns etc. - local wininfo = vim.fn.getwininfo(self:get_winnr(nil, "View:grow")) + local wininfo = vim.fn.getwininfo(self:get_winid(nil, "View:grow")) if type(wininfo) == "table" and type(wininfo[1]) == "table" then padding = padding + wininfo[1].textoff end @@ -440,12 +441,12 @@ function View:resize(size) return end - local winnr = self:get_winnr(nil, "View:resize") or 0 + local winid = self:get_winid(nil, "View:resize") or 0 local new_size = self:get_width() - if new_size ~= vim.api.nvim_win_get_width(winnr) then - vim.api.nvim_win_set_width(winnr, new_size) + if new_size ~= vim.api.nvim_win_get_width(winid) then + vim.api.nvim_win_set_width(winid, new_size) if not self.preserve_window_proportions then vim.cmd(":wincmd =") end @@ -471,13 +472,13 @@ function View:set_current_win(callsite) log.line("dev", "View:set_current_win(%-20.20s) t%d w%3s->w%3s %s", callsite, current_tab, - globals.TABPAGES[current_tab].winnr, + globals.TABPAGES[current_tab].winid, current_win, - (globals.TABPAGES[current_tab].winnr == current_win) and "" or "MISMATCH" + (globals.TABPAGES[current_tab].winid == current_win) and "" or "MISMATCH" ) end - globals.TABPAGES[current_tab].winnr = current_win + globals.TABPAGES[current_tab].winid = current_win end ---Open the tree in the a window @@ -505,7 +506,7 @@ function View:abandon_current_window() if self.explorer.opts.experimental.multi_instance then log.line("dev", "View:abandon_current_window() t%d w%s b%s member b%s %s", tab, - globals.TABPAGES[tab] and globals.TABPAGES[tab].winnr or nil, + globals.TABPAGES[tab] and globals.TABPAGES[tab].winid or nil, globals.BUFNR_PER_TAB[tab], self.bufnr_by_tab[tab], (globals.BUFNR_PER_TAB[tab] == self.bufnr_by_tab[tab]) and "" or "MISMATCH") @@ -517,7 +518,7 @@ function View:abandon_current_window() globals.BUFNR_PER_TAB[tab] = nil if globals.TABPAGES[tab] then - globals.TABPAGES[tab].winnr = nil + globals.TABPAGES[tab].winid = nil end end @@ -528,7 +529,7 @@ function View:abandon_all_windows(callsite) log.line("dev", "View:abandon_all_windows(%-20.20s) t%d w%s b%s member b%s %s", callsite, tab, - globals.TABPAGES and globals.TABPAGES.winnr or nil, + globals.TABPAGES and globals.TABPAGES.winid or nil, globals.BUFNR_PER_TAB[tab], self.bufnr_by_tab[tab], (globals.BUFNR_PER_TAB[tab] == self.bufnr_by_tab[tab]) and "" or "MISMATCH") @@ -538,7 +539,7 @@ function View:abandon_all_windows(callsite) globals.BUFNR_PER_TAB[tab] = nil if globals.TABPAGES[tab] then - globals.TABPAGES[tab].winnr = nil + globals.TABPAGES[tab].winid = nil end end end @@ -550,38 +551,39 @@ function View:is_visible(opts) if globals.TABPAGES[opts.tabpage] == nil then return false end - local winnr = globals.TABPAGES[opts.tabpage].winnr - return winnr and vim.api.nvim_win_is_valid(winnr) + local winid = globals.TABPAGES[opts.tabpage].winid + return winid and vim.api.nvim_win_is_valid(winid) end if opts and opts.any_tabpage then for _, v in pairs(globals.TABPAGES) do - if v.winnr and vim.api.nvim_win_is_valid(v.winnr) then + if v.winid and vim.api.nvim_win_is_valid(v.winid) then return true end end return false end - return self:get_winnr(nil, "View:is_visible1") ~= nil and vim.api.nvim_win_is_valid(self:get_winnr(nil, "View:is_visible2") or 0) + local winid = self:get_winid(nil, "View:is_visible") + return winid ~= nil and vim.api.nvim_win_is_valid(winid or 0) end ---@param opts table|nil function View:set_cursor(opts) if self:is_visible() then - pcall(vim.api.nvim_win_set_cursor, self:get_winnr(nil, "View:set_cursor"), opts) + pcall(vim.api.nvim_win_set_cursor, self:get_winid(nil, "View:set_cursor"), opts) end end ----@param winnr number|nil +---@param winid number|nil ---@param open_if_closed boolean|nil -function View:focus(winnr, open_if_closed) - local wnr = winnr or self:get_winnr(nil, "View:focus1") +function View:focus(winid, open_if_closed) + local wnr = winid or self:get_winid(nil, "View:focus1") if vim.api.nvim_win_get_tabpage(wnr or 0) ~= vim.api.nvim_win_get_tabpage(0) then self:close(nil, "View:focus") self:open() - wnr = self:get_winnr(nil, "View:focus2") + wnr = self:get_winid(nil, "View:focus2") elseif open_if_closed and not self:is_visible() then self:open() end @@ -593,14 +595,14 @@ end --- Retrieve the winid of the open tree. ---@param opts ApiTreeWinIdOpts|nil ----@return number|nil winid unlike get_winnr(), this returns nil if the nvim-tree window is not visible +---@return number|nil winid unlike get_winid(), this returns nil if the nvim-tree window is not visible function View:api_winid(opts) local tabpage = opts and opts.tabpage if tabpage == 0 then tabpage = vim.api.nvim_get_current_tabpage() end if self:is_visible({ tabpage = tabpage }) then - return self:get_winnr(tabpage, "View:winid") + return self:get_winid(tabpage, "View:winid") else return nil end @@ -637,7 +639,7 @@ end ---@param tabpage number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage. ---@param callsite string ---@return number|nil -function View:get_winnr(tabpage, callsite) +function View:get_winid(tabpage, callsite) local tabid = tabpage or vim.api.nvim_get_current_tabpage() local tabinfo = globals.TABPAGES[tabid] local tabinfo_winid = nil @@ -646,21 +648,21 @@ function View:get_winnr(tabpage, callsite) local msg_fault = "" if not tabinfo then msg_fault = "no tabinfo" - elseif not tabinfo.winnr then - msg_fault = "no tabinfo.winnr" - elseif not vim.api.nvim_win_is_valid(tabinfo.winnr) then - msg_fault = string.format("invalid tabinfo.winnr %d", tabinfo.winnr) + elseif not tabinfo.winid then + msg_fault = "no tabinfo.winid" + elseif not vim.api.nvim_win_is_valid(tabinfo.winid) then + msg_fault = string.format("invalid tabinfo.winid %d", tabinfo.winid) else - tabinfo_winid = tabinfo.winnr + tabinfo_winid = tabinfo.winid end - local winid = self:winid(tabid, "View:get_winnr") + local winid = self:winid(tabid, "View:get_winid") if winid ~= tabinfo_winid then msg_fault = "MISMATCH" end - local msg = string.format("View:get_winnr(%3s, %-20.20s) globals.TABPAGES[%s].winnr=w%s view.winid(%s)=w%s %s", + local msg = string.format("View:get_winid(%3s, %-20.20s) globals.TABPAGES[%s].winid=w%s view.winid(%s)=w%s %s", tabpage, callsite, tabid, tabinfo_winid, @@ -675,8 +677,9 @@ function View:get_winnr(tabpage, callsite) end end - if tabinfo and tabinfo.winnr and vim.api.nvim_win_is_valid(tabinfo.winnr) then - return tabinfo.winnr + -- legacy codepath + if tabinfo and tabinfo.winid and vim.api.nvim_win_is_valid(tabinfo.winid) then + return tabinfo.winid end end @@ -703,7 +706,7 @@ function View:get_bufnr(callsite) end function View:prevent_buffer_override() - local view_winnr = self:get_winnr(nil, "View:prevent_buffer_override") + local view_winid = self:get_winid(nil, "View:prevent_buffer_override") local view_bufnr = self:get_bufnr("View:prevent_buffer_override") -- need to schedule to let the new buffer populate the window @@ -717,9 +720,9 @@ function View:prevent_buffer_override() if not bufname:match("NvimTree") then for i, tabpage in ipairs(globals.TABPAGES) do - if tabpage.winnr == view_winnr then + if tabpage.winid == view_winid then if self.explorer.opts.experimental.multi_instance then - log.line("dev", "View:prevent_buffer_override() t%d w%d clearing", i, view_winnr) + log.line("dev", "View:prevent_buffer_override() t%d w%d clearing", i, view_winid) end globals.TABPAGES[i] = nil @@ -727,7 +730,7 @@ function View:prevent_buffer_override() end end end - if curwin ~= view_winnr or bufname == "" or curbuf == view_bufnr then + if curwin ~= view_winid or bufname == "" or curbuf == view_bufnr then return end @@ -762,9 +765,9 @@ end -- used on ColorScheme event function View:reset_winhl() - local winnr = self:get_winnr(nil, "View:reset_winhl1") - if winnr and vim.api.nvim_win_is_valid(winnr) then - vim.wo[self:get_winnr(nil, "View:reset_winhl2")].winhl = appearance.WIN_HL + local winid = self:get_winid(nil, "View:reset_winhl1") + if winid and vim.api.nvim_win_is_valid(winid) then + vim.wo[winid].winhl = appearance.WIN_HL end end diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index 20fe23dbdd4..cd6442cc044 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -15,7 +15,7 @@ function M.set_target_win() local id = vim.api.nvim_get_current_win() - if explorer and id == explorer.view:get_winnr(nil, "lib.set_target_win") then + if explorer and id == explorer.view:get_winid(nil, "lib.set_target_win") then M.target_winid = 0 return end diff --git a/lua/nvim-tree/renderer/init.lua b/lua/nvim-tree/renderer/init.lua index f782973726a..99ac61b508b 100644 --- a/lua/nvim-tree/renderer/init.lua +++ b/lua/nvim-tree/renderer/init.lua @@ -105,24 +105,25 @@ function Renderer:draw() if not bufnr or not vim.api.nvim_buf_is_loaded(bufnr) then return end + local winid = self.explorer.view:get_winid(nil, "Renderer:draw") local profile = log.profile_start("draw") - local cursor = vim.api.nvim_win_get_cursor(self.explorer.view:get_winnr(nil, "Renderer:draw1") or 0) + local cursor = vim.api.nvim_win_get_cursor(winid or 0) local builder = Builder(self.explorer):build() self:_draw(bufnr, builder.lines, builder.hl_range_args, builder.signs, builder.extmarks, builder.virtual_lines) if cursor and #builder.lines >= cursor[1] then - vim.api.nvim_win_set_cursor(self.explorer.view:get_winnr(nil, "Renderer:draw2") or 0, cursor) + vim.api.nvim_win_set_cursor(winid or 0, cursor) end self.explorer.view:grow_from_content() log.profile_end(profile) - events._dispatch_on_tree_rendered(bufnr, self.explorer.view:get_winnr(nil, "Renderer:draw3")) + events._dispatch_on_tree_rendered(bufnr, winid) end return Renderer From d6cd465462890e473e8ace7cce5f9bcc14cdbe8b Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Thu, 19 Jun 2025 16:46:27 +1000 Subject: [PATCH 03/17] refactor(#2826): winnr->winid consistently --- lua/nvim-tree/explorer/view.lua | 10 +++++----- lua/nvim-tree/multi-instance-debug.lua | 14 +++++++------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index f0465192322..896a5b666ec 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -578,18 +578,18 @@ end ---@param winid number|nil ---@param open_if_closed boolean|nil function View:focus(winid, open_if_closed) - local wnr = winid or self:get_winid(nil, "View:focus1") + local wid = winid or self:get_winid(nil, "View:focus1") - if vim.api.nvim_win_get_tabpage(wnr or 0) ~= vim.api.nvim_win_get_tabpage(0) then + if vim.api.nvim_win_get_tabpage(wid or 0) ~= vim.api.nvim_win_get_tabpage(0) then self:close(nil, "View:focus") self:open() - wnr = self:get_winid(nil, "View:focus2") + wid = self:get_winid(nil, "View:focus2") elseif open_if_closed and not self:is_visible() then self:open() end - if wnr then - vim.api.nvim_set_current_win(wnr) + if wid then + vim.api.nvim_set_current_win(wid) end end diff --git a/lua/nvim-tree/multi-instance-debug.lua b/lua/nvim-tree/multi-instance-debug.lua index 5b4604837cd..d33f34a1313 100644 --- a/lua/nvim-tree/multi-instance-debug.lua +++ b/lua/nvim-tree/multi-instance-debug.lua @@ -3,7 +3,7 @@ local globals = require("nvim-tree.globals") local M = {} --- Debugging only. ---- Tabs show TABPAGES winnr and BUFNR_PER_TAB bufnr for the tab. +--- Tabs show TABPAGES winid and BUFNR_PER_TAB bufnr for the tab. --- Orphans for inexistent tab_ids are shown at the right. --- lib.target_winid is always shown at the right next to a close button. --- Enable with: @@ -33,10 +33,10 @@ function M.tab_line() -- tab_id itself tl = tl .. " t" .. tab_id - -- winnr, if present + -- winid, if present local tp = globals.TABPAGES[tab_id] if tp then - tl = tl .. " w" .. (tp.winnr or "nil") + tl = tl .. " w" .. (tp.winid or "nil") else tl = tl .. " " end @@ -67,7 +67,7 @@ function M.tab_line() end for tab_id, tp in pairs(tabpages) do orphans[tab_id] = orphans[tab_id] or {} - orphans[tab_id].winnr = tp.winnr + orphans[tab_id].winid = tp.winid end -- right-align @@ -78,9 +78,9 @@ function M.tab_line() -- inexistent tab tl = tl .. "%#error#| t" .. tab_id - -- maybe winnr - if orphan.winnr then - tl = tl .. " w" .. (orphan.winnr or "nil") + -- maybe winid + if orphan.winid then + tl = tl .. " w" .. (orphan.winid or "nil") else tl = tl .. " " end From de2ae0b06f447d75e8c1d6434a40b404dc394090 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Fri, 20 Jun 2025 08:32:40 +1000 Subject: [PATCH 04/17] refactor(#2826): consistent use of buffer registry, tidy, add todos --- lua/nvim-tree/explorer/init.lua | 3 +++ lua/nvim-tree/explorer/view.lua | 43 ++++++++++++++++++--------------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index 6625d5b0870..54b30c60057 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -85,6 +85,9 @@ function Explorer:destroy() self.sorters:destroy() self.view:destroy() + -- TODO existing buffer is retained by global and re-used. Delete it or retain it. + -- see wipe_rogue_buffer + vim.api.nvim_del_augroup_by_id(self.augroup_id) RootNode.destroy(self) diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index 896a5b666ec..6653fd242fc 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -7,15 +7,7 @@ local globals = require("nvim-tree.globals") local Class = require("nvim-tree.classic") ----@class OpenInWinOpts ----@field hijack_current_buf boolean|nil default true ----@field resize boolean|nil default true ----@field winid number|nil 0 or nil for current - -local DEFAULT_MIN_WIDTH = 30 -local DEFAULT_MAX_WIDTH = -1 -local DEFAULT_PADDING = 1 - +---Window and buffer related settings and operations ---@class (exact) View: Class ---@field live_filter table ---@field side string @@ -101,6 +93,7 @@ local BUFFER_OPTIONS = { { name = "swapfile", value = false }, } +-- TODO multi-instance remove this; delete buffers rather than retaining them ---@private ---@param bufnr integer ---@return boolean @@ -113,6 +106,7 @@ function View:matches_bufnr(bufnr) return false end +-- TODO multi-instance remove this; delete buffers rather than retaining them ---@private function View:wipe_rogue_buffer() for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do @@ -128,13 +122,12 @@ function View:create_buffer(bufnr) self:wipe_rogue_buffer() local tab = vim.api.nvim_get_current_tabpage() - globals.BUFNR_PER_TAB[tab] = bufnr or vim.api.nvim_create_buf(false, false) - if self.explorer.opts.experimental.multi_instance then - self.bufnr_by_tab[tab] = globals.BUFNR_PER_TAB[tab] - end + bufnr = bufnr or vim.api.nvim_create_buf(false, false) - bufnr = self:get_bufnr("View:create_buffer") + -- set both bufnr registries + globals.BUFNR_PER_TAB[tab] = bufnr + self.bufnr_by_tab[tab] = bufnr vim.api.nvim_buf_set_name(bufnr, "NvimTree_" .. tab) @@ -481,6 +474,11 @@ function View:set_current_win(callsite) globals.TABPAGES[current_tab].winid = current_win end +---@class OpenInWinOpts +---@field hijack_current_buf boolean|nil default true +---@field resize boolean|nil default true +---@field winid number|nil 0 or nil for current + ---Open the tree in the a window ---@param opts OpenInWinOpts|nil function View:open_in_win(opts) @@ -510,13 +508,14 @@ function View:abandon_current_window() globals.BUFNR_PER_TAB[tab], self.bufnr_by_tab[tab], (globals.BUFNR_PER_TAB[tab] == self.bufnr_by_tab[tab]) and "" or "MISMATCH") - - self.bufnr_by_tab[tab] = nil end - -- TODO multi-instance kill the buffer instead of retaining + -- TODO multi-instance maybe kill the buffer instead of retaining + -- reset both bufnr registries globals.BUFNR_PER_TAB[tab] = nil + self.bufnr_by_tab[tab] = nil + if globals.TABPAGES[tab] then globals.TABPAGES[tab].winid = nil end @@ -614,6 +613,8 @@ function View:restore_tab_state() self:set_cursor(globals.CURSORS[tabpage]) end +--- TODO multi-instance remove comment +--- not legacy codepath --- winid containing the buffer ---@param tabpage number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage. ---@param callsite string @@ -689,7 +690,7 @@ end function View:get_bufnr(callsite) local tab = vim.api.nvim_get_current_tabpage() if self.explorer.opts.experimental.multi_instance then - local msg = string.format("View:get_bufnr(%-20.20s) globals.BUFNR_PER_TAB[%s]=b%s view.bufnr_by_tab[%s]=b%s MISMATCH", + local msg = string.format("View:get_bufnr(%-20.20s) globals.BUFNR_PER_TAB[%s]=b%s view.bufnr_by_tab[%s]=b%s %s", callsite, tab, globals.BUFNR_PER_TAB[tab], tab, self.bufnr_by_tab[tab], @@ -765,7 +766,7 @@ end -- used on ColorScheme event function View:reset_winhl() - local winid = self:get_winid(nil, "View:reset_winhl1") + local winid = self:get_winid(nil, "View:reset_winhl") if winid and vim.api.nvim_win_is_valid(winid) then vim.wo[winid].winhl = appearance.WIN_HL end @@ -777,6 +778,10 @@ function View:is_width_determined() return type(self.width) ~= "function" end +local DEFAULT_MIN_WIDTH = 30 +local DEFAULT_MAX_WIDTH = -1 +local DEFAULT_PADDING = 1 + ---Configure width-related config ---@param width string|function|number|table|nil function View:configure_width(width) From 414e576bc2c710729a2751bfc4c38bf2ac15b8f6 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Fri, 20 Jun 2025 09:03:19 +1000 Subject: [PATCH 05/17] refactor(#2826): remove unnecessary view members float, hide_root_folder; use explorer opts --- lua/nvim-tree/actions/fs/remove-file.lua | 6 +++--- lua/nvim-tree/actions/node/open-file.lua | 2 +- lua/nvim-tree/explorer/live-filter.lua | 4 ++-- lua/nvim-tree/explorer/view.lua | 18 +++++++----------- 4 files changed, 13 insertions(+), 17 deletions(-) diff --git a/lua/nvim-tree/actions/fs/remove-file.lua b/lua/nvim-tree/actions/fs/remove-file.lua index fd71ae6a376..ba1325f74ef 100644 --- a/lua/nvim-tree/actions/fs/remove-file.lua +++ b/lua/nvim-tree/actions/fs/remove-file.lua @@ -18,7 +18,7 @@ local function close_windows(windows) -- Prevent from closing when the win count equals 1 or 2, -- where the win to remove could be the last opened. -- For details see #2503. - if explorer and explorer.view.float.enable and #vim.api.nvim_list_wins() < 3 then + if explorer and explorer.opts.view.float.enable and #vim.api.nvim_list_wins() < 3 then return end @@ -36,12 +36,12 @@ local function clear_buffer(absolute_path) for _, buf in pairs(bufs) do if buf.name == absolute_path then local tree_winnr = vim.api.nvim_get_current_win() - if buf.hidden == 0 and (#bufs > 1 or explorer and explorer.view.float.enable) then + if buf.hidden == 0 and (#bufs > 1 or explorer and explorer.opts.view.float.enable) then vim.api.nvim_set_current_win(buf.windows[1]) vim.cmd(":bn") end vim.api.nvim_buf_delete(buf.bufnr, { force = true }) - if explorer and not explorer.view.float.quit_on_focus_loss then + if explorer and not explorer.opts.view.float.quit_on_focus_loss then vim.api.nvim_set_current_win(tree_winnr) end if M.config.actions.remove_file.close_window then diff --git a/lua/nvim-tree/actions/node/open-file.lua b/lua/nvim-tree/actions/node/open-file.lua index b3bcce75a5a..b44870946d6 100644 --- a/lua/nvim-tree/actions/node/open-file.lua +++ b/lua/nvim-tree/actions/node/open-file.lua @@ -346,7 +346,7 @@ local function open_in_new_window(filename, mode) end end - if (mode == "preview" or mode == "preview_no_picker") and explorer and explorer.view.float.enable then + if (mode == "preview" or mode == "preview_no_picker") and explorer and explorer.opts.view.float.enable then -- ignore "WinLeave" autocmd on preview -- because the registered "WinLeave" -- will kill the floating window immediately diff --git a/lua/nvim-tree/explorer/live-filter.lua b/lua/nvim-tree/explorer/live-filter.lua index 9bd24406964..fbc643311e7 100644 --- a/lua/nvim-tree/explorer/live-filter.lua +++ b/lua/nvim-tree/explorer/live-filter.lua @@ -61,7 +61,7 @@ local overlay_bufnr = 0 local overlay_winnr = 0 local function remove_overlay(self) - if self.explorer.view.float.enable and self.explorer.view.float.quit_on_focus_loss then + if self.explorer.opts.view.float.enable and self.explorer.opts.view.float.quit_on_focus_loss then -- return to normal nvim-tree float behaviour when filter window is closed vim.api.nvim_create_autocmd("WinLeave", { pattern = "NvimTree_*", @@ -171,7 +171,7 @@ local function calculate_overlay_win_width(self) end local function create_overlay(self) - if self.explorer.view.float.enable then + if self.explorer.opts.view.float.enable then -- don't close nvim-tree float when focus is changed to filter window vim.api.nvim_clear_autocmds({ event = "WinLeave", diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index 6653fd242fc..658deb8bf55 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -11,11 +11,9 @@ local Class = require("nvim-tree.classic") ---@class (exact) View: Class ---@field live_filter table ---@field side string ----@field float table ---@field private explorer Explorer ---@field private adaptive_size boolean ---@field private centralize_selection boolean ----@field private hide_root_folder boolean ---@field private winopts table ---@field private height integer ---@field private preserve_window_proportions boolean @@ -40,9 +38,7 @@ function View:new(args) self.explorer = args.explorer self.adaptive_size = false self.centralize_selection = self.explorer.opts.view.centralize_selection - self.float = self.explorer.opts.view.float self.height = self.explorer.opts.view.height - 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.live_filter = { prev_focused_node = nil, } @@ -217,16 +213,16 @@ end ---@private ---@return table function View:open_win_config() - if type(self.float.open_win_config) == "function" then - return self.float.open_win_config() + if type(self.explorer.opts.view.float.open_win_config) == "function" then + return self.explorer.opts.view.float.open_win_config() else - return self.float.open_win_config + return self.explorer.opts.view.float.open_win_config end end ---@private function View:open_window() - if self.float.enable then + if self.explorer.opts.view.float.enable then vim.api.nvim_open_win(0, true, self:open_win_config()) else vim.api.nvim_command("vsp") @@ -405,9 +401,9 @@ end ---@param size string|number|nil function View:resize(size) - if self.float.enable and not self.adaptive_size then + if self.explorer.opts.view.float.enable and not self.adaptive_size then -- if the floating windows's adaptive size is not desired, then the - -- float size should be defined in view.float.open_win_config + -- float size should be defined in self.explorer.opts.view.float.open_win_config return end @@ -761,7 +757,7 @@ end ---@param cwd string|nil ---@return boolean function View:is_root_folder_visible(cwd) - return cwd ~= "/" and not self.hide_root_folder + return cwd ~= "/" and self.explorer.opts.renderer.root_folder_label ~= false end -- used on ColorScheme event From f1e9d5165cfabfaa98553f3c19eff7a6851a0972 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Fri, 20 Jun 2025 09:12:29 +1000 Subject: [PATCH 06/17] refactor(#2826): remove unused view members centralize_selection and preserve_window_proportions --- lua/nvim-tree/explorer/view.lua | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index 658deb8bf55..305aead7786 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -13,10 +13,8 @@ local Class = require("nvim-tree.classic") ---@field side string ---@field private explorer Explorer ---@field private adaptive_size boolean ----@field private centralize_selection boolean ---@field private winopts table ---@field private height integer ----@field private preserve_window_proportions boolean ---@field private initial_width integer ---@field private width (fun():integer)|integer|string ---@field private max_width integer @@ -37,9 +35,7 @@ function View:new(args) self.explorer = args.explorer self.adaptive_size = false - self.centralize_selection = self.explorer.opts.view.centralize_selection self.height = self.explorer.opts.view.height - self.preserve_window_proportions = self.explorer.opts.view.preserve_window_proportions self.side = (self.explorer.opts.view.side == "right") and "right" or "left" self.live_filter = { prev_focused_node = nil, } self.bufnr_by_tab = {} @@ -436,7 +432,7 @@ function View:resize(size) if new_size ~= vim.api.nvim_win_get_width(winid) then vim.api.nvim_win_set_width(winid, new_size) - if not self.preserve_window_proportions then + if not self.explorer.opts.view.preserve_window_proportions then vim.cmd(":wincmd =") end end From 5fbd6745ebecc5cb7d649488009108bfa2c5d3ab Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Fri, 20 Jun 2025 09:19:33 +1000 Subject: [PATCH 07/17] refactor(#2826): remove unused view member height --- lua/nvim-tree/explorer/view.lua | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index 305aead7786..bbc43894460 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -14,7 +14,6 @@ local Class = require("nvim-tree.classic") ---@field private explorer Explorer ---@field private adaptive_size boolean ---@field private winopts table ----@field private height integer ---@field private initial_width integer ---@field private width (fun():integer)|integer|string ---@field private max_width integer @@ -35,7 +34,6 @@ function View:new(args) self.explorer = args.explorer self.adaptive_size = false - self.height = self.explorer.opts.view.height self.side = (self.explorer.opts.view.side == "right") and "right" or "left" self.live_filter = { prev_focused_node = nil, } self.bufnr_by_tab = {} @@ -419,7 +417,6 @@ function View:resize(size) if size then self.width = size - self.height = size end if not self:is_visible() then @@ -770,6 +767,7 @@ function View:is_width_determined() return type(self.width) ~= "function" end +-- These are needed as they are populated only by the user, not configuration local DEFAULT_MIN_WIDTH = 30 local DEFAULT_MAX_WIDTH = -1 local DEFAULT_PADDING = 1 From 3615c7dffe88116ef19071eafe36b5cd8a38621e Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Fri, 20 Jun 2025 09:33:07 +1000 Subject: [PATCH 08/17] refactor(#2826): temporarily reuse BUFNR_PER_TAB in view constructor --- lua/nvim-tree/explorer/view.lua | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index bbc43894460..ae6cbe08f94 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -32,13 +32,13 @@ local View = Class:extend() function View:new(args) args.explorer:log_new("View") - self.explorer = args.explorer - self.adaptive_size = false - self.side = (self.explorer.opts.view.side == "right") and "right" or "left" - self.live_filter = { prev_focused_node = nil, } - self.bufnr_by_tab = {} + self.explorer = args.explorer + self.adaptive_size = false + self.side = (self.explorer.opts.view.side == "right") and "right" or "left" + self.live_filter = { prev_focused_node = nil, } + self.bufnr_by_tab = {} - self.winopts = { + self.winopts = { relativenumber = self.explorer.opts.view.relativenumber, number = self.explorer.opts.view.number, list = false, @@ -59,6 +59,10 @@ function View:new(args) self:configure_width(self.explorer.opts.view.width) self.initial_width = self:get_width() + + -- TODO multi-instance remove this; delete buffers rather than retaining them + local tabid = vim.api.nvim_get_current_tabpage() + self.bufnr_by_tab[tabid] = globals.BUFNR_PER_TAB[tabid] end function View:destroy() From 09ec00c085fac6da36f44a997ead03394e9e97c2 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Fri, 20 Jun 2025 10:01:12 +1000 Subject: [PATCH 09/17] refactor(#2826): get_winid returns new after consistency check --- lua/nvim-tree/explorer/view.lua | 12 ++++++++---- lua/nvim-tree/lib.lua | 1 + 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index ae6cbe08f94..8f1ce3d39b7 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -539,6 +539,7 @@ end ---@param opts table|nil ---@return boolean function View:is_visible(opts) + -- TODO multi-instance rewrite and consistency check if opts and opts.tabpage then if globals.TABPAGES[opts.tabpage] == nil then return false @@ -618,10 +619,10 @@ function View:winid(tabpage, callsite) local msg = string.format("View:winid(%3s, %-20.20s)", tabpage, callsite) if bufnr then - for _, w in pairs(vim.api.nvim_tabpage_list_wins(tabpage or 0)) do - if vim.api.nvim_win_get_buf(w) == bufnr then - log.line("dev", "%s b%d : w%s", msg, bufnr, w) - return w + for _, winid in pairs(vim.api.nvim_tabpage_list_wins(tabpage or 0)) do + if vim.api.nvim_win_get_buf(winid) == bufnr then + log.line("dev", "%s b%d : w%s", msg, bufnr, winid) + return winid end end else @@ -669,6 +670,8 @@ function View:get_winid(tabpage, callsite) if winid ~= tabinfo_winid then notify.error(msg) end + + return winid end -- legacy codepath @@ -712,6 +715,7 @@ function View:prevent_buffer_override() local curbuf = vim.api.nvim_win_get_buf(curwin) local bufname = vim.api.nvim_buf_get_name(curbuf) + --- TODO multi-instance this can be removed as winid() will handle it if not bufname:match("NvimTree") then for i, tabpage in ipairs(globals.TABPAGES) do if tabpage.winid == view_winid then diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index cd6442cc044..854e33bfd09 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -135,6 +135,7 @@ function M.open(opts) open_view_and_draw() end + -- TODO multi-instance is this actually necessary? if explorer then explorer.view:restore_tab_state() end From 83fdff7c4a84950d6f3ec3cdcbbeb57e51b9bd0a Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Fri, 20 Jun 2025 11:18:38 +1000 Subject: [PATCH 10/17] refactor(#2826): globals.TABPAGES -> WINID_PER_TAB --- lua/nvim-tree.lua | 2 +- lua/nvim-tree/explorer/view.lua | 127 ++++++------------------- lua/nvim-tree/globals.lua | 2 +- lua/nvim-tree/multi-instance-debug.lua | 10 +- 4 files changed, 36 insertions(+), 105 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 45087633f9f..78a27a7a696 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -668,7 +668,7 @@ function M.purge_all_state() local explorer = core.get_explorer() if explorer then explorer.view:close_all_tabs() - explorer.view:abandon_all_windows("purge_all_state") + explorer.view:abandon_all_windows() require("nvim-tree.git").purge_state() explorer:destroy() core.reset_explorer() diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index 8f1ce3d39b7..a54b4f2e4c0 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -69,14 +69,6 @@ function View:destroy() self.explorer:log_destroy("View") end --- The initial state of a tab -local tabinitial = { - -- The position of the cursor { line, column } - cursor = { 0, 0 }, - -- The NvimTree window number - winid = nil, -} - ---@type { name: string, value: any }[] local BUFFER_OPTIONS = { { name = "bufhidden", value = "wipe" }, @@ -163,24 +155,6 @@ local move_tbl = { right = "L", } --- setup_tabpage sets up the initial state of a tab ----@private ----@param tabpage integer ----@param callsite string -function View:setup_tabpage(tabpage, callsite) - local winid = vim.api.nvim_get_current_win() - - if self.explorer.opts.experimental.multi_instance then - log.line("dev", "View:setup_tabpage(%3s, %-20.20s) w%d %s", - tabpage, - callsite, - winid, - globals.TABPAGES[tabpage] and vim.inspect(globals.TABPAGES[tabpage], { newline = "" }) or "tabinitial") - end - - globals.TABPAGES[tabpage] = vim.tbl_extend("force", globals.TABPAGES[tabpage] or tabinitial, { winid = winid }) -end - ---@private function View:set_window_options_and_buffer() pcall(vim.api.nvim_command, "buffer " .. self:get_bufnr("View:set_window_options_and_buffer")) @@ -226,7 +200,7 @@ function View:open_window() vim.api.nvim_command("vsp") self:reposition_window() end - self:setup_tabpage(vim.api.nvim_get_current_tabpage(), "View:open_window") + globals.WINID_PER_TAB[vim.api.nvim_get_current_tabpage()] = vim.api.nvim_get_current_win() self:set_window_options_and_buffer() end @@ -308,7 +282,7 @@ function View:close_this_tab_only() end function View:close_all_tabs() - for tabpage, _ in pairs(globals.TABPAGES) do + for tabpage, _ in pairs(globals.WINID_PER_TAB) do self:close_internal(tabpage) end end @@ -449,22 +423,9 @@ function View:reposition_window() end ---@private ----@param callsite string -function View:set_current_win(callsite) +function View:set_current_win() local current_tab = vim.api.nvim_get_current_tabpage() - local current_win = vim.api.nvim_get_current_win() - - if self.explorer.opts.experimental.multi_instance then - log.line("dev", "View:set_current_win(%-20.20s) t%d w%3s->w%3s %s", - callsite, - current_tab, - globals.TABPAGES[current_tab].winid, - current_win, - (globals.TABPAGES[current_tab].winid == current_win) and "" or "MISMATCH" - ) - end - - globals.TABPAGES[current_tab].winid = current_win + globals.WINID_PER_TAB[current_tab] = vim.api.nvim_get_current_win() end ---@class OpenInWinOpts @@ -481,8 +442,8 @@ function View:open_in_win(opts) vim.api.nvim_set_current_win(opts.winid) end self:create_buffer(opts.hijack_current_buf and vim.api.nvim_get_current_buf()) - self:setup_tabpage(vim.api.nvim_get_current_tabpage(), "View:open_in_win") - self:set_current_win("View:open_in_win") + globals.WINID_PER_TAB[vim.api.nvim_get_current_tabpage()] = vim.api.nvim_get_current_win() + self:set_current_win() self:set_window_options_and_buffer() if opts.resize then self:reposition_window() @@ -497,7 +458,7 @@ function View:abandon_current_window() if self.explorer.opts.experimental.multi_instance then log.line("dev", "View:abandon_current_window() t%d w%s b%s member b%s %s", tab, - globals.TABPAGES[tab] and globals.TABPAGES[tab].winid or nil, + globals.WINID_PER_TAB[tab], globals.BUFNR_PER_TAB[tab], self.bufnr_by_tab[tab], (globals.BUFNR_PER_TAB[tab] == self.bufnr_by_tab[tab]) and "" or "MISMATCH") @@ -509,30 +470,14 @@ function View:abandon_current_window() globals.BUFNR_PER_TAB[tab] = nil self.bufnr_by_tab[tab] = nil - if globals.TABPAGES[tab] then - globals.TABPAGES[tab].winid = nil - end + globals.WINID_PER_TAB[tab] = nil end ----@param callsite string -function View:abandon_all_windows(callsite) +function View:abandon_all_windows() + -- TODO multi-instance kill the buffer instead of retaining for tab, _ in pairs(vim.api.nvim_list_tabpages()) do - if self.explorer.opts.experimental.multi_instance then - log.line("dev", "View:abandon_all_windows(%-20.20s) t%d w%s b%s member b%s %s", - callsite, - tab, - globals.TABPAGES and globals.TABPAGES.winid or nil, - globals.BUFNR_PER_TAB[tab], - self.bufnr_by_tab[tab], - (globals.BUFNR_PER_TAB[tab] == self.bufnr_by_tab[tab]) and "" or "MISMATCH") - end - - -- TODO multi-instance kill the buffer instead of retaining - globals.BUFNR_PER_TAB[tab] = nil - if globals.TABPAGES[tab] then - globals.TABPAGES[tab].winid = nil - end + globals.WINID_PER_TAB[tab] = nil end end @@ -541,16 +486,16 @@ end function View:is_visible(opts) -- TODO multi-instance rewrite and consistency check if opts and opts.tabpage then - if globals.TABPAGES[opts.tabpage] == nil then + if not globals.WINID_PER_TAB[opts.tabpage] then return false end - local winid = globals.TABPAGES[opts.tabpage].winid + local winid = globals.WINID_PER_TAB[opts.tabpage] return winid and vim.api.nvim_win_is_valid(winid) end if opts and opts.any_tabpage then - for _, v in pairs(globals.TABPAGES) do - if v.winid and vim.api.nvim_win_is_valid(v.winid) then + for _, winid in pairs(globals.WINID_PER_TAB) do + if winid and vim.api.nvim_win_is_valid(winid) then return true end end @@ -611,22 +556,16 @@ end --- not legacy codepath --- winid containing the buffer ---@param tabpage number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage. ----@param callsite string ---@return integer? winid -function View:winid(tabpage, callsite) +function View:winid(tabpage) local bufnr = self.bufnr_by_tab[tabpage] - local msg = string.format("View:winid(%3s, %-20.20s)", tabpage, callsite) - if bufnr then for _, winid in pairs(vim.api.nvim_tabpage_list_wins(tabpage or 0)) do if vim.api.nvim_win_get_buf(winid) == bufnr then - log.line("dev", "%s b%d : w%s", msg, bufnr, winid) return winid end end - else - log.line("dev", "%s no bufnr", msg) end end @@ -636,28 +575,26 @@ end ---@return number|nil function View:get_winid(tabpage, callsite) local tabid = tabpage or vim.api.nvim_get_current_tabpage() - local tabinfo = globals.TABPAGES[tabid] local tabinfo_winid = nil if self.explorer.opts.experimental.multi_instance then + local msg_fault = "" - if not tabinfo then - msg_fault = "no tabinfo" - elseif not tabinfo.winid then - msg_fault = "no tabinfo.winid" - elseif not vim.api.nvim_win_is_valid(tabinfo.winid) then - msg_fault = string.format("invalid tabinfo.winid %d", tabinfo.winid) + if not globals.WINID_PER_TAB[tabid] then + msg_fault = "no WINID_PER_TAB" + elseif not vim.api.nvim_win_is_valid(globals.WINID_PER_TAB[tabid]) then + msg_fault = string.format("invalid globals.WINID_PER_TAB[tabid] %d", globals.WINID_PER_TAB[tabid]) else - tabinfo_winid = tabinfo.winid + tabinfo_winid = globals.WINID_PER_TAB[tabid] end - local winid = self:winid(tabid, "View:get_winid") + local winid = self:winid(tabid) if winid ~= tabinfo_winid then msg_fault = "MISMATCH" end - local msg = string.format("View:get_winid(%3s, %-20.20s) globals.TABPAGES[%s].winid=w%s view.winid(%s)=w%s %s", + local msg = string.format("View:get_winid(%3s, %-20.20s) globals.TABPAGES[%s]=w%s view.winid(%s)=w%s %s", tabpage, callsite, tabid, tabinfo_winid, @@ -670,13 +607,11 @@ function View:get_winid(tabpage, callsite) if winid ~= tabinfo_winid then notify.error(msg) end - - return winid end -- legacy codepath - if tabinfo and tabinfo.winid and vim.api.nvim_win_is_valid(tabinfo.winid) then - return tabinfo.winid + if tabinfo_winid and vim.api.nvim_win_is_valid(tabinfo_winid) then + return tabinfo_winid end end @@ -717,13 +652,9 @@ function View:prevent_buffer_override() --- TODO multi-instance this can be removed as winid() will handle it if not bufname:match("NvimTree") then - for i, tabpage in ipairs(globals.TABPAGES) do - if tabpage.winid == view_winid then - if self.explorer.opts.experimental.multi_instance then - log.line("dev", "View:prevent_buffer_override() t%d w%d clearing", i, view_winid) - end - - globals.TABPAGES[i] = nil + for i, winid in ipairs(globals.WINID_PER_TAB) do + if winid == view_winid then + globals.WINID_PER_TAB[i] = nil break end end diff --git a/lua/nvim-tree/globals.lua b/lua/nvim-tree/globals.lua index 58e3452f7ee..00c857767b3 100644 --- a/lua/nvim-tree/globals.lua +++ b/lua/nvim-tree/globals.lua @@ -2,7 +2,7 @@ local M = { -- from View - TABPAGES = {}, + WINID_PER_TAB = {}, BUFNR_PER_TAB = {}, CURSORS = {}, } diff --git a/lua/nvim-tree/multi-instance-debug.lua b/lua/nvim-tree/multi-instance-debug.lua index d33f34a1313..d76c0b565ea 100644 --- a/lua/nvim-tree/multi-instance-debug.lua +++ b/lua/nvim-tree/multi-instance-debug.lua @@ -3,7 +3,7 @@ local globals = require("nvim-tree.globals") local M = {} --- Debugging only. ---- Tabs show TABPAGES winid and BUFNR_PER_TAB bufnr for the tab. +--- Tabs show WINID_PER_TAB winid and BUFNR_PER_TAB bufnr for the tab. --- Orphans for inexistent tab_ids are shown at the right. --- lib.target_winid is always shown at the right next to a close button. --- Enable with: @@ -15,7 +15,7 @@ function M.tab_line() local cur_tab_id = vim.api.nvim_get_current_tabpage() local bufnr_per_tab = vim.deepcopy(globals.BUFNR_PER_TAB) - local tabpages = vim.deepcopy(globals.TABPAGES) + local tabpages = vim.deepcopy(globals.WINID_PER_TAB) local tl = "%#TabLine#" @@ -34,9 +34,9 @@ function M.tab_line() tl = tl .. " t" .. tab_id -- winid, if present - local tp = globals.TABPAGES[tab_id] + local tp = globals.WINID_PER_TAB[tab_id] if tp then - tl = tl .. " w" .. (tp.winid or "nil") + tl = tl .. " w" .. (tp or "nil") else tl = tl .. " " end @@ -67,7 +67,7 @@ function M.tab_line() end for tab_id, tp in pairs(tabpages) do orphans[tab_id] = orphans[tab_id] or {} - orphans[tab_id].winid = tp.winid + orphans[tab_id].winid = tp end -- right-align From e875f15b327a9a63f9b37fb12e177d403be8c14b Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Fri, 20 Jun 2025 12:01:24 +1000 Subject: [PATCH 11/17] refactor(#2826): consistent naming of tabid --- lua/nvim-tree/explorer/view.lua | 139 +++++++++++++------------ lua/nvim-tree/globals.lua | 4 +- lua/nvim-tree/multi-instance-debug.lua | 28 ++--- 3 files changed, 89 insertions(+), 82 deletions(-) diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index a54b4f2e4c0..e879b594c5a 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -18,7 +18,7 @@ local Class = require("nvim-tree.classic") ---@field private width (fun():integer)|integer|string ---@field private max_width integer ---@field private padding integer ----@field private bufnr_by_tab table stored per tab until multi-instance is complete +---@field private bufnr_by_tabid table stored per tab until multi-instance is complete local View = Class:extend() ---@class View @@ -36,7 +36,7 @@ function View:new(args) self.adaptive_size = false self.side = (self.explorer.opts.view.side == "right") and "right" or "left" self.live_filter = { prev_focused_node = nil, } - self.bufnr_by_tab = {} + self.bufnr_by_tabid = {} self.winopts = { relativenumber = self.explorer.opts.view.relativenumber, @@ -62,7 +62,7 @@ function View:new(args) -- TODO multi-instance remove this; delete buffers rather than retaining them local tabid = vim.api.nvim_get_current_tabpage() - self.bufnr_by_tab[tabid] = globals.BUFNR_PER_TAB[tabid] + self.bufnr_by_tabid[tabid] = globals.BUFNR_BY_TABID[tabid] end function View:destroy() @@ -84,7 +84,7 @@ local BUFFER_OPTIONS = { ---@param bufnr integer ---@return boolean function View:matches_bufnr(bufnr) - for _, b in pairs(globals.BUFNR_PER_TAB) do + for _, b in pairs(globals.BUFNR_BY_TABID) do if b == bufnr then return true end @@ -107,15 +107,15 @@ end function View:create_buffer(bufnr) self:wipe_rogue_buffer() - local tab = vim.api.nvim_get_current_tabpage() + local tabid = vim.api.nvim_get_current_tabpage() bufnr = bufnr or vim.api.nvim_create_buf(false, false) -- set both bufnr registries - globals.BUFNR_PER_TAB[tab] = bufnr - self.bufnr_by_tab[tab] = bufnr + globals.BUFNR_BY_TABID[tabid] = bufnr + self.bufnr_by_tabid[tabid] = bufnr - vim.api.nvim_buf_set_name(bufnr, "NvimTree_" .. tab) + vim.api.nvim_buf_set_name(bufnr, "NvimTree_" .. tabid) for _, option in ipairs(BUFFER_OPTIONS) do vim.api.nvim_set_option_value(option.name, option.value, { buf = bufnr }) @@ -200,7 +200,7 @@ function View:open_window() vim.api.nvim_command("vsp") self:reposition_window() end - globals.WINID_PER_TAB[vim.api.nvim_get_current_tabpage()] = vim.api.nvim_get_current_win() + globals.WINID_BY_TABID[vim.api.nvim_get_current_tabpage()] = vim.api.nvim_get_current_win() self:set_window_options_and_buffer() end @@ -237,26 +237,26 @@ end ---save_tab_state saves any state that should be preserved across redraws. ---@private ----@param tabnr integer -function View:save_tab_state(tabnr) - local tabpage = tabnr or vim.api.nvim_get_current_tabpage() - globals.CURSORS[tabpage] = vim.api.nvim_win_get_cursor(self:get_winid(tabpage, "View:save_tab_state") or 0) +---@param tabid integer +function View:save_tab_state(tabid) + tabid = tabid or vim.api.nvim_get_current_tabpage() + globals.CURSORS[tabid] = vim.api.nvim_win_get_cursor(self:get_winid(tabid, "View:save_tab_state") or 0) end ---@private ----@param tabpage integer -function View:close_internal(tabpage) +---@param tabid integer +function View:close_internal(tabid) if self.explorer.opts.experimental.multi_instance then - log.line("dev", "View:close_internal(t%s)", tabpage) + log.line("dev", "View:close_internal(t%s)", tabid) end - if not self:is_visible({ tabpage = tabpage }) then + if not self:is_visible({ tabpage = tabid }) then return end - self:save_tab_state(tabpage) + self:save_tab_state(tabid) switch_buf_if_last_buf() - local tree_win = self:get_winid(tabpage, "View:close_internal") + local tree_win = self:get_winid(tabid, "View:close_internal") local current_win = vim.api.nvim_get_current_win() - for _, win in pairs(vim.api.nvim_tabpage_list_wins(tabpage)) do + for _, win in pairs(vim.api.nvim_tabpage_list_wins(tabid)) do if vim.api.nvim_win_get_config(win).relative == "" then local prev_win = vim.fn.winnr("#") -- this tab only if tree_win == current_win and prev_win > 0 then @@ -264,7 +264,7 @@ function View:close_internal(tabpage) end if vim.api.nvim_win_is_valid(tree_win or 0) then if self.explorer.opts.experimental.multi_instance then - log.line("dev", "View:close_internal(t%s) w%s", tabpage, tree_win) + log.line("dev", "View:close_internal(t%s) w%s", tabid, tree_win) end local success, error = pcall(vim.api.nvim_win_close, tree_win or 0, true) if not success then @@ -278,26 +278,34 @@ function View:close_internal(tabpage) end function View:close_this_tab_only() + if self.explorer.opts.experimental.multi_instance then + log.line("dev", "View:close_this_tab_only()") + end self:close_internal(vim.api.nvim_get_current_tabpage()) end +-- TODO this is broken at 1.13.0 - current tab does not close when tab.sync.close is set function View:close_all_tabs() - for tabpage, _ in pairs(globals.WINID_PER_TAB) do - self:close_internal(tabpage) + log.line("dev", "View:close_all_tabs() globals.WINID_BY_TABID=%s", vim.inspect(globals.WINID_BY_TABID)) + for tabid, _ in pairs(globals.WINID_BY_TABID) do + if self.explorer.opts.experimental.multi_instance then + log.line("dev", "View:close_all_tabs()") + end + self:close_internal(tabid) end end ----@param tabpage integer|nil +---@param tabid integer|nil ---@param callsite string -function View:close(tabpage, callsite) +function View:close(tabid, callsite) if self.explorer.opts.experimental.multi_instance then - log.line("dev", "View:close(t%s, %s)", tabpage, callsite) + log.line("dev", "View:close(t%s, %s)", tabid, callsite) end if self.explorer.opts.tab.sync.close then self:close_all_tabs() - elseif tabpage then - self:close_internal(tabpage) + elseif tabid then + self:close_internal(tabid) else self:close_this_tab_only() end @@ -425,7 +433,7 @@ end ---@private function View:set_current_win() local current_tab = vim.api.nvim_get_current_tabpage() - globals.WINID_PER_TAB[current_tab] = vim.api.nvim_get_current_win() + globals.WINID_BY_TABID[current_tab] = vim.api.nvim_get_current_win() end ---@class OpenInWinOpts @@ -442,7 +450,7 @@ function View:open_in_win(opts) vim.api.nvim_set_current_win(opts.winid) end self:create_buffer(opts.hijack_current_buf and vim.api.nvim_get_current_buf()) - globals.WINID_PER_TAB[vim.api.nvim_get_current_tabpage()] = vim.api.nvim_get_current_win() + globals.WINID_BY_TABID[vim.api.nvim_get_current_tabpage()] = vim.api.nvim_get_current_win() self:set_current_win() self:set_window_options_and_buffer() if opts.resize then @@ -458,26 +466,26 @@ function View:abandon_current_window() if self.explorer.opts.experimental.multi_instance then log.line("dev", "View:abandon_current_window() t%d w%s b%s member b%s %s", tab, - globals.WINID_PER_TAB[tab], - globals.BUFNR_PER_TAB[tab], - self.bufnr_by_tab[tab], - (globals.BUFNR_PER_TAB[tab] == self.bufnr_by_tab[tab]) and "" or "MISMATCH") + globals.WINID_BY_TABID[tab], + globals.BUFNR_BY_TABID[tab], + self.bufnr_by_tabid[tab], + (globals.BUFNR_BY_TABID[tab] == self.bufnr_by_tabid[tab]) and "" or "MISMATCH") end -- TODO multi-instance maybe kill the buffer instead of retaining -- reset both bufnr registries - globals.BUFNR_PER_TAB[tab] = nil - self.bufnr_by_tab[tab] = nil + globals.BUFNR_BY_TABID[tab] = nil + self.bufnr_by_tabid[tab] = nil - globals.WINID_PER_TAB[tab] = nil + globals.WINID_BY_TABID[tab] = nil end function View:abandon_all_windows() -- TODO multi-instance kill the buffer instead of retaining for tab, _ in pairs(vim.api.nvim_list_tabpages()) do - globals.BUFNR_PER_TAB[tab] = nil - globals.WINID_PER_TAB[tab] = nil + globals.BUFNR_BY_TABID[tab] = nil + globals.WINID_BY_TABID[tab] = nil end end @@ -486,15 +494,15 @@ end function View:is_visible(opts) -- TODO multi-instance rewrite and consistency check if opts and opts.tabpage then - if not globals.WINID_PER_TAB[opts.tabpage] then + if not globals.WINID_BY_TABID[opts.tabpage] then return false end - local winid = globals.WINID_PER_TAB[opts.tabpage] + local winid = globals.WINID_BY_TABID[opts.tabpage] return winid and vim.api.nvim_win_is_valid(winid) end if opts and opts.any_tabpage then - for _, winid in pairs(globals.WINID_PER_TAB) do + for _, winid in pairs(globals.WINID_BY_TABID) do if winid and vim.api.nvim_win_is_valid(winid) then return true end @@ -548,20 +556,19 @@ end --- Restores the state of a NvimTree window if it was initialized before. function View:restore_tab_state() - local tabpage = vim.api.nvim_get_current_tabpage() - self:set_cursor(globals.CURSORS[tabpage]) + self:set_cursor(globals.CURSORS[vim.api.nvim_get_current_tabpage()]) end --- TODO multi-instance remove comment --- not legacy codepath --- winid containing the buffer ----@param tabpage number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage. +---@param tabid number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage. ---@return integer? winid -function View:winid(tabpage) - local bufnr = self.bufnr_by_tab[tabpage] +function View:winid(tabid) + local bufnr = self.bufnr_by_tabid[tabid] if bufnr then - for _, winid in pairs(vim.api.nvim_tabpage_list_wins(tabpage or 0)) do + for _, winid in pairs(vim.api.nvim_tabpage_list_wins(tabid or 0)) do if vim.api.nvim_win_get_buf(winid) == bufnr then return winid end @@ -570,22 +577,22 @@ function View:winid(tabpage) end --- Returns the window number for nvim-tree within the tabpage specified ----@param tabpage number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage. +---@param tabid number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage. ---@param callsite string ---@return number|nil -function View:get_winid(tabpage, callsite) - local tabid = tabpage or vim.api.nvim_get_current_tabpage() +function View:get_winid(tabid, callsite) + local tabid_param = tabid + tabid = tabid or vim.api.nvim_get_current_tabpage() local tabinfo_winid = nil if self.explorer.opts.experimental.multi_instance then - local msg_fault = "" - if not globals.WINID_PER_TAB[tabid] then - msg_fault = "no WINID_PER_TAB" - elseif not vim.api.nvim_win_is_valid(globals.WINID_PER_TAB[tabid]) then - msg_fault = string.format("invalid globals.WINID_PER_TAB[tabid] %d", globals.WINID_PER_TAB[tabid]) + if not globals.WINID_BY_TABID[tabid] then + msg_fault = "no WINID_BY_TABID" + elseif not vim.api.nvim_win_is_valid(globals.WINID_BY_TABID[tabid]) then + msg_fault = string.format("invalid globals.WINID_BY_TABID[tabid] %d", globals.WINID_BY_TABID[tabid]) else - tabinfo_winid = globals.WINID_PER_TAB[tabid] + tabinfo_winid = globals.WINID_BY_TABID[tabid] end local winid = self:winid(tabid) @@ -595,7 +602,7 @@ function View:get_winid(tabpage, callsite) end local msg = string.format("View:get_winid(%3s, %-20.20s) globals.TABPAGES[%s]=w%s view.winid(%s)=w%s %s", - tabpage, + tabid_param, callsite, tabid, tabinfo_winid, tabid, winid, @@ -621,20 +628,20 @@ end function View:get_bufnr(callsite) local tab = vim.api.nvim_get_current_tabpage() if self.explorer.opts.experimental.multi_instance then - local msg = string.format("View:get_bufnr(%-20.20s) globals.BUFNR_PER_TAB[%s]=b%s view.bufnr_by_tab[%s]=b%s %s", + local msg = string.format("View:get_bufnr(%-20.20s) globals.BUFNR_BY_TABID[%s]=b%s view.bufnr_by_tab[%s]=b%s %s", callsite, - tab, globals.BUFNR_PER_TAB[tab], - tab, self.bufnr_by_tab[tab], - (globals.BUFNR_PER_TAB[tab] == self.bufnr_by_tab[tab]) and "" or "MISMATCH" + tab, globals.BUFNR_BY_TABID[tab], + tab, self.bufnr_by_tabid[tab], + (globals.BUFNR_BY_TABID[tab] == self.bufnr_by_tabid[tab]) and "" or "MISMATCH" ) - if globals.BUFNR_PER_TAB[tab] ~= self.bufnr_by_tab[tab] then + if globals.BUFNR_BY_TABID[tab] ~= self.bufnr_by_tabid[tab] then notify.error(msg) end log.line("dev", msg) end - return globals.BUFNR_PER_TAB[tab] + return globals.BUFNR_BY_TABID[tab] end function View:prevent_buffer_override() @@ -652,9 +659,9 @@ function View:prevent_buffer_override() --- TODO multi-instance this can be removed as winid() will handle it if not bufname:match("NvimTree") then - for i, winid in ipairs(globals.WINID_PER_TAB) do + for i, winid in ipairs(globals.WINID_BY_TABID) do if winid == view_winid then - globals.WINID_PER_TAB[i] = nil + globals.WINID_BY_TABID[i] = nil break end end diff --git a/lua/nvim-tree/globals.lua b/lua/nvim-tree/globals.lua index 00c857767b3..66211260bc6 100644 --- a/lua/nvim-tree/globals.lua +++ b/lua/nvim-tree/globals.lua @@ -2,8 +2,8 @@ local M = { -- from View - WINID_PER_TAB = {}, - BUFNR_PER_TAB = {}, + WINID_BY_TABID = {}, + BUFNR_BY_TABID = {}, CURSORS = {}, } diff --git a/lua/nvim-tree/multi-instance-debug.lua b/lua/nvim-tree/multi-instance-debug.lua index d76c0b565ea..0482d62f673 100644 --- a/lua/nvim-tree/multi-instance-debug.lua +++ b/lua/nvim-tree/multi-instance-debug.lua @@ -3,7 +3,7 @@ local globals = require("nvim-tree.globals") local M = {} --- Debugging only. ---- Tabs show WINID_PER_TAB winid and BUFNR_PER_TAB bufnr for the tab. +--- Tabs show WINID_BY_TABID winid and BUFNR_BY_TABID bufnr for the tab. --- Orphans for inexistent tab_ids are shown at the right. --- lib.target_winid is always shown at the right next to a close button. --- Enable with: @@ -11,30 +11,30 @@ local M = {} --- vim.opt.showtabline = 2 ---@return string function M.tab_line() - local tab_ids = vim.api.nvim_list_tabpages() - local cur_tab_id = vim.api.nvim_get_current_tabpage() + local tabids = vim.api.nvim_list_tabpages() + local tabid_cur = vim.api.nvim_get_current_tabpage() - local bufnr_per_tab = vim.deepcopy(globals.BUFNR_PER_TAB) - local tabpages = vim.deepcopy(globals.WINID_PER_TAB) + local bufnr_by_tabid = vim.deepcopy(globals.BUFNR_BY_TABID) + local winid_by_tabid = vim.deepcopy(globals.WINID_BY_TABID) local tl = "%#TabLine#" - for i, tab_id in ipairs(tab_ids) do + for i, tabid in ipairs(tabids) do -- click to select tl = tl .. "%" .. i .. "T" -- style - if tab_id == cur_tab_id then + if tabid == tabid_cur then tl = tl .. "%#StatusLine#|" else tl = tl .. "|%#TabLine#" end -- tab_id itself - tl = tl .. " t" .. tab_id + tl = tl .. " t" .. tabid -- winid, if present - local tp = globals.WINID_PER_TAB[tab_id] + local tp = globals.WINID_BY_TABID[tabid] if tp then tl = tl .. " w" .. (tp or "nil") else @@ -42,7 +42,7 @@ function M.tab_line() end -- bufnr, if present - local bpt = globals.BUFNR_PER_TAB[tab_id] + local bpt = globals.BUFNR_BY_TABID[tabid] if bpt then tl = tl .. " b" .. bpt else @@ -52,8 +52,8 @@ function M.tab_line() tl = tl .. " " -- remove actively mapped - bufnr_per_tab[tab_id] = nil - tabpages[tab_id] = nil + bufnr_by_tabid[tabid] = nil + winid_by_tabid[tabid] = nil end -- close last and reset @@ -61,11 +61,11 @@ function M.tab_line() -- collect orphans local orphans = {} - for tab_id, bufnr in pairs(bufnr_per_tab) do + for tab_id, bufnr in pairs(bufnr_by_tabid) do orphans[tab_id] = orphans[tab_id] or {} orphans[tab_id].bufnr = bufnr end - for tab_id, tp in pairs(tabpages) do + for tab_id, tp in pairs(winid_by_tabid) do orphans[tab_id] = orphans[tab_id] or {} orphans[tab_id].winid = tp end From d72f85f5244dc111d2a09edd329ef28b9a56d0ef Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Fri, 20 Jun 2025 12:33:16 +1000 Subject: [PATCH 12/17] refactor(#2826): more consistency checking --- lua/nvim-tree/explorer/init.lua | 3 - lua/nvim-tree/explorer/view.lua | 117 ++++++++++++++++++++++++-------- 2 files changed, 87 insertions(+), 33 deletions(-) diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index 54b30c60057..6625d5b0870 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -85,9 +85,6 @@ function Explorer:destroy() self.sorters:destroy() self.view:destroy() - -- TODO existing buffer is retained by global and re-used. Delete it or retain it. - -- see wipe_rogue_buffer - vim.api.nvim_del_augroup_by_id(self.augroup_id) RootNode.destroy(self) diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index e879b594c5a..0cbdb899241 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -32,13 +32,13 @@ local View = Class:extend() function View:new(args) args.explorer:log_new("View") - self.explorer = args.explorer - self.adaptive_size = false - self.side = (self.explorer.opts.view.side == "right") and "right" or "left" - self.live_filter = { prev_focused_node = nil, } - self.bufnr_by_tabid = {} + self.explorer = args.explorer + self.adaptive_size = false + self.side = (self.explorer.opts.view.side == "right") and "right" or "left" + self.live_filter = { prev_focused_node = nil, } + self.bufnr_by_tabid = {} - self.winopts = { + self.winopts = { relativenumber = self.explorer.opts.view.relativenumber, number = self.explorer.opts.view.number, list = false, @@ -246,10 +246,13 @@ end ---@private ---@param tabid integer function View:close_internal(tabid) + --- BEGIN multi-instance FF if self.explorer.opts.experimental.multi_instance then log.line("dev", "View:close_internal(t%s)", tabid) end - if not self:is_visible({ tabpage = tabid }) then + --- END multi-instance FF + + if not self:is_visible({ tabpage = tabid }, "View:close_internal") then return end self:save_tab_state(tabid) @@ -263,9 +266,12 @@ function View:close_internal(tabid) vim.api.nvim_set_current_win(vim.fn.win_getid(prev_win)) end if vim.api.nvim_win_is_valid(tree_win or 0) then + --- BEGIN multi-instance FF if self.explorer.opts.experimental.multi_instance then log.line("dev", "View:close_internal(t%s) w%s", tabid, tree_win) end + --- END multi-instance FF + --- local success, error = pcall(vim.api.nvim_win_close, tree_win or 0, true) if not success then notify.debug("Failed to close window: " .. error) @@ -278,9 +284,12 @@ function View:close_internal(tabid) end function View:close_this_tab_only() + --- BEGIN multi-instance FF if self.explorer.opts.experimental.multi_instance then log.line("dev", "View:close_this_tab_only()") end + --- END multi-instance FF + self:close_internal(vim.api.nvim_get_current_tabpage()) end @@ -288,9 +297,12 @@ end function View:close_all_tabs() log.line("dev", "View:close_all_tabs() globals.WINID_BY_TABID=%s", vim.inspect(globals.WINID_BY_TABID)) for tabid, _ in pairs(globals.WINID_BY_TABID) do + --- BEGIN multi-instance FF if self.explorer.opts.experimental.multi_instance then log.line("dev", "View:close_all_tabs()") end + --- END multi-instance FF + self:close_internal(tabid) end end @@ -298,9 +310,11 @@ end ---@param tabid integer|nil ---@param callsite string function View:close(tabid, callsite) + --- BEGIN multi-instance FF if self.explorer.opts.experimental.multi_instance then log.line("dev", "View:close(t%s, %s)", tabid, callsite) end + --- END multi-instance FF if self.explorer.opts.tab.sync.close then self:close_all_tabs() @@ -313,7 +327,7 @@ end ---@param options table|nil function View:open(options) - if self:is_visible() then + if self:is_visible(nil, "View:open") then return end @@ -405,7 +419,7 @@ function View:resize(size) self.width = size end - if not self:is_visible() then + if not self:is_visible(nil, "View:resize") then return end @@ -463,6 +477,7 @@ end function View:abandon_current_window() local tab = vim.api.nvim_get_current_tabpage() + --- BEGIN multi-instance FF if self.explorer.opts.experimental.multi_instance then log.line("dev", "View:abandon_current_window() t%d w%s b%s member b%s %s", tab, @@ -471,8 +486,7 @@ function View:abandon_current_window() self.bufnr_by_tabid[tab], (globals.BUFNR_BY_TABID[tab] == self.bufnr_by_tabid[tab]) and "" or "MISMATCH") end - - -- TODO multi-instance maybe kill the buffer instead of retaining + --- END multi-instance FF -- reset both bufnr registries globals.BUFNR_BY_TABID[tab] = nil @@ -482,7 +496,6 @@ function View:abandon_current_window() end function View:abandon_all_windows() - -- TODO multi-instance kill the buffer instead of retaining for tab, _ in pairs(vim.api.nvim_list_tabpages()) do globals.BUFNR_BY_TABID[tab] = nil globals.WINID_BY_TABID[tab] = nil @@ -490,10 +503,35 @@ function View:abandon_all_windows() end ---@param opts table|nil +---@param callsite string ---@return boolean -function View:is_visible(opts) - -- TODO multi-instance rewrite and consistency check +function View:is_visible(opts, callsite) + local msg + + --- BEGIN multi-instance FF + if self.explorer.opts.experimental.multi_instance then + msg = string.format("View:is_visible(%s, %-20.20s)", vim.inspect(opts, { newline = "" }), callsite) + end + --- END multi-instance FF + if opts and opts.tabpage then + --- BEGIN multi-instance FF + if self.explorer.opts.experimental.multi_instance then + local winid = self:winid(opts.tabpage) + local winid_by_tabid = opts.tabpage and globals.WINID_BY_TABID[opts.tabpage] or nil + msg = string.format("%s globals.WINID_BY_TABID[%s]=w%s view.winid(%s)=w%s", + msg, + opts.tabpage, winid_by_tabid, + opts.tabpage, winid + ) + if winid ~= winid_by_tabid then + msg = string.format("%s MISMATCH", msg) + notify.error(msg) + end + log.line("dev", "%s", msg) + end + --- END multi-instance FF + if not globals.WINID_BY_TABID[opts.tabpage] then return false end @@ -502,8 +540,24 @@ function View:is_visible(opts) end if opts and opts.any_tabpage then - for _, winid in pairs(globals.WINID_BY_TABID) do - if winid and vim.api.nvim_win_is_valid(winid) then + for tabid, winid_by_tabid in pairs(globals.WINID_BY_TABID) do + --- BEGIN multi-instance FF + if self.explorer.opts.experimental.multi_instance then + local winid = self:winid(tabid) + msg = string.format("%s globals.WINID_BY_TABID[%s]=w%s view.winid(%s)=w%s", + msg, + tabid, winid_by_tabid, + tabid, winid + ) + if winid ~= winid_by_tabid then + msg = string.format("%s MISMATCH", msg) + notify.error(msg) + end + log.line("dev", "%s", msg) + end + --- END multi-instance FF + + if winid_by_tabid and vim.api.nvim_win_is_valid(winid_by_tabid) then return true end end @@ -516,7 +570,7 @@ end ---@param opts table|nil function View:set_cursor(opts) - if self:is_visible() then + if self:is_visible(nil, "View:set_cursor") then pcall(vim.api.nvim_win_set_cursor, self:get_winid(nil, "View:set_cursor"), opts) end end @@ -530,7 +584,7 @@ function View:focus(winid, open_if_closed) self:close(nil, "View:focus") self:open() wid = self:get_winid(nil, "View:focus2") - elseif open_if_closed and not self:is_visible() then + elseif open_if_closed and not self:is_visible(nil, "View:focus") then self:open() end @@ -547,7 +601,7 @@ function View:api_winid(opts) if tabpage == 0 then tabpage = vim.api.nvim_get_current_tabpage() end - if self:is_visible({ tabpage = tabpage }) then + if self:is_visible({ tabpage = tabpage }, "View:api_winid") then return self:get_winid(tabpage, "View:winid") else return nil @@ -559,8 +613,6 @@ function View:restore_tab_state() self:set_cursor(globals.CURSORS[vim.api.nvim_get_current_tabpage()]) end ---- TODO multi-instance remove comment ---- not legacy codepath --- winid containing the buffer ---@param tabid number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage. ---@return integer? winid @@ -583,8 +635,9 @@ end function View:get_winid(tabid, callsite) local tabid_param = tabid tabid = tabid or vim.api.nvim_get_current_tabpage() - local tabinfo_winid = nil + local global_winid = nil + --- BEGIN multi-instance FF if self.explorer.opts.experimental.multi_instance then local msg_fault = "" if not globals.WINID_BY_TABID[tabid] then @@ -592,33 +645,34 @@ function View:get_winid(tabid, callsite) elseif not vim.api.nvim_win_is_valid(globals.WINID_BY_TABID[tabid]) then msg_fault = string.format("invalid globals.WINID_BY_TABID[tabid] %d", globals.WINID_BY_TABID[tabid]) else - tabinfo_winid = globals.WINID_BY_TABID[tabid] + global_winid = globals.WINID_BY_TABID[tabid] end local winid = self:winid(tabid) - if winid ~= tabinfo_winid then + if winid ~= global_winid then msg_fault = "MISMATCH" end - local msg = string.format("View:get_winid(%3s, %-20.20s) globals.TABPAGES[%s]=w%s view.winid(%s)=w%s %s", + local msg = string.format("View:get_winid(%3s, %-20.20s) globals.WINID_BY_TABID[%s]=w%s view.winid(%s)=w%s %s", tabid_param, callsite, - tabid, tabinfo_winid, + tabid, global_winid, tabid, winid, msg_fault ) log.line("dev", "%s", msg) - if winid ~= tabinfo_winid then + if winid ~= global_winid then notify.error(msg) end end + --- END multi-instance FF -- legacy codepath - if tabinfo_winid and vim.api.nvim_win_is_valid(tabinfo_winid) then - return tabinfo_winid + if global_winid and vim.api.nvim_win_is_valid(global_winid) then + return global_winid end end @@ -627,6 +681,8 @@ end ---@return number function View:get_bufnr(callsite) local tab = vim.api.nvim_get_current_tabpage() + + --- BEGIN multi-instance FF if self.explorer.opts.experimental.multi_instance then local msg = string.format("View:get_bufnr(%-20.20s) globals.BUFNR_BY_TABID[%s]=b%s view.bufnr_by_tab[%s]=b%s %s", callsite, @@ -641,6 +697,8 @@ function View:get_bufnr(callsite) log.line("dev", msg) end + --- END multi-instance FF + return globals.BUFNR_BY_TABID[tab] end @@ -657,7 +715,6 @@ function View:prevent_buffer_override() local curbuf = vim.api.nvim_win_get_buf(curwin) local bufname = vim.api.nvim_buf_get_name(curbuf) - --- TODO multi-instance this can be removed as winid() will handle it if not bufname:match("NvimTree") then for i, winid in ipairs(globals.WINID_BY_TABID) do if winid == view_winid then From 4d6c42356a421ec34435743137246becd8c9c201 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Fri, 20 Jun 2025 12:36:09 +1000 Subject: [PATCH 13/17] refactor(#2826): more consistency checking --- lua/nvim-tree.lua | 4 ++-- lua/nvim-tree/actions/finders/find-file.lua | 4 ++-- lua/nvim-tree/actions/tree/find-file.lua | 2 +- lua/nvim-tree/actions/tree/open.lua | 2 +- lua/nvim-tree/actions/tree/toggle.lua | 2 +- lua/nvim-tree/explorer/init.lua | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 78a27a7a696..41335c2fb23 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -74,7 +74,7 @@ end function M.tab_enter() local explorer = core.get_explorer() - if explorer and explorer.view:is_visible({ any_tabpage = true }) then + if explorer and explorer.view:is_visible({ any_tabpage = true }, "nvim-tree.tab_enter") then local bufname = vim.api.nvim_buf_get_name(0) local ft @@ -97,7 +97,7 @@ end function M.open_on_directory() local explorer = core.get_explorer() - local should_proceed = _config.hijack_directories.auto_open or explorer and explorer.view:is_visible() + local should_proceed = _config.hijack_directories.auto_open or explorer and explorer.view:is_visible(nil, "nvim-tree.open_on_directory") if not should_proceed then return end diff --git a/lua/nvim-tree/actions/finders/find-file.lua b/lua/nvim-tree/actions/finders/find-file.lua index 2ea494fd85c..2a56b92dbf2 100644 --- a/lua/nvim-tree/actions/finders/find-file.lua +++ b/lua/nvim-tree/actions/finders/find-file.lua @@ -13,7 +13,7 @@ local running = {} ---@param path string relative or absolute function M.fn(path) local explorer = core.get_explorer() - if not explorer or not explorer.view:is_visible() then + if not explorer or not explorer.view:is_visible(nil, "finders/find-file.fn1") then return end @@ -83,7 +83,7 @@ function M.fn(path) end) :iterate() - if found and explorer.view:is_visible() then + if found and explorer.view:is_visible(nil, "finders/find-file.fn2") then explorer.renderer:draw() explorer.view:set_cursor({ line, 0 }) end diff --git a/lua/nvim-tree/actions/tree/find-file.lua b/lua/nvim-tree/actions/tree/find-file.lua index 73d1df3c75e..0694183fd70 100644 --- a/lua/nvim-tree/actions/tree/find-file.lua +++ b/lua/nvim-tree/actions/tree/find-file.lua @@ -41,7 +41,7 @@ function M.fn(opts) end local explorer = core.get_explorer() - if explorer and explorer.view:is_visible() then + if explorer and explorer.view:is_visible(nil, "tree/find-file.fn") then -- focus if opts.focus then lib.set_target_win() diff --git a/lua/nvim-tree/actions/tree/open.lua b/lua/nvim-tree/actions/tree/open.lua index 17e2c581a7b..6ee4adf137e 100644 --- a/lua/nvim-tree/actions/tree/open.lua +++ b/lua/nvim-tree/actions/tree/open.lua @@ -25,7 +25,7 @@ function M.fn(opts) local explorer = core.get_explorer() - if explorer and explorer.view:is_visible() then + if explorer and explorer.view:is_visible(nil, "open.fn") then -- focus lib.set_target_win() explorer.view:focus() diff --git a/lua/nvim-tree/actions/tree/toggle.lua b/lua/nvim-tree/actions/tree/toggle.lua index d2ba0262a30..d92630361ff 100644 --- a/lua/nvim-tree/actions/tree/toggle.lua +++ b/lua/nvim-tree/actions/tree/toggle.lua @@ -42,7 +42,7 @@ function M.fn(opts, no_focus, cwd, bang) opts.path = nil end - if explorer and explorer.view:is_visible() then + if explorer and explorer.view:is_visible(nil, "toggle.fn") then -- close explorer.view:close(nil, "toggle.fn") else diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index 6625d5b0870..947403a6fda 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -532,7 +532,7 @@ function Explorer:reload_explorer() local projects = git.reload_all_projects() self:refresh_nodes(projects) - if self.view:is_visible() then + if self.view:is_visible(nil, "Explorer:reload_explorer") then self.renderer:draw() end event_running = false From d84dfad1c368a44767044f20cc35f0c41c17a2b2 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Fri, 20 Jun 2025 12:46:30 +1000 Subject: [PATCH 14/17] refactor(#2826): move global CURSORS to view member --- lua/nvim-tree/explorer/view.lua | 11 ++++++----- lua/nvim-tree/globals.lua | 1 - lua/nvim-tree/lib.lua | 3 +-- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index 0cbdb899241..a3e6d90000d 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -19,6 +19,7 @@ local Class = require("nvim-tree.classic") ---@field private max_width integer ---@field private padding integer ---@field private bufnr_by_tabid table stored per tab until multi-instance is complete +---@field private cursor integer[] as per vim.api.nvim_win_get_cursor local View = Class:extend() ---@class View @@ -235,12 +236,12 @@ local function switch_buf_if_last_buf() end end ----save_tab_state saves any state that should be preserved across redraws. +---save any state that should be preserved on reopening ---@private ---@param tabid integer function View:save_tab_state(tabid) tabid = tabid or vim.api.nvim_get_current_tabpage() - globals.CURSORS[tabid] = vim.api.nvim_win_get_cursor(self:get_winid(tabid, "View:save_tab_state") or 0) + self.cursor = vim.api.nvim_win_get_cursor(self:get_winid(tabid, "View:save_tab_state") or 0) end ---@private @@ -608,9 +609,9 @@ function View:api_winid(opts) end end ---- Restores the state of a NvimTree window if it was initialized before. -function View:restore_tab_state() - self:set_cursor(globals.CURSORS[vim.api.nvim_get_current_tabpage()]) +--- restore any state from last close +function View:restore_state() + self:set_cursor(self.cursor) end --- winid containing the buffer diff --git a/lua/nvim-tree/globals.lua b/lua/nvim-tree/globals.lua index 66211260bc6..a2ab7527960 100644 --- a/lua/nvim-tree/globals.lua +++ b/lua/nvim-tree/globals.lua @@ -4,7 +4,6 @@ local M = { -- from View WINID_BY_TABID = {}, BUFNR_BY_TABID = {}, - CURSORS = {}, } return M diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index 854e33bfd09..6c1ef1ef2d1 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -135,9 +135,8 @@ function M.open(opts) open_view_and_draw() end - -- TODO multi-instance is this actually necessary? if explorer then - explorer.view:restore_tab_state() + explorer.view:restore_state() end end From 82cc80ffa435c9f47c0244007e3af139cc915ae0 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Fri, 20 Jun 2025 12:47:04 +1000 Subject: [PATCH 15/17] Revert "refactor(#2826): move global CURSORS to view member" This reverts commit d84dfad1c368a44767044f20cc35f0c41c17a2b2. --- lua/nvim-tree/explorer/view.lua | 11 +++++------ lua/nvim-tree/globals.lua | 1 + lua/nvim-tree/lib.lua | 3 ++- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index a3e6d90000d..0cbdb899241 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -19,7 +19,6 @@ local Class = require("nvim-tree.classic") ---@field private max_width integer ---@field private padding integer ---@field private bufnr_by_tabid table stored per tab until multi-instance is complete ----@field private cursor integer[] as per vim.api.nvim_win_get_cursor local View = Class:extend() ---@class View @@ -236,12 +235,12 @@ local function switch_buf_if_last_buf() end end ----save any state that should be preserved on reopening +---save_tab_state saves any state that should be preserved across redraws. ---@private ---@param tabid integer function View:save_tab_state(tabid) tabid = tabid or vim.api.nvim_get_current_tabpage() - self.cursor = vim.api.nvim_win_get_cursor(self:get_winid(tabid, "View:save_tab_state") or 0) + globals.CURSORS[tabid] = vim.api.nvim_win_get_cursor(self:get_winid(tabid, "View:save_tab_state") or 0) end ---@private @@ -609,9 +608,9 @@ function View:api_winid(opts) end end ---- restore any state from last close -function View:restore_state() - self:set_cursor(self.cursor) +--- Restores the state of a NvimTree window if it was initialized before. +function View:restore_tab_state() + self:set_cursor(globals.CURSORS[vim.api.nvim_get_current_tabpage()]) end --- winid containing the buffer diff --git a/lua/nvim-tree/globals.lua b/lua/nvim-tree/globals.lua index a2ab7527960..66211260bc6 100644 --- a/lua/nvim-tree/globals.lua +++ b/lua/nvim-tree/globals.lua @@ -4,6 +4,7 @@ local M = { -- from View WINID_BY_TABID = {}, BUFNR_BY_TABID = {}, + CURSORS = {}, } return M diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index 6c1ef1ef2d1..854e33bfd09 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -135,8 +135,9 @@ function M.open(opts) open_view_and_draw() end + -- TODO multi-instance is this actually necessary? if explorer then - explorer.view:restore_state() + explorer.view:restore_tab_state() end end From 51b269dc7197829acb8b36bacaa1581f00f17c9b Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Fri, 20 Jun 2025 12:52:58 +1000 Subject: [PATCH 16/17] refactor(#2826): move global CURSORS to view member --- lua/nvim-tree/explorer/view.lua | 32 ++++++++++++++++++-------------- lua/nvim-tree/lib.lua | 3 +-- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index 0cbdb899241..802530597b7 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -18,7 +18,10 @@ local Class = require("nvim-tree.classic") ---@field private width (fun():integer)|integer|string ---@field private max_width integer ---@field private padding integer ----@field private bufnr_by_tabid table stored per tab until multi-instance is complete +-- TODO multi-instance remove or replace with single member +---@field private bufnr_by_tabid table +-- TODO multi-instance change to single member +---@field private cursors_by_tabid table as per vim.api.nvim_win_get_cursor local View = Class:extend() ---@class View @@ -32,13 +35,14 @@ local View = Class:extend() function View:new(args) args.explorer:log_new("View") - self.explorer = args.explorer - self.adaptive_size = false - self.side = (self.explorer.opts.view.side == "right") and "right" or "left" - self.live_filter = { prev_focused_node = nil, } - self.bufnr_by_tabid = {} + self.explorer = args.explorer + self.adaptive_size = false + self.side = (self.explorer.opts.view.side == "right") and "right" or "left" + self.live_filter = { prev_focused_node = nil, } + self.bufnr_by_tabid = {} + self.cursors_by_tabid = {} - self.winopts = { + self.winopts = { relativenumber = self.explorer.opts.view.relativenumber, number = self.explorer.opts.view.number, list = false, @@ -235,12 +239,12 @@ local function switch_buf_if_last_buf() end end ----save_tab_state saves any state that should be preserved across redraws. +---save any state that should be preserved on reopening ---@private ---@param tabid integer -function View:save_tab_state(tabid) +function View:save_state(tabid) tabid = tabid or vim.api.nvim_get_current_tabpage() - globals.CURSORS[tabid] = vim.api.nvim_win_get_cursor(self:get_winid(tabid, "View:save_tab_state") or 0) + self.cursors_by_tabid[tabid] = vim.api.nvim_win_get_cursor(self:get_winid(tabid, "View:save_tab_state") or 0) end ---@private @@ -255,7 +259,7 @@ function View:close_internal(tabid) if not self:is_visible({ tabpage = tabid }, "View:close_internal") then return end - self:save_tab_state(tabid) + self:save_state(tabid) switch_buf_if_last_buf() local tree_win = self:get_winid(tabid, "View:close_internal") local current_win = vim.api.nvim_get_current_win() @@ -608,9 +612,9 @@ function View:api_winid(opts) end end ---- Restores the state of a NvimTree window if it was initialized before. -function View:restore_tab_state() - self:set_cursor(globals.CURSORS[vim.api.nvim_get_current_tabpage()]) +---restore any state from last close +function View:restore_state() + self:set_cursor(self.cursors_by_tabid[vim.api.nvim_get_current_tabpage()]) end --- winid containing the buffer diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index 854e33bfd09..6c1ef1ef2d1 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -135,9 +135,8 @@ function M.open(opts) open_view_and_draw() end - -- TODO multi-instance is this actually necessary? if explorer then - explorer.view:restore_tab_state() + explorer.view:restore_state() end end From 0830436cca6cf00dcd709e76bc4a3476f1c9704e Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Fri, 20 Jun 2025 13:11:21 +1000 Subject: [PATCH 17/17] refactor(#2826): consistency check returns new --- lua/nvim-tree/explorer/view.lua | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/lua/nvim-tree/explorer/view.lua b/lua/nvim-tree/explorer/view.lua index 802530597b7..7fa651e8b8b 100644 --- a/lua/nvim-tree/explorer/view.lua +++ b/lua/nvim-tree/explorer/view.lua @@ -533,14 +533,13 @@ function View:is_visible(opts, callsite) notify.error(msg) end log.line("dev", "%s", msg) - end - --- END multi-instance FF - if not globals.WINID_BY_TABID[opts.tabpage] then - return false + return winid and vim.api.nvim_win_is_valid(winid) or false + --- END multi-instance FF + else + local winid = globals.WINID_BY_TABID[opts.tabpage] + return winid and vim.api.nvim_win_is_valid(winid) end - local winid = globals.WINID_BY_TABID[opts.tabpage] - return winid and vim.api.nvim_win_is_valid(winid) end if opts and opts.any_tabpage then @@ -558,11 +557,15 @@ function View:is_visible(opts, callsite) notify.error(msg) end log.line("dev", "%s", msg) - end - --- END multi-instance FF - if winid_by_tabid and vim.api.nvim_win_is_valid(winid_by_tabid) then - return true + if winid and vim.api.nvim_win_is_valid(winid) then + return true + end + --- END multi-instance FF + else + if winid_by_tabid and vim.api.nvim_win_is_valid(winid_by_tabid) then + return true + end end end return false @@ -671,6 +674,8 @@ function View:get_winid(tabid, callsite) if winid ~= global_winid then notify.error(msg) end + + return winid end --- END multi-instance FF @@ -700,6 +705,8 @@ function View:get_bufnr(callsite) end log.line("dev", msg) + + return self.bufnr_by_tabid[tab] end --- END multi-instance FF