feat(diagnostics): More responsive and configurable diagnostics signs. (#303)

This commit is contained in:
Sindre T. Strøm
2021-04-16 17:57:16 +02:00
committed by GitHub
parent 090697e71f
commit da09da3318
6 changed files with 51 additions and 18 deletions

View File

@@ -1,14 +1,16 @@
local a = vim.api
local utils = require'nvim-tree.utils'
local view = require'nvim-tree.view'
local config = require'nvim-tree.config'
local icon_state = config.get_icon_state()
local get_diagnostics = vim.lsp.diagnostic.get_all
local M = {}
local function get_highest_severity(diagnostics)
local severity = 0
local function get_lowest_severity(diagnostics)
local severity = math.huge
for _, v in ipairs(diagnostics) do
if v.severity > severity then
if v.severity < severity then
severity = v.severity
end
end
@@ -16,21 +18,22 @@ local function get_highest_severity(diagnostics)
end
local sign_names = {
{ "NvimTreeSignHint", "NvimTreeLspDiagnosticsHint" },
{ "NvimTreeSignInformation", "NvimTreeLspDiagnosticsInformation" },
{ "NvimTreeSignWarning", "NvimTreeLspDiagnosticsWarning" },
{ "NvimTreeSignError", "NvimTreeLspDiagnosticsError" },
{ "NvimTreeSignWarning", "NvimTreeLspDiagnosticsWarning" },
{ "NvimTreeSignInformation", "NvimTreeLspDiagnosticsInformation" },
{ "NvimTreeSignHint", "NvimTreeLspDiagnosticsHint" },
}
for _, v in ipairs(sign_names) do
vim.fn.sign_define(v[1], { text="", texthl=v[2]})
end
vim.fn.sign_define(sign_names[1][1], { text=icon_state.icons.lsp.error, texthl=sign_names[1][2]})
vim.fn.sign_define(sign_names[2][1], { text=icon_state.icons.lsp.warning, texthl=sign_names[2][2]})
vim.fn.sign_define(sign_names[3][1], { text=icon_state.icons.lsp.info, texthl=sign_names[3][2]})
vim.fn.sign_define(sign_names[4][1], { text=icon_state.icons.lsp.hint, texthl=sign_names[4][2]})
local signs = {}
local function add_sign(linenr, severity)
local buf = view.View.bufnr
if not vim.fn.bufexists(buf) or not vim.fn.bufloaded(buf) then return end
if not a.nvim_buf_is_valid(buf) or not a.nvim_buf_is_loaded(buf) then return end
local sign_name = sign_names[severity][1]
table.insert(signs, vim.fn.sign_place(1, 'NvimTreeDiagnosticSigns', sign_name, buf, { lnum = linenr+1 }))
end
@@ -39,10 +42,12 @@ function M.update()
local buffer_severity = {}
for buf, diagnostics in pairs(get_diagnostics()) do
local bufname = a.nvim_buf_get_name(buf)
if not buffer_severity[bufname] then
local severity = get_highest_severity(diagnostics)
buffer_severity[bufname] = severity
if a.nvim_buf_is_valid(buf) then
local bufname = a.nvim_buf_get_name(buf)
if not buffer_severity[bufname] then
local severity = get_lowest_severity(diagnostics)
buffer_severity[bufname] = severity
end
end
end
@@ -58,7 +63,7 @@ function M.update()
signs = {}
end
for bufname, severity in pairs(buffer_severity) do
if severity > 0 then
if 0 < severity and severity < 5 then
local node, line = utils.find_node(nodes, function(node)
return node.absolute_path == bufname
end)