Skip to content

Commit 7251251

Browse files
committed
use factory pattern for decorators
1 parent d092ccd commit 7251251

File tree

10 files changed

+66
-50
lines changed

10 files changed

+66
-50
lines changed

lua/nvim-tree/renderer/builder.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ function Builder:new(opts, explorer)
6868
virtual_lines = {},
6969
decorators = {
7070
-- priority order
71-
DecoratorCut:new(opts, explorer),
72-
DecoratorCopied:new(opts, explorer),
73-
DecoratorDiagnostics:new(opts, explorer),
74-
DecoratorBookmarks:new(opts, explorer),
75-
DecoratorModified:new(opts, explorer),
76-
DecoratorHidden:new(opts, explorer),
77-
DecoratorOpened:new(opts, explorer),
78-
DecoratorGit:new(opts, explorer),
71+
DecoratorCut:create(opts, explorer),
72+
DecoratorCopied:create(opts, explorer),
73+
DecoratorDiagnostics:create(opts, explorer),
74+
DecoratorBookmarks:create(opts, explorer),
75+
DecoratorModified:create(opts, explorer),
76+
DecoratorHidden:create(opts, explorer),
77+
DecoratorOpened:create(opts, explorer),
78+
DecoratorGit:create(opts, explorer),
7979
},
8080
hidden_display = Builder:setup_hidden_display_function(opts),
8181
}

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

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

66
---@class (exact) DecoratorBookmarks: Decorator
7-
---@field icon HighlightedString
7+
---@field icon HighlightedString?
88
local DecoratorBookmarks = Decorator:new()
99

10+
---Static factory method
1011
---@param opts table
1112
---@param explorer Explorer
1213
---@return DecoratorBookmarks
13-
function DecoratorBookmarks:new(opts, explorer)
14-
local o = Decorator.new(self, {
14+
function DecoratorBookmarks:create(opts, explorer)
15+
---@type DecoratorBookmarks
16+
local o = {
1517
explorer = explorer,
1618
enabled = true,
1719
hl_pos = HL_POSITION[opts.renderer.highlight_bookmarks] or HL_POSITION.none,
1820
icon_placement = ICON_PLACEMENT[opts.renderer.icons.bookmarks_placement] or ICON_PLACEMENT.none,
19-
})
20-
---@cast o DecoratorBookmarks
21+
}
22+
o = self:new(o) --[[@as DecoratorBookmarks]]
2123

