Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ require'nvim-tree'.setup { -- BEGIN_DEFAULT_OPTS
icons = {
webdev_colors = true,
git_placement = "before",
}
},
},
hijack_directories = {
enable = true,
Expand Down Expand Up @@ -216,6 +216,10 @@ require'nvim-tree'.setup { -- BEGIN_DEFAULT_OPTS
cmd = "trash",
require_confirm = true,
},
live_filter = {
prefix = "[FILTER]: ",
always_show_folders = true,
},
log = {
enable = false,
truncate = false,
Expand Down Expand Up @@ -269,6 +273,7 @@ require'nvim-tree'.setup { -- BEGIN_DEFAULT_OPTS
- `S` will prompt the user to enter a path and then expands the tree to match the path
- `.` will enter vim command mode with the file the cursor is on
- `C-k` will toggle a popup with file infos about the file under the cursor
- `f` will allow you to filter nodes dynamically based on regex matching.

### Settings

Expand Down Expand Up @@ -330,6 +335,8 @@ local list = {
{ 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" },
Expand Down
28 changes: 27 additions & 1 deletion doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ Values may be functions. Warning: this may result in unexpected behaviour.
icons = {
webdev_colors = true,
git_placement = "before",
}
},
},
hijack_directories = {
enable = true,
Expand Down Expand Up @@ -184,6 +184,10 @@ Values may be functions. Warning: this may result in unexpected behaviour.
cmd = "trash",
require_confirm = true,
},
live_filter = {
prefix = "[FILTER]: ",
always_show_folders = true,
},
log = {
enable = false,
truncate = false,
Expand Down Expand Up @@ -519,6 +523,20 @@ Configuration for various actions.
'+' (system), otherwise, it will be stored in '1' and '"'.
Type: `boolean`, Default: `true`

*nvim-tree.live_filter*
Configurations for the live_filtering feature.
The live filter allows you to filter the tree nodes dynamically, based on
regex matching (see |vim.regex|).
This feature is bound to the `f` key by default.
The filter can be cleared with the `F` key by default.

*nvim-tree.live_filter.prefix*
Prefix of the filter displayed in the buffer.
Type: `string`, Default: `"[FILTER]: "`

*nvim-tree.live_filter.always_show_folders*
Wether to filter folders or not.
Type: `boolean`, Default: `true`

*nvim-tree.log*
Configuration for diagnostic logging.
Expand Down Expand Up @@ -764,6 +782,8 @@ Defaults to:
{ 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" },
Expand All @@ -773,6 +793,7 @@ Defaults to:
{ key = "U", action = "toggle_custom" },
}
<

The `list` option in `view.mappings.list` is a table of

- key can be either a string or a table of string (lhs)
Expand Down Expand Up @@ -879,6 +900,11 @@ NvimTreeFileRenamed
NvimTreeFileNew
NvimTreeFileDeleted

There are 2 highlight groups for the live filter feature

NvimTreeLiveFilterPrefix
NvimTreeLiveFilterValue

==============================================================================
vinegar style *nvim-tree-vinegar*

Expand Down
5 changes: 5 additions & 0 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,10 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
cmd = "trash",
require_confirm = true,
},
live_filter = {
prefix = "[FILTER]: ",
always_show_folders = true,
},
log = {
enable = false,
truncate = false,
Expand Down Expand Up @@ -538,6 +542,7 @@ function M.setup(conf)
require("nvim-tree.view").setup(opts)
require("nvim-tree.lib").setup(opts)
require("nvim-tree.renderer").setup(opts)
require("nvim-tree.live-filter").setup(opts)

setup_vim_commands()
setup_autocommands(opts)
Expand Down
48 changes: 25 additions & 23 deletions lua/nvim-tree/actions/find-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
-- 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 iterate_nodes(node.nodes) ~= nil then
return i
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
-- mandatory to iterate i
elseif node.open then
iterate_nodes(node.nodes)
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions lua/nvim-tree/actions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand 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),
Expand Down Expand Up @@ -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
Expand Down
68 changes: 22 additions & 46 deletions lua/nvim-tree/actions/movements.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

-- 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]
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)

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

Expand Down
2 changes: 2 additions & 0 deletions lua/nvim-tree/colors.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ local function get_hl_groups()
GitNew = { fg = colors.yellow },

WindowPicker = { gui = "bold", fg = "#ededed", bg = "#4493c8" },
LiveFilterPrefix = { gui = "bold", fg = colors.purple },
LiveFilterValue = { gui = "bold", fg = "#fff" },
}
end

Expand Down
4 changes: 4 additions & 0 deletions lua/nvim-tree/core.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local events = require "nvim-tree.events"
local explorer = require "nvim-tree.explorer"
local live_filter = require "nvim-tree.live-filter"
local view = require "nvim-tree.view"

local M = {}
Expand Down Expand Up @@ -28,6 +29,9 @@ function M.get_nodes_starting_line()
if view.is_root_folder_visible(M.get_cwd()) then
offset = offset + 1
end
if live_filter.filter then
return offset + 1
end
return offset
end

Expand Down
2 changes: 2 additions & 0 deletions lua/nvim-tree/explorer/explore.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local builders = require "nvim-tree.explorer.node-builders"
local common = require "nvim-tree.explorer.common"
local sorters = require "nvim-tree.explorer.sorters"
local filters = require "nvim-tree.explorer.filters"
local live_filter = require "nvim-tree.live-filter"

local M = {}

Expand Down Expand Up @@ -76,6 +77,7 @@ function M.explore(node, status)
end

sorters.merge_sort(node.nodes, sorters.node_comparator)
live_filter.apply_filter(node)
return node.nodes
end

Expand Down
2 changes: 2 additions & 0 deletions lua/nvim-tree/explorer/reload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local builders = require "nvim-tree.explorer.node-builders"
local common = require "nvim-tree.explorer.common"
local filters = require "nvim-tree.explorer.filters"
local sorters = require "nvim-tree.explorer.sorters"
local live_filter = require "nvim-tree.live-filter"

local M = {}

Expand Down Expand Up @@ -77,6 +78,7 @@ function M.reload(node, status)
end

sorters.merge_sort(node.nodes, sorters.node_comparator)
live_filter.apply_filter(node)
return node.nodes
end

Expand Down
Loading