add FilterTypes named map
This commit is contained in:
@@ -22,43 +22,43 @@ end
|
|||||||
|
|
||||||
---@param explorer Explorer
|
---@param explorer Explorer
|
||||||
local function custom(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)
|
reload(explorer)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param explorer Explorer
|
---@param explorer Explorer
|
||||||
local function git_ignored(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)
|
reload(explorer)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param explorer Explorer
|
---@param explorer Explorer
|
||||||
local function git_clean(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)
|
reload(explorer)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param explorer Explorer
|
---@param explorer Explorer
|
||||||
local function no_buffer(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)
|
reload(explorer)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param explorer Explorer
|
---@param explorer Explorer
|
||||||
local function no_bookmark(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)
|
reload(explorer)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param explorer Explorer
|
---@param explorer Explorer
|
||||||
local function dotfiles(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)
|
reload(explorer)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param explorer Explorer
|
---@param explorer Explorer
|
||||||
local function enable(explorer)
|
local function enable(explorer)
|
||||||
explorer.filters.config.enable = not explorer.filters.config.enable
|
explorer.filters.enabled = not explorer.filters.enabled
|
||||||
reload(explorer)
|
reload(explorer)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -3,8 +3,11 @@ local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON
|
|||||||
|
|
||||||
local Class = require("nvim-tree.classic")
|
local Class = require("nvim-tree.classic")
|
||||||
|
|
||||||
|
---@alias FilterTypes "custom" | "dotfiles" | "git_ignored" | "git_clean" | "no_buffer" | "no_bookmark"
|
||||||
|
|
||||||
---@class (exact) Filters: Class
|
---@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 explorer Explorer
|
||||||
---@field private exclude_list string[] filters.exclude
|
---@field private exclude_list string[] filters.exclude
|
||||||
---@field private ignore_list table<string, boolean> filters.custom string table
|
---@field private ignore_list table<string, boolean> filters.custom string table
|
||||||
@@ -23,14 +26,15 @@ function Filters:new(args)
|
|||||||
self.ignore_list = {}
|
self.ignore_list = {}
|
||||||
self.exclude_list = self.explorer.opts.filters.exclude
|
self.exclude_list = self.explorer.opts.filters.exclude
|
||||||
self.custom_function = nil
|
self.custom_function = nil
|
||||||
self.config = {
|
|
||||||
enable = self.explorer.opts.filters.enable,
|
self.enabled = self.explorer.opts.filters.enable
|
||||||
filter_custom = true,
|
self.states = {
|
||||||
filter_dotfiles = self.explorer.opts.filters.dotfiles,
|
custom = true,
|
||||||
filter_git_ignored = self.explorer.opts.filters.git_ignored,
|
dotfiles = self.explorer.opts.filters.dotfiles,
|
||||||
filter_git_clean = self.explorer.opts.filters.git_clean,
|
git_ignored = self.explorer.opts.filters.git_ignored,
|
||||||
filter_no_buffer = self.explorer.opts.filters.no_buffer,
|
git_clean = self.explorer.opts.filters.git_clean,
|
||||||
filter_no_bookmark = self.explorer.opts.filters.no_bookmark,
|
no_buffer = self.explorer.opts.filters.no_buffer,
|
||||||
|
no_bookmark = self.explorer.opts.filters.no_bookmark,
|
||||||
}
|
}
|
||||||
|
|
||||||
local custom_filter = self.explorer.opts.filters.custom
|
local custom_filter = self.explorer.opts.filters.custom
|
||||||
@@ -71,12 +75,12 @@ local function git(self, path, project)
|
|||||||
xy = xy or project.dirs.indirect[path] and project.dirs.indirect[path][1]
|
xy = xy or project.dirs.indirect[path] and project.dirs.indirect[path][1]
|
||||||
|
|
||||||
-- filter ignored; overrides clean as they are effectively dirty
|
-- 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
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
-- filter clean
|
-- filter clean
|
||||||
if self.config.filter_git_clean and not xy then
|
if self.states.git_clean and not xy then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -88,7 +92,7 @@ end
|
|||||||
---@param bufinfo table vim.fn.getbufinfo { buflisted = 1 }
|
---@param bufinfo table vim.fn.getbufinfo { buflisted = 1 }
|
||||||
---@return boolean
|
---@return boolean
|
||||||
local function buf(self, path, bufinfo)
|
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
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -105,7 +109,7 @@ end
|
|||||||
---@param path string
|
---@param path string
|
||||||
---@return boolean
|
---@return boolean
|
||||||
local function dotfile(self, path)
|
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
|
end
|
||||||
|
|
||||||
---Bookmark is present
|
---Bookmark is present
|
||||||
@@ -114,7 +118,7 @@ end
|
|||||||
---@param bookmarks table<string, string|nil> path, filetype table of bookmarked files
|
---@param bookmarks table<string, string|nil> path, filetype table of bookmarked files
|
||||||
---@return boolean
|
---@return boolean
|
||||||
local function bookmark(self, path, path_type, bookmarks)
|
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
|
return false
|
||||||
end
|
end
|
||||||
-- if bookmark is empty, we should see a empty filetree
|
-- if bookmark is empty, we should see a empty filetree
|
||||||
@@ -149,7 +153,7 @@ end
|
|||||||
---@param path string
|
---@param path string
|
||||||
---@return boolean
|
---@return boolean
|
||||||
local function custom(self, path)
|
local function custom(self, path)
|
||||||
if not self.config.filter_custom then
|
if not self.states.custom then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -191,7 +195,7 @@ function Filters:prepare(project)
|
|||||||
bookmarks = {},
|
bookmarks = {},
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.config.filter_no_buffer then
|
if self.states.no_buffer then
|
||||||
status.bufinfo = vim.fn.getbufinfo({ buflisted = 1 })
|
status.bufinfo = vim.fn.getbufinfo({ buflisted = 1 })
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -211,7 +215,7 @@ end
|
|||||||
---@param status table from prepare
|
---@param status table from prepare
|
||||||
---@return boolean
|
---@return boolean
|
||||||
function Filters:should_filter(path, fs_stat, status)
|
function Filters:should_filter(path, fs_stat, status)
|
||||||
if not self.config.enable then
|
if not self.enabled then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -233,7 +237,7 @@ end
|
|||||||
---@param status table from prepare
|
---@param status table from prepare
|
||||||
---@return FILTER_REASON
|
---@return FILTER_REASON
|
||||||
function Filters:should_filter_as_reason(path, fs_stat, status)
|
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
|
return FILTER_REASON.none
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -100,7 +100,7 @@ function Explorer:create_autocmds()
|
|||||||
vim.api.nvim_create_autocmd("BufReadPost", {
|
vim.api.nvim_create_autocmd("BufReadPost", {
|
||||||
group = self.augroup_id,
|
group = self.augroup_id,
|
||||||
callback = function(data)
|
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()
|
utils.debounce("Buf:filter_buffer_" .. self.uid_explorer, self.opts.view.debounce_delay, function()
|
||||||
self:reload_explorer()
|
self:reload_explorer()
|
||||||
end)
|
end)
|
||||||
@@ -112,7 +112,7 @@ function Explorer:create_autocmds()
|
|||||||
vim.api.nvim_create_autocmd("BufUnload", {
|
vim.api.nvim_create_autocmd("BufUnload", {
|
||||||
group = self.augroup_id,
|
group = self.augroup_id,
|
||||||
callback = function(data)
|
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()
|
utils.debounce("Buf:filter_buffer_" .. self.uid_explorer, self.opts.view.debounce_delay, function()
|
||||||
self:reload_explorer()
|
self:reload_explorer()
|
||||||
end)
|
end)
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ end
|
|||||||
---@param node Node
|
---@param node Node
|
||||||
---@return boolean
|
---@return boolean
|
||||||
local function matches(self, node)
|
local function matches(self, node)
|
||||||
if not self.explorer.filters.config.enable then
|
if not self.explorer.filters.enabled then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user