Skip to content

Commit 8da6664

Browse files
committed
feat(view): add filters.no_buffer
1 parent 48c3afa commit 8da6664

File tree

10 files changed

+57
-10
lines changed

10 files changed

+57
-10
lines changed

doc/nvim-tree-lua.txt

+11-1
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,10 @@ Subsequent calls to setup will replace the previous configuration.
304304
},
305305
filters = {
306306
dotfiles = false,
307+
git_clean = false,
308+
no_buffer = false,
307309
custom = {},
308310
exclude = {},
309-
git_clean = false,
310311
},
311312
filesystem_watchers = {
312313
enable = true,
@@ -893,6 +894,12 @@ Filtering options.
893894
*nvim-tree.filters.git_clean*
894895
Do not show files with no git status. This will show ignored files when
895896
|nvim-tree.git.ignore| is set, as they are effectively dirty.
897+
Toggle via the `toggle_git_clean` action, default mapping `C`.
898+
Type: `boolean`, Default: `false`
899+
900+
*nvim-tree.filters.no_buffer*
901+
Do not show files that have no open buffer.
902+
Toggle via the `toggle_no_buffer` action, default mapping `B`.
896903
Type: `boolean`, Default: `false`
897904

898905
*nvim-tree.filters.custom*
@@ -1181,6 +1188,7 @@ exists.
11811188
- expand_all
11821189
- toggle_gitignore_filter
11831190
- toggle_git_clean_filter
1191+
- toggle_no_buffer_filter
11841192
- toggle_custom_filter
11851193
- toggle_hidden_filter
11861194
- toggle_help
@@ -1315,6 +1323,7 @@ DEFAULT MAPPINGS *nvim-tree-default-mappings
13151323
`C` toggle_git_clean toggle visibility of git clean via |filters.git_clean| option
13161324
`I` toggle_git_ignored toggle visibility of files/folders hidden via |git.ignore| option
13171325
`H` toggle_dotfiles toggle visibility of dotfiles via |filters.dotfiles| option
1326+
`B` toggle_no_buffer toggle visibility of files/folders hidden via |filters.no_buffer| option
13181327
`U` toggle_custom toggle visibility of files/folders hidden via |filters.custom| option
13191328
`R` refresh refresh the tree
13201329
`a` create add a file; leaving a trailing `/` will add a directory
@@ -1365,6 +1374,7 @@ DEFAULT MAPPINGS *nvim-tree-default-mappings
13651374
{ key = "C", action = "toggle_git_clean" },
13661375
{ key = "I", action = "toggle_git_ignored" },
13671376
{ key = "H", action = "toggle_dotfiles" },
1377+
{ key = "B", action = "toggle_no_buffer" },
13681378
{ key = "U", action = "toggle_custom" },
13691379
{ key = "R", action = "refresh" },
13701380
{ key = "a", action = "create" },

lua/nvim-tree.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -582,9 +582,10 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
582582
},
583583
filters = {
584584
dotfiles = false,
585+
git_clean = false,
586+
no_buffer = false,
585587
custom = {},
586588
exclude = {},
587-
git_clean = false,
588589
},
589590
filesystem_watchers = {
590591
enable = true,

lua/nvim-tree/actions/dispatch.lua

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ local Actions = {
1313
toggle_custom = require("nvim-tree.actions.tree-modifiers.toggles").custom,
1414
toggle_git_ignored = require("nvim-tree.actions.tree-modifiers.toggles").git_ignored,
1515
toggle_git_clean = require("nvim-tree.actions.tree-modifiers.toggles").git_clean,
16+
toggle_no_buffer = require("nvim-tree.actions.tree-modifiers.toggles").no_buffer,
1617

1718
-- Filesystem operations
1819
copy_absolute_path = require("nvim-tree.actions.fs.copy-paste").copy_absolute_path,

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ 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 }
18+
1719
handle, _ = vim.loop.fs_scandir(dir)
1820
if not handle then
1921
return
@@ -34,7 +36,7 @@ local function search(search_dir, input_path)
3436
break
3537
end
3638

37-
if not filters.should_ignore(path) then
39+
if not filters.should_ignore(path, bufinfo) then
3840
if string.find(path, "/" .. input_path .. "$") then
3941
return path
4042
end

lua/nvim-tree/actions/init.lua

+5
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ local DEFAULT_MAPPINGS = {
9191
action = "toggle_dotfiles",
9292
desc = "toggle visibility of dotfiles via |filters.dotfiles| option",
9393
},
94+
{
95+
key = "B",
96+
action = "toggle_no_buffer",
97+
desc = "toggle visibility of files/folders hidden via |filters.no_buffer| option",
98+
},
9499
{
95100
key = "U",
96101
action = "toggle_custom",

lua/nvim-tree/actions/tree-modifiers/toggles.lua

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ function M.git_clean()
2020
return reloaders.reload_explorer()
2121
end
2222

23+
function M.no_buffer()
24+
filters.config.filter_no_buffer = not filters.config.filter_no_buffer
25+
return reloaders.reload_explorer()
26+
end
27+
2328
function M.dotfiles()
2429
filters.config.filter_dotfiles = not filters.config.filter_dotfiles
2530
return reloaders.reload_explorer()

lua/nvim-tree/api.lua

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Api.tree.collapse_all = require("nvim-tree.actions.tree-modifiers.collapse-all")
3939
Api.tree.expand_all = inject_node(require("nvim-tree.actions.tree-modifiers.expand-all").fn)
4040
Api.tree.toggle_gitignore_filter = require("nvim-tree.actions.tree-modifiers.toggles").git_ignored
4141
Api.tree.toggle_git_clean_filter = require("nvim-tree.actions.tree-modifiers.toggles").git_clean
42+
Api.tree.toggle_no_buffer_filter = require("nvim-tree.actions.tree-modifiers.toggles").no_buffer
4243
Api.tree.toggle_custom_filter = require("nvim-tree.actions.tree-modifiers.toggles").custom
4344
Api.tree.toggle_hidden_filter = require("nvim-tree.actions.tree-modifiers.toggles").dotfiles
4445
Api.tree.toggle_help = require("nvim-tree.actions.tree-modifiers.toggles").help

lua/nvim-tree/explorer/explore.lua

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +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 }
1819
while true do
1920
local name, t = vim.loop.fs_scandir_next(handle)
2021
if not name then
@@ -23,7 +24,11 @@ local function populate_children(handle, cwd, node, status)
2324

2425
local abs = utils.path_join { cwd, name }
2526
t = get_type_from(t, abs)
26-
if not filters.should_ignore(abs) and not filters.should_ignore_git(abs, status) and not nodes_by_path[abs] then
27+
if
28+
not filters.should_ignore(abs, bufinfo)
29+
and not filters.should_ignore_git(abs, status)
30+
and not nodes_by_path[abs]
31+
then
2732
local child = nil
2833
if t == "directory" and vim.loop.fs_access(abs, "R") then
2934
child = builders.folder(node, abs, name)

lua/nvim-tree/explorer/filters.lua

+20-5
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,27 @@ 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 }
1920
---@return boolean
20-
function M.should_ignore(path)
21+
function M.should_ignore(path, bufinfo)
2122
local basename = utils.path_basename(path)
2223

