Skip to content

Commit e69dae1

Browse files
committed
feat(#2415): create Decorator class for modified and bookmarks
1 parent af65274 commit e69dae1

File tree

10 files changed

+207
-152
lines changed

10 files changed

+207
-152
lines changed

lua/nvim-tree/enum.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ M.HL_POSITION = {
1212
---Setup options for "*_placement"
1313
---@enum ICON_PLACEMENT
1414
M.ICON_PLACEMENT = {
15-
signcolumn = 0,
16-
before = 1,
17-
after = 2,
15+
none = 0,
16+
signcolumn = 1,
17+
before = 2,
18+
after = 3,
1819
}
1920

2021
return M

lua/nvim-tree/marks/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function M.clear_marks()
3737
end
3838

3939
function M.get_mark(node)
40-
return NvimTreeMarks[node.absolute_path]
40+
return node and NvimTreeMarks[node.absolute_path]
4141
end
4242

4343
function M.get_marks()

lua/nvim-tree/modified.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ end
2626
---@param node table
2727
---@return boolean
2828
function M.is_modified(node)
29-
return M.config.enable
29+
return node
30+
and M.config.enable
3031
and M._record[node.absolute_path]
3132
and (not node.nodes or M.config.show_on_dirs)
3233
and (not node.open or M.config.show_on_open_dirs)

lua/nvim-tree/renderer/builder.lua

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@ local core = require "nvim-tree.core"
44
local git = require "nvim-tree.renderer.components.git"
55
local pad = require "nvim-tree.renderer.components.padding"
66
local icons = require "nvim-tree.renderer.components.icons"
7-
local modified = require "nvim-tree.renderer.components.modified"
87
local diagnostics = require "nvim-tree.renderer.components.diagnostics"
9-
local bookmarks = require "nvim-tree.renderer.components.bookmarks"
108

119
local HL_POSITION = require("nvim-tree.enum").HL_POSITION
10+
local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
1211

12+
--- @class Builder
13+
--- @field decorators Decorator[]
1314
local Builder = {}
1415
Builder.__index = Builder
1516

1617
local DEFAULT_ROOT_FOLDER_LABEL = ":~:s?$?/..?"
1718

18-
function Builder.new(root_cwd)
19+
function Builder.new(root_cwd, decorators)
1920
return setmetatable({
2021
index = 0,
2122
depth = 0,
@@ -24,6 +25,7 @@ function Builder.new(root_cwd)
2425
markers = {},
2526
signs = {},
2627
root_cwd = root_cwd,
28+
decorators = decorators,
2729
}, Builder)
2830
end
2931

@@ -79,22 +81,6 @@ function Builder:configure_diagnostics_icon_placement(where)
7981
return self
8082
end
8183

82-
function Builder:configure_bookmark_icon_placement(where)
83-
if where ~= "after" and where ~= "before" and where ~= "signcolumn" then
84-
where = "before" -- default before
85-
end
86-
self.bookmarks_placement = where
87-
return self
88-
end
89-
90-
function Builder:configure_modified_placement(where)
91-
if where ~= "after" and where ~= "before" and where ~= "signcolumn" then
92-
where = "after" -- default after
93-
end
94-
self.modified_placement = where
95-
return self
96-
end
97-
9884
function Builder:configure_symlink_destination(show)
9985
self.symlink_destination = show
10086
return self
@@ -247,8 +233,8 @@ end
247233
---@param node table
248234
---@return HighlightedString|nil icon
249235
function Builder:_get_modified_icon(node)
250-
local modified_icon = modified.get_icon(node)
251-
if modified_icon and self.modified_placement == "signcolumn" then
236+
local modified_icon = self.decorators.modified:get_icon(node)
237+
if modified_icon and self.decorators.modified.icon_placement == ICON_PLACEMENT.signcolumn then
252238
table.insert(self.signs, {
253239
sign = modified_icon.hl[1],
254240
lnum = self.index + 1,
@@ -262,8 +248,8 @@ end
262248
---@param node table
263249
---@return HighlightedString[]|nil icon
264250
function Builder:_get_bookmark_icon(node)
265-
local bookmark_icon = bookmarks.get_icon(node)
266-
if bookmark_icon and self.bookmarks_placement == "signcolumn" then
251+
local bookmark_icon = self.decorators.bookmark:get_icon(node)
252+
if bookmark_icon and self.decorators.bookmark.icon_placement == ICON_PLACEMENT.signcolumn then
267253
table.insert(self.signs, {
268254
sign = bookmark_icon.hl[1],
269255
lnum = self.index + 1,
@@ -309,6 +295,23 @@ function Builder:_append_highlight(node, get_hl, icon_hl, name_hl)
309295
end
310296
end
311297

298+
---Append optional highlighting to icon or name.
299+
---@param node table
300+
---@param decorator Decorator
301+
---@param icon_hl string[] icons to append to
302+
---@param name_hl string[] names to append to
303+
function Builder:_append_dec_highlight(node, decorator, icon_hl, name_hl)
304+
local pos, hl = decorator:get_highlight(node)
305+
if pos ~= HL_POSITION.none and hl then
306+
if pos == HL_POSITION.all or pos == HL_POSITION.icon then
307+
table.insert(icon_hl, hl)
308+
end
309+
if pos == HL_POSITION.all or pos == HL_POSITION.name then
310+
table.insert(name_hl, hl)
311+
end
312+
end
313+
end
314+
312315
---@param indent_markers HighlightedString[]
313316
---@param arrows HighlightedString[]|nil
314317
---@param icon HighlightedString
@@ -341,13 +344,13 @@ function Builder:_format_line(indent_markers, arrows, icon, name, git_icons, dia
341344
if git_icons and self.git_placement == "before" then
342345
add_to_end(line, git_icons)
343346
end
344-
if modified_icon and self.modified_placement == "before" then
347+
if modified_icon and self.decorators.modified.icon_placement == ICON_PLACEMENT.before then
345348
add_to_end(line, { modified_icon })
346349
end
347350
if diagnostics_icon and self.diagnostics_placement == "before" then
348351
add_to_end(line, { diagnostics_icon })
349352
end
350-
if bookmark_icon and self.bookmarks_placement == "before" then
353+
if bookmark_icon and self.decorators.bookmark.icon_placement == ICON_PLACEMENT.before then
351354
add_to_end(line, { bookmark_icon })
352355
end
353356

@@ -356,13 +359,13 @@ function Builder:_format_line(indent_markers, arrows, icon, name, git_icons, dia
356359
if git_icons and self.git_placement == "after" then
357360
add_to_end(line, git_icons)
358361
end
359-
if modified_icon and self.modified_placement == "after" then
362+
if modified_icon and self.decorators.modified.icon_placement == ICON_PLACEMENT.after then
360363
add_to_end(line, { modified_icon })
361364
end
362365
if diagnostics_icon and self.diagnostics_placement == "after" then
363366
add_to_end(line, { diagnostics_icon })
364367
end
365-
if bookmark_icon and self.bookmarks_placement == "after" then
368+
if bookmark_icon and self.decorators.bookmark.icon_placement == ICON_PLACEMENT.after then
366369
add_to_end(line, { bookmark_icon })
367370
end
368371

@@ -405,9 +408,8 @@ function Builder:_build_line(node, idx, num_children, unloaded_bufnr)
405408

406409
-- extra highighting
407410
self:_append_highlight(node, git.get_highlight, icon.hl, name.hl)
408-
-- TODO opened
409-
self:_append_highlight(node, modified.get_highlight, icon.hl, name.hl)
410-
self:_append_highlight(node, bookmarks.get_highlight, icon.hl, name.hl)
411+
self:_append_dec_highlight(node, self.decorators.modified, icon.hl, name.hl)
412+
self:_append_dec_highlight(node, self.decorators.bookmark, icon.hl, name.hl)
411413
self:_append_highlight(node, diagnostics.get_highlight, icon.hl, name.hl)
412414
self:_append_highlight(node, copy_paste.get_highlight, icon.hl, name.hl)
413415

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

Lines changed: 0 additions & 55 deletions
This file was deleted.

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

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
local marks = require "nvim-tree.marks"
2+
3+
local HL_POSITION = require("nvim-tree.enum").HL_POSITION
4+
local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
5+
6+
local Decorator = require "nvim-tree.renderer.decorator"
7+
8+
--- @class DecoratorBookmark: Decorator
9+
--- @field icon HighlightedString
10+
local DecoratorBookmark = Decorator:new()
11+
12+
--- @param opts table
13+
--- @return DecoratorBookmark
14+
function DecoratorBookmark:new(opts)
15+
local o = Decorator.new(self, {
16+
hl_pos = HL_POSITION[opts.renderer.highlight_bookmarks] or HL_POSITION.none,
17+
icon_placement = ICON_PLACEMENT[opts.renderer.icons.bookmarks_placement] or ICON_PLACEMENT.none,
18+
})
19+
---@cast o DecoratorBookmark
20+
21+
if opts.renderer.icons.show.bookmarks then
22+
o.icon = {
23+
str = opts.renderer.icons.glyphs.bookmark,
24+
hl = { "NvimTreeBookmark" },
25+
}
26+
o:define_sign(o.icon)
27+
end
28+
29+
return o
30+
end
31+
32+
--- Bookmark icon: renderer.icons.show.bookmarks and node is marked
33+
function DecoratorBookmark:get_icon(node)
34+
if marks.get_mark(node) then
35+
return self.icon
36+
end
37+
end
38+
39+
--- Bookmark highlight: renderer.highlight_bookmarks and node is marked
40+
function DecoratorBookmark:get_highlight(node)
41+
if self.hl_pos == HL_POSITION.none then
42+
return HL_POSITION.none, nil
43+
end
44+
45+
local mark = marks.get_mark(node)
46+
if mark then
47+
return self.hl_pos, "NvimTreeBookmarkHL"
48+
else
49+
return HL_POSITION.none, nil
50+
end
51+
end
52+
53+
return DecoratorBookmark
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--- @class Decorator
2+
--- @field hl_pos HL_POSITION
3+
--- @field icon_placement ICON_PLACEMENT
4+
local Decorator = {}
5+
6+
--- @param o Decorator|nil
7+
--- @return Decorator
8+
function Decorator:new(o)
9+
o = o or {}
10+
setmetatable(o, self)
11+
self.__index = self
12+
13+
return o
14+
end
15+
16+
---@diagnostic disable: unused-local
17+
18+
--- Node icon
19+
--- @param node table
20+
--- @return HighlightedString|nil modified icon
21+
function Decorator:get_icon(node) end
22+
23+
--- Node highlight
24+
--- @param node table
25+
--- @return HL_POSITION|nil position
26+
--- @return string|nil group
27+
function Decorator:get_highlight(node) end
28+
29+
---@diagnostic enable: unused-local
30+
31+
--- Define a sign
32+
--- @param icon HighlightedString|nil
33+
function Decorator:define_sign(icon)
34+
if icon and #icon.hl > 0 then
35+
vim.fn.sign_define(icon.hl[1], {
36+
text = icon.str,
37+
texthl = icon.hl[1],
38+
})
39+
end
40+
end
41+
42+
return Decorator

0 commit comments

Comments
 (0)