Skip to content

Commit 65bae44

Browse files
authored
refactor(#2826): View tracks winids and bufnrs via events, unused for now (#3170)
* refactor(#2826): add View.bufnr and use for all public methods * use View.bufnr for all internal methods * revert View.bufnr, add buffer local autocommands * View revert to globals.BUFNR_BY_TABID until bufnr and winid are tracked for the view * View track winids and bufnrs via events
1 parent a9156c0 commit 65bae44

File tree

2 files changed

+77
-34
lines changed

2 files changed

+77
-34
lines changed

lua/nvim-tree/explorer/init.lua

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -165,22 +165,6 @@ function Explorer:create_autocmds()
165165
end,
166166
})
167167

168-
-- prevent new opened file from opening in the same window as nvim-tree
169-
vim.api.nvim_create_autocmd("BufWipeout", {
170-
group = self.augroup_id,
171-
pattern = "NvimTree_*",
172-
callback = function()
173-
if not utils.is_nvim_tree_buf(0) then
174-
return
175-
end
176-
if self.opts.actions.open_file.eject then
177-
self.view:prevent_buffer_override()
178-
else
179-
self.view:abandon_current_window()
180-
end
181-
end,
182-
})
183-
184168
vim.api.nvim_create_autocmd("BufEnter", {
185169
group = self.augroup_id,
186170
pattern = "NvimTree_*",

lua/nvim-tree/explorer/view.lua

Lines changed: 77 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ local Class = require("nvim-tree.classic")
1818
---@field private width (fun():integer)|integer|string
1919
---@field private max_width integer
2020
---@field private padding integer
21-
-- TODO multi-instance remove or replace with single member
21+
-- TODO multi-instance replace with single members
2222
---@field private bufnr_by_tabid table<integer, integer>
23+
---@field private winid_by_tabid table<integer, integer>
2324
local View = Class:extend()
2425

2526
---@class View
@@ -33,13 +34,14 @@ local View = Class:extend()
3334
function View:new(args)
3435
args.explorer:log_new("View")
3536

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

42-
self.winopts = {
44+
self.winopts = {
4345
relativenumber = self.explorer.opts.view.relativenumber,
4446
number = self.explorer.opts.view.number,
4547
list = false,
@@ -60,10 +62,6 @@ function View:new(args)
6062

6163
self:configure_width(self.explorer.opts.view.width)
6264
self.initial_width = self:get_width()
63-
64-
-- TODO multi-instance remove this; delete buffers rather than retaining them
65-
local tabid = vim.api.nvim_get_current_tabpage()
66-
self.bufnr_by_tabid[tabid] = globals.BUFNR_BY_TABID[tabid]
6765
end
6866

6967
function View:destroy()
@@ -80,6 +78,64 @@ local BUFFER_OPTIONS = {
8078
{ name = "swapfile", value = false },
8179
}
8280

81+
---Buffer local autocommands to track state, deleted on buffer wipeout
82+
---@private
83+
---@param bufnr integer
84+
function View:create_autocmds(bufnr)
85+
-- clear bufnr and winid
86+
-- eject buffer opened in the nvim-tree window and create a new buffer
87+
vim.api.nvim_create_autocmd("BufWipeout", {
88+
group = self.explorer.augroup_id,
89+
buffer = bufnr,
90+
callback = function(data)
91+
log.line("dev",
92+
"View BufWipeout\n bufnr = %s\n data.buf = %s\n self.bufnr_by_tabid = %s\n self.winid_by_tabid = %s",
93+
bufnr,
94+
data.buf,
95+
vim.inspect(self.bufnr_by_tabid, { newline = "" }),
96+
vim.inspect(self.winid_by_tabid, { newline = "" }),
97+
vim.inspect(data, { newline = "" })
98+
)
99+
100+
-- clear the tab's buffer
101+
self.bufnr_by_tabid = vim.tbl_map(function(b)
102+
return b ~= bufnr and b or nil
103+
end, self.bufnr_by_tabid)
104+
105+
-- clear the tab's window(s)
106+
local winids = vim.fn.win_findbuf(bufnr)
107+
self.winid_by_tabid = vim.tbl_map(function(winid)
108+
return not vim.tbl_contains(winids, winid) and winid or nil
109+
end, self.winid_by_tabid)
110+
111+
if self.explorer.opts.actions.open_file.eject then
112+
self:prevent_buffer_override()
113+
else
114+
self:abandon_current_window()
115+
end
116+
end,
117+
})
118+
119+
-- set winid
120+
vim.api.nvim_create_autocmd("BufWinEnter", {
121+
group = self.explorer.augroup_id,
122+
buffer = bufnr,
123+
callback = function(data)
124+
local tabid = vim.api.nvim_get_current_tabpage()
125+
126+
log.line("dev",
127+
"View BufWinEnter\n bufnr = %s\n data.buf = %s\n self.bufnr_by_tabid = %s\n self.winid_by_tabid = %s",
128+
bufnr,
129+
data.buf,
130+
vim.inspect(self.bufnr_by_tabid, { newline = "" }),
131+
vim.inspect(self.winid_by_tabid, { newline = "" })
132+
)
133+
134+
self.winid_by_tabid[tabid] = vim.fn.bufwinid(data.buf) -- first on current tabpage
135+
end,
136+
})
137+
end
138+
83139
-- TODO multi-instance remove this; delete buffers rather than retaining them
84140
---@private
85141
---@param bufnr integer
@@ -112,16 +168,18 @@ function View:create_buffer(bufnr)
112168

113169
bufnr = bufnr or vim.api.nvim_create_buf(false, false)
114170

115-
-- set both bufnr registries
116-
globals.BUFNR_BY_TABID[tabid] = bufnr
117171
self.bufnr_by_tabid[tabid] = bufnr
118172

173+
globals.BUFNR_BY_TABID[tabid] = bufnr
174+
119175
vim.api.nvim_buf_set_name(bufnr, "NvimTree_" .. tabid)
120176

121177
for _, option in ipairs(BUFFER_OPTIONS) do
122178
vim.api.nvim_set_option_value(option.name, option.value, { buf = bufnr })
123179
end
124180

181+
self:create_autocmds(bufnr)
182+
125183
require("nvim-tree.keymap").on_attach(bufnr)
126184

127185
events._dispatch_tree_attached_post(bufnr)
@@ -158,7 +216,9 @@ local move_tbl = {
158216

159217
---@private
160218
function View:set_window_options_and_buffer()
161-
pcall(vim.api.nvim_command, "buffer " .. self:get_bufnr())
219+
if not pcall(vim.api.nvim_command, "buffer " .. self:get_bufnr()) then
220+
return
221+
end
162222

163223
if vim.fn.has("nvim-0.10") == 1 then
164224
local eventignore = vim.api.nvim_get_option_value("eventignore", {})
@@ -446,9 +506,7 @@ end
446506
function View:abandon_current_window()
447507
local tab = vim.api.nvim_get_current_tabpage()
448508

449-
-- reset both bufnr registries
450509
globals.BUFNR_BY_TABID[tab] = nil
451-
self.bufnr_by_tabid[tab] = nil
452510

453511
globals.WINID_BY_TABID[tab] = nil
454512
end
@@ -532,7 +590,7 @@ end
532590
---@param tabid number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage.
533591
---@return integer? winid
534592
function View:winid(tabid)
535-
local bufnr = self.bufnr_by_tabid[tabid]
593+
local bufnr = globals.BUFNR_BY_TABID[tabid]
536594

537595
if bufnr then
538596
for _, winid in pairs(vim.api.nvim_tabpage_list_wins(tabid or 0)) do
@@ -543,6 +601,7 @@ function View:winid(tabid)
543601
end
544602
end
545603

604+
--- TODO this needs to be refactored away; it's private now to contain it
546605
--- Returns the window number for nvim-tree within the tabpage specified
547606
---@param tabid number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage.
548607
---@return number|nil
@@ -556,7 +615,7 @@ end
556615
function View:get_bufnr()
557616
local tab = vim.api.nvim_get_current_tabpage()
558617

559-
return self.bufnr_by_tabid[tab]
618+
return globals.BUFNR_BY_TABID[tab]
560619
end
561620

562621
function View:prevent_buffer_override()

0 commit comments

Comments
 (0)