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

@@ -44,6 +44,12 @@ function Builder:configure_picture_map(picture_map)
return self
end
function Builder:configure_filter(filter, prefix)
self.filter_prefix = prefix
self.filter = filter
return self
end
function Builder:configure_opened_file_highlighting(level)
if level == 1 then
self.open_file_highlight = "icon"
@@ -221,8 +227,8 @@ function Builder:_build_file(node, padding, git_highlight, git_icons_tbl)
end
end
function Builder:_build_line(tree, node, idx)
local padding = pad.get_padding(self.depth, idx, tree, node, self.markers)
function Builder:_build_line(node, idx, num_children)
local padding = pad.get_padding(self.depth, idx, num_children, node, self.markers)
if self.depth > 0 then
self:_insert_highlight("NvimTreeIndentMarker", 0, string.len(padding))
@@ -256,9 +262,28 @@ function Builder:_build_line(tree, node, idx)
end
end
function Builder:_get_nodes_number(nodes)
if not self.filter then
return #nodes
end
local i = 0
for _, n in pairs(nodes) do
if not n.hidden then
i = i + 1
end
end
return i
end
function Builder:build(tree)
for idx, node in ipairs(tree.nodes) do
self:_build_line(tree, node, idx)
local num_children = self:_get_nodes_number(tree.nodes)
local idx = 1
for _, node in ipairs(tree.nodes) do
if not node.hidden then
self:_build_line(node, idx, num_children)
idx = idx + 1
end
end
return self
@@ -277,6 +302,15 @@ function Builder:build_header(show_header)
self.index = 1
end
if self.filter then
local filter_line = self.filter_prefix .. "/" .. self.filter .. "/"
self:_insert_line(filter_line)
local prefix_length = string.len(self.filter_prefix)
self:_insert_highlight("NvimTreeLiveFilterPrefix", 0, prefix_length)
self:_insert_highlight("NvimTreeLiveFilterValue", prefix_length, string.len(filter_line))
self.index = self.index + 1
end
return self
end

View File

@@ -14,13 +14,13 @@ local function get_padding_arrows(icon_state)
end
end
local function get_padding_indent_markers(depth, idx, tree, _, markers)
local function get_padding_indent_markers(depth, idx, nodes_number, _, markers)
local padding = ""
if depth ~= 0 then
local rdepth = depth / 2
markers[rdepth] = idx ~= #tree.nodes
markers[rdepth] = idx ~= nodes_number
for i = 1, rdepth do
if idx == #tree.nodes and i == rdepth then
if idx == nodes_number and i == rdepth then
padding = padding .. M.config.indent_markers.icons.corner
elseif markers[i] then
padding = padding .. M.config.indent_markers.icons.edge

View File

@@ -8,6 +8,7 @@ local icon_component = require "nvim-tree.renderer.components.icons"
local help = require "nvim-tree.renderer.help"
local git = require "nvim-tree.renderer.components.git"
local Builder = require "nvim-tree.renderer.builder"
local live_filter = require "nvim-tree.live-filter"
local api = vim.api
@@ -87,6 +88,7 @@ function M.draw()
:configure_opened_file_highlighting(vim.g.nvim_tree_highlight_opened_files)
:configure_git_icons_padding(vim.g.nvim_tree_icon_padding)
:configure_git_icons_placement(M.config.icons.git_placement)
:configure_filter(live_filter.filter, live_filter.prefix)
:build_header(view.is_root_folder_visible(core.get_cwd()))
:build(core.get_explorer())
:unwrap()