Skip to content

Commit 84d5f0a

Browse files
committed
add FilterTypes named map
1 parent 67cc06e commit 84d5f0a

File tree

4 files changed

+32
-28
lines changed

4 files changed

+32
-28
lines changed

lua/nvim-tree/actions/tree/modifiers/toggles.lua

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,43 +22,43 @@ end
2222

2323
---@param explorer Explorer
2424
local function custom(explorer)
25-
explorer.filters.config.filter_custom = not explorer.filters.config.filter_custom
25+
explorer.filters.states.custom = not explorer.filters.states.custom
2626
reload(explorer)
2727
end
2828

2929
---@param explorer Explorer
3030
local function git_ignored(explorer)
31-
explorer.filters.config.filter_git_ignored = not explorer.filters.config.filter_git_ignored
31+
explorer.filters.states.git_ignored = not explorer.filters.states.git_ignored
3232
reload(explorer)
3333
end
3434

3535
---@param explorer Explorer
3636
local function git_clean(explorer)
37-
explorer.filters.config.filter_git_clean = not explorer.filters.config.filter_git_clean
37+
explorer.filters.states.git_clean = not explorer.filters.states.git_clean
3838
reload(explorer)
3939
end
4040

4141
---@param explorer Explorer
4242
local function no_buffer(explorer)
43-
explorer.filters.config.filter_no_buffer = not explorer.filters.config.filter_no_buffer
43+
explorer.filters.states.no_buffer = not explorer.filters.states.no_buffer
4444
reload(explorer)
4545
end
4646

4747
---@param explorer Explorer
4848
local function no_bookmark(explorer)
49-
explorer.filters.config.filter_no_bookmark = not explorer.filters.config.filter_no_bookmark
49+
explorer.filters.states.no_bookmark = not explorer.filters.states.no_bookmark
5050
reload(explorer)
5151
end
5252

5353
---@param explorer Explorer
5454
local function dotfiles(explorer)
55-
explorer.filters.config.filter_dotfiles = not explorer.filters.config.filter_dotfiles
55+
explorer.filters.states.dotfiles = not explorer.filters.states.dotfiles
5656
reload(explorer)
5757
end
5858

5959
---@param explorer Explorer
6060
local function enable(explorer)
61-
explorer.filters.config.enable = not explorer.filters.config.enable
61+
explorer.filters.enabled = not explorer.filters.enabled
6262
reload(explorer)
6363
end
6464

lua/nvim-tree/explorer/filters.lua

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON
33

44
local Class = require("nvim-tree.classic")
55

