typechecked optargs constructors for decorators, WIP

This commit is contained in:
Alexander Courtis
2024-11-06 17:50:52 +11:00
parent 692fff7745
commit 4da6f4baaa
6 changed files with 93 additions and 89 deletions

View File

@@ -7,11 +7,11 @@ local DirectoryNode = require("nvim-tree.node.directory")
local DecoratorBookmarks = require("nvim-tree.renderer.decorator.bookmarks") local DecoratorBookmarks = require("nvim-tree.renderer.decorator.bookmarks")
local DecoratorCopied = require("nvim-tree.renderer.decorator.copied") local DecoratorCopied = require("nvim-tree.renderer.decorator.copied")
local DecoratorCut = require("nvim-tree.renderer.decorator.cut") local DecoratorCut = require("nvim-tree.renderer.decorator.cut")
local DecoratorDiagnostics = require("nvim-tree.renderer.decorator.diagnostics") -- local DecoratorDiagnostics = require("nvim-tree.renderer.decorator.diagnostics")
local DecoratorGit = require("nvim-tree.renderer.decorator.git") local DecoratorGit = require("nvim-tree.renderer.decorator.git")
local DecoratorModified = require("nvim-tree.renderer.decorator.modified") -- local DecoratorModified = require("nvim-tree.renderer.decorator.modified")
local DecoratorHidden = require("nvim-tree.renderer.decorator.hidden") -- local DecoratorHidden = require("nvim-tree.renderer.decorator.hidden")
local DecoratorOpened = require("nvim-tree.renderer.decorator.opened") -- local DecoratorOpened = require("nvim-tree.renderer.decorator.opened")
local pad = require("nvim-tree.renderer.components.padding") local pad = require("nvim-tree.renderer.components.padding")
@@ -61,14 +61,14 @@ function Builder:new(opts, explorer)
virtual_lines = {}, virtual_lines = {},
decorators = { decorators = {
-- priority order -- priority order
DecoratorCut:create(opts, explorer), DecoratorCut({ explorer = explorer }),
DecoratorCopied:create(opts, explorer), DecoratorCopied({ explorer = explorer }),
DecoratorDiagnostics:create(opts, explorer), -- DecoratorDiagnostics({ explorer = explorer }),
DecoratorBookmarks:create(opts, explorer), DecoratorBookmarks({ explorer = explorer }),
DecoratorModified:create(opts, explorer), -- DecoratorModified({ explorer = explorer }),
DecoratorHidden:create(opts, explorer), -- DecoratorHidden({ explorer = explorer }),
DecoratorOpened:create(opts, explorer), -- DecoratorOpened({ explorer = explorer }),
DecoratorGit:create(opts, explorer), DecoratorGit({ explorer = explorer })
}, },
hidden_display = Builder:setup_hidden_display_function(opts), hidden_display = Builder:setup_hidden_display_function(opts),
} }

View File

@@ -5,31 +5,28 @@ local Decorator = require("nvim-tree.renderer.decorator")
---@class (exact) DecoratorBookmarks: Decorator ---@class (exact) DecoratorBookmarks: Decorator
---@field icon HighlightedString? ---@field icon HighlightedString?
local DecoratorBookmarks = Decorator:new() local DecoratorBookmarks = Decorator:extend()
---Static factory method ---@class DecoratorBookmarks
---@param opts table ---@overload fun(explorer: DecoratorArgs): DecoratorBookmarks
---@param explorer Explorer
---@return DecoratorBookmarks ---@private
function DecoratorBookmarks:create(opts, explorer) ---@param args DecoratorArgs
---@type DecoratorBookmarks function DecoratorBookmarks:new(args)
local o = { Decorator.new(self, {
explorer = explorer, explorer = args.explorer,
enabled = true, enabled = true,
hl_pos = HL_POSITION[opts.renderer.highlight_bookmarks] or HL_POSITION.none, hl_pos = HL_POSITION[args.explorer.opts.renderer.highlight_bookmarks] or HL_POSITION.none,
icon_placement = ICON_PLACEMENT[opts.renderer.icons.bookmarks_placement] or ICON_PLACEMENT.none, icon_placement = ICON_PLACEMENT[args.explorer.opts.renderer.icons.bookmarks_placement] or ICON_PLACEMENT.none,
} })
o = self:new(o)
if opts.renderer.icons.show.bookmarks then if self.explorer.opts.renderer.icons.show.bookmarks then
o.icon = { self.icon = {
str = opts.renderer.icons.glyphs.bookmark, str = self.explorer.opts.renderer.icons.glyphs.bookmark,
hl = { "NvimTreeBookmarkIcon" }, hl = { "NvimTreeBookmarkIcon" },
} }
o:define_sign(o.icon) self:define_sign(self.icon)
end end
return o
end end
---Bookmark icon: renderer.icons.show.bookmarks and node is marked ---Bookmark icon: renderer.icons.show.bookmarks and node is marked

View File

@@ -4,24 +4,20 @@ local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
local Decorator = require("nvim-tree.renderer.decorator") local Decorator = require("nvim-tree.renderer.decorator")
---@class (exact) DecoratorCopied: Decorator ---@class (exact) DecoratorCopied: Decorator
---@field icon HighlightedString? local DecoratorCopied = Decorator:extend()
local DecoratorCopied = Decorator:new()
---Static factory method ---@class DecoratorCopied
---@param opts table ---@overload fun(explorer: DecoratorArgs): DecoratorCopied
---@param explorer Explorer
---@return DecoratorCopied ---@private
function DecoratorCopied:create(opts, explorer) ---@param args DecoratorArgs
---@type DecoratorCopied function DecoratorCopied:new(args)
local o = { Decorator.new(self, {
explorer = explorer, explorer = args.explorer,
enabled = true, enabled = true,
hl_pos = HL_POSITION[opts.renderer.highlight_clipboard] or HL_POSITION.none, hl_pos = HL_POSITION[args.explorer.opts.renderer.highlight_clipboard] or HL_POSITION.none,
icon_placement = ICON_PLACEMENT.none, icon_placement = ICON_PLACEMENT.none,
} })
o = self:new(o)
return o
end end
---Copied highlight: renderer.highlight_clipboard and node is copied ---Copied highlight: renderer.highlight_clipboard and node is copied

