refactor(collapse-all): extract buffer matching logic
This commit is contained in:
@@ -5,28 +5,33 @@ local Iterator = require "nvim-tree.iterators.node-iterator"
|
|||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
local function buf_match()
|
||||||
|
local buffer_paths = vim.tbl_map(function(buffer)
|
||||||
|
return vim.api.nvim_buf_get_name(buffer)
|
||||||
|
end, vim.api.nvim_list_bufs())
|
||||||
|
|
||||||
|
return function(path)
|
||||||
|
for _, buffer_path in ipairs(buffer_paths) do
|
||||||
|
local matches = utils.str_find(buffer_path, path)
|
||||||
|
if matches then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function M.fn(keep_buffers)
|
function M.fn(keep_buffers)
|
||||||
if not core.get_explorer() then
|
if not core.get_explorer() then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local buffer_paths = vim.tbl_map(function(buffer)
|
local matches = buf_match()
|
||||||
return vim.api.nvim_buf_get_name(buffer)
|
|
||||||
end, vim.api.nvim_list_bufs())
|
|
||||||
|
|
||||||
Iterator.builder(core.get_explorer().nodes)
|
Iterator.builder(core.get_explorer().nodes)
|
||||||
:hidden()
|
:hidden()
|
||||||
:applier(function(node)
|
:applier(function(node)
|
||||||
node.open = false
|
node.open = keep_buffers == true and matches(node.absolute_path)
|
||||||
if keep_buffers == true then
|
|
||||||
for _, buffer_path in ipairs(buffer_paths) do
|
|
||||||
local matches = utils.str_find(buffer_path, node.absolute_path)
|
|
||||||
if matches then
|
|
||||||
node.open = true
|
|
||||||
return
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end)
|
end)
|
||||||
:recursor(function(n)
|
:recursor(function(n)
|
||||||
return n.nodes
|
return n.nodes
|
||||||
|
|||||||
Reference in New Issue
Block a user