feat: add custom icons to language server diagnostics

This commit is contained in:
Sven Vogel 2024-11-08 17:47:59 +01:00
parent e602c90efc
commit 3159d83be7
2 changed files with 62 additions and 3 deletions

20
lua/config/icons.lua Normal file
View File

@ -0,0 +1,20 @@
--
-- Icons
--
-- ..............................................
--
-- Author: Sven Vogel
-- Created: 08.11.2024
-- Edited: 08.11.2024
--
-- ==============================================
return {
diagnostics = {
error = "",
warn = "",
hint = "",
info = ""
}
}

View File

@ -12,6 +12,7 @@
-- ==============================================
ui = require 'config.ui'
icons = require 'config.icons'
return {
{
@ -29,8 +30,6 @@ return {
enabled = true,
},
diagnostics = {
virtual_text = true,
signs = true,
update_in_insert = false,
underline = true,
severity_sort = false,
@ -38,9 +37,24 @@ return {
border = ui.border,
source = 'always',
},
virtual_text = {
spacing = 4,
source = "if_many",
-- this will set set the prefix to a function that returns the diagnostics icon based on the severity
-- this only works on a recent 0.10.0 build. Will be set to "●" when not supported
prefix = "icons"
},
signs = {
text = {
[vim.diagnostic.severity.ERROR] = icons.diagnostics.error,
[vim.diagnostic.severity.WARN] = icons.diagnostics.warn,
[vim.diagnostic.severity.HINT] = icons.diagnostics.hint,
[vim.diagnostic.severity.INFO] = icons.diagnostics.info
}
}
}
},
config = function(opts)
config = function(_, opts)
require("mason").setup()
local handlers = {
@ -57,6 +71,31 @@ return {
require("mason-lspconfig").setup({ handlers = handlers })
-- Setup diagnostic signs
--
-- Source: https://www.lazyvim.org/plugins/lsp#mason-lspconfignvim
-- ..............................................
-- diagnostics signs
if type(opts.diagnostics.signs) ~= "boolean" then
for severity, icon in pairs(opts.diagnostics.signs.text) do
local name = vim.diagnostic.severity[severity]:lower():gsub("^%l", string.upper)
name = "DiagnosticSign" .. name
vim.fn.sign_define(name, { text = icon, texthl = name, numhl = "" })
end
end
-- diagnostic sign as prefix for virtual text
if type(opts.diagnostics.virtual_text) == "table" and opts.diagnostics.virtual_text.prefix == "icons" then
opts.diagnostics.virtual_text.prefix = vim.fn.has("nvim-0.10.0") == 0 and "" or function(diagnostic)
for d, icon in pairs(icons.diagnostics) do
if diagnostic.severity == vim.diagnostic.severity[d:upper()] then
return icon
end
end
end
end
-- Setup diagnostic user interface
--
-- Source: https://vonheikemen.github.io/devlog/tools/setup-nvim-lspconfig-plus-nvim-cmp