Filter uses classic, tidy opts

This commit is contained in:
Alexander Courtis
2024-11-08 12:00:44 +11:00
parent 2c172cf037
commit 67cc06e651
4 changed files with 32 additions and 31 deletions

View File

@@ -1,47 +1,48 @@
local utils = require("nvim-tree.utils") local utils = require("nvim-tree.utils")
local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON local FILTER_REASON = require("nvim-tree.enum").FILTER_REASON
---@class Filters to handle all opts.filters and related API local Class = require("nvim-tree.classic")
---@class (exact) Filters: Class
---@field config table hydrated user opts.filters ---@field config table hydrated user opts.filters
---@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 string[] filters.custom string table ---@field private ignore_list table<string, boolean> filters.custom string table
---@field private custom_function (fun(absolute_path: string): boolean)|nil filters.custom function ---@field private custom_function (fun(absolute_path: string): boolean)|nil filters.custom function
local Filters = {} local Filters = Class:extend()
---@param opts table user options ---@class Filters
---@param explorer Explorer ---@overload fun(args: FiltersArgs): Filters
---@return Filters
function Filters:new(opts, explorer) ---@class (exact) FiltersArgs
local o = { ---@field explorer Explorer
explorer = explorer,
ignore_list = {}, ---@param args FiltersArgs
exclude_list = opts.filters.exclude, function Filters:new(args)
custom_function = nil, self.explorer = args.explorer
config = { self.ignore_list = {}
enable = opts.filters.enable, self.exclude_list = self.explorer.opts.filters.exclude
self.custom_function = nil
self.config = {
enable = self.explorer.opts.filters.enable,
filter_custom = true, filter_custom = true,
filter_dotfiles = opts.filters.dotfiles, filter_dotfiles = self.explorer.opts.filters.dotfiles,
filter_git_ignored = opts.filters.git_ignored, filter_git_ignored = self.explorer.opts.filters.git_ignored,
filter_git_clean = opts.filters.git_clean, filter_git_clean = self.explorer.opts.filters.git_clean,
filter_no_buffer = opts.filters.no_buffer, filter_no_buffer = self.explorer.opts.filters.no_buffer,
filter_no_bookmark = opts.filters.no_bookmark, filter_no_bookmark = self.explorer.opts.filters.no_bookmark,
},
} }
local custom_filter = opts.filters.custom local custom_filter = self.explorer.opts.filters.custom
if type(custom_filter) == "function" then if type(custom_filter) == "function" then
o.custom_function = custom_filter self.custom_function = custom_filter
else else
if custom_filter and #custom_filter > 0 then if custom_filter and #custom_filter > 0 then
for _, filter_name in pairs(custom_filter) do for _, filter_name in pairs(custom_filter) do
o.ignore_list[filter_name] = true self.ignore_list[filter_name] = true
end end
end end
end end
setmetatable(o, self)
self.__index = self
return o
end end
---@param path string ---@param path string

View File

@@ -59,7 +59,7 @@ function Explorer:new(args)
self.sorters = Sorter(config) self.sorters = Sorter(config)
self.renderer = Renderer({ explorer = self }) self.renderer = Renderer({ explorer = self })
self.filters = Filters:new(config, self) self.filters = Filters({ explorer = self })
self.live_filter = LiveFilter({ explorer = self }) self.live_filter = LiveFilter({ explorer = self })
self.marks = Marks:new(config, self) self.marks = Marks:new(config, self)
self.clipboard = Clipboard:new(config, self) self.clipboard = Clipboard:new(config, self)

View File

@@ -21,8 +21,8 @@ local LiveFilter = Class:extend()
---@param args LiveFilterArgs ---@param args LiveFilterArgs
function LiveFilter:new(args) function LiveFilter:new(args)
self.explorer = args.explorer self.explorer = args.explorer
self.prefix = args.explorer.opts.live_filter.prefix self.prefix = self.explorer.opts.live_filter.prefix
self.always_show_folders = args.explorer.opts.live_filter.always_show_folders self.always_show_folders = self.explorer.opts.live_filter.always_show_folders
self.filter = nil self.filter = nil
end end

View File

@@ -71,7 +71,7 @@ function Builder:new(args)
DecoratorOpened({ explorer = args.explorer }), DecoratorOpened({ explorer = args.explorer }),
DecoratorGit({ explorer = args.explorer }) DecoratorGit({ explorer = args.explorer })
} }
self.hidden_display = Builder:setup_hidden_display_function(args.explorer.opts) self.hidden_display = Builder:setup_hidden_display_function(self.explorer.opts)
end end
---Insert ranged highlight groups into self.highlights ---Insert ranged highlight groups into self.highlights