chore(iterators): create Iterator module and migrate iterators to use it (#1392)

This commit is contained in:
Kiyan
2022-07-04 14:13:14 +02:00
committed by GitHub
parent 70bdf496ea
commit f43b8af8f4
7 changed files with 177 additions and 154 deletions

View File

@@ -1,6 +1,7 @@
local renderer = require "nvim-tree.renderer"
local utils = require "nvim-tree.utils"
local core = require "nvim-tree.core"
local Iterator = require "nvim-tree.iterators.node-iterator"
local M = {}
@@ -9,34 +10,29 @@ function M.fn(keep_buffers)
return
end
local buffer_paths = {}
for _, buffer in ipairs(vim.api.nvim_list_bufs()) do
table.insert(buffer_paths, vim.api.nvim_buf_get_name(buffer))
end
local buffer_paths = vim.tbl_map(function(buffer)
return vim.api.nvim_buf_get_name(buffer)
end, vim.api.nvim_list_bufs())
local function iter(nodes)
for _, node in pairs(nodes) do
if node.open then
local new_open = false
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
new_open = true
end
Iterator.builder(core.get_explorer().nodes)
:hidden()
:applier(function(node)
node.open = false
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
node.open = new_open
end
if node.nodes then
iter(node.nodes)
end
end
end
end)
:recursor(function(n)
return n.nodes
end)
:iterate()
iter(core.get_explorer().nodes)
renderer.draw()
end

View File

@@ -1,6 +1,7 @@
local core = require "nvim-tree.core"
local renderer = require "nvim-tree.renderer"
local utils = require "nvim-tree.utils"
local Iterator = require "nvim-tree.iterators.node-iterator"
local M = {}
@@ -20,34 +21,38 @@ local function expand(node)
end
end
local function should_expand(expansion_count, node)
local should_halt = expansion_count >= M.MAX_FOLDER_DISCOVERY
local should_exclude = M.EXCLUDE[node.name]
return not should_halt and node.nodes and not node.open and not should_exclude
end
local function gen_iterator()
local expansion_count = 0
local function iterate(parent)
if expansion_count >= M.MAX_FOLDER_DISCOVERY then
return true
end
return function(parent)
if parent.parent and parent.nodes and not parent.open then
expansion_count = expansion_count + 1
expand(parent)
end
for _, node in pairs(parent.nodes) do
if node.nodes and not node.open and not M.EXCLUDE[node.name] then
expansion_count = expansion_count + 1
expand(node)
end
if node.open then
if iterate(node) then
return true
Iterator.builder(parent.nodes)
:hidden()
:applier(function(node)
if should_expand(expansion_count, node) then
expansion_count = expansion_count + 1
expand(node)
end
end
end)
:recursor(function(node)
return expansion_count < M.MAX_FOLDER_DISCOVERY and node.open and node.nodes
end)
:iterate()
if expansion_count >= M.MAX_FOLDER_DISCOVERY then
return true
end
end
return iterate
end
function M.fn(base_node)

View File

@@ -4,6 +4,7 @@ local view = require "nvim-tree.view"
local utils = require "nvim-tree.utils"
local renderer = require "nvim-tree.renderer"
local core = require "nvim-tree.core"
local Iterator = require "nvim-tree.iterators.node-iterator"
local M = {}
@@ -24,53 +25,31 @@ function M.fn(fname)
return
end
local i = core.get_nodes_starting_line() - 1
local tree_altered = false
local line = core.get_nodes_starting_line()
local function iterate_nodes(nodes)
for _, node in ipairs(nodes) do
if not node.hidden then
i = i + 1
local found = Iterator.builder(core.get_explorer().nodes)
:matcher(function(node)
return node.absolute_path == fname_real or node.link_to == fname_real
end)
:applier(function(node)
line = line + 1
local abs_match = vim.startswith(fname_real, node.absolute_path .. utils.path_separator)
local link_match = node.link_to and vim.startswith(fname_real, node.link_to .. utils.path_separator)
if not node.absolute_path or not uv.fs_stat(node.absolute_path) then
break
end
-- match against node absolute and link, as symlinks themselves will differ
if node.absolute_path == fname_real or node.link_to == fname_real then
return i
end
local abs_match = vim.startswith(fname_real, node.absolute_path .. utils.path_separator)
local link_match = node.link_to and vim.startswith(fname_real, node.link_to .. utils.path_separator)
local path_matches = node.nodes and (abs_match or link_match)
if path_matches then
if not node.open then
node.open = true
tree_altered = true
end
if #node.nodes == 0 then
core.get_explorer():expand(node)
end
if iterate_nodes(node.nodes) ~= nil then
return i
end
-- mandatory to iterate i
elseif node.open then
iterate_nodes(node.nodes)
if abs_match or link_match then
node.open = true
if #node.nodes == 0 then
core.get_explorer():expand(node)
end
end
end
end)
:iterate()
if found and view.is_visible() then
renderer.draw()
view.set_cursor { line, 0 }
end
local index = iterate_nodes(core.get_explorer().nodes)
if tree_altered then
renderer.draw()
end
if index and view.is_visible() then
view.set_cursor { index, 0 }
end
running[fname] = false
log.profile_end(ps, "find file %s", fname)