View File

@@ -4,23 +4,20 @@ local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
local Decorator = require("nvim-tree.renderer.decorator") local Decorator = require("nvim-tree.renderer.decorator")
---@class (exact) DecoratorCut: Decorator ---@class (exact) DecoratorCut: Decorator
local DecoratorCut = Decorator:new() local DecoratorCut = Decorator:extend()
---Static factory method ---@class DecoratorCut
---@param opts table ---@overload fun(explorer: DecoratorArgs): DecoratorCut
---@param explorer Explorer
---@return DecoratorCut ---@private
function DecoratorCut:create(opts, explorer) ---@param args DecoratorArgs
---@type DecoratorCut function DecoratorCut:new(args)
local o = { Decorator.new(self, {
explorer = explorer, explorer = args.explorer,
enabled = true, enabled = true,
hl_pos = HL_POSITION[opts.renderer.highlight_clipboard] or HL_POSITION.none, hl_pos = HL_POSITION[args.explorer.opts.renderer.highlight_clipboard] or HL_POSITION.none,
icon_placement = ICON_PLACEMENT.none, icon_placement = ICON_PLACEMENT.none,
} })
o = self:new(o)
return o
end end
---Cut highlight: renderer.highlight_clipboard and node is cut ---Cut highlight: renderer.highlight_clipboard and node is cut

View File

@@ -20,40 +20,37 @@ local DirectoryNode = require("nvim-tree.node.directory")
---@field folder_hl_by_xy table<GitXY, string>? ---@field folder_hl_by_xy table<GitXY, string>?
---@field icons_by_status GitIconsByStatus? ---@field icons_by_status GitIconsByStatus?
---@field icons_by_xy GitIconsByXY? ---@field icons_by_xy GitIconsByXY?
local DecoratorGit = Decorator:new() local DecoratorGit = Decorator:extend()
---Static factory method ---@class DecoratorGit
---@param opts table ---@overload fun(explorer: DecoratorArgs): DecoratorGit
---@param explorer Explorer
---@return DecoratorGit
function DecoratorGit:create(opts, explorer)
---@type DecoratorGit
local o = {
explorer = explorer,
enabled = opts.git.enable,
hl_pos = HL_POSITION[opts.renderer.highlight_git] or HL_POSITION.none,
icon_placement = ICON_PLACEMENT[opts.renderer.icons.git_placement] or ICON_PLACEMENT.none,
}
o = self:new(o)
if not o.enabled then ---@private
return o ---@param args DecoratorArgs
function DecoratorGit:new(args)
Decorator.new(self, {
explorer = args.explorer,
enabled = args.explorer.opts.git.enable,
hl_pos = HL_POSITION[args.explorer.opts.renderer.highlight_git] or HL_POSITION.none,
icon_placement = ICON_PLACEMENT[args.explorer.opts.renderer.icons.git_placement] or ICON_PLACEMENT.none,
})
if not self.enabled then
return
end end
if o.hl_pos ~= HL_POSITION.none then if self.hl_pos ~= HL_POSITION.none then
o:build_file_folder_hl_by_xy() self:build_file_folder_hl_by_xy()
end end
if opts.renderer.icons.show.git then if self.explorer.opts.renderer.icons.show.git then
o:build_icons_by_status(opts.renderer.icons.glyphs.git) self:build_icons_by_status(self.explorer.opts.renderer.icons.glyphs.git)
o:build_icons_by_xy(o.icons_by_status) self:build_icons_by_xy(self.icons_by_status)
for _, icon in pairs(o.icons_by_status) do for _, icon in pairs(self.icons_by_status) do
self:define_sign(icon) self:define_sign(icon)
end end
end end
return o
end end
---@param glyphs GitGlyphsByStatus ---@param glyphs GitGlyphsByStatus

View File

@@ -1,4 +1,4 @@
local Class = require("nvim-tree.class") local Class = require("nvim-tree.classic")
local HL_POSITION = require("nvim-tree.enum").HL_POSITION local HL_POSITION = require("nvim-tree.enum").HL_POSITION
local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
@@ -10,7 +10,24 @@ local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
---@field protected enabled boolean ---@field protected enabled boolean
---@field protected hl_pos HL_POSITION ---@field protected hl_pos HL_POSITION
---@field protected icon_placement ICON_PLACEMENT ---@field protected icon_placement ICON_PLACEMENT
local Decorator = Class:new() local Decorator = Class:extend()
---@class (exact) DecoratorArgs
---@field explorer Explorer
---@class (exact) AbstractDecoratorArgs: DecoratorArgs
---@field enabled boolean
---@field hl_pos HL_POSITION
---@field icon_placement ICON_PLACEMENT
---@protected
---@param args AbstractDecoratorArgs
function Decorator:new(args)
self.explorer = args.explorer
self.enabled = args.enabled
self.hl_pos = args.hl_pos
self.icon_placement = args.icon_placement
end
---Maybe highlight groups ---Maybe highlight groups
---@param node Node ---@param node Node