Skip to content

Commit 800f94d

Browse files
committed
feat(#2415): create Decorator class for modified and bookmarks
1 parent 98cb7e6 commit 800f94d

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
@@ -45,7 +45,7 @@ end
4545
---@param node Node|MinimalNode
4646
---@return table|nil
4747
function M.get_mark(node)
48-
return NvimTreeMarks[node.absolute_path]
48+
return node and NvimTreeMarks[node.absolute_path]
4949
end
5050

5151
---@return table

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 Node
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
@@ -5,18 +5,19 @@ local notify = require "nvim-tree.notify"
55
local git = require "nvim-tree.renderer.components.git"
66
local pad = require "nvim-tree.renderer.components.padding"
77
local icons = require "nvim-tree.renderer.components.icons"
8-
local modified = require "nvim-tree.renderer.components.modified"
98
local diagnostics = require "nvim-tree.renderer.components.diagnostics"
10-
local bookmarks = require "nvim-tree.renderer.components.bookmarks"
119

1210
local HL_POSITION = require("nvim-tree.enum").HL_POSITION
11+
local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
1312

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

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

19-
function Builder.new(root_cwd)
20+
function Builder.new(root_cwd, decorators)
2021
return setmetatable({
2122
index = 0,
2223
depth = 0,
@@ -25,6 +26,7 @@ function Builder.new(root_cwd)
2526
markers = {},
2627
signs = {},
2728
root_cwd = root_cwd,
29+
decorators = decorators,
2830
}, Builder)
2931
end
3032

@@ -80,22 +82,6 @@ function Builder:configure_diagnostics_icon_placement(where)
8082
return self
8183
end
8284

83-
function Builder:configure_bookmark_icon_placement(where)
84-
if where ~= "after" and where ~= "before" and where ~= "signcolumn" then
85-
where = "before" -- default before
86-
end
87-
self.bookmarks_placement = where
88-
return self
89-
end
90-
91-
function Builder:configure_modified_placement(where)
92-
if where ~= "after" and where ~= "before" and where ~= "signcolumn" then
93-
where = "after" -- default after
94-
end
95-
self.modified_placement = where
96-
return self
97-
end
98-
9985
function Builder:configure_symlink_destination(show)
10086
self.symlink_destination = show
10187
return self
@@ -265,8 +251,8 @@ end
265251
---@param node table
266252
---@return HighlightedString|nil icon
267253
function Builder:_get_modified_icon(node)
268-
local modified_icon = modified.get_icon(node)
269-
if modified_icon and self.modified_placement == "signcolumn" then
254+
local modified_icon = self.decorators.modified:get_icon(node)
255+
if modified_icon and self.decorators.modified.icon_placement == ICON_PLACEMENT.signcolumn then
270256
table.insert(self.signs, {
271257
sign = modified_icon.hl[1],
272258
lnum = self.index + 1,
@@ -280,8 +266,8 @@ end
280266
---@param node table
281267
---@return HighlightedString[]|nil icon
282268
function Builder:_get_bookmark_icon(node)
283-
local bookmark_icon = bookmarks.get_icon(node)
284-
if bookmark_icon and self.bookmarks_placement == "signcolumn" then
269+
local bookmark_icon = self.decorators.bookmark:get_icon(node)
270+
if bookmark_icon and self.decorators.bookmark.icon_placement == ICON_PLACEMENT.signcolumn then
285271
table.insert(self.signs, {
286272
sign = bookmark_icon.hl[1],
287273
lnum = self.index + 1,
@@ -327,6 +313,23 @@ function Builder:_append_highlight(node, get_hl, icon_hl, name_hl)
327313
end
328314
end
329315

316+
---Append optional highlighting to icon or name.
317+
---@param node table
318+
---@param decorator Decorator
319+
---@param icon_hl string[] icons to append to
320+
---@param name_hl string[] names to append to
321+
function Builder:_append_dec_highlight(node, decorator, icon_hl, name_hl)
322+
local pos, hl = decorator:get_highlight(node)
323+
if pos ~= HL_POSITION.none and hl then
324+
if pos == HL_POSITION.all or pos == HL_POSITION.icon then
325+
table.insert(icon_hl, hl)
326+
end
327+
if pos == HL_POSITION.all or pos == HL_POSITION.name then
328+
table.insert(name_hl, hl)
329+
end
330+
end
331+
end
332+
330333
---@param indent_markers HighlightedString[]
331334
---@param arrows HighlightedString[]|nil
332335
---@param icon HighlightedString
@@ -359,13 +362,13 @@ function Builder:_format_line(indent_markers, arrows, icon, name, git_icons, dia
359362
if git_icons and self.git_placement == "before" then
360363
add_to_end(line, git_icons)
361364
end
362-
if modified_icon and self.modified_placement == "before" then
365+
if modified_icon and self.decorators.modified.icon_placement == ICON_PLACEMENT.before then
363366
add_to_end(line, { modified_icon })
364367
end
365368
if diagnostics_icon and self.diagnostics_placement == "before" then
366369
add_to_end(line, { diagnostics_icon })
367370
end
368-
if bookmark_icon and self.bookmarks_placement == "before" then
371+
if bookmark_icon and self.decorators.bookmark.icon_placement == ICON_PLACEMENT.before then
369372
add_to_end(line, { bookmark_icon })
370373
end
371374

@@ -374,13 +377,13 @@ function Builder:_format_line(indent_markers, arrows, icon, name, git_icons, dia
374377
if git_icons and self.git_placement == "after" then
375378
add_to_end(line, git_icons)
376379
end
377-
if modified_icon and self.modified_placement == "after" then
380+
if modified_icon and self.decorators.modified.icon_placement == ICON_PLACEMENT.after then
378381
add_to_end(line, { modified_icon })
379382
end
380383
if diagnostics_icon and self.diagnostics_placement == "after" then
381384
add_to_end(line, { diagnostics_icon })
382385
end
383-
if bookmark_icon and self.bookmarks_placement == "after" then
386+
if bookmark_icon and self.decorators.bookmark.icon_placement == ICON_PLACEMENT.after then
384387
add_to_end(line, { bookmark_icon })
385388
end
386389

@@ -423,9 +426,8 @@ function Builder:_build_line(node, idx, num_children, unloaded_bufnr)
423426

424427
-- extra highighting
425428
self:_append_highlight(node, git.get_highlight, icon.hl, name.hl)
426-
-- TODO opened
427-
self:_append_highlight(node, modified.get_highlight, icon.hl, name.hl)
428-
self:_append_highlight(node, bookmarks.get_highlight, icon.hl, name.hl)
429+
self:_append_dec_highlight(node, self.decorators.modified, icon.hl, name.hl)
430+
self:_append_dec_highlight(node, self.decorators.bookmark, icon.hl, name.hl)
429431
self:_append_highlight(node, diagnostics.get_highlight, icon.hl, name.hl)
430432
self:_append_highlight(node, copy_paste.get_highlight, icon.hl, name.hl)
431433

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)