24+
-- exclusions override all filters
2325
if is_excluded(path) then
2426
return false
2527
end
2628

29+
-- filter files with no open buffer and directories containing no open buffers
30+
if M.config.filter_no_buffer and type(bufinfo) == "table" then
31+
for _, buf in ipairs(bufinfo) do
32+
if buf.name:find(path, 1, true) then
33+
return false
34+
end
35+
end
36+
return true
37+
end
38+
39+
-- filter dotfiles
2740
if M.config.filter_dotfiles then
2841
if basename:sub(1, 1) == "." then
2942
return true
@@ -34,6 +47,7 @@ function M.should_ignore(path)
3447
return false
3548
end
3649

50+
-- filter custom regexes
3751
local relpath = utils.path_relative(path, vim.loop.cwd())
3852
for pat, _ in pairs(M.ignore_list) do
3953
if vim.fn.match(relpath, pat) ~= -1 or vim.fn.match(basename, pat) ~= -1 then
@@ -56,20 +70,20 @@ function M.should_ignore_git(path, status)
5670
return false
5771
end
5872

59-
-- exclusions override all
73+
-- exclusions override all filters
6074
if is_excluded(path) then
6175
return false
6276
end
6377

64-
-- default to clean
78+
-- default status to clean
6579
local st = status.files[path] or status.dirs[path] or " "
6680

67-
-- ignored overrides clean as they are effectively dirty
81+
-- filter ignored; overrides clean as they are effectively dirty
6882
if M.config.filter_git_ignored and st == "!!" then
6983
return true
7084
end
7185

72-
-- clean last
86+
-- filter clean
7387
if M.config.filter_git_clean and st == " " then
7488
return true
7589
end
@@ -83,6 +97,7 @@ function M.setup(opts)
8397
filter_dotfiles = opts.filters.dotfiles,
8498
filter_git_ignored = opts.git.ignore,
8599
filter_git_clean = opts.filters.git_clean,
100+
filter_no_buffer = opts.filters.no_buffer,
86101
}
87102

88103
M.ignore_list = {}

lua/nvim-tree/explorer/reload.lua

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ 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 }
48+
4749
if node.group_next then
4850
node.nodes = { node.group_next }
4951
node.group_next = nil
@@ -71,7 +73,7 @@ function M.reload(node, status)
7173

7274
local abs = utils.path_join { cwd, name }
7375
t = t or (fs_stat_cached(abs) or {}).type
74-
if not filters.should_ignore(abs) and not filters.should_ignore_git(abs, status) then
76+
if not filters.should_ignore(abs, bufinfo) and not filters.should_ignore_git(abs, status) then
7577
child_names[abs] = true
7678

7779
-- Recreate node if type changes.

0 commit comments

Comments
 (0)