Skip to content

Commit de2ae0b

Browse files
committed
refactor(#2826): consistent use of buffer registry, tidy, add todos
1 parent d6cd465 commit de2ae0b

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

lua/nvim-tree/explorer/init.lua

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

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

9093
RootNode.destroy(self)

lua/nvim-tree/explorer/view.lua

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,7 @@ local globals = require("nvim-tree.globals")
77

88
local Class = require("nvim-tree.classic")
99

10-
---@class OpenInWinOpts
11-
---@field hijack_current_buf boolean|nil default true
12-
---@field resize boolean|nil default true
13-
---@field winid number|nil 0 or nil for current
14-
15-
local DEFAULT_MIN_WIDTH = 30
16-
local DEFAULT_MAX_WIDTH = -1
17-
local DEFAULT_PADDING = 1
18-
10+
---Window and buffer related settings and operations
1911
---@class (exact) View: Class
2012
---@field live_filter table
2113
---@field side string
@@ -101,6 +93,7 @@ local BUFFER_OPTIONS = {
10193
{ name = "swapfile", value = false },
10294
}
10395

96+
-- TODO multi-instance remove this; delete buffers rather than retaining them
10497
---@private
10598
---@param bufnr integer
10699
---@return boolean
@@ -113,6 +106,7 @@ function View:matches_bufnr(bufnr)
113106
return false
114107
end
115108

109+
-- TODO multi-instance remove this; delete buffers rather than retaining them
116110
---@private
117111
function View:wipe_rogue_buffer()
118112
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
@@ -128,13 +122,12 @@ function View:create_buffer(bufnr)
128122
self:wipe_rogue_buffer()
129123

130124
local tab = vim.api.nvim_get_current_tabpage()
131-
globals.BUFNR_PER_TAB[tab] = bufnr or vim.api.nvim_create_buf(false, false)
132125

133-
if self.explorer.opts.experimental.multi_instance then
134-
self.bufnr_by_tab[tab] = globals.BUFNR_PER_TAB[tab]
135-
end
126+
bufnr = bufnr or vim.api.nvim_create_buf(false, false)
136127

137-
bufnr = self:get_bufnr("View:create_buffer")
128+
-- set both bufnr registries
129+
globals.BUFNR_PER_TAB[tab] = bufnr
130+
self.bufnr_by_tab[tab] = bufnr
138131

139132
vim.api.nvim_buf_set_name(bufnr, "NvimTree_" .. tab)
140133

@@ -481,6 +474,11 @@ function View:set_current_win(callsite)
481474
globals.TABPAGES[current_tab].winid = current_win
482475
end
483476

477+
---@class OpenInWinOpts
478+
---@field hijack_current_buf boolean|nil default true
479+
---@field resize boolean|nil default true
480+
---@field winid number|nil 0 or nil for current
481+
484482
---Open the tree in the a window
485483
---@param opts OpenInWinOpts|nil
486484
function View:open_in_win(opts)
@@ -510,13 +508,14 @@ function View:abandon_current_window()
510508
globals.BUFNR_PER_TAB[tab],
511509
self.bufnr_by_tab[tab],
512510
(globals.BUFNR_PER_TAB[tab] == self.bufnr_by_tab[tab]) and "" or "MISMATCH")
513-
514-
self.bufnr_by_tab[tab] = nil
515511
end
516512

517-
-- TODO multi-instance kill the buffer instead of retaining
513+
-- TODO multi-instance maybe kill the buffer instead of retaining
518514

515+
-- reset both bufnr registries
519516
globals.BUFNR_PER_TAB[tab] = nil
517+
self.bufnr_by_tab[tab] = nil
518+
520519
if globals.TABPAGES[tab] then
521520
globals.TABPAGES[tab].winid = nil
522521
end
@@ -614,6 +613,8 @@ function View:restore_tab_state()
614613
self:set_cursor(globals.CURSORS[tabpage])
615614
end
616615

616+
--- TODO multi-instance remove comment
617+
--- not legacy codepath
617618
--- winid containing the buffer
618619
---@param tabpage number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage.
619620
---@param callsite string
@@ -689,7 +690,7 @@ end
689690
function View:get_bufnr(callsite)
690691
local tab = vim.api.nvim_get_current_tabpage()
691692
if self.explorer.opts.experimental.multi_instance then
692-
local msg = string.format("View:get_bufnr(%-20.20s) globals.BUFNR_PER_TAB[%s]=b%s view.bufnr_by_tab[%s]=b%s MISMATCH",
693+
local msg = string.format("View:get_bufnr(%-20.20s) globals.BUFNR_PER_TAB[%s]=b%s view.bufnr_by_tab[%s]=b%s %s",
693694
callsite,
694695
tab, globals.BUFNR_PER_TAB[tab],
695696
tab, self.bufnr_by_tab[tab],
@@ -765,7 +766,7 @@ end
765766

766767
-- used on ColorScheme event
767768
function View:reset_winhl()
768-
local winid = self:get_winid(nil, "View:reset_winhl1")
769+
local winid = self:get_winid(nil, "View:reset_winhl")
769770
if winid and vim.api.nvim_win_is_valid(winid) then
770771
vim.wo[winid].winhl = appearance.WIN_HL
771772
end
@@ -777,6 +778,10 @@ function View:is_width_determined()
777778
return type(self.width) ~= "function"
778779
end
779780

781+
local DEFAULT_MIN_WIDTH = 30
782+
local DEFAULT_MAX_WIDTH = -1
783+
local DEFAULT_PADDING = 1
784+
780785
---Configure width-related config
781786
---@param width string|function|number|table|nil
782787
function View:configure_width(width)

0 commit comments

Comments
 (0)