Skip to content

Commit

Permalink
add FilterTypes named map
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-courtis committed Nov 8, 2024
1 parent 67cc06e commit 84d5f0a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
14 changes: 7 additions & 7 deletions lua/nvim-tree/actions/tree/modifiers/toggles.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,43 @@ end

---@param explorer Explorer
local function custom(explorer)
explorer.filters.config.filter_custom = not explorer.filters.config.filter_custom
explorer.filters.states.custom = not explorer.filters.states.custom
reload(explorer)
end

---@param explorer Explorer
local function git_ignored(explorer)
explorer.filters.config.filter_git_ignored = not explorer.filters.config.filter_git_ignored
explorer.filters.states.git_ignored = not explorer.filters.states.git_ignored
reload(explorer)
end

---@param explorer Explorer
local function git_clean(explorer)
explorer.filters.config.filter_git_clean = not explorer.filters.config.filter_git_clean
explorer.filters.states.git_clean = not explorer.filters.states.git_clean
reload(explorer)
end

---@param explorer Explorer
local function no_buffer(explorer)
explorer.filters.config.filter_no_buffer = not explorer.filters.config.filter_no_buffer
explorer.filters.states.no_buffer = not explorer.filters.states.no_buffer
reload(explorer)
end

---@param explorer Explorer
local function no_bookmark(explorer)
explorer.filters.config.filter_no_bookmark = not explorer.filters.config.filter_no_bookmark
explorer.filters.states.no_bookmark = not explorer.filters.states.no_bookmark
reload(explorer)
end

---@param explorer Explorer
local function dotfiles(explorer)
explorer.filters.config.filter_dotfiles = not explorer.filters.config.filter_dotfiles
explorer.filters.states.dotfiles = not explorer.filters.states.dotfiles
reload(explorer)
end

---@param explorer Explorer
local function enable(explorer)
explorer.filters.config.enable = not explorer.filters.config.enable
explorer.filters.enabled = not explorer.filters.enabled
reload(explorer)
end

Expand Down
40 changes: 22 additions & 18 deletions lua/nvim-tree/explorer/filters.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON

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

---@alias FilterTypes "custom" | "dotfiles" | "git_ignored" | "git_clean" | "no_buffer" | "no_bookmark"

---@class (exact) Filters: Class
---@field config table hydrated user opts.filters
---@field enabled boolean
---@field states table<FilterTypes, boolean>
---@field private explorer Explorer
---@field private exclude_list string[] filters.exclude
---@field private ignore_list table<string, boolean> filters.custom string table
Expand All @@ -23,14 +26,15 @@ function Filters:new(args)
self.ignore_list = {}
self.exclude_list = self.explorer.opts.filters.exclude
self.custom_function = nil
self.config = {
enable = self.explorer.opts.filters.enable,
filter_custom = true,
filter_dotfiles = self.explorer.opts.filters.dotfiles,
filter_git_ignored = self.explorer.opts.filters.git_ignored,
filter_git_clean = self.explorer.opts.filters.git_clean,
filter_no_buffer = self.explorer.opts.filters.no_buffer,
filter_no_bookmark = self.explorer.opts.filters.no_bookmark,

self.enabled = self.explorer.opts.filters.enable
self.states = {
custom = true,
dotfiles = self.explorer.opts.filters.dotfiles,
git_ignored = self.explorer.opts.filters.git_ignored,
git_clean = self.explorer.opts.filters.git_clean,
no_buffer = self.explorer.opts.filters.no_buffer,
no_bookmark = self.explorer.opts.filters.no_bookmark,
}

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

-- filter ignored; overrides clean as they are effectively dirty
if self.config.filter_git_ignored and xy == "!!" then
if self.states.git_ignored and xy == "!!" then
return true
end

-- filter clean
if self.config.filter_git_clean and not xy then
if self.states.git_clean and not xy then
return true
end

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

Expand All @@ -105,7 +109,7 @@ end
---@param path string
---@return boolean
local function dotfile(self, path)
return self.config.filter_dotfiles and utils.path_basename(path):sub(1, 1) == "."
return self.states.dotfiles and utils.path_basename(path):sub(1, 1) == "."
end

---Bookmark is present
Expand All @@ -114,7 +118,7 @@ end
---@param bookmarks table<string, string|nil> path, filetype table of bookmarked files
---@return boolean
local function bookmark(self, path, path_type, bookmarks)
if not self.config.filter_no_bookmark then
if not self.states.no_bookmark then
return false
end
-- if bookmark is empty, we should see a empty filetree
Expand Down Expand Up @@ -149,7 +153,7 @@ end
---@param path string
---@return boolean
local function custom(self, path)
if not self.config.filter_custom then
if not self.states.custom then
return false
end

Expand Down Expand Up @@ -191,7 +195,7 @@ function Filters:prepare(project)
bookmarks = {},
}

if self.config.filter_no_buffer then
if self.states.no_buffer then
status.bufinfo = vim.fn.getbufinfo({ buflisted = 1 })
end

Expand All @@ -211,7 +215,7 @@ end
---@param status table from prepare
---@return boolean
function Filters:should_filter(path, fs_stat, status)
if not self.config.enable then
if not self.enabled then
return false
end

Expand All @@ -233,7 +237,7 @@ end
---@param status table from prepare
---@return FILTER_REASON
function Filters:should_filter_as_reason(path, fs_stat, status)
if not self.config.enable then
if not self.enabled then
return FILTER_REASON.none
end

Expand Down
4 changes: 2 additions & 2 deletions lua/nvim-tree/explorer/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function Explorer:create_autocmds()
vim.api.nvim_create_autocmd("BufReadPost", {
group = self.augroup_id,
callback = function(data)
if (self.filters.config.filter_no_buffer or self.opts.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == "" then
if (self.filters.states.no_buffer or self.opts.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == "" then
utils.debounce("Buf:filter_buffer_" .. self.uid_explorer, self.opts.view.debounce_delay, function()
self:reload_explorer()
end)
Expand All @@ -112,7 +112,7 @@ function Explorer:create_autocmds()
vim.api.nvim_create_autocmd("BufUnload", {
group = self.augroup_id,
callback = function(data)
if (self.filters.config.filter_no_buffer or self.opts.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == "" then
if (self.filters.states.no_buffer or self.opts.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == "" then
utils.debounce("Buf:filter_buffer_" .. self.uid_explorer, self.opts.view.debounce_delay, function()
self:reload_explorer()
end)
Expand Down
2 changes: 1 addition & 1 deletion lua/nvim-tree/explorer/live-filter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ end
---@param node Node
---@return boolean
local function matches(self, node)
if not self.explorer.filters.config.enable then
if not self.explorer.filters.enabled then
return true
end

Expand Down

0 comments on commit 84d5f0a

Please sign in to comment.