Filters private methods

This commit is contained in:
Alexander Courtis 2024-11-09 09:35:55 +11:00
parent ddd28ecd01
commit 07a9fb17c0
2 changed files with 38 additions and 31 deletions

View File

@ -7,7 +7,7 @@ local Class = require("nvim-tree.classic")
---@class (exact) Filters: Class
---@field enabled boolean
---@field states table<FilterType, boolean>
---@field state table<FilterType, boolean>
---@field private explorer Explorer
---@field private exclude_list string[] filters.exclude
---@field private ignore_list table<string, boolean> filters.custom string table
@ -29,7 +29,7 @@ function Filters:new(args)
self.custom_function = nil
self.enabled = self.explorer.opts.filters.enable
self.states = {
self.state = {
custom = true,
dotfiles = self.explorer.opts.filters.dotfiles,
git_ignored = self.explorer.opts.filters.git_ignored,
@ -50,9 +50,10 @@ function Filters:new(args)
end
end
---@private
---@param path string
---@return boolean
local function is_excluded(self, path)
function Filters:is_excluded(path)
for _, node in ipairs(self.exclude_list) do
if path:match(node) then
return true
@ -62,10 +63,11 @@ local function is_excluded(self, path)
end
---Check if the given path is git clean/ignored
---@private
---@param path string Absolute path
---@param project GitProject from prepare
---@return boolean
local function git(self, path, project)
function Filters:git(path, project)
if type(project) ~= "table" or type(project.files) ~= "table" or type(project.dirs) ~= "table" then
return false
end
@ -76,12 +78,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.states.git_ignored and xy == "!!" then
if self.state.git_ignored and xy == "!!" then
return true
end
-- filter clean
if self.states.git_clean and not xy then
if self.state.git_clean and not xy then
return true
end
@ -89,11 +91,12 @@ local function git(self, path, project)
end
---Check if the given path has no listed buffer
---@private
---@param path string Absolute path
---@param bufinfo table vim.fn.getbufinfo { buflisted = 1 }
---@return boolean
local function buf(self, path, bufinfo)
if not self.states.no_buffer or type(bufinfo) ~= "table" then
function Filters:buf(path, bufinfo)
if not self.state.no_buffer or type(bufinfo) ~= "table" then
return false
end
@ -107,19 +110,21 @@ local function buf(self, path, bufinfo)
return true
end
---@private
---@param path string
---@return boolean
local function dotfile(self, path)
return self.states.dotfiles and utils.path_basename(path):sub(1, 1) == "."
function Filters:dotfile(path)
return self.state.dotfiles and utils.path_basename(path):sub(1, 1) == "."
end
---Bookmark is present
---@private
---@param path string
---@param path_type string|nil filetype of path
---@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.states.no_bookmark then
function Filters:bookmark(path, path_type, bookmarks)
if not self.state.no_bookmark then
return false
end
-- if bookmark is empty, we should see a empty filetree
@ -151,10 +156,11 @@ local function bookmark(self, path, path_type, bookmarks)
return true
end
---@private
---@param path string
---@return boolean
local function custom(self, path)
if not self.states.custom then
function Filters:custom(path)
if not self.state.custom then
return false
end
@ -196,7 +202,7 @@ function Filters:prepare(project)
bookmarks = {},
}
if self.states.no_buffer then
if self.state.no_buffer then
status.bufinfo = vim.fn.getbufinfo({ buflisted = 1 })
end
@ -221,15 +227,15 @@ function Filters:should_filter(path, fs_stat, status)
end
-- exclusions override all filters
if is_excluded(self, path) then
if self:is_excluded(path) then
return false
end
return git(self, path, status.project)
or buf(self, path, status.bufinfo)
or dotfile(self, path)
or custom(self, path)
or bookmark(self, path, fs_stat and fs_stat.type, status.bookmarks)
return self:git(path, status.project)
or self:buf(path, status.bufinfo)
or self:dotfile(path)
or self:custom(path)
or self:bookmark(path, fs_stat and fs_stat.type, status.bookmarks)
end
--- Check if the given path should be filtered, and provide the reason why it was
@ -242,19 +248,19 @@ function Filters:should_filter_as_reason(path, fs_stat, status)
return FILTER_REASON.none
end
if is_excluded(self, path) then
if self:is_excluded(path) then
return FILTER_REASON.none
end
if git(self, path, status.project) then
if self:git(path, status.project) then
return FILTER_REASON.git
elseif buf(self, path, status.bufinfo) then
elseif self:buf(path, status.bufinfo) then
return FILTER_REASON.buf
elseif dotfile(self, path) then
elseif self:dotfile(path) then
return FILTER_REASON.dotfile
elseif custom(self, path) then
elseif self:custom(path) then
return FILTER_REASON.custom
elseif bookmark(self, path, fs_stat and fs_stat.type, status.bookmarks) then
elseif self:bookmark(path, fs_stat and fs_stat.type, status.bookmarks) then
return FILTER_REASON.bookmark
else
return FILTER_REASON.none
@ -262,12 +268,13 @@ function Filters:should_filter_as_reason(path, fs_stat, status)
end
---Toggle a type and refresh
---@private
---@param type FilterType? nil to disable all
function Filters:toggle(type)
if not type or self.states[type] == nil then
if not type or self.state[type] == nil then
self.enabled = not self.enabled
else
self.states[type] = not self.states[type]
self.state[type] = not self.state[type]
end
local node = self.explorer:get_node_at_cursor()

View File

@ -101,7 +101,7 @@ function Explorer:create_autocmds()
vim.api.nvim_create_autocmd("BufReadPost", {
group = self.augroup_id,
callback = function(data)
if (self.filters.states.no_buffer or self.opts.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == "" then
if (self.filters.state.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)
@ -113,7 +113,7 @@ function Explorer:create_autocmds()
vim.api.nvim_create_autocmd("BufUnload", {
group = self.augroup_id,
callback = function(data)
if (self.filters.states.no_buffer or self.opts.highlight_opened_files ~= "none") and vim.bo[data.buf].buftype == "" then
if (self.filters.state.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)