Skip to content

Commit cdafb14

Browse files
committed
feat(nvim-tree#3119): Expose filter functions to the api
1 parent ea5097a commit cdafb14

File tree

5 files changed

+163
-67
lines changed

5 files changed

+163
-67
lines changed

lua/nvim-tree/actions/finders/search-node.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ local function search(search_dir, input_path)
2323
local function iter(dir)
2424
local realpath, path, name, stat, handle, _
2525

26-
local filter_status = explorer.filters:prepare()
26+
explorer.filters:prepare()
2727

2828
handle, _ = vim.loop.fs_scandir(dir)
2929
if not handle then
@@ -46,7 +46,7 @@ local function search(search_dir, input_path)
4646
break
4747
end
4848

49-
if not explorer.filters:should_filter(path, stat, filter_status) then
49+
if not explorer.filters:should_filter(path) then
5050
if string.find(path, "/" .. input_path .. "$") then
5151
return path
5252
end

lua/nvim-tree/api.lua

+22-2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ local Api = {
4242
commands = {},
4343
diagnostics = {},
4444
decorator = {},
45+
filters = {},
4546
}
4647

4748
---Print error when setup not called.
@@ -92,7 +93,7 @@ local function wrap_node_or_nil(fn)
9293
end
9394
end
9495

95-
---Invoke a member's method on the singleton explorer.
96+
---Invoke a member"s method on the singleton explorer.
9697
---Print error when setup not called.
9798
---@param explorer_member string explorer member name
9899
---@param member_method string method name to invoke on member
@@ -108,7 +109,18 @@ local function wrap_explorer_member_args(explorer_member, member_method, ...)
108109
end)
109110
end
110111

111-
---Invoke a member's method on the singleton explorer.
112+
---@param filter_api_method string
113+
---@return fun(path: string): boolean
114+
local function wrap_explorer_filter_function(filter_api_method)
115+
return wrap(function(path)
116+
local explorer = core.get_explorer()
117+
if explorer then
118+
return explorer.filters.api[filter_api_method](explorer.filters, path)
119+
end
120+
end)
121+
end
122+
123+
---Invoke a member"s method on the singleton explorer.
112124
---Print error when setup not called.
113125
---@param explorer_member string explorer member name
114126
---@param member_method string method name to invoke on member
@@ -356,4 +368,12 @@ end)
356368
---@type nvim_tree.api.decorator.UserDecorator
357369
Api.decorator.UserDecorator = UserDecorator --[[@as nvim_tree.api.decorator.UserDecorator]]
358370

371+
Api.filters.custom = wrap_explorer_filter_function("custom")
372+
Api.filters.dotfile = wrap_explorer_filter_function("dotfile")
373+
Api.filters.git_ignored = wrap_explorer_filter_function("git_ignored")
374+
Api.filters.git_clean = wrap_explorer_filter_function("git_clean")
375+
Api.filters.no_buffer = wrap_explorer_filter_function("no_buffer")
376+
Api.filters.no_bookmark = wrap_explorer_filter_function("no_bookmark")
377+
Api.filters.filter_reason = wrap_explorer_filter_function("filter_reason")
378+
359379
return Api

lua/nvim-tree/enum.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ local M = {}
44
---@enum FILTER_REASON
55
M.FILTER_REASON = {
66
none = 0, -- It's not filtered
7-
git = 1,
7+
git_clean = 1,
88
buf = 2,
99
dotfile = 4,
1010
custom = 8,
1111
bookmark = 16,
12+
git_ignore = 32
1213
}
1314

1415
return M

0 commit comments

Comments
 (0)