Skip to content

Commit bbf8c52

Browse files
committed
feat(live-filter): add ability to live filter out nodes in the tree
1 parent 20797a8 commit bbf8c52

File tree

13 files changed

+341
-171
lines changed

13 files changed

+341
-171
lines changed

lua/nvim-tree/actions/change-dir.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function M.force_dirchange(foldername, with_open)
3535
vim.cmd("lcd " .. vim.fn.fnameescape(foldername))
3636
end
3737
end
38-
require("nvim-tree.core").init(foldername)
38+
core.init(foldername)
3939
if with_open then
4040
require("nvim-tree.lib").open()
4141
else

lua/nvim-tree/actions/find-file.lua

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,35 @@ function M.fn(fname)
1313
end
1414
running[fname] = true
1515

16-
local i = view.is_root_folder_visible() and 1 or 0
16+
local i = core.get_nodes_starting_line() - 1
1717
local tree_altered = false
1818

1919
local function iterate_nodes(nodes)
2020
for _, node in ipairs(nodes) do
21-
i = i + 1
22-
if node.absolute_path == fname then
23-
return i
24-
end
25-
26-
local path_matches = node.nodes and vim.startswith(fname, node.absolute_path .. utils.path_separator)
27-
if path_matches then
28-
if not node.open then
29-
node.open = true
30-
tree_altered = true
31-
end
32-
33-
if #node.nodes == 0 then
34-
core.get_explorer():expand(node)
21+
if not node.hidden then
22+
i = i + 1
23+
if node.absolute_path == fname then
24+
return i
3525
end
3626

37-
if iterate_nodes(node.nodes) ~= nil then
38-
return i
27+
local path_matches = node.nodes and vim.startswith(fname, node.absolute_path .. utils.path_separator)
28+
if path_matches then
29+
if not node.open then
30+
node.open = true
31+
tree_altered = true
32+
end
33+
34+
if #node.nodes == 0 then
35+
core.get_explorer():expand(node)
36+
end
37+
38+
if iterate_nodes(node.nodes) ~= nil then
39+
return i
40+
end
41+
-- mandatory to iterate i
42+
elseif node.open then
43+
iterate_nodes(node.nodes)
3944
end
40-
-- mandatory to iterate i
41-
elseif node.open then
42-
iterate_nodes(node.nodes)
4345
end
4446
end
4547
end

