Skip to content

Commit fd0bcad

Browse files
committed
Revert "refactor(#2826): View tracks winids and bufnrs via events, unused for now (#3170)"
This reverts commit 65bae44.
1 parent 99b2980 commit fd0bcad

File tree

2 files changed

+34
-77
lines changed

2 files changed

+34
-77
lines changed

lua/nvim-tree/explorer/init.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,22 @@ 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+
168184
vim.api.nvim_create_autocmd("BufEnter", {
169185
group = self.augroup_id,
170186
pattern = "NvimTree_*",

lua/nvim-tree/explorer/view.lua

Lines changed: 18 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ 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 replace with single members
21+
-- TODO multi-instance remove or replace with single member
2222
---@field private bufnr_by_tabid table<integer, integer>
23-
---@field private winid_by_tabid table<integer, integer>
2423
local View = Class:extend()
2524

2625
---@class View
@@ -34,14 +33,13 @@ local View = Class:extend()
3433
function View:new(args)
3534
args.explorer:log_new("View")
3635

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 = {}
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 = {}
4341

44-
self.winopts = {
42+
self.winopts = {
4543
relativenumber = self.explorer.opts.view.relativenumber,
4644
number = self.explorer.opts.view.number,
4745
list = false,
@@ -62,6 +60,10 @@ function View:new(args)
6260

6361
self:configure_width(self.explorer.opts.view.width)
6462
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]
6567
end
6668

6769
function View:destroy()
@@ -78,64 +80,6 @@ local BUFFER_OPTIONS = {
7880
{ name = "swapfile", value = false },
7981
}
8082

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-
13983
-- TODO multi-instance remove this; delete buffers rather than retaining them
14084
---@private
14185
---@param bufnr integer
@@ -168,18 +112,16 @@ function View:create_buffer(bufnr)
168112

169113
bufnr = bufnr or vim.api.nvim_create_buf(false, false)
170114

171-
self.bufnr_by_tabid[tabid] = bufnr
172-
115+
-- set both bufnr registries
173116
globals.BUFNR_BY_TABID[tabid] = bufnr
117+
self.bufnr_by_tabid[tabid] = bufnr
174118

175119
vim.api.nvim_buf_set_name(bufnr, "NvimTree_" .. tabid)
176120

177121
for _, option in ipairs(BUFFER_OPTIONS) do
178122
vim.api.nvim_set_option_value(option.name, option.value, { buf = bufnr })
179123
end
180124

181-
self:create_autocmds(bufnr)
182-
183125
require("nvim-tree.keymap").on_attach(bufnr)
184126

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

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

223163
if vim.fn.has("nvim-0.10") == 1 then
224164
local eventignore = vim.api.nvim_get_option_value("eventignore", {})
@@ -506,7 +446,9 @@ end
506446
function View:abandon_current_window()
507447
local tab = vim.api.nvim_get_current_tabpage()
508448

449+
-- reset both bufnr registries
509450
globals.BUFNR_BY_TABID[tab] = nil
451+
self.bufnr_by_tabid[tab] = nil
510452

511453
globals.WINID_BY_TABID[tab] = nil
512454
end
@@ -590,7 +532,7 @@ end
590532
---@param tabid number|nil (optional) the number of the chosen tabpage. Defaults to current tabpage.
591533
---@return integer? winid
592534
function View:winid(tabid)
593-
local bufnr = globals.BUFNR_BY_TABID[tabid]
535+
local bufnr = self.bufnr_by_tabid[tabid]
594536

595537
if bufnr then
596538
for _, winid in pairs(vim.api.nvim_tabpage_list_wins(tabid or 0)) do
@@ -601,7 +543,6 @@ function View:winid(tabid)
601543
end
602544
end
603545

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

618-
return globals.BUFNR_BY_TABID[tab]
559+
return self.bufnr_by_tabid[tab]
619560
end
620561

621562
function View:prevent_buffer_override()

0 commit comments

Comments
 (0)