From 67cc06e6513d389914127c5e0a175e67ad576cba Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Fri, 8 Nov 2024 12:00:44 +1100 Subject: [PATCH] Filter uses classic, tidy opts --- lua/nvim-tree/explorer/filters.lua | 55 +++++++++++++------------- lua/nvim-tree/explorer/init.lua | 2 +- lua/nvim-tree/explorer/live-filter.lua | 4 +- lua/nvim-tree/renderer/builder.lua | 2 +- 4 files changed, 32 insertions(+), 31 deletions(-) diff --git a/lua/nvim-tree/explorer/filters.lua b/lua/nvim-tree/explorer/filters.lua index 8de9f150..0fb44ec8 100644 --- a/lua/nvim-tree/explorer/filters.lua +++ b/lua/nvim-tree/explorer/filters.lua @@ -1,47 +1,48 @@ local utils = require("nvim-tree.utils") 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 private explorer Explorer ---@field private exclude_list string[] filters.exclude ----@field private ignore_list string[] filters.custom string table +---@field private ignore_list table filters.custom string table ---@field private custom_function (fun(absolute_path: string): boolean)|nil filters.custom function -local Filters = {} +local Filters = Class:extend() ----@param opts table user options ----@param explorer Explorer ----@return Filters -function Filters:new(opts, explorer) - local o = { - explorer = explorer, - ignore_list = {}, - exclude_list = opts.filters.exclude, - custom_function = nil, - config = { - enable = opts.filters.enable, - filter_custom = true, - filter_dotfiles = opts.filters.dotfiles, - filter_git_ignored = opts.filters.git_ignored, - filter_git_clean = opts.filters.git_clean, - filter_no_buffer = opts.filters.no_buffer, - filter_no_bookmark = opts.filters.no_bookmark, - }, +---@class Filters +---@overload fun(args: FiltersArgs): Filters + +---@class (exact) FiltersArgs +---@field explorer Explorer + +---@param args FiltersArgs +function Filters:new(args) + self.explorer = args.explorer + 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, } - local custom_filter = opts.filters.custom + local custom_filter = self.explorer.opts.filters.custom if type(custom_filter) == "function" then - o.custom_function = custom_filter + self.custom_function = custom_filter else if custom_filter and #custom_filter > 0 then for _, filter_name in pairs(custom_filter) do - o.ignore_list[filter_name] = true + self.ignore_list[filter_name] = true end end end - setmetatable(o, self) - self.__index = self - return o end ---@param path string diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index 36c3391e..23434fab 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -59,7 +59,7 @@ function Explorer:new(args) self.sorters = Sorter(config) self.renderer = Renderer({ explorer = self }) - self.filters = Filters:new(config, self) + self.filters = Filters({ explorer = self }) self.live_filter = LiveFilter({ explorer = self }) self.marks = Marks:new(config, self) self.clipboard = Clipboard:new(config, self) diff --git a/lua/nvim-tree/explorer/live-filter.lua b/lua/nvim-tree/explorer/live-filter.lua index 30170f40..c59b0c0b 100644 --- a/lua/nvim-tree/explorer/live-filter.lua +++ b/lua/nvim-tree/explorer/live-filter.lua @@ -21,8 +21,8 @@ local LiveFilter = Class:extend() ---@param args LiveFilterArgs function LiveFilter:new(args) self.explorer = args.explorer - self.prefix = args.explorer.opts.live_filter.prefix - self.always_show_folders = args.explorer.opts.live_filter.always_show_folders + self.prefix = self.explorer.opts.live_filter.prefix + self.always_show_folders = self.explorer.opts.live_filter.always_show_folders self.filter = nil end diff --git a/lua/nvim-tree/renderer/builder.lua b/lua/nvim-tree/renderer/builder.lua index 2e2f1594..29b3d337 100644 --- a/lua/nvim-tree/renderer/builder.lua +++ b/lua/nvim-tree/renderer/builder.lua @@ -71,7 +71,7 @@ function Builder:new(args) DecoratorOpened({ 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 ---Insert ranged highlight groups into self.highlights