From 4a87b8b46b4a30107971871df3cb7f4c30fdd5d0 Mon Sep 17 00:00:00 2001 From: darcy <44690813+dxrcy@users.noreply.github.com> Date: Sun, 11 Feb 2024 17:18:40 +1100 Subject: [PATCH] feat(#2654): filters.custom may be a function (#2655) * feat(#2654): add `binaries` field to `filters` * feat(#2648): allow functions in `filters.custom` * ci: fix: stylua check * ci: fix: add new keybind and config to docs * fix: replace os-specific binary filter with `vim.fn.executable` * fix: remove function and mapping for `binaries` filter * fix: add `node` parameter to custom filter function * fix: update doc for custom filter function signature * fix: add custom filter to `ACCEPTED_TYPES` * fix: accept single function for custom filter * fix: change custom filter on `ACCEPTED_TYPES` * fix: revert to using `path` for custom filter function * fix: use `function` type for custom filter * fix: type for custom filter in help * fix: custom filter single function no longer mutates `M.config.filter_custom` * fix: remove dead `if` statement in custom filter * fix: separate custom filter function from `M.ignore_list` * doc nit --------- Co-authored-by: darcy <44690813+darccyy@users.noreply.github.com> Co-authored-by: Alexander Courtis --- doc/nvim-tree-lua.txt | 2 +- lua/nvim-tree.lua | 3 +++ lua/nvim-tree/explorer/filters.lua | 16 +++++++++++++--- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index bac74a14..ced571f1 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 1cfead10..07d06140 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 b05fe4ed..395e3927 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