Skip to content

Commit f7de34e

Browse files
committed
feat(view): filters.no_buffer misses unloaded, handles buffer in/out
1 parent 8da6664 commit f7de34e

File tree

6 files changed

+35
-13
lines changed

6 files changed

+35
-13
lines changed

lua/nvim-tree.lua

+17
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ local reloaders = require "nvim-tree.actions.reloaders.reloaders"
1111
local copy_paste = require "nvim-tree.actions.fs.copy-paste"
1212
local collapse_all = require "nvim-tree.actions.tree-modifiers.collapse-all"
1313
local git = require "nvim-tree.git"
14+
local filters = require "nvim-tree.explorer.filters"
1415

1516
local _config = {}
1617

@@ -353,6 +354,22 @@ local function setup_autocommands(opts)
353354
create_nvim_tree_autocmd("BufWritePost", { callback = reloaders.reload_explorer })
354355
end
355356

357+
create_nvim_tree_autocmd("BufReadPost", {
358+
callback = function()
359+
if filters.config.filter_no_buffer then
360+
reloaders.reload_explorer()
361+
end
362+
end,
363+
})
364+
365+
create_nvim_tree_autocmd("BufUnload", {
366+
callback = function(data)
367+
if filters.config.filter_no_buffer then
368+
reloaders.reload_explorer(nil, data.buf)
369+
end
370+
end,
371+
})
372+
356373
if not has_watchers and opts.git.enable then
357374
create_nvim_tree_autocmd("User", {
358375
pattern = { "FugitiveChanged", "NeogitStatusRefreshed" },

lua/nvim-tree/actions/finders/search-node.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ local function search(search_dir, input_path)
1414
local function iter(dir)
1515
local realpath, path, name, stat, handle, _
1616

17-
local bufinfo = vim.fn.getbufinfo { bufloaded = 1, buflisted = 1 }
17+
local bufinfo = vim.fn.getbufinfo { buflisted = 1 }
1818

1919
handle, _ = vim.loop.fs_scandir(dir)
2020
if not handle then

lua/nvim-tree/actions/reloaders/reloaders.lua

+7-5
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ local core = require "nvim-tree.core"
66

77
local M = {}
88

9-
local function refresh_nodes(node, projects)
9+
local function refresh_nodes(node, projects, unloaded_bufnr)
1010
local cwd = node.cwd or node.link_to or node.absolute_path
1111
local project_root = git.get_project_root(cwd)
12-
explorer_module.reload(node, projects[project_root] or {})
12+
explorer_module.reload(node, projects[project_root] or {}, unloaded_bufnr)
1313
for _, _node in ipairs(node.nodes) do
1414
if _node.nodes and _node.open then
15-
refresh_nodes(_node, projects)
15+
refresh_nodes(_node, projects, unloaded_bufnr)
1616
end
1717
end
1818
end
@@ -33,14 +33,16 @@ function M.reload_node_status(parent_node, projects)
3333
end
3434

3535
local event_running = false
36-
function M.reload_explorer()
36+
---@param _ table unused node passed by action
37+
---@param unloaded_bufnr number optional bufnr recently unloaded via BufUnload event
38+
function M.reload_explorer(_, unloaded_bufnr)
3739
if event_running or not core.get_explorer() or vim.v.exiting ~= vim.NIL then
3840
return
3941
end
4042
event_running = true
4143

4244
local projects = git.reload()
43-
refresh_nodes(core.get_explorer(), projects)
45+
refresh_nodes(core.get_explorer(), projects, unloaded_bufnr)
4446
if view.is_visible() then
4547
renderer.draw()
4648
end

lua/nvim-tree/explorer/explore.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ end
1515
local function populate_children(handle, cwd, node, status)
1616
local node_ignored = node.git_status == "!!"
1717
local nodes_by_path = utils.bool_record(node.nodes, "absolute_path")
18-
local bufinfo = vim.fn.getbufinfo { bufloaded = 1, buflisted = 1 }
18+
local bufinfo = vim.fn.getbufinfo { buflisted = 1 }
1919
while true do
2020
local name, t = vim.loop.fs_scandir_next(handle)
2121
if not name then

lua/nvim-tree/explorer/filters.lua

+6-3
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,23 @@ end
1616

1717
---Check if the given path should be ignored.
1818
---@param path string Absolute path
19-
---@param bufinfo table vim.fn.getbufinfo { bufloaded = 1, buflisted = 1 }
19+
---@param bufinfo table vim.fn.getbufinfo { buflisted = 1 }
20+
---@param unloaded_bufnr number optional bufnr recently unloaded via BufUnload event
2021
---@return boolean
21-
function M.should_ignore(path, bufinfo)
22+
function M.should_ignore(path, bufinfo, unloaded_bufnr)
2223
local basename = utils.path_basename(path)
2324

2425
-- exclusions override all filters
2526
if is_excluded(path) then
2627
return false
2728
end
2829

30+
-- TODO exact match for files, ../ for dirs
31+
2932
-- filter files with no open buffer and directories containing no open buffers
3033
if M.config.filter_no_buffer and type(bufinfo) == "table" then
3134
for _, buf in ipairs(bufinfo) do
32-
if buf.name:find(path, 1, true) then
35+
if buf.name:find(path, 1, true) and buf.bufnr ~= unloaded_bufnr then
3336
return false
3437
end
3538
end

lua/nvim-tree/explorer/reload.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ local function update_parent_statuses(node, project, root)
3434
end
3535
end
3636

37-
function M.reload(node, status)
37+
function M.reload(node, status, unloaded_bufnr)
3838
local cwd = node.link_to or node.absolute_path
3939
local handle = vim.loop.fs_scandir(cwd)
4040
if type(handle) == "string" then
@@ -44,7 +44,7 @@ function M.reload(node, status)
4444

4545
local ps = log.profile_start("reload %s", node.absolute_path)
4646

47-
local bufinfo = vim.fn.getbufinfo { bufloaded = 1, buflisted = 1 }
47+
local bufinfo = vim.fn.getbufinfo { buflisted = 1 }
4848

4949
if node.group_next then
5050
node.nodes = { node.group_next }
@@ -73,7 +73,7 @@ function M.reload(node, status)
7373

7474
local abs = utils.path_join { cwd, name }
7575
t = t or (fs_stat_cached(abs) or {}).type
76-
if not filters.should_ignore(abs, bufinfo) and not filters.should_ignore_git(abs, status) then
76+
if not filters.should_ignore(abs, bufinfo, unloaded_bufnr) and not filters.should_ignore_git(abs, status) then
7777
child_names[abs] = true
7878

7979
-- Recreate node if type changes.

0 commit comments

Comments
 (0)