diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index bac74a14d71..ced571f17c4 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -1262,7 +1262,7 @@ Enabling this is not useful as there is no means yet to persist bookmarks. Custom list of vim regex for file/directory names that will not be shown. Backslashes must be escaped e.g. "^\\.git". See |string-match|. Toggle via |nvim-tree-api.tree.toggle_custom_filter()|, default `U` - Type: {string}, Default: `{}` + Type: {string} | `function(absolute_path)`, Default: `{}` *nvim-tree.filters.exclude* List of directories or files to exclude from filtering: always show them. diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 1cfead102ab..07d06140363 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -623,6 +623,9 @@ local ACCEPTED_TYPES = { group_empty = { "boolean", "function" }, root_folder_label = { "function", "string", "boolean" }, }, + filters = { + custom = { "function" }, + }, actions = { open_file = { window_picker = { diff --git a/lua/nvim-tree/explorer/filters.lua b/lua/nvim-tree/explorer/filters.lua index b05fe4edbdc..395e3927b75 100644 --- a/lua/nvim-tree/explorer/filters.lua +++ b/lua/nvim-tree/explorer/filters.lua @@ -4,6 +4,7 @@ local marks = require "nvim-tree.marks" local M = { ignore_list = {}, exclude_list = {}, + custom_function = nil, } ---@param path string @@ -84,6 +85,11 @@ local function custom(path) local basename = utils.path_basename(path) + -- filter user's custom function + if M.custom_function and M.custom_function(path) then + return true + end + -- filter custom regexes local relpath = utils.path_relative(path, vim.loop.cwd()) for pat, _ in pairs(M.ignore_list) do @@ -153,9 +159,13 @@ function M.setup(opts) M.exclude_list = opts.filters.exclude local custom_filter = opts.filters.custom - if custom_filter and #custom_filter > 0 then - for _, filter_name in pairs(custom_filter) do - M.ignore_list[filter_name] = true + if type(custom_filter) == "function" then + M.custom_function = custom_filter + else + if custom_filter and #custom_filter > 0 then + for _, filter_name in pairs(custom_filter) do + M.ignore_list[filter_name] = true + end end end end