Skip to content

Commit

Permalink
feat(#2654): filters.custom may be a function (#2655)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Alexander Courtis <[email protected]>
  • Loading branch information
3 people authored Feb 11, 2024
1 parent 2dbe4ea commit 4a87b8b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
16 changes: 13 additions & 3 deletions lua/nvim-tree/explorer/filters.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local marks = require "nvim-tree.marks"
local M = {
ignore_list = {},
exclude_list = {},
custom_function = nil,
}

---@param path string
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4a87b8b

Please sign in to comment.