6+
---@alias FilterTypes "custom" | "dotfiles" | "git_ignored" | "git_clean" | "no_buffer" | "no_bookmark"
7+
68
---@class (exact) Filters: Class
7-
---@field config table hydrated user opts.filters
9+
---@field enabled boolean
10+
---@field states table<FilterTypes, boolean>
811
---@field private explorer Explorer
912
---@field private exclude_list string[] filters.exclude
1013
---@field private ignore_list table<string, boolean> filters.custom string table
@@ -23,14 +26,15 @@ function Filters:new(args)
2326
self.ignore_list = {}
2427
self.exclude_list = self.explorer.opts.filters.exclude
2528
self.custom_function = nil
26-
self.config = {
27-
enable = self.explorer.opts.filters.enable,
28-
filter_custom = true,
29-
filter_dotfiles = self.explorer.opts.filters.dotfiles,
30-
filter_git_ignored = self.explorer.opts.filters.git_ignored,
31-
filter_git_clean = self.explorer.opts.filters.git_clean,
32-
filter_no_buffer = self.explorer.opts.filters.no_buffer,
33-
filter_no_bookmark = self.explorer.opts.filters.no_bookmark,
29+
30+
self.enabled = self.explorer.opts.filters.enable
31+
self.states = {
32+
custom = true,
33+
dotfiles = self.explorer.opts.filters.dotfiles,
34+
git_ignored = self.explorer.opts.filters.git_ignored,
35+
git_clean = self.explorer.opts.filters.git_clean,
36+
no_buffer = self.explorer.opts.filters.no_buffer,
37+
no_bookmark = self.explorer.opts.filters.no_bookmark,
3438
}
3539

3640
local custom_filter = self.explorer.opts.filters.custom
@@ -71,12 +75,12 @@ local function git(self, path, project)
7175
xy = xy or project.dirs.indirect[path] and project.dirs.indirect[path][1]
7276

7377
-- filter ignored; overrides clean as they are effectively dirty
74-
if self.config.filter_git_ignored and xy == "!!" then
78+
if self.states.git_ignored and xy == "!!" then
7579
return true
7680
end
7781

7882
-- filter clean
79-
if self.config.filter_git_clean and not xy then
83+
if self.states.git_clean and not xy then
8084
return true
8185
end
8286

@@ -88,7 +92,7 @@ end
8892
---@param bufinfo table vim.fn.getbufinfo { buflisted = 1 }
8993
---@return boolean
9094
local function buf(self, path, bufinfo)
91-
if not self.config.filter_no_buffer or type(bufinfo) ~= "table" then
95+
if not self.states.no_buffer or type(bufinfo) ~= "table" then
9296
return false
9397
end
9498

@@ -105,7 +109,7 @@ end
105109
---@param path string
106110
---@return boolean
107111
local function dotfile(self, path)
108-
return self.config.filter_dotfiles and utils.path_basename(path):sub(1, 1) == "."
112+
return self.states.dotfiles and utils.path_basename(path):sub(1, 1) == "."
109113
end
110114

111115
---Bookmark is present
@@ -114,7 +118,7 @@ end
114118
---@param bookmarks table<string, string|nil> path, filetype table of bookmarked files
115119
---@return boolean
116120
local function bookmark(self, path, path_type, bookmarks)
117-
if not self.config.filter_no_bookmark then
121+
if not self.states.no_bookmark then
118122
return false
119123
end
120124
-- if bookmark is empty, we should see a empty filetree
@@ -149,7 +153,7 @@ end
149153
---@param path string
150154
---@return boolean
151155
local function custom(self, path)
152-
if not self.config.filter_custom then
156+
if not self.states.custom then
153157
return false
154158
end
155159

@@ -191,7 +195,7 @@ function Filters:prepare(project)
191195
bookmarks = {},
192196
}
193197

194-
if self.config.filter_no_buffer then
198+
if self.states.no_buffer then
195199
status.bufinfo = vim.fn.getbufinfo({ buflisted = 1 })
196200
end
197201

@@ -211,7 +215,7 @@ end
211215
---@param status table from prepare
212216
---@return boolean
213217
function Filters:should_filter(path, fs_stat, status)
214-
if not self.config.enable then
218+
if not self.enabled then
215219
return false
216220
end
217221

@@ -233,7 +237,7 @@ end
233237
---@param status table from prepare
234238
---@return FILTER_REASON
235239
function Filters:should_filter_as_reason(path, fs_stat, status)
236-
if not self.config.enable then
240+
if not self.enabled then
237241
return FILTER_REASON.none
238242
end
239243

lua/nvim-tree/explorer/init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ function Explorer:create_autocmds()
100100
vim.api.nvim_create_autocmd("BufReadPost", {
101101
group = self.augroup_id,
102102
callback = function(data)
103-
if (self.filters.config.filter_no_buffer or self.opts.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == "" then
103+
if (self.filters.states.no_buffer or self.opts.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == "" then
104104
utils.debounce("Buf:filter_buffer_" .. self.uid_explorer, self.opts.view.debounce_delay, function()
105105
self:reload_explorer()
106106
end)
@@ -112,7 +112,7 @@ function Explorer:create_autocmds()
112112
vim.api.nvim_create_autocmd("BufUnload", {
113113
group = self.augroup_id,
114114
callback = function(data)
115-
if (self.filters.config.filter_no_buffer or self.opts.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == "" then
115+
if (self.filters.states.no_buffer or self.opts.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == "" then
116116
utils.debounce("Buf:filter_buffer_" .. self.uid_explorer, self.opts.view.debounce_delay, function()
117117
self:reload_explorer()
118118
end)

lua/nvim-tree/explorer/live-filter.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ end
8181
---@param node Node
8282
---@return boolean
8383
local function matches(self, node)
84-
if not self.explorer.filters.config.enable then
84+
if not self.explorer.filters.enabled then
8585
return true
8686
end
8787

0 commit comments

Comments
 (0)