add better lsp diagnostic integration

This commit is contained in:
kiyan 2021-04-16 00:03:56 +02:00
parent 75338221b0
commit b5ef2fb7f8
4 changed files with 55 additions and 26 deletions

View File

@ -36,7 +36,7 @@ let g:nvim_tree_disable_netrw = 0 "1 by default, disables netrw
let g:nvim_tree_hijack_netrw = 0 "1 by default, prevents netrw from automatically opening when opening directories (but lets you keep its other utilities)
let g:nvim_tree_add_trailing = 1 "0 by default, append a trailing slash to folder names
let g:nvim_tree_group_empty = 1 " 0 by default, compact folders that only contain a single folder into one node in the file tree
let g:nvim_tree_lsp_diagnostics = 1 "0 by default, will show lsp diagnostics in the tree. See :help nvim_tree_lsp_diagnostics
let g:nvim_tree_lsp_diagnostics = 1 "0 by default, will show lsp diagnostics in the signcolumn. See :help nvim_tree_lsp_diagnostics
let g:nvim_tree_show_icons = {
\ 'git': 1,
\ 'folders': 0,
@ -174,7 +174,7 @@ This plugin is very fast because it uses the `libuv` `scandir` and `scandir_next
- Change directory with `.`
- Add / Rename / delete files
- Git integration (icons and file highlight)
- Lsp diagnostics integration (file highlight)
- Lsp diagnostics integration (signs)
- Indent markers
- Mouse support
- It's fast

View File

@ -227,8 +227,8 @@ together. 0 by default.
|g:nvim_tree_lsp_diagnostics| *g:nvim_tree_lsp_diagnostics*
Can be 0 or 1. When 1, will show nvim-lsp diagnostics in the tree
by highlighting filenames with `NvimTreeLspDiagnostics`.
Can be 0 or 1. When 1, will show nvim-lsp diagnostics in the signcolumn
of the tree highlighted by diagnostic severity.
Code will be executed on `BufWritePost`. 0 by default.
==============================================================================
@ -373,7 +373,11 @@ NvimTreeSpecialFile
NvimTreeImageFile
NvimTreeMarkdownFile
NvimTreeIndentMarker
NvimTreeLspDiagnostics
LspDiagnosticsError
LspDiagnosticsWarning
LspDiagnosticsInformation
LspDiagnosticsHint
NvimTreeLicenseIcon
NvimTreeYamlIcon

View File

@ -34,7 +34,6 @@ local function get_hl_groups()
Symlink = { gui = 'bold', fg = colors.cyan },
FolderIcon = { fg = '#8094b4' },
RootFolder = { fg = colors.purple },
LspDiagnostics = { gui = 'underline' },
ExecFile = { gui = 'bold', fg = colors.green },
SpecialFile = { gui = 'bold,underline', fg = colors.yellow },
@ -68,6 +67,10 @@ local function get_links()
FileDeleted = 'NvimTreeGitDeleted',
Popup = 'Normal',
GitIgnored = 'Comment',
LspDiagnosticsError = "LspDiagnosticsDefaultError",
LspDiagnosticsWarning = "LspDiagnosticsDefaultWarning",
LspDiagnosticsInformation = "LspDiagnosticsDefaultInformation",
LspDiagnosticsHint = "LspDiagnosticsDefaultHint",
}
end

View File

@ -1,24 +1,38 @@
local a = vim.api
local utils = require'nvim-tree.utils'
local view = require'nvim-tree.view'
local get_diagnostics = vim.lsp.diagnostic.get_all
local M = {}
local function get_severity(diagnostics)
local function get_highest_severity(diagnostics)
local severity = 0
for _, v in ipairs(diagnostics) do
if v.severity > 0 then
return v.severity
if v.severity > severity then
severity = v.severity
end
end
return 0
return severity
end
local function highlight_node(node, linenr)
local buf = require'nvim-tree.view'.View.bufnr
local sign_names = {
{ "NvimTreeSignHint", "NvimTreeLspDiagnosticsHint" },
{ "NvimTreeSignInformation", "NvimTreeLspDiagnosticsInformation" },
{ "NvimTreeSignWarning", "NvimTreeLspDiagnosticsWarning" },
{ "NvimTreeSignError", "NvimTreeLspDiagnosticsError" },
}
for _, v in ipairs(sign_names) do
vim.fn.sign_define(v[1], { text="", texthl=v[2]})
end
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
local line = a.nvim_buf_get_lines(buf, linenr, linenr+1, false)[1]
local starts_at = vim.fn.stridx(line, node.name)
a.nvim_buf_add_highlight(buf, -1, 'NvimTreeLspDiagnostics', linenr, starts_at, -1)
local sign_name = sign_names[severity][1]
table.insert(signs, vim.fn.sign_place(1, 'NvimTreeDiagnosticSigns', sign_name, buf, { lnum = linenr+1 }))
end
function M.update()
@ -27,22 +41,30 @@ function M.update()
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_severity(diagnostics)
local severity = get_highest_severity(diagnostics)
buffer_severity[bufname] = severity
end
end
vim.defer_fn(function()
local nodes = require'nvim-tree.lib'.Tree.entries
for bufname, severity in pairs(buffer_severity) do
if severity > 0 then
local node, line = utils.find_node(nodes, function(node)
return node.absolute_path == bufname
end)
if node then highlight_node(node, line) end
end
local nodes = require'nvim-tree.lib'.Tree.entries
if #signs then
vim.fn.sign_unplacelist(vim.tbl_map(function(sign)
return {
buffer = view.View.bufnr,
group = "NvimTreeDiagnosticSigns",
id = sign
}
end, signs))
signs = {}
end
for bufname, severity in pairs(buffer_severity) do
if severity > 0 then
local node, line = utils.find_node(nodes, function(node)
return node.absolute_path == bufname
end)
if node then add_sign(line, severity) end
end
end, 100)
end
end
return M