Skip to content

Commit b49eb1d

Browse files
committed
feat(#2415): create DecoratorDiagnostics
1 parent 800f94d commit b49eb1d

File tree

6 files changed

+127
-55
lines changed

6 files changed

+127
-55
lines changed

lua/nvim-tree/renderer/builder.lua

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ 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 diagnostics = require "nvim-tree.renderer.components.diagnostics"
98

109
local HL_POSITION = require("nvim-tree.enum").HL_POSITION
1110
local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
@@ -74,14 +73,6 @@ function Builder:configure_git_icons_placement(where)
7473
return self
7574
end
7675

77-
function Builder:configure_diagnostics_icon_placement(where)
78-
if where ~= "after" and where ~= "before" and where ~= "signcolumn" then
79-
where = "before" -- default before
80-
end
81-
self.diagnostics_placement = where
82-
return self
83-
end
84-
8576
function Builder:configure_symlink_destination(show)
8677
self.symlink_destination = show
8778
return self
@@ -236,8 +227,8 @@ end
236227
---@param node table
237228
---@return HighlightedString[]|nil icon
238229
function Builder:_get_diagnostics_icon(node)
239-
local diagnostics_icon = diagnostics.get_icon(node)
240-
if diagnostics_icon and self.diagnostics_placement == "signcolumn" then
230+
local diagnostics_icon = self.decorators.diagnostics:get_icon(node)
231+
if diagnostics_icon and self.decorators.diagnostics.icon_placement == ICON_PLACEMENT.signcolumn then
241232
table.insert(self.signs, {
242233
sign = diagnostics_icon.hl[1],
243234
lnum = self.index + 1,
@@ -266,8 +257,8 @@ end
266257
---@param node table
267258
---@return HighlightedString[]|nil icon
268259
function Builder:_get_bookmark_icon(node)
269-
local bookmark_icon = self.decorators.bookmark:get_icon(node)
270-
if bookmark_icon and self.decorators.bookmark.icon_placement == ICON_PLACEMENT.signcolumn then
260+
local bookmark_icon = self.decorators.bookmarks:get_icon(node)
261+
if bookmark_icon and self.decorators.bookmarks.icon_placement == ICON_PLACEMENT.signcolumn then
271262
table.insert(self.signs, {
272263
sign = bookmark_icon.hl[1],
273264
lnum = self.index + 1,
@@ -319,12 +310,12 @@ end
319310
---@param icon_hl string[] icons to append to
320311
---@param name_hl string[] names to append to
321312
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
313+
local hl = decorator:get_highlight(node)
314+
if hl then
315+
if decorator.hl_pos == HL_POSITION.all or decorator.hl_pos == HL_POSITION.icon then
325316
table.insert(icon_hl, hl)
326317
end
327-
if pos == HL_POSITION.all or pos == HL_POSITION.name then
318+
if decorator.hl_pos == HL_POSITION.all or decorator.hl_pos == HL_POSITION.name then
328319
table.insert(name_hl, hl)
329320
end
330321
end
@@ -365,10 +356,10 @@ function Builder:_format_line(indent_markers, arrows, icon, name, git_icons, dia
365356
if modified_icon and self.decorators.modified.icon_placement == ICON_PLACEMENT.before then
366357
add_to_end(line, { modified_icon })
367358
end
368-
if diagnostics_icon and self.diagnostics_placement == "before" then
359+
if diagnostics_icon and self.decorators.diagnostics.icon_placement == ICON_PLACEMENT.before then
369360
add_to_end(line, { diagnostics_icon })
370361
end
371-
if bookmark_icon and self.decorators.bookmark.icon_placement == ICON_PLACEMENT.before then
362+
if bookmark_icon and self.decorators.bookmarks.icon_placement == ICON_PLACEMENT.before then
372363
add_to_end(line, { bookmark_icon })
373364
end
374365

@@ -380,10 +371,10 @@ function Builder:_format_line(indent_markers, arrows, icon, name, git_icons, dia
380371
if modified_icon and self.decorators.modified.icon_placement == ICON_PLACEMENT.after then
381372
add_to_end(line, { modified_icon })
382373
end
383-
if diagnostics_icon and self.diagnostics_placement == "after" then
374+
if diagnostics_icon and self.decorators.diagnostics.icon_placement == ICON_PLACEMENT.after then
384375
add_to_end(line, { diagnostics_icon })
385376
end
386-
if bookmark_icon and self.decorators.bookmark.icon_placement == ICON_PLACEMENT.after then
377+
if bookmark_icon and self.decorators.bookmarks.icon_placement == ICON_PLACEMENT.after then
387378
add_to_end(line, { bookmark_icon })
388379
end
389380

@@ -427,8 +418,8 @@ function Builder:_build_line(node, idx, num_children, unloaded_bufnr)
427418
-- extra highighting
428419
self:_append_highlight(node, git.get_highlight, icon.hl, name.hl)
429420
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)
431-
self:_append_highlight(node, diagnostics.get_highlight, icon.hl, name.hl)
421+
self:_append_dec_highlight(node, self.decorators.bookmarks, icon.hl, name.hl)
422+
self:_append_dec_highlight(node, self.decorators.diagnostics, icon.hl, name.hl)
432423
self:_append_highlight(node, copy_paste.get_highlight, icon.hl, name.hl)
433424

434425
local line = self:_format_line(indent_markers, arrows, icon, name, git_icons, diagnostics_icon, modified_icon, bookmark_icon)

lua/nvim-tree/renderer/decorator/bookmark.lua renamed to lua/nvim-tree/renderer/decorator/bookmarks.lua

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
55

66
local Decorator = require "nvim-tree.renderer.decorator"
77

8-
--- @class DecoratorBookmark: Decorator
8+
--- @class DecoratorBookmarks: Decorator
99
--- @field icon HighlightedString
10-
local DecoratorBookmark = Decorator:new()
10+
local DecoratorBookmarks = Decorator:new()
1111

1212
--- @param opts table
13-
--- @return DecoratorBookmark
14-
function DecoratorBookmark:new(opts)
13+
--- @return DecoratorBookmarks
14+
function DecoratorBookmarks:new(opts)
1515
local o = Decorator.new(self, {
1616
hl_pos = HL_POSITION[opts.renderer.highlight_bookmarks] or HL_POSITION.none,
1717
icon_placement = ICON_PLACEMENT[opts.renderer.icons.bookmarks_placement] or ICON_PLACEMENT.none,
1818
})
19-
---@cast o DecoratorBookmark
19+
---@cast o DecoratorBookmarks
2020

2121
if opts.renderer.icons.show.bookmarks then
2222
o.icon = {
@@ -29,25 +29,18 @@ function DecoratorBookmark:new(opts)
2929
return o
3030
end
3131

32-
--- Bookmark icon: renderer.icons.show.bookmarks and node is marked
33-
function DecoratorBookmark:get_icon(node)
32+
--- Bookmark icon: renderer.icons.show.bookmarks and node is marked
33+
function DecoratorBookmarks:get_icon(node)
3434
if marks.get_mark(node) then
3535
return self.icon
3636
end
3737
end
3838

3939
--- 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
40+
function DecoratorBookmarks:get_highlight(node)
41+
if self.hl_pos ~= HL_POSITION.none and marks.get_mark(node) then
42+
return "NvimTreeBookmarkHL"
5043
end
5144
end
5245

53-
return DecoratorBookmark
46+
return DecoratorBookmarks
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
local HL_POSITION = require("nvim-tree.enum").HL_POSITION
2+
local ICON_PLACEMENT = require("nvim-tree.enum").ICON_PLACEMENT
3+
4+
local Decorator = require "nvim-tree.renderer.decorator"
5+
6+
-- highlight groups by severity
7+
local HG_ICON = {
8+
[vim.diagnostic.severity.ERROR] = "NvimTreeDiagnosticErrorIcon",
9+
[vim.diagnostic.severity.WARN] = "NvimTreeDiagnosticWarningIcon",
10+
[vim.diagnostic.severity.INFO] = "NvimTreeDiagnosticInfoIcon",
11+
[vim.diagnostic.severity.HINT] = "NvimTreeDiagnosticHintIcon",
12+
}
13+
local HG_FILE = {
14+
[vim.diagnostic.severity.ERROR] = "NvimTreeDiagnosticErrorFileHL",
15+
[vim.diagnostic.severity.WARN] = "NvimTreeDiagnosticWarningFileHL",
16+
[vim.diagnostic.severity.INFO] = "NvimTreeDiagnosticInfoFileHL",
17+
[vim.diagnostic.severity.HINT] = "NvimTreeDiagnosticHintFileHL",
18+
}
19+
local HG_FOLDER = {
20+
[vim.diagnostic.severity.ERROR] = "NvimTreeDiagnosticErrorFolderHL",
21+
[vim.diagnostic.severity.WARN] = "NvimTreeDiagnosticWarningFolderHL",
22+
[vim.diagnostic.severity.INFO] = "NvimTreeDiagnosticInfoFolderHL",
23+
[vim.diagnostic.severity.HINT] = "NvimTreeDiagnosticHintFolderHL",
24+
}
25+
-- opts.diagnostics.icons.
26+
local ICON_KEYS = {
27+
["error"] = vim.diagnostic.severity.ERROR,
28+
["warning"] = vim.diagnostic.severity.WARN,
29+
["info"] = vim.diagnostic.severity.INFO,
30+
["hint"] = vim.diagnostic.severity.HINT,
31+
}
32+
33+
--- @class DecoratorDiagnostics: Decorator
34+
--- @field enabled boolean
35+
--- @field icons HighlightedString[]
36+
local DecoratorDiagnostics = Decorator:new()
37+
38+
--- @param opts table
39+
--- @return DecoratorDiagnostics
40+
function DecoratorDiagnostics:new(opts)
41+
local o = Decorator.new(self, {
42+
hl_pos = HL_POSITION[opts.renderer.highlight_diagnostics] or HL_POSITION.none,
43+
icon_placement = ICON_PLACEMENT[opts.renderer.icons.diagnostics_placement] or ICON_PLACEMENT.none,
44+
})
45+
---@cast o DecoratorDiagnostics
46+
47+
o.enabled = opts.diagnostics.enable
48+
if not o.enabled then
49+
return o
50+
end
51+
52+
if opts.renderer.icons.show.diagnostics then
53+
o.icons = {}
54+
for name, sev in pairs(ICON_KEYS) do
55+
o.icons[sev] = {
56+
str = opts.diagnostics.icons[name],
57+
hl = { HG_ICON[sev] },
58+
}
59+
o:define_sign(o.icons[sev])
60+
end
61+
end
62+
63+
return o
64+
end
65+
66+
--- Diagnostic icon: diagnostics.enable, renderer.icons.show.diagnostics and node has status
67+
function DecoratorDiagnostics:get_icon(node)
68+
if node and self.enabled and self.icons then
69+
return self.icons[node.diag_status]
70+
end
71+
end
72+
73+
--- Diagnostic highlight: diagnostics.enable, renderer.highlight_diagnostics and node has status
74+
function DecoratorDiagnostics:get_highlight(node)
75+
if not node or not self.enabled or self.hl_pos == HL_POSITION.none then
76+
return nil
77+
end
78+
79+
local group
80+
if node.nodes then
81+
group = HG_FOLDER[node.diag_status]
82+
else
83+
group = HG_FILE[node.diag_status]
84+
end
85+
86+
if group then
87+
return group
88+
else
89+
return nil
90+
end
91+
end
92+
93+
return DecoratorDiagnostics

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ end
2020
--- @return HighlightedString|nil modified icon
2121
function Decorator:get_icon(node) end
2222

23-
--- Node highlight
23+
--- Node highlight group
2424
--- @param node table
25-
--- @return HL_POSITION|nil position
2625
--- @return string|nil group
2726
function Decorator:get_highlight(node) end
2827

2928
---@diagnostic enable: unused-local
3029

3130
--- Define a sign
31+
--- @protected
3232
--- @param icon HighlightedString|nil
3333
function Decorator:define_sign(icon)
3434
if icon and #icon.hl > 0 then

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ end
4545
--- Modified highlight: modified.enable, renderer.highlight_modified and node is modified
4646
function DecoratorModified:get_highlight(node)
4747
if not self.enabled or self.hl_pos == HL_POSITION.none or not modified.is_modified(node) then
48-
return HL_POSITION.none, nil
48+
return nil
4949
end
5050

5151
if node.nodes then
52-
return self.hl_pos, "NvimTreeModifiedFolderHL"
52+
return "NvimTreeModifiedFolderHL"
5353
else
54-
return self.hl_pos, "NvimTreeModifiedFileHL"
54+
return "NvimTreeModifiedFileHL"
5555
end
5656
end
5757

lua/nvim-tree/renderer/init.lua

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ local _padding = require "nvim-tree.renderer.components.padding"
77
local icon_component = require "nvim-tree.renderer.components.icons"
88
local full_name = require "nvim-tree.renderer.components.full-name"
99
local git = require "nvim-tree.renderer.components.git"
10-
local diagnostics = require "nvim-tree.renderer.components.diagnostics"
1110
local Builder = require "nvim-tree.renderer.builder"
1211
local live_filter = require "nvim-tree.live-filter"
1312

14-
local DecoratorBookmark = require "nvim-tree.renderer.decorator.bookmark"
13+
local DecoratorBookmarks = require "nvim-tree.renderer.decorator.bookmarks"
14+
local DecoratorDiagnostics = require "nvim-tree.renderer.decorator.diagnostics"
1515
local DecoratorModified = require "nvim-tree.renderer.decorator.modified"
1616

1717
local M = {
@@ -76,7 +76,6 @@ function M.draw(unloaded_bufnr)
7676
:configure_opened_file_highlighting(M.config.highlight_opened_files)
7777
:configure_icon_padding(M.config.icons.padding)
7878
:configure_git_icons_placement(M.config.icons.git_placement)
79-
:configure_diagnostics_icon_placement(M.config.icons.diagnostics_placement)
8079
:configure_symlink_destination(M.config.symlink_destination)
8180
:configure_filter(live_filter.filter, live_filter.prefix)
8281
:configure_group_name_modifier(M.config.group_empty)
@@ -105,18 +104,14 @@ function M.setup(opts)
105104
_padding.setup(opts)
106105
full_name.setup(opts)
107106
git.setup(opts)
108-
diagnostics.setup(opts)
109107
icon_component.setup(opts)
110108

111109
-- TODO change to array: precedence should follow order
112110
M.decorators = {
113-
bookmark = DecoratorBookmark:new(opts),
111+
bookmarks = DecoratorBookmarks:new(opts),
112+
diagnostics = DecoratorDiagnostics:new(opts),
114113
modified = DecoratorModified:new(opts),
115114
}
116-
117-
for _, d in pairs(M.decorators) do
118-
log.line("dev", "d = %s", vim.inspect(d))
119-
end
120115
end
121116

122117
return M

0 commit comments

Comments
 (0)