Skip to content

Commit 4da6f4b

Browse files
committed
typechecked optargs constructors for decorators, WIP
1 parent 692fff7 commit 4da6f4b

File tree

6 files changed

+97
-93
lines changed

6 files changed

+97
-93
lines changed

lua/nvim-tree/renderer/builder.lua

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ local DirectoryNode = require("nvim-tree.node.directory")
77
local DecoratorBookmarks = require("nvim-tree.renderer.decorator.bookmarks")
88
local DecoratorCopied = require("nvim-tree.renderer.decorator.copied")
99
local DecoratorCut = require("nvim-tree.renderer.decorator.cut")
10-
local DecoratorDiagnostics = require("nvim-tree.renderer.decorator.diagnostics")
10+
-- local DecoratorDiagnostics = require("nvim-tree.renderer.decorator.diagnostics")
1111
local DecoratorGit = require("nvim-tree.renderer.decorator.git")
12-
local DecoratorModified = require("nvim-tree.renderer.decorator.modified")
13-
local DecoratorHidden = require("nvim-tree.renderer.decorator.hidden")
14-
local DecoratorOpened = require("nvim-tree.renderer.decorator.opened")
12+
-- local DecoratorModified = require("nvim-tree.renderer.decorator.modified")
13+
-- local DecoratorHidden = require("nvim-tree.renderer.decorator.hidden")
14+
-- local DecoratorOpened = require("nvim-tree.renderer.decorator.opened")
1515

1616
local pad = require("nvim-tree.renderer.components.padding")
1717

@@ -61,14 +61,14 @@ function Builder:new(opts, explorer)
6161
virtual_lines = {},
6262
decorators = {
6363
-- priority order
64-
DecoratorCut:create(opts, explorer),
65-
DecoratorCopied:create(opts, explorer),
66-
DecoratorDiagnostics:create(opts, explorer),
67-
DecoratorBookmarks:create(opts, explorer),
68-
DecoratorModified:create(opts, explorer),
69-
DecoratorHidden:create(opts, explorer),
70-
DecoratorOpened:create(opts, explorer),
71-
DecoratorGit:create(opts, explorer),
64+
DecoratorCut({ explorer = explorer }),
65+
DecoratorCopied({ explorer = explorer }),
66+
-- DecoratorDiagnostics({ explorer = explorer }),
67+
DecoratorBookmarks({ explorer = explorer }),
68+
-- DecoratorModified({ explorer = explorer }),
69+
-- DecoratorHidden({ explorer = explorer }),
70+
-- DecoratorOpened({ explorer = explorer }),
71+
DecoratorGit({ explorer = explorer })
7272
},
7373
hidden_display = Builder:setup_hidden_display_function(opts),
7474
}

lua/nvim-tree/renderer/decorator/bookmarks.lua

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,28 @@ local Decorator = require("nvim-tree.renderer.decorator")
55