lua/nvim-tree/actions/init.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ local M = {
4040
{ key = "]c", action = "next_git_item" },
4141
{ key = "-", action = "dir_up" },
4242
{ key = "s", action = "system_open" },
43+
{ key = "f", action = "live_filter" },
4344
{ key = "q", action = "close" },
4445
{ key = "g?", action = "toggle_help" },
4546
{ key = "W", action = "collapse_all" },
@@ -65,6 +66,7 @@ local keypress_funcs = {
6566
first_sibling = require("nvim-tree.actions.movements").sibling(-math.huge),
6667
full_rename = require("nvim-tree.actions.rename-file").fn(true),
6768
last_sibling = require("nvim-tree.actions.movements").sibling(math.huge),
69+
live_filter = require("nvim-tree.live-filter").start_filtering,
6870
next_git_item = require("nvim-tree.actions.movements").find_git_item "next",
6971
next_sibling = require("nvim-tree.actions.movements").sibling(1),
7072
parent_node = require("nvim-tree.actions.movements").parent_node(false),
@@ -92,6 +94,11 @@ function M.on_keypress(action)
9294
if view.is_help_ui() and action ~= "toggle_help" then
9395
return
9496
end
97+
98+
if action == "live_filter" then
99+
return keypress_funcs[action]()
100+
end
101+
95102
local node = lib.get_node_at_cursor()
96103
if not node then
97104
return

lua/nvim-tree/actions/movements.lua

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ local view = require "nvim-tree.view"
33
local diagnostics = require "nvim-tree.diagnostics"
44
local renderer = require "nvim-tree.renderer"
55
local core = require "nvim-tree.core"
6-
76
local lib = function()
87
return require "nvim-tree.lib"
98
end
@@ -17,19 +16,21 @@ local function get_line_from_node(node, find_parent)
1716
node_path = node.absolute_path:match("(.*)" .. utils.path_separator)
1817
end
1918

20-
local line = 2
19+
local line = core.get_nodes_starting_line()
2120
local function iter(nodes, recursive)
2221
for _, _node in ipairs(nodes) do
23-
local n = lib().get_last_group_node(_node)
24-
if node_path == n.absolute_path then
25-
return line, _node
26-
end
22+
if not _node.hidden then
23+
local n = lib().get_last_group_node(_node)
24+
if node_path == n.absolute_path then
25+
return line, _node
26+
end
2727

28-
line = line + 1
29-
if _node.open == true and recursive then
30-
local _, child = iter(_node.nodes, recursive)
31-
if child ~= nil then
32-
return line, child
28+
line = line + 1
29+
if _node.open == true and recursive then
30+
local _, child = iter(_node.nodes, recursive)
31+
if child ~= nil then
32+
return line, child
33+
end
3334
end
3435
end
3536
end
@@ -38,36 +39,28 @@ local function get_line_from_node(node, find_parent)
3839
end
3940

4041
function M.parent_node(should_close)
42+
should_close = should_close or false
43+
4144
return function(node)
4245
if node.name == ".." then
4346
return
4447
end
4548

46-
should_close = should_close or false
47-
local altered_tree = false
48-
49-
local iter = get_line_from_node(node, true)
49+
local line, parent = get_line_from_node(node, true)(core.get_explorer().nodes, true)
5050
if node.open == true and should_close then
5151
node.open = false
52-
altered_tree = true
52+
view.set_cursor { line, 0 }
5353
else
54-
local line, parent = iter(core.get_explorer().nodes, true)
5554
if parent == nil then
5655
line = 1
5756
elseif should_close then
5857
parent.open = false
59-
altered_tree = true
60-
end
61-
if not view.is_root_folder_visible() then
62-
line = line - 1
6358
end
6459
view.set_cursor { line, 0 }
6560
end
6661

67-
if altered_tree then
68-
diagnostics.update()
69-
renderer.draw()
70-
end
62+
diagnostics.update()
63+
renderer.draw()
7164
end
7265
end
7366

@@ -91,7 +84,7 @@ function M.sibling(direction)
9184
end
9285

9386
if line > 0 then
94-
parent = core.get_explorer()
87+
parent = core.get_explorer().nodes
9588
else
9689
_, parent = iter(core.get_explorer().nodes, true)
9790
if parent ~= nil and #parent.nodes > 1 then
@@ -111,17 +104,17 @@ function M.sibling(direction)
111104
local target_node = parent.nodes[index]
112105

113106
line, _ = get_line_from_node(target_node)(core.get_explorer().nodes, true)
114-
if not view.is_root_folder_visible() then
115-
line = line - 1
116-
end
117107
view.set_cursor { line, 0 }
118108
end
119109
end
120110

121111
function M.find_git_item(where)
122112
return function()
123113
local node_cur = lib().get_node_at_cursor()
124-
local nodes_by_line = lib().get_nodes_by_line(core.get_explorer().nodes, view.View.hide_root_folder and 1 or 2)
114+
local nodes_by_line = lib().get_nodes_by_line(
115+
core.get_explorer().nodes,
116+
core.get_nodes_starting_line()
117+
)
125118

126119
local cur, first, prev, nex = nil, nil, nil, nil
127120
for line, node in pairs(nodes_by_line) do

lua/nvim-tree/actions/search-node.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function M.fn()
8585
end
8686

8787
if found_something and view.is_visible() then
88-
if view.is_root_folder_visible() then
88+
if view.is_root_folder_visible(core.get_cwd()) then
8989
index = index + 1
9090
end
9191

lua/nvim-tree/core.lua

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
local events = require "nvim-tree.events"
22
local explorer = require "nvim-tree.explorer"
3+
local live_filter = require "nvim-tree.live-filter"
4+
local view = require "nvim-tree.view"
35

46
local M = {}
57

6-
local first_init_done = false
7-
88
TreeExplorer = nil
9+
local first_init_done = false
910

1011
function M.init(foldername)
1112
TreeExplorer = explorer.Explorer.new(foldername)
@@ -15,12 +16,23 @@ function M.init(foldername)
1516
end
1617
end
1718

19+
function M.get_cwd()
20+
return TreeExplorer.cwd
21+
end
22+
1823
function M.get_explorer()
1924
return TreeExplorer
2025
end
2126

22-
function M.get_cwd()
23-
return TreeExplorer.cwd
27+
function M.get_nodes_starting_line()
28+
local offset = 1
29+
if view.is_root_folder_visible(M.get_cwd()) then
30+
offset = offset + 1
31+
end
32+
if live_filter.filter then
33+
return offset + 1
34+
end
35+
return offset
2436
end
2537

2638
return M

lua/nvim-tree/explorer/explore.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local builders = require "nvim-tree.explorer.node-builders"
66
local common = require "nvim-tree.explorer.common"
77
local sorters = require "nvim-tree.explorer.sorters"
88
local filters = require "nvim-tree.explorer.filters"
9+
local live_filter = require "nvim-tree.live-filter"
910

1011
local M = {}
1112

@@ -66,6 +67,7 @@ function M.explore(node, status)
6667
end
6768

6869
sorters.merge_sort(node.nodes, sorters.node_comparator)
70+
live_filter.apply_filter(node)
6971
return node.nodes
7072
end
7173

lua/nvim-tree/explorer/reload.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local builders = require "nvim-tree.explorer.node-builders"
66
local common = require "nvim-tree.explorer.common"
77
local filters = require "nvim-tree.explorer.filters"
88
local sorters = require "nvim-tree.explorer.sorters"
9+
local live_filter = require "nvim-tree.live-filter"
910

1011
local M = {}
1112

@@ -89,6 +90,7 @@ function M.reload(node, status)
8990
end
9091

9192
sorters.merge_sort(node.nodes, sorters.node_comparator)
93+
live_filter.apply_filter(node)
9294
return node.nodes
9395
end
9496

lua/nvim-tree/lib.lua

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ function M.get_nodes_by_line(nodes_all, line_start)
1414
local line = line_start
1515
local function iter(nodes)
1616
for _, node in ipairs(nodes) do
17-
nodes_by_line[line] = node
18-
line = line + 1
19-
if node.open == true then
20-
local child = iter(node.nodes)
21-
if child ~= nil then
22-
return child
17+
if not node.hidden then
18+
nodes_by_line[line] = node
19+
line = line + 1
20+
if node.open == true then
21+
local child = iter(node.nodes)
22+
if child ~= nil then
23+
return child
24+
end
2325
end
2426
end
2527
end
@@ -32,27 +34,25 @@ function M.get_node_at_cursor()
3234
if not core.get_explorer() then
3335
return
3436
end
37+
3538
local winnr = view.get_winnr()
36-
local hide_root_folder = view.View.hide_root_folder
3739
if not winnr then
3840
return
3941
end
42+
4043
local cursor = api.nvim_win_get_cursor(view.get_winnr())
4144
local line = cursor[1]
4245
if view.is_help_ui() then
4346
local help_lines = require("nvim-tree.renderer.help").compute_lines()
4447
local help_text = M.get_nodes_by_line(help_lines, 1)[line]
4548
return { name = help_text }
46-
else
47-
if line == 1 and core.get_explorer().cwd ~= "/" and not hide_root_folder then
48-
return { name = ".." }
49-
end
49+
end
5050

51-
if core.get_explorer().cwd == "/" then
52-
line = line + 1
53-
end
54-
return M.get_nodes_by_line(core.get_explorer().nodes, view.View.hide_root_folder and 1 or 2)[line]
51+
if line == 1 and view.is_root_folder_visible() then
52+
return { name = ".." }
5553
end
54+
55+
return M.get_nodes_by_line(core.get_explorer().nodes, core.get_nodes_starting_line())[line]
5656
end
5757

5858
-- If node is grouped, return the last node in the group. Otherwise, return the given node.

0 commit comments

Comments
 (0)