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_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_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_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 = { let g:nvim_tree_show_icons = {
\ 'git': 1, \ 'git': 1,
\ 'folders': 0, \ 'folders': 0,
@ -174,7 +174,7 @@ This plugin is very fast because it uses the `libuv` `scandir` and `scandir_next
- Change directory with `.` - Change directory with `.`
- Add / Rename / delete files - Add / Rename / delete files
- Git integration (icons and file highlight) - Git integration (icons and file highlight)
- Lsp diagnostics integration (file highlight) - Lsp diagnostics integration (signs)
- Indent markers - Indent markers
- Mouse support - Mouse support
- It's fast - It's fast

View File

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

View File

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

View File

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