66
---@class (exact) DecoratorBookmarks: Decorator
77
---@field icon HighlightedString?
8-
local DecoratorBookmarks = Decorator:new()
9-
10-
---Static factory method
11-
---@param opts table
12-
---@param explorer Explorer
13-
---@return DecoratorBookmarks
14-
function DecoratorBookmarks:create(opts, explorer)
15-
---@type DecoratorBookmarks
16-
local o = {
17-
explorer = explorer,
18-
enabled = true,
19-
hl_pos = HL_POSITION[opts.renderer.highlight_bookmarks] or HL_POSITION.none,
20-
icon_placement = ICON_PLACEMENT[opts.renderer.icons.bookmarks_placement] or ICON_PLACEMENT.none,
21-
}
22-
o = self:new(o)
23-
24-
if opts.renderer.icons.show.bookmarks then
25-
o.icon = {
26-
str = opts.renderer.icons.glyphs.bookmark,
8+
local DecoratorBookmarks = Decorator:extend()
9+
10+
---@class DecoratorBookmarks
11+
---@overload fun(explorer: DecoratorArgs): DecoratorBookmarks
12+
13+
---@private
14+
---@param args DecoratorArgs
15+
function DecoratorBookmarks:new(args)
16+
Decorator.new(self, {
17+
explorer = args.explorer,
18+
enabled = true,
19+
hl_pos = HL_POSITION[args.explorer.opts.renderer.highlight_bookmarks] or HL_POSITION.none,
20+
icon_placement = ICON_PLACEMENT[args.explorer.opts.renderer.icons.bookmarks_placement] or ICON_PLACEMENT.none,
21+
})
22+
23+
if self.explorer.opts.renderer.icons.show.bookmarks then
24+
self.icon = {
25+
str = self.explorer.opts.renderer.icons.glyphs.bookmark,
2726
hl = { "NvimTreeBookmarkIcon" },
2827
}
29-
o:define_sign(o.icon)
28+
self:define_sign(self.icon)
3029
end
31-
32-
return o
3330
end
3431

3532
---Bookmark icon: renderer.icons.show.bookmarks and node is marked

lua/nvim-tree/renderer/decorator/copied.lua

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,20 @@ local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
44
local Decorator = require("nvim-tree.renderer.decorator")
55

66
---@class (exact) DecoratorCopied: Decorator
7-
---@field icon HighlightedString?
8-
local DecoratorCopied = Decorator:new()
7+
local DecoratorCopied = Decorator:extend()
98

10-
---Static factory method
11-
---@param opts table
12-
---@param explorer Explorer
13-
---@return DecoratorCopied
14-
function DecoratorCopied:create(opts, explorer)
15-
---@type DecoratorCopied
16-
local o = {
17-
explorer = explorer,
18-
enabled = true,
19-
hl_pos = HL_POSITION[opts.renderer.highlight_clipboard] or HL_POSITION.none,
20-
icon_placement = ICON_PLACEMENT.none,
21-
}
22-
o = self:new(o)
9+
---@class DecoratorCopied
10+
---@overload fun(explorer: DecoratorArgs): DecoratorCopied
2311

24-
return o
12+
---@private
13+
---@param args DecoratorArgs
14+
function DecoratorCopied:new(args)
15+
Decorator.new(self, {
16+
explorer = args.explorer,
17+
enabled = true,
18+
hl_pos = HL_POSITION[args.explorer.opts.renderer.highlight_clipboard] or HL_POSITION.none,
19+
icon_placement = ICON_PLACEMENT.none,
20+
})
2521
end
2622

2723
---Copied highlight: renderer.highlight_clipboard and node is copied

lua/nvim-tree/renderer/decorator/cut.lua

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,20 @@ local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
44
local Decorator = require("nvim-tree.renderer.decorator")
55

66
---@class (exact) DecoratorCut: Decorator
7-
local DecoratorCut = Decorator:new()
7+
local DecoratorCut = Decorator:extend()
88

9-
---Static factory method
10-
---@param opts table
11-
---@param explorer Explorer
12-
---@return DecoratorCut
13-
function DecoratorCut:create(opts, explorer)
14-
---@type DecoratorCut
15-
local o = {
16-
explorer = explorer,
17-
enabled = true,
18-
hl_pos = HL_POSITION[opts.renderer.highlight_clipboard] or HL_POSITION.none,
19-
icon_placement = ICON_PLACEMENT.none,
20-
}
21-
o = self:new(o)
9+
---@class DecoratorCut
10+
---@overload fun(explorer: DecoratorArgs): DecoratorCut
2211

23-
return o
12+
---@private
13+
---@param args DecoratorArgs
14+
function DecoratorCut:new(args)
15+
Decorator.new(self, {
16+
explorer = args.explorer,
17+
enabled = true,
18+
hl_pos = HL_POSITION[args.explorer.opts.renderer.highlight_clipboard] or HL_POSITION.none,
19+
icon_placement = ICON_PLACEMENT.none,
20+
})
2421
end
2522

2623
---Cut highlight: renderer.highlight_clipboard and node is cut

lua/nvim-tree/renderer/decorator/git.lua

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,40 +20,37 @@ local DirectoryNode = require("nvim-tree.node.directory")
2020
---@field folder_hl_by_xy table<GitXY, string>?
2121
---@field icons_by_status GitIconsByStatus?
2222
---@field icons_by_xy GitIconsByXY?
23-
local DecoratorGit = Decorator:new()
24-
25-
---Static factory method
26-
---@param opts table
27-
---@param explorer Explorer
28-
---@return DecoratorGit
29-
function DecoratorGit:create(opts, explorer)
30-
---@type DecoratorGit
31-
local o = {
32-
explorer = explorer,
33-
enabled = opts.git.enable,
34-
hl_pos = HL_POSITION[opts.renderer.highlight_git] or HL_POSITION.none,
35-
icon_placement = ICON_PLACEMENT[opts.renderer.icons.git_placement] or ICON_PLACEMENT.none,
36-
}
37-
o = self:new(o)
38-
39-
if not o.enabled then
40-
return o
23+
local DecoratorGit = Decorator:extend()
24+
25+
---@class DecoratorGit
26+
---@overload fun(explorer: DecoratorArgs): DecoratorGit
27+
28+
---@private
29+
---@param args DecoratorArgs
30+
function DecoratorGit:new(args)
31+
Decorator.new(self, {
32+
explorer = args.explorer,
33+
enabled = args.explorer.opts.git.enable,
34+
hl_pos = HL_POSITION[args.explorer.opts.renderer.highlight_git] or HL_POSITION.none,
35+
icon_placement = ICON_PLACEMENT[args.explorer.opts.renderer.icons.git_placement] or ICON_PLACEMENT.none,
36+
})
37+
38+
if not self.enabled then
39+
return
4140
end
4241

43-
if o.hl_pos ~= HL_POSITION.none then
44-
o:build_file_folder_hl_by_xy()
42+
if self.hl_pos ~= HL_POSITION.none then
43+
self:build_file_folder_hl_by_xy()
4544
end
4645

47-
if opts.renderer.icons.show.git then
48-
o:build_icons_by_status(opts.renderer.icons.glyphs.git)
49-
o:build_icons_by_xy(o.icons_by_status)
46+
if self.explorer.opts.renderer.icons.show.git then
47+
self:build_icons_by_status(self.explorer.opts.renderer.icons.glyphs.git)
48+
self:build_icons_by_xy(self.icons_by_status)
5049

51-
for _, icon in pairs(o.icons_by_status) do
50+
for _, icon in pairs(self.icons_by_status) do
5251
self:define_sign(icon)
5352
end
5453
end
55-
56-
return o
5754
end
5855

5956
---@param glyphs GitGlyphsByStatus

lua/nvim-tree/renderer/decorator/init.lua

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
local Class = require("nvim-tree.class")
1+
local Class = require("nvim-tree.classic")
22

33
local HL_POSITION = require("nvim-tree.enum").HL_POSITION
44
local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
@@ -10,7 +10,24 @@ local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
1010
---@field protected enabled boolean
1111
---@field protected hl_pos HL_POSITION
1212
---@field protected icon_placement ICON_PLACEMENT
13-
local Decorator = Class:new()
13+
local Decorator = Class:extend()
14+
15+
---@class (exact) DecoratorArgs
16+
---@field explorer Explorer
17+
18+
---@class (exact) AbstractDecoratorArgs: DecoratorArgs
19+
---@field enabled boolean
20+
---@field hl_pos HL_POSITION
21+
---@field icon_placement ICON_PLACEMENT
22+
23+
---@protected
24+
---@param args AbstractDecoratorArgs
25+
function Decorator:new(args)
26+
self.explorer = args.explorer
27+
self.enabled = args.enabled
28+
self.hl_pos = args.hl_pos
29+
self.icon_placement = args.icon_placement
30+
end
1431

1532
---Maybe highlight groups
1633
---@param node Node

0 commit comments

Comments
 (0)