feat(diagnostics): emulate VSCode Error Lens with virtual text and lines

Added functionality to mimic VSCode's Error Lens behavior:
	- Displays diagnostics as virtual text with severity icons during insert mode.
	- Switches to virtual lines with formatted messages in normal mode for better readability.

	Improves real-time feedback while coding and enhances the visual presentation of diagnostics.
This commit is contained in:
glaulher 2025-07-09 10:21:26 -03:00
parent 3338d39206
commit 632f7b06cd
1 changed files with 45 additions and 22 deletions

View File

@ -628,33 +628,56 @@ require('lazy').setup({
-- Diagnostic Config -- Diagnostic Config
-- See :help vim.diagnostic.Opts -- See :help vim.diagnostic.Opts
local diagnostic_icons = {
[vim.diagnostic.severity.ERROR] = '󰅚 ',
[vim.diagnostic.severity.WARN] = '󰀪 ',
[vim.diagnostic.severity.INFO] = '󰋽 ',
[vim.diagnostic.severity.HINT] = '󰌶 ',
}
vim.diagnostic.config { vim.diagnostic.config {
update_in_insert = true,
severity_sort = true, severity_sort = true,
float = { border = 'rounded', source = 'if_many' }, float = { border = 'rounded', source = 'if_many' },
underline = { severity = vim.diagnostic.severity.ERROR }, underline = true, -- { severity = vim.diagnostic.severity.ERROR }
signs = vim.g.have_nerd_font and { signs = vim.g.have_nerd_font and { text = diagnostic_icons } or {},
text = {
[vim.diagnostic.severity.ERROR] = '󰅚 ',
[vim.diagnostic.severity.WARN] = '󰀪 ',
[vim.diagnostic.severity.INFO] = '󰋽 ',
[vim.diagnostic.severity.HINT] = '󰌶 ',
},
} or {},
virtual_text = {
source = 'if_many',
spacing = 2,
format = function(diagnostic)
local diagnostic_message = {
[vim.diagnostic.severity.ERROR] = diagnostic.message,
[vim.diagnostic.severity.WARN] = diagnostic.message,
[vim.diagnostic.severity.INFO] = diagnostic.message,
[vim.diagnostic.severity.HINT] = diagnostic.message,
}
return diagnostic_message[diagnostic.severity]
end,
},
} }
-- Diagnostic configuration similar to VS Code's Error Lens.
-- In insert mode, diagnostics are displayed as inline virtual text.
-- In normal mode, diagnostics are shown as virtual lines below the affected lines.
---@param enable boolean
local function set_virtual_text(enable)
vim.diagnostic.config {
virtual_lines = not enable and {
format = function(diagnostic)
return (diagnostic_icons[diagnostic.severity] or '') .. diagnostic.message
end,
} or false,
virtual_text = enable and {
source = 'if_many',
spacing = 2,
format = function(diagnostic)
return (diagnostic_icons[diagnostic.severity] or '') .. diagnostic.message
end,
} or false,
}
end
set_virtual_text(false)
vim.api.nvim_create_autocmd('InsertEnter', {
callback = function()
set_virtual_text(true)
end,
})
vim.api.nvim_create_autocmd('InsertLeave', {
callback = function()
set_virtual_text(false)
end,
})
-- LSP servers and clients are able to communicate to each other what features they support. -- LSP servers and clients are able to communicate to each other what features they support.
-- By default, Neovim doesn't support everything that is in the LSP specification. -- By default, Neovim doesn't support everything that is in the LSP specification.
-- When you add blink.cmp, luasnip, etc. Neovim now has *more* capabilities. -- When you add blink.cmp, luasnip, etc. Neovim now has *more* capabilities.