Skip to content

Commit 232053f

Browse files
committed
feat(#2415): create DecoratorDiagnostics
1 parent e69dae1 commit 232053f

File tree

7 files changed

+127
-145
lines changed

7 files changed

+127
-145
lines changed

lua/nvim-tree/renderer/builder.lua

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

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

76-
function Builder:configure_diagnostics_icon_placement(where)
77-
if where ~= "after" and where ~= "before" and where ~= "signcolumn" then
78-
where = "before" -- default before
79-
end
80-
self.diagnostics_placement = where
81-
return self
82-
end
83-
8475
function Builder:configure_symlink_destination(show)
8576
self.symlink_destination = show
8677
return self
@@ -218,8 +209,8 @@ end
218209
---@param node table
219210
---@return HighlightedString[]|nil icon
220211
function Builder:_get_diagnostics_icon(node)
221-
local diagnostics_icon = diagnostics.get_icon(node)
222-
if diagnostics_icon and self.diagnostics_placement == "signcolumn" then
212+
local diagnostics_icon = self.decorators.diagnostics:get_icon(node)
213+
if diagnostics_icon and self.decorators.diagnostics.icon_placement == ICON_PLACEMENT.signcolumn then
223214
table.insert(self.signs, {
224215
sign = diagnostics_icon.hl[1],
225216
lnum = self.index + 1,
@@ -248,8 +239,8 @@ end
248239
---@param node table
249240
---@return HighlightedString[]|nil icon
250241
function Builder:_get_bookmark_icon(node)
251-
local bookmark_icon = self.decorators.bookmark:get_icon(node)
252-
if bookmark_icon and self.decorators.bookmark.icon_placement == ICON_PLACEMENT.signcolumn then
242+
local bookmark_icon = self.decorators.bookmarks:get_icon(node)
243+
if bookmark_icon and self.decorators.bookmarks.icon_placement == ICON_PLACEMENT.signcolumn then
253244
table.insert(self.signs, {
254245
sign = bookmark_icon.hl[1],
255246
lnum = self.index + 1,
@@ -301,12 +292,12 @@ end
301292
---@param icon_hl string[] icons to append to
302293
---@param name_hl string[] names to append to
303294
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
295+
local hl = decorator:get_highlight(node)
296+
if hl then
297+
if decorator.hl_pos == HL_POSITION.all or decorator.hl_pos == HL_POSITION.icon then
307298
table.insert(icon_hl, hl)
308299
end
309-
if pos == HL_POSITION.all or pos == HL_POSITION.name then
300+
if decorator.hl_pos == HL_POSITION.all or decorator.hl_pos == HL_POSITION.name then
310301
table.insert(name_hl, hl)
311302
end
312303
end
@@ -347,10 +338,10 @@ function Builder:_format_line(indent_markers, arrows, icon, name, git_icons, dia
347338
if modified_icon and self.decorators.modified.icon_placement == ICON_PLACEMENT.before then
348339
add_to_end(line, { modified_icon })
349340
end
350-
if diagnostics_icon and self.diagnostics_placement == "before" then
341+
if diagnostics_icon and self.decorators.diagnostics.icon_placement == ICON_PLACEMENT.before then
351342
add_to_end(line, { diagnostics_icon })
352343
end
353-
if bookmark_icon and self.decorators.bookmark.icon_placement == ICON_PLACEMENT.before then
344+
if bookmark_icon and self.decorators.bookmarks.icon_placement == ICON_PLACEMENT.before then
354345
add_to_end(line, { bookmark_icon })
355346
end
356347

@@ -362,10 +353,10 @@ function Builder:_format_line(indent_markers, arrows, icon, name, git_icons, dia
362353
if modified_icon and self.decorators.modified.icon_placement == ICON_PLACEMENT.after then
363354
add_to_end(line, { modified_icon })
364355
end
365-
if diagnostics_icon and self.diagnostics_placement == "after" then
356+
if diagnostics_icon and self.decorators.diagnostics.icon_placement == ICON_PLACEMENT.after then
366357
add_to_end(line, { diagnostics_icon })
367358
end
368-
if bookmark_icon and self.decorators.bookmark.icon_placement == ICON_PLACEMENT.after then
359+
if bookmark_icon and self.decorators.bookmarks.icon_placement == ICON_PLACEMENT.after then
369360
add_to_end(line, { bookmark_icon })
370361
end
371362

@@ -409,8 +400,8 @@ function Builder:_build_line(node, idx, num_children, unloaded_bufnr)
409400
-- extra highighting
410401
self:_append_highlight(node, git.get_highlight, icon.hl, name.hl)
411402
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)
413-
self:_append_highlight(node, diagnostics.get_highlight, icon.hl, name.hl)
403+
self:_append_dec_highlight(node, self.decorators.bookmarks, icon.hl, name.hl)
404+
self:_append_dec_highlight(node, self.decorators.diagnostics, icon.hl, name.hl)
414405
self:_append_highlight(node, copy_paste.get_highlight, icon.hl, name.hl)
415406

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

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

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

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

0 commit comments

Comments
 (0)