fix(#2519): diagnostics overhaul

Signed-off-by: iusmac <iusico.maxim@libero.it>
This commit is contained in:
iusmac
2023-12-22 19:58:20 +01:00
parent 50f30bcd8c
commit 7bc3616ed8
3 changed files with 47 additions and 30 deletions

View File

@@ -1,6 +1,5 @@
local utils = require "nvim-tree.utils" local utils = require "nvim-tree.utils"
local view = require "nvim-tree.view" local view = require "nvim-tree.view"
local core = require "nvim-tree.core"
local log = require "nvim-tree.log" local log = require "nvim-tree.log"
local M = {} local M = {}
@@ -12,6 +11,16 @@ local severity_levels = {
Hint = 4, Hint = 4,
} }
--- A dictionary tree containing buffer-severity mappings.
---@type table
local buffer_severity_dict = {}
---@param path string
---@return string
local function uniformize_path(path)
return utils.canonical_path(path:gsub("\\", "/"))
end
---@return table ---@return table
local function from_nvim_lsp() local function from_nvim_lsp()
local buffer_severity = {} local buffer_severity = {}
@@ -25,7 +34,7 @@ local function from_nvim_lsp()
for _, diagnostic in ipairs(vim.diagnostic.get(nil, { severity = M.severity })) do for _, diagnostic in ipairs(vim.diagnostic.get(nil, { severity = M.severity })) do
local buf = diagnostic.bufnr local buf = diagnostic.bufnr
if vim.api.nvim_buf_is_valid(buf) then if vim.api.nvim_buf_is_valid(buf) then
local bufname = vim.api.nvim_buf_get_name(buf) local bufname = uniformize_path(vim.api.nvim_buf_get_name(buf))
local lowest_severity = buffer_severity[bufname] local lowest_severity = buffer_severity[bufname]
if not lowest_severity or diagnostic.severity < lowest_severity then if not lowest_severity or diagnostic.severity < lowest_severity then
buffer_severity[bufname] = diagnostic.severity buffer_severity[bufname] = diagnostic.severity
@@ -67,7 +76,7 @@ local function from_coc()
local buffer_severity = {} local buffer_severity = {}
for bufname, severity in pairs(diagnostics) do for bufname, severity in pairs(diagnostics) do
if is_severity_in_range(severity, M.severity) then if is_severity_in_range(severity, M.severity) then
buffer_severity[bufname] = severity buffer_severity[uniformize_path(bufname)] = severity
end end
end end
@@ -79,47 +88,52 @@ local function is_using_coc()
end end
function M.update() function M.update()
if not M.enable or not core.get_explorer() or not view.is_buf_valid(view.get_bufnr()) then if not M.enable then
return return
end end
utils.debounce("diagnostics", M.debounce_delay, function() utils.debounce("diagnostics", M.debounce_delay, function()
local profile = log.profile_start "diagnostics update" local profile = log.profile_start "diagnostics update"
log.line("diagnostics", "update")
local buffer_severity
if is_using_coc() then if is_using_coc() then
buffer_severity = from_coc() buffer_severity_dict = from_coc()
else else
buffer_severity = from_nvim_lsp() buffer_severity_dict = from_nvim_lsp()
end end
log.node("diagnostics", buffer_severity_dict, "update")
local nodes_by_line = utils.get_nodes_by_line(core.get_explorer().nodes, core.get_nodes_starting_line()) log.profile_end(profile)
for _, node in pairs(nodes_by_line) do if view.is_buf_valid(view.get_bufnr()) then
node.diag_status = nil require("nvim-tree.renderer").draw()
end end
end)
end
for bufname, severity in pairs(buffer_severity) do ---@param node Node
local bufpath = utils.canonical_path(bufname) function M.update_node_severity_level(node)
log.line("diagnostics", " bufpath '%s' severity %d", bufpath, severity) if not M.enable then
if 0 < severity and severity < 5 then return
for line, node in pairs(nodes_by_line) do end
local nodepath = utils.canonical_path(node.absolute_path)
log.line("diagnostics", " %d checking nodepath '%s'", line, nodepath)
local node_contains_buf = vim.startswith(bufpath:gsub("\\", "/"), nodepath:gsub("\\", "/") .. "/") local is_folder = node.nodes ~= nil
if M.show_on_dirs and node_contains_buf and (not node.open or M.show_on_open_dirs) then local nodepath = uniformize_path(node.absolute_path)
log.line("diagnostics", " matched fold node '%s'", node.absolute_path)
node.diag_status = severity if is_folder then
elseif nodepath == bufpath then local max_severity = nil
log.line("diagnostics", " matched file node '%s'", node.absolute_path) if M.show_on_dirs and (not node.open or M.show_on_open_dirs) then
node.diag_status = severity for bufname, severity in pairs(buffer_severity_dict) do
local node_contains_buf = vim.startswith(bufname, nodepath .. "/")
if node_contains_buf then
if severity == M.severity.max then
max_severity = severity
break
else
max_severity = math.min(max_severity or severity, severity)
end end
end end
end end
end end
log.profile_end(profile) node.diag_status = max_severity
require("nvim-tree.renderer").draw() else
end) node.diag_status = buffer_severity_dict[nodepath]
end
end end
function M.setup(opts) function M.setup(opts)

View File

@@ -17,6 +17,7 @@
---@field parent DirNode ---@field parent DirNode
---@field type string ---@field type string
---@field watcher function|nil ---@field watcher function|nil
---@field diag_status integer|nil
---@class DirNode: BaseNode ---@class DirNode: BaseNode
---@field has_children boolean ---@field has_children boolean

View File

@@ -413,6 +413,8 @@ end
function Builder:_build_line(node, idx, num_children, unloaded_bufnr) function Builder:_build_line(node, idx, num_children, unloaded_bufnr)
local copy_paste = require "nvim-tree.actions.fs.copy-paste" local copy_paste = require "nvim-tree.actions.fs.copy-paste"
require("nvim-tree.diagnostics").update_node_severity_level(node)
-- various components -- various components
local indent_markers = pad.get_indent_markers(self.depth, idx, num_children, node, self.markers) local indent_markers = pad.get_indent_markers(self.depth, idx, num_children, node, self.markers)
local arrows = pad.get_arrows(node) local arrows = pad.get_arrows(node)