Skip to content

feat(#2654): filters.custom may be a function #2655

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 21 commits into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
b754082
feat(#2654): add `binaries` field to `filters`
dxrcy Jan 29, 2024
e942899
feat(#2648): allow functions in `filters.custom`
dxrcy Jan 29, 2024
be1e6e4
Merge branch 'nvim-tree:master' into master
dxrcy Jan 30, 2024
d65092d
ci: fix: stylua check
dxrcy Jan 30, 2024
0e46599
ci: fix: add new keybind and config to docs
dxrcy Jan 30, 2024
1304c03
fix: replace os-specific binary filter with `vim.fn.executable`
dxrcy Feb 3, 2024
89653f3
fix: remove function and mapping for `binaries` filter
dxrcy Feb 3, 2024
7978157
fix: add `node` parameter to custom filter function
dxrcy Feb 4, 2024
7339881
fix: update doc for custom filter function signature
dxrcy Feb 4, 2024
0e880dc
fix: add custom filter to `ACCEPTED_TYPES`
dxrcy Feb 4, 2024
2e4a1d1
fix: accept single function for custom filter
dxrcy Feb 4, 2024
5e416f3
fix: change custom filter on `ACCEPTED_TYPES`
dxrcy Feb 4, 2024
b447d69
fix: revert to using `path` for custom filter function
dxrcy Feb 5, 2024
38ded48
fix: use `function` type for custom filter
dxrcy Feb 5, 2024
8363ac5
fix: type for custom filter in help
dxrcy Feb 5, 2024
c4b4aae
fix: custom filter single function no longer mutates `M.config.filter…
dxrcy Feb 5, 2024
1e054f3
Merge branch 'nvim-tree:master' into master
dxrcy Feb 5, 2024
340c581
fix: remove dead `if` statement in custom filter
dxrcy Feb 7, 2024
8385e89
Merge branch 'nvim-tree:master' into master
dxrcy Feb 11, 2024
e771143
fix: separate custom filter function from `M.ignore_list`
dxrcy Feb 11, 2024
c3336b5
doc nit
alex-courtis Feb 11, 2024
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
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" },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, looking good!

},
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With some hacking

function M.setup(opts)
  M.config = {
    filter_custom = opts.filters.custom,
    filter_dotfiles = opts.filters.dotfiles,
    filter_git_ignored = opts.filters.git_ignored,
    filter_git_clean = opts.filters.git_clean,
    filter_no_buffer = opts.filters.no_buffer,
    filter_no_bookmark = opts.filters.no_bookmark,
  }

  M.ignore_list = {}
  M.exclude_list = opts.filters.exclude

  log.line("dev", "filters setup M='%s'", vim.inspect(M))
  -- 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
  --   end
  -- end
end

I was able to pass the custom function through, however it's never executed as ignore_list is null.

I reckon you'll need to directly call the custom function outside of this loop.

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