feat(live-filter): add ability to live filter out nodes in the tree (#1056)

This commit is contained in:
Kiyan
2022-05-17 10:03:49 +02:00
committed by GitHub
parent 99e32fea14
commit 6343813a35
16 changed files with 312 additions and 102 deletions

View File

@@ -29,35 +29,37 @@ function M.fn(fname)
local function iterate_nodes(nodes)
for _, node in ipairs(nodes) do
i = i + 1
if not node.hidden then
i = i + 1
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
if not node.absolute_path or not uv.fs_stat(node.absolute_path) then
break
end
if #node.nodes == 0 then
core.get_explorer():expand(node)
end
if iterate_nodes(node.nodes) ~= nil then
-- 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
-- mandatory to iterate i
elseif node.open then
iterate_nodes(node.nodes)
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)
end
end
end
end

View File

@@ -40,6 +40,8 @@ local M = {
{ key = "]c", action = "next_git_item" },
{ key = "-", action = "dir_up" },
{ key = "s", action = "system_open" },
{ key = "f", action = "live_filter" },
{ key = "F", action = "clear_live_filter" },
{ key = "q", action = "close" },
{ key = "g?", action = "toggle_help" },
{ key = "W", action = "collapse_all" },
@@ -65,6 +67,8 @@ local keypress_funcs = {
first_sibling = require("nvim-tree.actions.movements").sibling(-math.huge),
full_rename = require("nvim-tree.actions.rename-file").fn(true),
last_sibling = require("nvim-tree.actions.movements").sibling(math.huge),
live_filter = require("nvim-tree.live-filter").start_filtering,
clear_live_filter = require("nvim-tree.live-filter").clear_filter,
next_git_item = require("nvim-tree.actions.movements").find_git_item "next",
next_sibling = require("nvim-tree.actions.movements").sibling(1),
parent_node = require("nvim-tree.actions.movements").parent_node(false),
@@ -92,6 +96,11 @@ function M.on_keypress(action)
if view.is_help_ui() and action ~= "toggle_help" then
return
end
if action == "live_filter" or action == "clear_live_filter" then
return keypress_funcs[action]()
end
local node = lib.get_node_at_cursor()
if not node then
return

View File

@@ -6,34 +6,25 @@ local lib = require "nvim-tree.lib"
local M = {}
local function get_line_from_node(node, find_parent)
local function get_index_of(node, nodes)
local node_path = node.absolute_path
local line = 1
if find_parent then
node_path = node.absolute_path:match("(.*)" .. utils.path_separator)
end
local line = core.get_nodes_starting_line()
local function iter(nodes, recursive)
for _, _node in ipairs(nodes) do
for _, _node in ipairs(nodes) do
if not _node.hidden then
local n = lib.get_last_group_node(_node)
if node_path == n.absolute_path then
return line, _node
return line
end
line = line + 1
if _node.open == true and recursive then
local _, child = iter(_node.nodes, recursive)
if child ~= nil then
return line, child
end
end
end
end
return iter
end
function M.parent_node(should_close)
should_close = should_close or false
return function(node)
if should_close and node.open then
node.open = false
@@ -64,41 +55,26 @@ function M.sibling(direction)
return
end
local iter = get_line_from_node(node, true)
local node_path = node.absolute_path
local parent = node.parent or core.get_explorer()
local parent_nodes = vim.tbl_filter(function(n)
return not n.hidden
end, parent.nodes)
local line = 0
local parent, _
local node_index = get_index_of(node, parent_nodes)
-- Check if current node is already at root nodes
for index, _node in ipairs(core.get_explorer().nodes) do
if node_path == _node.absolute_path then
line = index
end
local target_idx = node_index + direction
if target_idx < 1 then
target_idx = 1
elseif target_idx > #parent_nodes then
target_idx = #parent_nodes
end
if line > 0 then
parent = core.get_explorer()
else
_, parent = iter(core.get_explorer().nodes, true)
if parent ~= nil and #parent.nodes > 1 then
line, _ = get_line_from_node(node)(parent.nodes)
end
local target_node = parent_nodes[target_idx]
local _, line = utils.find_node(core.get_explorer().nodes, function(n)
return n.absolute_path == target_node.absolute_path
end)
-- Ignore parent line count
line = line - 1
end
local index = line + direction
if index < 1 then
index = 1
elseif index > #parent.nodes then
index = #parent.nodes
end
local target_node = parent.nodes[index]
line, _ = get_line_from_node(target_node)(core.get_explorer().nodes, true)
view.set_cursor { line, 0 }
view.set_cursor { line + 1, 0 }
end
end