Skip to content

Commit 8d0c93d

Browse files
committed
refactor(collapse-all): extract buffer matching logic
1 parent 2d2cbe6 commit 8d0c93d

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

lua/nvim-tree/actions/tree-modifiers/collapse-all.lua

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,33 @@ local Iterator = require "nvim-tree.iterators.node-iterator"
55

66
local M = {}
77

8+
local function buf_match()
9+
local buffer_paths = vim.tbl_map(function(buffer)
10+
return vim.api.nvim_buf_get_name(buffer)
11+
end, vim.api.nvim_list_bufs())
12+
13+
return function(path)
14+
for _, buffer_path in ipairs(buffer_paths) do
15+
local matches = utils.str_find(buffer_path, path)
16+
if matches then
17+
return true
18+
end
19+
end
20+
return false
21+
end
22+
end
23+
824
function M.fn(keep_buffers)
925
if not core.get_explorer() then
1026
return
1127
end
1228

13-
local buffer_paths = vim.tbl_map(function(buffer)
14-
return vim.api.nvim_buf_get_name(buffer)
15-
end, vim.api.nvim_list_bufs())
29+
local matches = buf_match()
1630

1731
Iterator.builder(core.get_explorer().nodes)
1832
:hidden()
1933
:applier(function(node)
20-
node.open = false
21-
if keep_buffers == true then
22-
for _, buffer_path in ipairs(buffer_paths) do
23-
local matches = utils.str_find(buffer_path, node.absolute_path)
24-
if matches then
25-
node.open = true
26-
return
27-
end
28-
end
29-
end
34+
node.open = keep_buffers == true and matches(node.absolute_path)
3035
end)
3136
:recursor(function(n)
3237
return n.nodes

0 commit comments

Comments
 (0)