Skip to content

Commit 4615e32

Browse files
committed
fix(#2519): diagnostics overhaul
Signed-off-by: iusmac <iusico.maxim@libero.it>
1 parent 50f30bc commit 4615e32

File tree

2 files changed

+47
-31
lines changed

2 files changed

+47
-31
lines changed

lua/nvim-tree/diagnostics.lua

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
local utils = require "nvim-tree.utils"
22
local view = require "nvim-tree.view"
3-
local core = require "nvim-tree.core"
43
local log = require "nvim-tree.log"
54

65
local M = {}
@@ -12,6 +11,16 @@ local severity_levels = {
1211
Hint = 4,
1312
}
1413

14+
--- A dictionary tree containing buffer-severity mappings.
15+
---@type table
16+
local buffer_severity_dict = {}
17+
18+
---@param path string
19+
---@return string
20+
local function uniformize_path(path)
21+
return utils.canonical_path(path:gsub("\\", "/"))
22+
end
23+
1524
---@return table
1625
local function from_nvim_lsp()
1726
local buffer_severity = {}
@@ -25,7 +34,7 @@ local function from_nvim_lsp()
2534
for _, diagnostic in ipairs(vim.diagnostic.get(nil, { severity = M.severity })) do
2635
local buf = diagnostic.bufnr
2736
if vim.api.nvim_buf_is_valid(buf) then
28-
local bufname = vim.api.nvim_buf_get_name(buf)
37+
local bufname = uniformize_path(vim.api.nvim_buf_get_name(buf))
2938
local lowest_severity = buffer_severity[bufname]
3039
if not lowest_severity or diagnostic.severity < lowest_severity then
3140
buffer_severity[bufname] = diagnostic.severity
@@ -67,7 +76,7 @@ local function from_coc()
6776
local buffer_severity = {}
6877
for bufname, severity in pairs(diagnostics) do
6978
if is_severity_in_range(severity, M.severity) then
70-
buffer_severity[bufname] = severity
79+
buffer_severity[uniformize_path(bufname)] = severity
7180
end
7281
end
7382

@@ -79,47 +88,52 @@ local function is_using_coc()
7988
end
8089

8190
function M.update()
82-
if not M.enable or not core.get_explorer() or not view.is_buf_valid(view.get_bufnr()) then
91+
if not M.enable then
8392
return
8493
end
8594
utils.debounce("diagnostics", M.debounce_delay, function()
8695
local profile = log.profile_start "diagnostics update"
87-
log.line("diagnostics", "update")
88-
89-
local buffer_severity
9096
if is_using_coc() then
91-
buffer_severity = from_coc()
97+
buffer_severity_dict = from_coc()
9298
else
93-
buffer_severity = from_nvim_lsp()
99+
buffer_severity_dict = from_nvim_lsp()
94100
end
95-
96-
local nodes_by_line = utils.get_nodes_by_line(core.get_explorer().nodes, core.get_nodes_starting_line())
97-
for _, node in pairs(nodes_by_line) do
98-
node.diag_status = nil
101+
log.node("diagnostics", buffer_severity_dict, "update")
102+
log.profile_end(profile)
103+
if view.is_buf_valid(view.get_bufnr()) then
104+
require("nvim-tree.renderer").draw()
99105
end
106+
end)
107+
end
108+
109+
---@param node Node
110+
function M.update_node_severity_level(node)
111+
if not M.enable or node == nil then
112+
return
113+
end
100114

101-
for bufname, severity in pairs(buffer_severity) do
102-
local bufpath = utils.canonical_path(bufname)
103-
log.line("diagnostics", " bufpath '%s' severity %d", bufpath, severity)
104-
if 0 < severity and severity < 5 then
105-
for line, node in pairs(nodes_by_line) do
106-
local nodepath = utils.canonical_path(node.absolute_path)
107-
log.line("diagnostics", " %d checking nodepath '%s'", line, nodepath)
108-
109-
local node_contains_buf = vim.startswith(bufpath:gsub("\\", "/"), nodepath:gsub("\\", "/") .. "/")
110-
if M.show_on_dirs and node_contains_buf and (not node.open or M.show_on_open_dirs) then
111-
log.line("diagnostics", " matched fold node '%s'", node.absolute_path)
112-
node.diag_status = severity
113-
elseif nodepath == bufpath then
114-
log.line("diagnostics", " matched file node '%s'", node.absolute_path)
115-
node.diag_status = severity
115+
local is_folder = node.nodes ~= nil
116+
local nodepath = uniformize_path(node.absolute_path)
117+
118+
if is_folder then
119+
local max_severity = nil
120+
if M.show_on_dirs and (not node.open or M.show_on_open_dirs) then
121+
for bufname, severity in pairs(buffer_severity_dict) do
122+
local node_contains_buf = vim.startswith(bufname, nodepath .. "/")
123+
if node_contains_buf then
124+
if severity == M.severity.max then
125+
max_severity = severity
126+
break
127+
else
128+
max_severity = math.min(max_severity or severity, severity)
116129
end
117130
end
118131
end
119132
end
120-
log.profile_end(profile)
121-
require("nvim-tree.renderer").draw()
122-
end)
133+
node.diag_status = max_severity
134+
else
135+
node.diag_status = buffer_severity_dict[nodepath]
136+
end
123137
end
124138

125139
function M.setup(opts)

lua/nvim-tree/renderer/builder.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,8 @@ end
413413
function Builder:_build_line(node, idx, num_children, unloaded_bufnr)
414414
local copy_paste = require "nvim-tree.actions.fs.copy-paste"
415415

416+
require("nvim-tree.diagnostics").update_node_severity_level(node)
417+
416418
-- various components
417419
local indent_markers = pad.get_indent_markers(self.depth, idx, num_children, node, self.markers)
418420
local arrows = pad.get_arrows(node)

0 commit comments

Comments
 (0)