Skip to content

Commit 908478a

Browse files
refactor(#2828): multi instance nvim-tree.explorer.filters (#2841)
* refactor(#2828): multi instance nvim-tree.explorer.filters * fix: style * fix: apply suggestions from code review Co-authored-by: Alexander Courtis <[email protected]> --------- Co-authored-by: Alexander Courtis <[email protected]>
1 parent 1aa9852 commit 908478a

File tree

8 files changed

+142
-94
lines changed

8 files changed

+142
-94
lines changed

Diff for: lua/nvim-tree.lua

+14-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ local actions = require "nvim-tree.actions"
99
local legacy = require "nvim-tree.legacy"
1010
local core = require "nvim-tree.core"
1111
local git = require "nvim-tree.git"
12-
local filters = require "nvim-tree.explorer.filters"
1312
local buffers = require "nvim-tree.buffers"
1413
local notify = require "nvim-tree.notify"
1514

@@ -210,7 +209,13 @@ local function setup_autocommands(opts)
210209
create_nvim_tree_autocmd("BufReadPost", {
211210
callback = function(data)
212211
-- update opened file buffers
213-
if (filters.config.filter_no_buffer or renderer.config.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == "" then
212+
local explorer = core.get_explorer()
213+
if not explorer then
214+
return
215+
end
216+
if
217+
(explorer.filters.config.filter_no_buffer or renderer.config.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == ""
218+
then
214219
utils.debounce("Buf:filter_buffer", opts.view.debounce_delay, function()
215220
actions.reloaders.reload_explorer()
216221
end)
@@ -221,7 +226,13 @@ local function setup_autocommands(opts)
221226
create_nvim_tree_autocmd("BufUnload", {
222227
callback = function(data)
223228
-- update opened file buffers
224-
if (filters.config.filter_no_buffer or renderer.config.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == "" then
229+
local explorer = core.get_explorer()
230+
if not explorer then
231+
return
232+
end
233+
if
234+
(explorer.filters.config.filter_no_buffer or renderer.config.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == ""
235+
then
225236
utils.debounce("Buf:filter_buffer", opts.view.debounce_delay, function()
226237
actions.reloaders.reload_explorer()
227238
end)

Diff for: lua/nvim-tree/actions/finders/search-node.lua

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local core = require "nvim-tree.core"
2-
local filters = require "nvim-tree.explorer.filters"
32
local find_file = require("nvim-tree.actions.finders.find-file").fn
43

54
local M = {}
@@ -9,6 +8,11 @@ local M = {}
98
---@return string|nil
109
local function search(search_dir, input_path)
1110
local realpaths_searched = {}
11+
local explorer = core.get_explorer()
12+
13+
if not explorer then
14+
return
15+
end
1216

1317
if not search_dir then
1418
return
@@ -19,7 +23,7 @@ local function search(search_dir, input_path)
1923
local function iter(dir)
2024
local realpath, path, name, stat, handle, _
2125

22-
local filter_status = filters.prepare()
26+
local filter_status = explorer.filters:prepare()
2327

2428
handle, _ = vim.loop.fs_scandir(dir)
2529
if not handle then
@@ -42,7 +46,7 @@ local function search(search_dir, input_path)
4246
break
4347
end
4448

45-
if not filters.should_filter(path, stat, filter_status) then
49+
if not explorer.filters:should_filter(path, stat, filter_status) then
4650
if string.find(path, "/" .. input_path .. "$") then
4751
return path
4852
end

Diff for: lua/nvim-tree/actions/tree/modifiers/toggles.lua

+32-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
local lib = require "nvim-tree.lib"
22
local utils = require "nvim-tree.utils"
3-
local filters = require "nvim-tree.explorer.filters"
43
local reloaders = require "nvim-tree.actions.reloaders"
5-
4+
local core = require "nvim-tree.core"
65
local M = {}
76

87
local function reload()
@@ -11,39 +10,56 @@ local function reload()
1110
utils.focus_node_or_parent(node)
1211
end
1312

14-
function M.custom()
15-
filters.config.filter_custom = not filters.config.filter_custom
13+
local function wrap_explorer(fn)
14+
return function(...)
15+
local explorer = core.get_explorer()
16+
if explorer then
17+
return fn(explorer, ...)
18+
end
19+
end
20+
end
21+
22+
local function custom(explorer)
23+
explorer.filters.config.filter_custom = not explorer.filters.config.filter_custom
1624
reload()
1725
end
1826

19-
function M.git_ignored()
20-
filters.config.filter_git_ignored = not filters.config.filter_git_ignored
27+
local function git_ignored(explorer)
28+
explorer.filters.config.filter_git_ignored = not explorer.filters.config.filter_git_ignored
2129
reload()
2230
end
2331

24-
function M.git_clean()
25-
filters.config.filter_git_clean = not filters.config.filter_git_clean
32+
local function git_clean(explorer)
33+
explorer.filters.config.filter_git_clean = not explorer.filters.config.filter_git_clean
2634
reload()
2735
end
2836

29-
function M.no_buffer()
30-
filters.config.filter_no_buffer = not filters.config.filter_no_buffer
37+
local function no_buffer(explorer)
38+
explorer.filters.config.filter_no_buffer = not explorer.filters.config.filter_no_buffer
3139
reload()
3240
end
3341

34-
function M.no_bookmark()
35-
filters.config.filter_no_bookmark = not filters.config.filter_no_bookmark
42+
local function no_bookmark(explorer)
43+
explorer.filters.config.filter_no_bookmark = not explorer.filters.config.filter_no_bookmark
3644
reload()
3745
end
3846

39-
function M.dotfiles()
40-
filters.config.filter_dotfiles = not filters.config.filter_dotfiles
47+
local function dotfiles(explorer)
48+
explorer.filters.config.filter_dotfiles = not explorer.filters.config.filter_dotfiles
4149
reload()
4250
end
4351

44-
function M.enable()
45-
filters.config.enable = not filters.config.enable
52+
local function enable(explorer)
53+
explorer.filters.config.enable = not explorer.filters.config.enable
4654
reload()
4755
end
4856

57+
M.custom = wrap_explorer(custom)
58+
M.git_ignored = wrap_explorer(git_ignored)
59+
M.git_clean = wrap_explorer(git_clean)
60+
M.no_buffer = wrap_explorer(no_buffer)
61+
M.no_bookmark = wrap_explorer(no_bookmark)
62+
M.dotfiles = wrap_explorer(dotfiles)
63+
M.enable = wrap_explorer(enable)
64+
4965
return M

Diff for: lua/nvim-tree/explorer/explore.lua

+8-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ local builders = require "nvim-tree.explorer.node-builders"
33
local explorer_node = require "nvim-tree.explorer.node"
44
local git = require "nvim-tree.git"
55
local sorters = require "nvim-tree.explorer.sorters"
6-
local filters = require "nvim-tree.explorer.filters"
76
local live_filter = require "nvim-tree.live-filter"
87
local log = require "nvim-tree.log"
98

@@ -15,10 +14,11 @@ local M = {}
1514
---@param cwd string
1615
---@param node Node
1716
---@param git_status table
18-
local function populate_children(handle, cwd, node, git_status)
17+
---@param parent Explorer
18+
local function populate_children(handle, cwd, node, git_status, parent)
1919
local node_ignored = explorer_node.is_git_ignored(node)
2020
local nodes_by_path = utils.bool_record(node.nodes, "absolute_path")
21-
local filter_status = filters.prepare(git_status)
21+
local filter_status = parent.filters:prepare(git_status)
2222
while true do
2323
local name, t = vim.loop.fs_scandir_next(handle)
2424
if not name then
@@ -31,7 +31,7 @@ local function populate_children(handle, cwd, node, git_status)
3131
---@type uv.fs_stat.result|nil
3232
local stat = vim.loop.fs_stat(abs)
3333

34-
if not filters.should_filter(abs, stat, filter_status) and not nodes_by_path[abs] and Watcher.is_fs_event_capable(abs) then
34+
if not parent.filters:should_filter(abs, stat, filter_status) and not nodes_by_path[abs] and Watcher.is_fs_event_capable(abs) then
3535
local child = nil
3636
if t == "directory" and vim.loop.fs_access(abs, "R") then
3737
child = builders.folder(node, abs, name, stat)
@@ -56,8 +56,9 @@ end
5656

5757
---@param node Node
5858
---@param status table
59+
---@param parent Explorer
5960
---@return Node[]|nil
60-
function M.explore(node, status)
61+
function M.explore(node, status, parent)
6162
local cwd = node.link_to or node.absolute_path
6263
local handle = vim.loop.fs_scandir(cwd)
6364
if not handle then
@@ -66,15 +67,15 @@ function M.explore(node, status)
6667

6768
local profile = log.profile_start("explore init %s", node.absolute_path)
6869

69-
populate_children(handle, cwd, node, status)
70+
populate_children(handle, cwd, node, status, parent)
7071

7172
local is_root = not node.parent
7273
local child_folder_only = explorer_node.has_one_child_folder(node) and node.nodes[1]
7374
if M.config.group_empty and not is_root and child_folder_only then
7475
local child_cwd = child_folder_only.link_to or child_folder_only.absolute_path
7576
local child_status = git.load_project_status(child_cwd)
7677
node.group_next = child_folder_only
77-
local ns = M.explore(child_folder_only, child_status)
78+
local ns = M.explore(child_folder_only, child_status, parent)
7879
node.nodes = ns or {}
7980

8081
log.profile_end(profile)

0 commit comments

Comments
 (0)