2224
if opts.renderer.icons.show.bookmarks then
2325
o.icon = {

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

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

66
---@class (exact) DecoratorCopied: Decorator
77
---@field enabled boolean
8-
---@field icon HighlightedString|nil
8+
---@field icon HighlightedString?
99
local DecoratorCopied = Decorator:new()
1010

11+
---Static factory method
1112
---@param opts table
1213
---@param explorer Explorer
1314
---@return DecoratorCopied
14-
function DecoratorCopied:new(opts, explorer)
15-
local o = Decorator.new(self, {
15+
function DecoratorCopied:create(opts, explorer)
16+
---@type DecoratorCopied
17+
local o = {
1618
explorer = explorer,
1719
enabled = true,
1820
hl_pos = HL_POSITION[opts.renderer.highlight_clipboard] or HL_POSITION.none,
1921
icon_placement = ICON_PLACEMENT.none,
20-
})
21-
---@cast o DecoratorCopied
22+
}
23+
o = self:new(o) --[[@as DecoratorCopied]]
2224

2325
return o
2426
end

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

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

66
---@class (exact) DecoratorCut: Decorator
7-
---@field enabled boolean
8-
---@field icon HighlightedString|nil
97
local DecoratorCut = Decorator:new()
108

9+
---Static factory method
1110
---@param opts table
1211
---@param explorer Explorer
1312
---@return DecoratorCut
14-
function DecoratorCut:new(opts, explorer)
15-
local o = Decorator.new(self, {
13+
function DecoratorCut:create(opts, explorer)
14+
---@type DecoratorCut
15+
local o = {
1616
explorer = explorer,
1717
enabled = true,
1818
hl_pos = HL_POSITION[opts.renderer.highlight_clipboard] or HL_POSITION.none,
1919
icon_placement = ICON_PLACEMENT.none,
20-
})
21-
---@cast o DecoratorCut
20+
}
21+
o = self:new(o) --[[@as DecoratorCut]]
2222

2323
return o
2424
end

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,22 @@ local ICON_KEYS = {
3333
}
3434

3535
---@class (exact) DecoratorDiagnostics: Decorator
36-
---@field icons HighlightedString[]
36+
---@field icons HighlightedString[]?
3737
local DecoratorDiagnostics = Decorator:new()
3838

39+
---Static factory method
3940
---@param opts table
4041
---@param explorer Explorer
4142
---@return DecoratorDiagnostics
42-
function DecoratorDiagnostics:new(opts, explorer)
43-
local o = Decorator.new(self, {
43+
function DecoratorDiagnostics:create(opts, explorer)
44+
---@type DecoratorDiagnostics
45+
local o = {
4446
explorer = explorer,
4547
enabled = opts.diagnostics.enable,
4648
hl_pos = HL_POSITION[opts.renderer.highlight_diagnostics] or HL_POSITION.none,
4749
icon_placement = ICON_PLACEMENT[opts.renderer.icons.diagnostics_placement] or ICON_PLACEMENT.none,
48-
})
49-
---@cast o DecoratorDiagnostics
50+
}
51+
o = self:new(o) --[[@as DecoratorDiagnostics]]
5052

5153
if not o.enabled then
5254
return o

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,25 @@ local Decorator = require("nvim-tree.renderer.decorator")
99
---@field ord number decreasing priority
1010

1111
---@class (exact) DecoratorGit: Decorator
12-
---@field file_hl table<string, string> by porcelain status e.g. "AM"
13-
---@field folder_hl table<string, string> by porcelain status
14-
---@field icons_by_status HighlightedStringGit[] by human status
15-
---@field icons_by_xy table<string, HighlightedStringGit[]> by porcelain status
12+
---@field file_hl table<string, string>? by porcelain status e.g. "AM"
13+
---@field folder_hl table<string, string>? by porcelain status
14+
---@field icons_by_status HighlightedStringGit[]? by human status
15+
---@field icons_by_xy table<string, HighlightedStringGit[]>? by porcelain status
1616
local DecoratorGit = Decorator:new()
1717

18+
---Static factory method
1819
---@param opts table
1920
---@param explorer Explorer
2021
---@return DecoratorGit
21-
function DecoratorGit:new(opts, explorer)
22-
local o = Decorator.new(self, {
22+
function DecoratorGit:create(opts, explorer)
23+
---@type DecoratorGit
24+
local o = {
2325
explorer = explorer,
2426
enabled = opts.git.enable,
2527
hl_pos = HL_POSITION[opts.renderer.highlight_git] or HL_POSITION.none,
2628
icon_placement = ICON_PLACEMENT[opts.renderer.icons.git_placement] or ICON_PLACEMENT.none,
27-
})
28-
---@cast o DecoratorGit
29+
}
30+
o = self:new(o) --[[@as DecoratorGit]]
2931

3032
if not o.enabled then
3133
return o

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@ local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
33
local Decorator = require("nvim-tree.renderer.decorator")
44

55
---@class (exact) DecoratorHidden: Decorator
6-
---@field icon HighlightedString|nil
6+
---@field icon HighlightedString?
77
local DecoratorHidden = Decorator:new()
88

9+
---Static factory method
910
---@param opts table
1011
---@param explorer Explorer
1112
---@return DecoratorHidden
12-
function DecoratorHidden:new(opts, explorer)
13-
local o = Decorator.new(self, {
13+
function DecoratorHidden:create(opts, explorer)
14+
---@type DecoratorHidden
15+
local o = {
1416
explorer = explorer,
1517
enabled = true,
1618
hl_pos = HL_POSITION[opts.renderer.highlight_hidden] or HL_POSITION.none,
1719
icon_placement = ICON_PLACEMENT[opts.renderer.icons.hidden_placement] or ICON_PLACEMENT.none,
18-
})
19-
---@cast o DecoratorHidden
20+
}
21+
o = self:new(o) --[[@as DecoratorHidden]]
2022

2123
if opts.renderer.icons.show.hidden then
2224
o.icon = {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
local HL_POSITION = require("nvim-tree.enum").HL_POSITION
22
local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
33

4+
---Abstract Decorator
5+
---Uses the factory pattern to instantiate child instances.
46
---@class (exact) Decorator
57
---@field private __index? table
68
---@field protected explorer Explorer

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@ local Decorator = require("nvim-tree.renderer.decorator")
99
---@field icon HighlightedString|nil
1010
local DecoratorModified = Decorator:new()
1111

12+
---Static factory method
1213
---@param opts table
1314
---@param explorer Explorer
1415
---@return DecoratorModified
15-
function DecoratorModified:new(opts, explorer)
16-
local o = Decorator.new(self, {
16+
function DecoratorModified:create(opts, explorer)
17+
---@type DecoratorModified
18+
local o = {
1719
explorer = explorer,
1820
enabled = opts.modified.enable,
1921
hl_pos = HL_POSITION[opts.renderer.highlight_modified] or HL_POSITION.none,
2022
icon_placement = ICON_PLACEMENT[opts.renderer.icons.modified_placement] or ICON_PLACEMENT.none,
21-
})
22-
---@cast o DecoratorModified
23+
}
24+
o = self:new(o) --[[@as DecoratorModified]]
2325

2426
if not o.enabled then
2527
return o

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,19 @@ local Decorator = require("nvim-tree.renderer.decorator")
1010
---@field icon HighlightedString|nil
1111
local DecoratorOpened = Decorator:new()
1212

13+
---Static factory method
1314
---@param opts table
1415
---@param explorer Explorer
1516
---@return DecoratorOpened
16-
function DecoratorOpened:new(opts, explorer)
17-
local o = Decorator.new(self, {
17+
function DecoratorOpened:create(opts, explorer)
18+
---@type DecoratorOpened
19+
local o = {
1820
explorer = explorer,
1921
enabled = true,
2022
hl_pos = HL_POSITION[opts.renderer.highlight_opened_files] or HL_POSITION.none,
2123
icon_placement = ICON_PLACEMENT.none,
22-
})
23-
---@cast o DecoratorOpened
24+
}
25+
o = self:new(o) --[[@as DecoratorOpened]]
2426

2527
return o
2628
end

0 commit comments

Comments
 (0)