From b754082c668568ec7f0f31905cb05f7031e11b43 Mon Sep 17 00:00:00 2001 From: darcy Date: Mon, 29 Jan 2024 12:24:59 +1100 Subject: [PATCH 01/18] feat(#2654): add `binaries` field to `filters` --- doc/nvim-tree-lua.txt | 1 + lua/nvim-tree.lua | 1 + .../actions/tree/modifiers/toggles.lua | 5 ++++ lua/nvim-tree/api.lua | 1 + lua/nvim-tree/explorer/filters.lua | 28 ++++++++++++++++++- lua/nvim-tree/keymap.lua | 1 + 6 files changed, 36 insertions(+), 1 deletion(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 4a4dee03612..25be1bebc35 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -177,6 +177,7 @@ Show the mappings: `g?` `g?` Help |nvim-tree-api.tree.toggle_help()| `gy` Copy Absolute Path |nvim-tree-api.fs.copy.absolute_path()| `H` Toggle Filter: Dotfiles |nvim-tree-api.tree.toggle_hidden_filter()| +`h` Toggle Filter: Binaries |nvim-tree-api.tree.toggle_binaries_filter()| `I` Toggle Filter: Git Ignore |nvim-tree-api.tree.toggle_gitignore_filter()| `J` Last Sibling |nvim-tree-api.node.navigate.sibling.last()| `K` First Sibling |nvim-tree-api.node.navigate.sibling.first()| diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 51beee21a38..6c2c2a4034a 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -500,6 +500,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS filters = { git_ignored = true, dotfiles = false, + binaries = false, git_clean = false, no_buffer = false, no_bookmark = false, diff --git a/lua/nvim-tree/actions/tree/modifiers/toggles.lua b/lua/nvim-tree/actions/tree/modifiers/toggles.lua index 709926ff7d3..5430374bdb7 100644 --- a/lua/nvim-tree/actions/tree/modifiers/toggles.lua +++ b/lua/nvim-tree/actions/tree/modifiers/toggles.lua @@ -41,4 +41,9 @@ function M.dotfiles() reload() end +function M.binaries() + filters.config.filter_binaries = not filters.config.filter_binaries + reload() +end + return M diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index 3c85071e899..45463095fc8 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -131,6 +131,7 @@ Api.tree.toggle_git_clean_filter = wrap(actions.tree.modifiers.toggles.git_clean Api.tree.toggle_no_buffer_filter = wrap(actions.tree.modifiers.toggles.no_buffer) Api.tree.toggle_custom_filter = wrap(actions.tree.modifiers.toggles.custom) Api.tree.toggle_hidden_filter = wrap(actions.tree.modifiers.toggles.dotfiles) +Api.tree.toggle_binaries_filter = wrap(actions.tree.modifiers.toggles.binaries) Api.tree.toggle_no_bookmark_filter = wrap(actions.tree.modifiers.toggles.no_bookmark) Api.tree.toggle_help = wrap(help.toggle) Api.tree.is_tree_buf = wrap(utils.is_nvim_tree_buf) diff --git a/lua/nvim-tree/explorer/filters.lua b/lua/nvim-tree/explorer/filters.lua index b05fe4edbdc..b2df9c124f5 100644 --- a/lua/nvim-tree/explorer/filters.lua +++ b/lua/nvim-tree/explorer/filters.lua @@ -69,6 +69,30 @@ local function dotfile(path) return M.config.filter_dotfiles and utils.path_basename(path):sub(1, 1) == "." end +---@param path string +---@return boolean +local function binary(path) + if not M.config.filter_binaries then + return false + end + + -- 4-byte 'magic number' for ELF + local magic_number = string.char(0x7f) .. "ELF" + + local fd = vim.loop.fs_open(path, "r", 438) + if not fd then + return false + end + local stat = vim.loop.fs_fstat(fd) + if not stat then + return false + end + local data = vim.loop.fs_read(fd, 4, 0) + vim.loop.fs_close(fd) + + return data == magic_number +end + ---@param path string ---@param bookmarks table absolute paths bookmarked local function bookmark(path, bookmarks) @@ -136,13 +160,15 @@ function M.should_filter(path, status) return false end - return git(path, status.git_status) or buf(path, status.bufinfo) or dotfile(path) or custom(path) or bookmark(path, status.bookmarks) + return git(path, status.git_status) or buf(path, status.bufinfo) or dotfile(path) or binary(path) or custom(path) or + bookmark(path, status.bookmarks) end function M.setup(opts) M.config = { filter_custom = true, filter_dotfiles = opts.filters.dotfiles, + filter_binaries = opts.filters.binaries, filter_git_ignored = opts.filters.git_ignored, filter_git_clean = opts.filters.git_clean, filter_no_buffer = opts.filters.no_buffer, diff --git a/lua/nvim-tree/keymap.lua b/lua/nvim-tree/keymap.lua index 700a479461e..c7041098d23 100644 --- a/lua/nvim-tree/keymap.lua +++ b/lua/nvim-tree/keymap.lua @@ -69,6 +69,7 @@ function M.default_on_attach(bufnr) vim.keymap.set('n', 'g?', api.tree.toggle_help, opts('Help')) vim.keymap.set('n', 'gy', api.fs.copy.absolute_path, opts('Copy Absolute Path')) vim.keymap.set('n', 'H', api.tree.toggle_hidden_filter, opts('Toggle Filter: Dotfiles')) + vim.keymap.set('n', 'h', api.tree.toggle_binaries_filter, opts('Toggle Filter: Binaries')) vim.keymap.set('n', 'I', api.tree.toggle_gitignore_filter, opts('Toggle Filter: Git Ignore')) vim.keymap.set('n', 'J', api.node.navigate.sibling.last, opts('Last Sibling')) vim.keymap.set('n', 'K', api.node.navigate.sibling.first, opts('First Sibling')) From e9428995ac066b12b0266e5c94232fe5ed8c8b68 Mon Sep 17 00:00:00 2001 From: darcy Date: Mon, 29 Jan 2024 12:34:24 +1100 Subject: [PATCH 02/18] feat(#2648): allow functions in `filters.custom` --- doc/nvim-tree-lua.txt | 2 +- lua/nvim-tree/explorer/filters.lua | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 25be1bebc35..c9224b29f52 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -1240,7 +1240,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(path)}, Default: `{}` *nvim-tree.filters.exclude* List of directories or files to exclude from filtering: always show them. diff --git a/lua/nvim-tree/explorer/filters.lua b/lua/nvim-tree/explorer/filters.lua index b2df9c124f5..ec498ddd1df 100644 --- a/lua/nvim-tree/explorer/filters.lua +++ b/lua/nvim-tree/explorer/filters.lua @@ -111,8 +111,14 @@ local function custom(path) -- filter custom regexes local relpath = utils.path_relative(path, vim.loop.cwd()) for pat, _ in pairs(M.ignore_list) do - if vim.fn.match(relpath, pat) ~= -1 or vim.fn.match(basename, pat) ~= -1 then - return true + if type(pat) == "function" then + if pat(path) then + return true + end + else + if vim.fn.match(relpath, pat) ~= -1 or vim.fn.match(basename, pat) ~= -1 then + return true + end end end From d65092d2dc38d609d7f9c537764bea0dc87d4ad3 Mon Sep 17 00:00:00 2001 From: darcy Date: Tue, 30 Jan 2024 11:05:46 +1100 Subject: [PATCH 03/18] ci: fix: stylua check --- lua/nvim-tree/explorer/filters.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lua/nvim-tree/explorer/filters.lua b/lua/nvim-tree/explorer/filters.lua index ec498ddd1df..ac9bfd6f3b5 100644 --- a/lua/nvim-tree/explorer/filters.lua +++ b/lua/nvim-tree/explorer/filters.lua @@ -166,8 +166,12 @@ function M.should_filter(path, status) return false end - return git(path, status.git_status) or buf(path, status.bufinfo) or dotfile(path) or binary(path) or custom(path) or - bookmark(path, status.bookmarks) + return git(path, status.git_status) + or buf(path, status.bufinfo) + or dotfile(path) + or binary(path) + or custom(path) + or bookmark(path, status.bookmarks) end function M.setup(opts) From 0e4659967311209d6b56c94a16147771baddf165 Mon Sep 17 00:00:00 2001 From: darcy Date: Tue, 30 Jan 2024 11:17:08 +1100 Subject: [PATCH 04/18] ci: fix: add new keybind and config to docs --- doc/nvim-tree-lua.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 70540033186..2ff3e563a54 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -505,6 +505,7 @@ Following is the default configuration. See |nvim-tree-opts| for details. filters = { git_ignored = true, dotfiles = false, + binaries = false, git_clean = false, no_buffer = false, no_bookmark = false, @@ -2194,6 +2195,7 @@ You are encouraged to copy these to your own |nvim-tree.on_attach| function. vim.keymap.set('n', 'g?', api.tree.toggle_help, opts('Help')) vim.keymap.set('n', 'gy', api.fs.copy.absolute_path, opts('Copy Absolute Path')) vim.keymap.set('n', 'H', api.tree.toggle_hidden_filter, opts('Toggle Filter: Dotfiles')) + vim.keymap.set('n', 'h', api.tree.toggle_binaries_filter, opts('Toggle Filter: Binaries')) vim.keymap.set('n', 'I', api.tree.toggle_gitignore_filter, opts('Toggle Filter: Git Ignore')) vim.keymap.set('n', 'J', api.node.navigate.sibling.last, opts('Last Sibling')) vim.keymap.set('n', 'K', api.node.navigate.sibling.first, opts('First Sibling')) From 1304c03b797f409d7499578cd52cc87bafb50875 Mon Sep 17 00:00:00 2001 From: darcy Date: Sat, 3 Feb 2024 17:21:09 +1100 Subject: [PATCH 05/18] fix: replace os-specific binary filter with `vim.fn.executable` --- lua/nvim-tree/explorer/filters.lua | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/lua/nvim-tree/explorer/filters.lua b/lua/nvim-tree/explorer/filters.lua index ac9bfd6f3b5..45a48a00824 100644 --- a/lua/nvim-tree/explorer/filters.lua +++ b/lua/nvim-tree/explorer/filters.lua @@ -75,22 +75,7 @@ local function binary(path) if not M.config.filter_binaries then return false end - - -- 4-byte 'magic number' for ELF - local magic_number = string.char(0x7f) .. "ELF" - - local fd = vim.loop.fs_open(path, "r", 438) - if not fd then - return false - end - local stat = vim.loop.fs_fstat(fd) - if not stat then - return false - end - local data = vim.loop.fs_read(fd, 4, 0) - vim.loop.fs_close(fd) - - return data == magic_number + return vim.fn.executable(path) == 1 end ---@param path string @@ -167,11 +152,11 @@ function M.should_filter(path, status) end return git(path, status.git_status) - or buf(path, status.bufinfo) - or dotfile(path) - or binary(path) - or custom(path) - or bookmark(path, status.bookmarks) + or buf(path, status.bufinfo) + or dotfile(path) + or binary(path) + or custom(path) + or bookmark(path, status.bookmarks) end function M.setup(opts) From 89653f39a767d1442a0faf4eb0b07458b72f7f0e Mon Sep 17 00:00:00 2001 From: darcy Date: Sat, 3 Feb 2024 17:25:59 +1100 Subject: [PATCH 06/18] fix: remove function and mapping for `binaries` filter --- doc/nvim-tree-lua.txt | 3 --- lua/nvim-tree.lua | 1 - .../actions/tree/modifiers/toggles.lua | 5 ----- lua/nvim-tree/api.lua | 1 - lua/nvim-tree/explorer/filters.lua | 17 +---------------- lua/nvim-tree/keymap.lua | 1 - 6 files changed, 1 insertion(+), 27 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 2ff3e563a54..0cc082c4eaf 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -178,7 +178,6 @@ Show the mappings: `g?` `g?` Help |nvim-tree-api.tree.toggle_help()| `gy` Copy Absolute Path |nvim-tree-api.fs.copy.absolute_path()| `H` Toggle Filter: Dotfiles |nvim-tree-api.tree.toggle_hidden_filter()| -`h` Toggle Filter: Binaries |nvim-tree-api.tree.toggle_binaries_filter()| `I` Toggle Filter: Git Ignore |nvim-tree-api.tree.toggle_gitignore_filter()| `J` Last Sibling |nvim-tree-api.node.navigate.sibling.last()| `K` First Sibling |nvim-tree-api.node.navigate.sibling.first()| @@ -505,7 +504,6 @@ Following is the default configuration. See |nvim-tree-opts| for details. filters = { git_ignored = true, dotfiles = false, - binaries = false, git_clean = false, no_buffer = false, no_bookmark = false, @@ -2195,7 +2193,6 @@ You are encouraged to copy these to your own |nvim-tree.on_attach| function. vim.keymap.set('n', 'g?', api.tree.toggle_help, opts('Help')) vim.keymap.set('n', 'gy', api.fs.copy.absolute_path, opts('Copy Absolute Path')) vim.keymap.set('n', 'H', api.tree.toggle_hidden_filter, opts('Toggle Filter: Dotfiles')) - vim.keymap.set('n', 'h', api.tree.toggle_binaries_filter, opts('Toggle Filter: Binaries')) vim.keymap.set('n', 'I', api.tree.toggle_gitignore_filter, opts('Toggle Filter: Git Ignore')) vim.keymap.set('n', 'J', api.node.navigate.sibling.last, opts('Last Sibling')) vim.keymap.set('n', 'K', api.node.navigate.sibling.first, opts('First Sibling')) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index ff44fa199f3..1cfead102ab 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -501,7 +501,6 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS filters = { git_ignored = true, dotfiles = false, - binaries = false, git_clean = false, no_buffer = false, no_bookmark = false, diff --git a/lua/nvim-tree/actions/tree/modifiers/toggles.lua b/lua/nvim-tree/actions/tree/modifiers/toggles.lua index 5430374bdb7..709926ff7d3 100644 --- a/lua/nvim-tree/actions/tree/modifiers/toggles.lua +++ b/lua/nvim-tree/actions/tree/modifiers/toggles.lua @@ -41,9 +41,4 @@ function M.dotfiles() reload() end -function M.binaries() - filters.config.filter_binaries = not filters.config.filter_binaries - reload() -end - return M diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index 45463095fc8..3c85071e899 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -131,7 +131,6 @@ Api.tree.toggle_git_clean_filter = wrap(actions.tree.modifiers.toggles.git_clean Api.tree.toggle_no_buffer_filter = wrap(actions.tree.modifiers.toggles.no_buffer) Api.tree.toggle_custom_filter = wrap(actions.tree.modifiers.toggles.custom) Api.tree.toggle_hidden_filter = wrap(actions.tree.modifiers.toggles.dotfiles) -Api.tree.toggle_binaries_filter = wrap(actions.tree.modifiers.toggles.binaries) Api.tree.toggle_no_bookmark_filter = wrap(actions.tree.modifiers.toggles.no_bookmark) Api.tree.toggle_help = wrap(help.toggle) Api.tree.is_tree_buf = wrap(utils.is_nvim_tree_buf) diff --git a/lua/nvim-tree/explorer/filters.lua b/lua/nvim-tree/explorer/filters.lua index 45a48a00824..59afda1155e 100644 --- a/lua/nvim-tree/explorer/filters.lua +++ b/lua/nvim-tree/explorer/filters.lua @@ -69,15 +69,6 @@ local function dotfile(path) return M.config.filter_dotfiles and utils.path_basename(path):sub(1, 1) == "." end ----@param path string ----@return boolean -local function binary(path) - if not M.config.filter_binaries then - return false - end - return vim.fn.executable(path) == 1 -end - ---@param path string ---@param bookmarks table absolute paths bookmarked local function bookmark(path, bookmarks) @@ -151,19 +142,13 @@ function M.should_filter(path, status) return false end - return git(path, status.git_status) - or buf(path, status.bufinfo) - or dotfile(path) - or binary(path) - or custom(path) - or bookmark(path, status.bookmarks) + return git(path, status.git_status) or buf(path, status.bufinfo) or dotfile(path) or custom(path) or bookmark(path, status.bookmarks) end function M.setup(opts) M.config = { filter_custom = true, filter_dotfiles = opts.filters.dotfiles, - filter_binaries = opts.filters.binaries, filter_git_ignored = opts.filters.git_ignored, filter_git_clean = opts.filters.git_clean, filter_no_buffer = opts.filters.no_buffer, diff --git a/lua/nvim-tree/keymap.lua b/lua/nvim-tree/keymap.lua index c7041098d23..700a479461e 100644 --- a/lua/nvim-tree/keymap.lua +++ b/lua/nvim-tree/keymap.lua @@ -69,7 +69,6 @@ function M.default_on_attach(bufnr) vim.keymap.set('n', 'g?', api.tree.toggle_help, opts('Help')) vim.keymap.set('n', 'gy', api.fs.copy.absolute_path, opts('Copy Absolute Path')) vim.keymap.set('n', 'H', api.tree.toggle_hidden_filter, opts('Toggle Filter: Dotfiles')) - vim.keymap.set('n', 'h', api.tree.toggle_binaries_filter, opts('Toggle Filter: Binaries')) vim.keymap.set('n', 'I', api.tree.toggle_gitignore_filter, opts('Toggle Filter: Git Ignore')) vim.keymap.set('n', 'J', api.node.navigate.sibling.last, opts('Last Sibling')) vim.keymap.set('n', 'K', api.node.navigate.sibling.first, opts('First Sibling')) From 7978157e23d66bfe3523d83e8a54f4d616ee2108 Mon Sep 17 00:00:00 2001 From: darcy Date: Sun, 4 Feb 2024 13:26:08 +1100 Subject: [PATCH 07/18] fix: add `node` parameter to custom filter function --- lua/nvim-tree/explorer/filters.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lua/nvim-tree/explorer/filters.lua b/lua/nvim-tree/explorer/filters.lua index 59afda1155e..a712184d8c0 100644 --- a/lua/nvim-tree/explorer/filters.lua +++ b/lua/nvim-tree/explorer/filters.lua @@ -83,12 +83,13 @@ local function custom(path) end local basename = utils.path_basename(path) + local node = utils.get_node_from_path(path) -- filter custom regexes local relpath = utils.path_relative(path, vim.loop.cwd()) for pat, _ in pairs(M.ignore_list) do if type(pat) == "function" then - if pat(path) then + if pat(path, node) then return true end else From 7339881c12df6e12e62ff5459325b0beea7a2a63 Mon Sep 17 00:00:00 2001 From: darcy Date: Sun, 4 Feb 2024 13:36:35 +1100 Subject: [PATCH 08/18] fix: update doc for custom filter function signature --- doc/nvim-tree-lua.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 0cc082c4eaf..4ca7272c977 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -1241,7 +1241,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 | function(path)}, Default: `{}` + Type: {string | function(path, node)}, Default: `{}` *nvim-tree.filters.exclude* List of directories or files to exclude from filtering: always show them. From 0e880dcd39b425fc9ede9ccb4d3642079710c493 Mon Sep 17 00:00:00 2001 From: darcy Date: Sun, 4 Feb 2024 13:49:34 +1100 Subject: [PATCH 09/18] fix: add custom filter to `ACCEPTED_TYPES` --- lua/nvim-tree.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 1cfead102ab..7ed9925cf67 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 = { { "string", "function" } }, + }, actions = { open_file = { window_picker = { From 2e4a1d1aebf0eba7b8063b44784e674330594c8a Mon Sep 17 00:00:00 2001 From: darcy Date: Sun, 4 Feb 2024 14:04:59 +1100 Subject: [PATCH 10/18] fix: accept single function for custom filter --- lua/nvim-tree.lua | 2 +- lua/nvim-tree/explorer/filters.lua | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 7ed9925cf67..b5a2007facc 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -624,7 +624,7 @@ local ACCEPTED_TYPES = { root_folder_label = { "function", "string", "boolean" }, }, filters = { - custom = { { "string", "function" } }, + custom = { { "string", "function" }, "function" }, }, actions = { open_file = { diff --git a/lua/nvim-tree/explorer/filters.lua b/lua/nvim-tree/explorer/filters.lua index a712184d8c0..5fa1d77fcb2 100644 --- a/lua/nvim-tree/explorer/filters.lua +++ b/lua/nvim-tree/explorer/filters.lua @@ -147,6 +147,10 @@ function M.should_filter(path, status) end function M.setup(opts) + if type(opts.filters.custom) == "function" then + opts.filters.custom = { opts.filters.custom } + end + M.config = { filter_custom = true, filter_dotfiles = opts.filters.dotfiles, From 5e416f373df2939f82744009ed1011aea0655076 Mon Sep 17 00:00:00 2001 From: darcy Date: Mon, 5 Feb 2024 09:53:09 +1100 Subject: [PATCH 11/18] fix: change custom filter on `ACCEPTED_TYPES` --- lua/nvim-tree.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index b5a2007facc..07ce1bf631c 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -624,7 +624,7 @@ local ACCEPTED_TYPES = { root_folder_label = { "function", "string", "boolean" }, }, filters = { - custom = { { "string", "function" }, "function" }, + custom = { "table", "function" }, }, actions = { open_file = { From b447d6935f27088ea87e8922b90064ffedab01a0 Mon Sep 17 00:00:00 2001 From: darcy Date: Mon, 5 Feb 2024 11:13:56 +1100 Subject: [PATCH 12/18] fix: revert to using `path` for custom filter function --- doc/nvim-tree-lua.txt | 2 +- lua/nvim-tree/explorer/filters.lua | 23 ++++++++++++++--------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 4ca7272c977..3bfc44c1bfe 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -1241,7 +1241,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 | function(path, node)}, Default: `{}` + Type: {string | function(path)} | function(path), Default: `{}` *nvim-tree.filters.exclude* List of directories or files to exclude from filtering: always show them. diff --git a/lua/nvim-tree/explorer/filters.lua b/lua/nvim-tree/explorer/filters.lua index 5fa1d77fcb2..6f776489f8a 100644 --- a/lua/nvim-tree/explorer/filters.lua +++ b/lua/nvim-tree/explorer/filters.lua @@ -83,13 +83,17 @@ local function custom(path) end local basename = utils.path_basename(path) - local node = utils.get_node_from_path(path) + + -- single filter function + if type(M.config.filter_custom) == "function" then + return M.config.filter_custom(path) + end -- filter custom regexes local relpath = utils.path_relative(path, vim.loop.cwd()) for pat, _ in pairs(M.ignore_list) do if type(pat) == "function" then - if pat(path, node) then + if pat(path) then return true end else @@ -147,10 +151,6 @@ function M.should_filter(path, status) end function M.setup(opts) - if type(opts.filters.custom) == "function" then - opts.filters.custom = { opts.filters.custom } - end - M.config = { filter_custom = true, filter_dotfiles = opts.filters.dotfiles, @@ -164,9 +164,14 @@ 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.config.filter_custom = 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 From 38ded48f358a97f1f06e83d5e6edc3b15da09e0f Mon Sep 17 00:00:00 2001 From: darcy Date: Mon, 5 Feb 2024 17:10:14 +1100 Subject: [PATCH 13/18] fix: use `function` type for custom filter --- lua/nvim-tree.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 07ce1bf631c..07d06140363 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -624,7 +624,7 @@ local ACCEPTED_TYPES = { root_folder_label = { "function", "string", "boolean" }, }, filters = { - custom = { "table", "function" }, + custom = { "function" }, }, actions = { open_file = { From 8363ac5aa18248a8ffdc537e27df3827b8bd5397 Mon Sep 17 00:00:00 2001 From: darcy Date: Mon, 5 Feb 2024 17:13:31 +1100 Subject: [PATCH 14/18] fix: type for custom filter in help --- doc/nvim-tree-lua.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 3bfc44c1bfe..5c8961289f3 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -1241,7 +1241,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 | function(path)} | function(path), Default: `{}` + Type: {string | function(absolute_path)}, Default: `{}` *nvim-tree.filters.exclude* List of directories or files to exclude from filtering: always show them. From c4b4aae541c0376536f79b88ec5f7f19c2abb26c Mon Sep 17 00:00:00 2001 From: darcy Date: Mon, 5 Feb 2024 17:15:34 +1100 Subject: [PATCH 15/18] fix: custom filter single function no longer mutates `M.config.filter_custom` --- lua/nvim-tree/explorer/filters.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lua/nvim-tree/explorer/filters.lua b/lua/nvim-tree/explorer/filters.lua index 6f776489f8a..13eab5173c9 100644 --- a/lua/nvim-tree/explorer/filters.lua +++ b/lua/nvim-tree/explorer/filters.lua @@ -164,9 +164,8 @@ function M.setup(opts) M.exclude_list = opts.filters.exclude local custom_filter = opts.filters.custom - if type(custom_filter) == "function" then - M.config.filter_custom = custom_filter + M.ignore_list[custom_filter] = true else if custom_filter and #custom_filter > 0 then for _, filter_name in pairs(custom_filter) do From 340c5812e7529a51c68652296eb4de92f893ecd0 Mon Sep 17 00:00:00 2001 From: darcy Date: Wed, 7 Feb 2024 12:05:11 +1100 Subject: [PATCH 16/18] fix: remove dead `if` statement in custom filter --- lua/nvim-tree/explorer/filters.lua | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lua/nvim-tree/explorer/filters.lua b/lua/nvim-tree/explorer/filters.lua index 13eab5173c9..f3beb616e68 100644 --- a/lua/nvim-tree/explorer/filters.lua +++ b/lua/nvim-tree/explorer/filters.lua @@ -84,11 +84,6 @@ local function custom(path) local basename = utils.path_basename(path) - -- single filter function - if type(M.config.filter_custom) == "function" then - return M.config.filter_custom(path) - end - -- filter custom regexes local relpath = utils.path_relative(path, vim.loop.cwd()) for pat, _ in pairs(M.ignore_list) do From e771143e20667a3f0472f1c880abd18f61a929c8 Mon Sep 17 00:00:00 2001 From: darcy Date: Sun, 11 Feb 2024 16:40:04 +1100 Subject: [PATCH 17/18] fix: separate custom filter function from `M.ignore_list` --- lua/nvim-tree/explorer/filters.lua | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lua/nvim-tree/explorer/filters.lua b/lua/nvim-tree/explorer/filters.lua index f3beb616e68..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,17 +85,16 @@ 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 - if type(pat) == "function" then - if pat(path) then - return true - end - else - if vim.fn.match(relpath, pat) ~= -1 or vim.fn.match(basename, pat) ~= -1 then - return true - end + if vim.fn.match(relpath, pat) ~= -1 or vim.fn.match(basename, pat) ~= -1 then + return true end end @@ -160,7 +160,7 @@ function M.setup(opts) local custom_filter = opts.filters.custom if type(custom_filter) == "function" then - M.ignore_list[custom_filter] = true + M.custom_function = custom_filter else if custom_filter and #custom_filter > 0 then for _, filter_name in pairs(custom_filter) do From c3336b52f03a9129bc29bdcf6dbe0aa3dcb4d0d3 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Sun, 11 Feb 2024 17:16:07 +1100 Subject: [PATCH 18/18] doc nit --- doc/nvim-tree-lua.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 8f9da4e60d4..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 | function(absolute_path)}, Default: `{}` + Type: {string} | `function(absolute_path)`, Default: `{}` *nvim-tree.filters.exclude* List of directories or files to exclude from filtering: always show them.