Skip to content

Commit 9af0dc4

Browse files
committed
replace enums with named maps
1 parent 5d7e543 commit 9af0dc4

File tree

11 files changed

+45
-92
lines changed

11 files changed

+45
-92
lines changed

lua/nvim-tree/enum.lua

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,5 @@
11
local M = {}
22

3-
---Setup options for "highlight_*"
4-
---@enum HL_POSITION
5-
M.HL_POSITION = {
6-
none = 0,
7-
icon = 1,
8-
name = 2,
9-
all = 4,
10-
}
11-
12-
---Setup options for "*_placement"
13-
---@enum ICON_PLACEMENT
14-
M.ICON_PLACEMENT = {
15-
none = 0,
16-
signcolumn = 1,
17-
before = 2,
18-
after = 3,
19-
right_align = 4,
20-
}
21-
223
---Reason for filter in filter.lua
234
---@enum FILTER_REASON
245
M.FILTER_REASON = {

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
local HL_POSITION = require("nvim-tree.enum").HL_POSITION
2-
local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
3-
41
local Decorator = require("nvim-tree.renderer.decorator")
52

63
---@class (exact) DecoratorBookmarks: Decorator
@@ -16,8 +13,8 @@ function DecoratorBookmarks:new(args)
1613
Decorator.new(self, {
1714
explorer = args.explorer,
1815
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,
16+
hl_pos = args.explorer.opts.renderer.highlight_bookmarks or "none",
17+
icon_placement = args.explorer.opts.renderer.icons.bookmarks_placement or "none",
2118
})
2219

2320
if self.explorer.opts.renderer.icons.show.bookmarks then
@@ -42,7 +39,7 @@ end
4239
---@param node Node
4340
---@return string|nil group
4441
function DecoratorBookmarks:calculate_highlight(node)
45-
if self.hl_pos ~= HL_POSITION.none and self.explorer.marks:get(node) then
42+
if self.range ~= "none" and self.explorer.marks:get(node) then
4643
return "NvimTreeBookmarkHL"
4744
end
4845
end

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
local HL_POSITION = require("nvim-tree.enum").HL_POSITION
2-
local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
3-
41
local Decorator = require("nvim-tree.renderer.decorator")
52

63
---@class (exact) DecoratorCopied: Decorator
@@ -15,16 +12,16 @@ function DecoratorCopied:new(args)
1512
Decorator.new(self, {
1613
explorer = args.explorer,
1714
enabled = true,
18-
hl_pos = HL_POSITION[args.explorer.opts.renderer.highlight_clipboard] or HL_POSITION.none,
19-
icon_placement = ICON_PLACEMENT.none,
15+
hl_pos = args.explorer.opts.renderer.highlight_clipboard or "none",
16+
icon_placement = "none",
2017
})
2118
end
2219

2320
---Copied highlight: renderer.highlight_clipboard and node is copied
2421
---@param node Node
2522
---@return string|nil group
2623
function DecoratorCopied:calculate_highlight(node)
27-
if self.hl_pos ~= HL_POSITION.none and self.explorer.clipboard:is_copied(node) then
24+
if self.range ~= "none" and self.explorer.clipboard:is_copied(node) then
2825
return "NvimTreeCopiedHL"
2926
end
3027
end

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
local HL_POSITION = require("nvim-tree.enum").HL_POSITION
2-
local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
3-
41
local Decorator = require("nvim-tree.renderer.decorator")
52

63
---@class (exact) DecoratorCut: Decorator
@@ -15,16 +12,16 @@ function DecoratorCut:new(args)
1512
Decorator.new(self, {
1613
explorer = args.explorer,
1714
enabled = true,
18-
hl_pos = HL_POSITION[args.explorer.opts.renderer.highlight_clipboard] or HL_POSITION.none,
19-
icon_placement = ICON_PLACEMENT.none,
15+
hl_pos = args.explorer.opts.renderer.highlight_clipboard or "none",
16+
icon_placement = "none",
2017
})
2118
end
2219

2320
---Cut highlight: renderer.highlight_clipboard and node is cut
2421
---@param node Node
2522
---@return string|nil group
2623
function DecoratorCut:calculate_highlight(node)
27-
if self.hl_pos ~= HL_POSITION.none and self.explorer.clipboard:is_cut(node) then
24+
if self.range ~= "none" and self.explorer.clipboard:is_cut(node) then
2825
return "NvimTreeCutHL"
2926
end
3027
end

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
local diagnostics = require("nvim-tree.diagnostics")
22

3-
local HL_POSITION = require("nvim-tree.enum").HL_POSITION
4-
local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
5-
63
local Decorator = require("nvim-tree.renderer.decorator")
74
local DirectoryNode = require("nvim-tree.node.directory")
85

@@ -46,8 +43,8 @@ function DecoratorDiagnostics:new(args)
4643
Decorator.new(self, {
4744
explorer = args.explorer,
4845
enabled = true,
49-
hl_pos = HL_POSITION[args.explorer.opts.renderer.highlight_diagnostics] or HL_POSITION.none,
50-
icon_placement = ICON_PLACEMENT[args.explorer.opts.renderer.icons.diagnostics_placement] or ICON_PLACEMENT.none,
46+
hl_pos = args.explorer.opts.renderer.highlight_diagnostics or "none",
47+
icon_placement = args.explorer.opts.renderer.icons.diagnostics_placement or "none",
5148
})
5249

5350
if not self.enabled then
@@ -84,7 +81,7 @@ end
8481
---@param node Node
8582
---@return string|nil group
8683
function DecoratorDiagnostics:calculate_highlight(node)
87-
if not node or not self.enabled or self.hl_pos == HL_POSITION.none then
84+
if not node or not self.enabled or self.range == "none" then
8885
return nil
8986
end
9087

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
local notify = require("nvim-tree.notify")
22

3-
local HL_POSITION = require("nvim-tree.enum").HL_POSITION
4-
local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
5-
63
local Decorator = require("nvim-tree.renderer.decorator")
74
local DirectoryNode = require("nvim-tree.node.directory")
85

@@ -31,15 +28,15 @@ function DecoratorGit:new(args)
3128
Decorator.new(self, {
3229
explorer = args.explorer,
3330
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,
31+
hl_pos = args.explorer.opts.renderer.highlight_git or "none",
32+
icon_placement = args.explorer.opts.renderer.icons.git_placement or "none",
3633
})
3734

3835
if not self.enabled then
3936
return
4037
end
4138

42-
if self.hl_pos ~= HL_POSITION.none then
39+
if self.range ~= "none" then
4340
self:build_file_folder_hl_by_xy()
4441
end
4542

@@ -162,7 +159,7 @@ function DecoratorGit:calculate_icons(node)
162159
for _, s in pairs(git_xy) do
163160
local icons = self.icons_by_xy[s]
164161
if not icons then
165-
if self.hl_pos == HL_POSITION.none then
162+
if self.range == "none" then
166163
notify.warn(string.format("Unrecognized git state '%s'", git_xy))
167164
end
168165
return nil
@@ -194,7 +191,7 @@ end
194191
---@param node Node
195192
---@return string|nil name
196193
function DecoratorGit:sign_name(node)
197-
if self.icon_placement ~= ICON_PLACEMENT.signcolumn then
194+
if self.icon_placement ~= "signcolumn" then
198195
return
199196
end
200197

@@ -208,7 +205,7 @@ end
208205
---@param node Node
209206
---@return string|nil group
210207
function DecoratorGit:calculate_highlight(node)
211-
if not node or not self.enabled or self.hl_pos == HL_POSITION.none then
208+
if not node or not self.enabled or self.range == "none" then
212209
return nil
213210
end
214211

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
local HL_POSITION = require("nvim-tree.enum").HL_POSITION
2-
local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
3-
41
local Decorator = require("nvim-tree.renderer.decorator")
52
local DirectoryNode = require("nvim-tree.node.directory")
63

@@ -17,8 +14,8 @@ function DecoratorHidden:new(args)
1714
Decorator.new(self, {
1815
explorer = args.explorer,
1916
enabled = true,
20-
hl_pos = HL_POSITION[args.explorer.opts.renderer.highlight_hidden] or HL_POSITION.none,
21-
icon_placement = ICON_PLACEMENT[args.explorer.opts.renderer.icons.hidden_placement] or ICON_PLACEMENT.none,
17+
hl_pos = args.explorer.opts.renderer.highlight_hidden or "none",
18+
icon_placement = args.explorer.opts.renderer.icons.hidden_placement or "none",
2219
})
2320

2421
if self.explorer.opts.renderer.icons.show.hidden then
@@ -43,7 +40,7 @@ end
4340
---@param node Node
4441
---@return string|nil group
4542
function DecoratorHidden:calculate_highlight(node)
46-
if not self.enabled or self.hl_pos == HL_POSITION.none or not node:is_dotfile() then
43+
if not self.enabled or self.range == "none" or not node:is_dotfile() then
4744
return nil
4845
end
4946

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

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

3-
local HL_POSITION = require("nvim-tree.enum").HL_POSITION
4-
local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
3+
---@alias DecoratorRange "none" | "icon" | "name" | "all"
4+
---@alias DecoratorIconPlacement "none" | "before" | "after" | "signcolumn" | "right_align"
55

66
---Abstract Decorator
77
---Uses the factory pattern to instantiate child instances.
88
---@class (exact) Decorator: Class
99
---@field protected explorer Explorer
1010
---@field protected enabled boolean
11-
---@field protected hl_pos HL_POSITION
12-
---@field protected icon_placement ICON_PLACEMENT
11+
---@field protected range DecoratorRange
12+
---@field protected icon_placement DecoratorIconPlacement
1313
local Decorator = Class:extend()
1414

1515
---@class (exact) DecoratorArgs
1616
---@field explorer Explorer
1717

1818
---@class (exact) AbstractDecoratorArgs: DecoratorArgs
1919
---@field enabled boolean
20-
---@field hl_pos HL_POSITION
21-
---@field icon_placement ICON_PLACEMENT
20+
---@field hl_pos DecoratorRange
21+
---@field icon_placement DecoratorIconPlacement
2222

2323
---@protected
2424
---@param args AbstractDecoratorArgs
2525
function Decorator:new(args)
2626
self.explorer = args.explorer
2727
self.enabled = args.enabled
28-
self.hl_pos = args.hl_pos
28+
self.range = args.hl_pos
2929
self.icon_placement = args.icon_placement
3030
end
3131

@@ -36,13 +36,13 @@ end
3636
function Decorator:groups_icon_name(node)
3737
local icon_hl, name_hl
3838

39-
if self.enabled and self.hl_pos ~= HL_POSITION.none then
39+
if self.enabled and self.range ~= "none" then
4040
local hl = self:calculate_highlight(node)
4141

42-
if self.hl_pos == HL_POSITION.all or self.hl_pos == HL_POSITION.icon then
42+
if self.range == "all" or self.range == "icon" then
4343
icon_hl = hl
4444
end
45-
if self.hl_pos == HL_POSITION.all or self.hl_pos == HL_POSITION.name then
45+
if self.range == "all" or self.range == "name" then
4646
name_hl = hl
4747
end
4848
end
@@ -54,7 +54,7 @@ end
5454
---@param node Node
5555
---@return string|nil name
5656
function Decorator:sign_name(node)
57-
if not self.enabled or self.icon_placement ~= ICON_PLACEMENT.signcolumn then
57+
if not self.enabled or self.icon_placement ~= "signcolumn" then
5858
return
5959
end
6060

@@ -64,33 +64,33 @@ function Decorator:sign_name(node)
6464
end
6565
end
6666

67-
---Icons when ICON_PLACEMENT.before
67+
---Icons when "before"
6868
---@param node Node
6969
---@return HighlightedString[]|nil icons
7070
function Decorator:icons_before(node)
71-
if not self.enabled or self.icon_placement ~= ICON_PLACEMENT.before then
71+
if not self.enabled or self.icon_placement ~= "before" then
7272
return
7373
end
7474

7575
return self:calculate_icons(node)
7676
end
7777

78-
---Icons when ICON_PLACEMENT.after
78+
---Icons when "after"
7979
---@param node Node
8080
---@return HighlightedString[]|nil icons
8181
function Decorator:icons_after(node)
82-
if not self.enabled or self.icon_placement ~= ICON_PLACEMENT.after then
82+
if not self.enabled or self.icon_placement ~= "after" then
8383
return
8484
end
8585

8686
return self:calculate_icons(node)
8787
end
8888

89-
---Icons when ICON_PLACEMENT.right_align
89+
---Icons when "right_align"
9090
---@param node Node
9191
---@return HighlightedString[]|nil icons
9292
function Decorator:icons_right_align(node)
93-
if not self.enabled or self.icon_placement ~= ICON_PLACEMENT.right_align then
93+
if not self.enabled or self.icon_placement ~= "right_align" then
9494
return
9595
end
9696

@@ -126,7 +126,7 @@ function Decorator:define_sign(icon)
126126

127127
-- don't use sign if not defined
128128
if #icon.str < 1 then
129-
self.icon_placement = ICON_PLACEMENT.none
129+
self.icon_placement = "none"
130130
return
131131
end
132132

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
local buffers = require("nvim-tree.buffers")
22

3-
local HL_POSITION = require("nvim-tree.enum").HL_POSITION
4-
local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
5-
63
local Decorator = require("nvim-tree.renderer.decorator")
74
local DirectoryNode = require("nvim-tree.node.directory")
85

@@ -19,8 +16,8 @@ function DecoratorModified:new(args)
1916
Decorator.new(self, {
2017
explorer = args.explorer,
2118
enabled = true,
22-
hl_pos = HL_POSITION[args.explorer.opts.renderer.highlight_modified] or HL_POSITION.none,
23-
icon_placement = ICON_PLACEMENT[args.explorer.opts.renderer.icons.modified_placement] or ICON_PLACEMENT.none,
19+
hl_pos = args.explorer.opts.renderer.highlight_modified or "none",
20+
icon_placement = args.explorer.opts.renderer.icons.modified_placement or "none",
2421
})
2522

2623
if not self.enabled then
@@ -49,7 +46,7 @@ end
4946
---@param node Node
5047
---@return string|nil group
5148
function DecoratorModified:calculate_highlight(node)
52-
if not self.enabled or self.hl_pos == HL_POSITION.none or not buffers.is_modified(node) then
49+
if not self.enabled or self.range == "none" or not buffers.is_modified(node) then
5350
return nil
5451
end
5552

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
local buffers = require("nvim-tree.buffers")
22

3-
local HL_POSITION = require("nvim-tree.enum").HL_POSITION
4-
local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
5-
63
local Decorator = require("nvim-tree.renderer.decorator")
74

85
---@class (exact) DecoratorOpened: Decorator
@@ -18,16 +15,16 @@ function DecoratorOpened:new(args)
1815
Decorator.new(self, {
1916
explorer = args.explorer,
2017
enabled = true,
21-
hl_pos = HL_POSITION[args.explorer.opts.renderer.highlight_opened_files] or HL_POSITION.none,
22-
icon_placement = ICON_PLACEMENT.none,
18+
hl_pos = args.explorer.opts.renderer.highlight_opened_files or "none",
19+
icon_placement = "none",
2320
})
2421
end
2522

2623
---Opened highlight: renderer.highlight_opened_files and node has an open buffer
2724
---@param node Node
2825
---@return string|nil group
2926
function DecoratorOpened:calculate_highlight(node)
30-
if self.hl_pos ~= HL_POSITION.none and buffers.is_opened(node) then
27+
if self.range ~= "none" and buffers.is_opened(node) then
3128
return "NvimTreeOpenedHL"
3229
end
3330
end

0 commit comments

Comments
 (0)