Skip to content

Commit 632f7b0

Browse files
committed
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.
1 parent 3338d39 commit 632f7b0

File tree

1 file changed

+45
-22
lines changed

1 file changed

+45
-22
lines changed

init.lua

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -628,33 +628,56 @@ require('lazy').setup({
628628

629629
-- Diagnostic Config
630630
-- See :help vim.diagnostic.Opts
631+
local diagnostic_icons = {
632+
[vim.diagnostic.severity.ERROR] = '󰅚 ',
633+
[vim.diagnostic.severity.WARN] = '󰀪 ',
634+
[vim.diagnostic.severity.INFO] = '󰋽 ',
635+
[vim.diagnostic.severity.HINT] = '󰌶 ',
636+
}
637+
631638
vim.diagnostic.config {
639+
update_in_insert = true,
632640
severity_sort = true,
633641
float = { border = 'rounded', source = 'if_many' },
634-
underline = { severity = vim.diagnostic.severity.ERROR },
635-
signs = vim.g.have_nerd_font and {
636-
text = {
637-
[vim.diagnostic.severity.ERROR] = '󰅚 ',
638-
[vim.diagnostic.severity.WARN] = '󰀪 ',
639-
[vim.diagnostic.severity.INFO] = '󰋽 ',
640-
[vim.diagnostic.severity.HINT] = '󰌶 ',
641-
},
642-
} or {},
643-
virtual_text = {
644-
source = 'if_many',
645-
spacing = 2,
646-
format = function(diagnostic)
647-
local diagnostic_message = {
648-
[vim.diagnostic.severity.ERROR] = diagnostic.message,
649-
[vim.diagnostic.severity.WARN] = diagnostic.message,
650-
[vim.diagnostic.severity.INFO] = diagnostic.message,
651-
[vim.diagnostic.severity.HINT] = diagnostic.message,
652-
}
653-
return diagnostic_message[diagnostic.severity]
654-
end,
655-
},
642+
underline = true, -- { severity = vim.diagnostic.severity.ERROR }
643+
signs = vim.g.have_nerd_font and { text = diagnostic_icons } or {},
656644
}
657645

646+
-- Diagnostic configuration similar to VS Code's Error Lens.
647+
-- In insert mode, diagnostics are displayed as inline virtual text.
648+
-- In normal mode, diagnostics are shown as virtual lines below the affected lines.
649+
---@param enable boolean
650+
local function set_virtual_text(enable)
651+
vim.diagnostic.config {
652+
virtual_lines = not enable and {
653+
format = function(diagnostic)
654+
return (diagnostic_icons[diagnostic.severity] or '') .. diagnostic.message
655+
end,
656+
} or false,
657+
virtual_text = enable and {
658+
source = 'if_many',
659+
spacing = 2,
660+
format = function(diagnostic)
661+
return (diagnostic_icons[diagnostic.severity] or '') .. diagnostic.message
662+
end,
663+
} or false,
664+
}
665+
end
666+
667+
set_virtual_text(false)
668+
669+
vim.api.nvim_create_autocmd('InsertEnter', {
670+
callback = function()
671+
set_virtual_text(true)
672+
end,
673+
})
674+
675+
vim.api.nvim_create_autocmd('InsertLeave', {
676+
callback = function()
677+
set_virtual_text(false)
678+
end,
679+
})
680+
658681
-- LSP servers and clients are able to communicate to each other what features they support.
659682
-- By default, Neovim doesn't support everything that is in the LSP specification.
660683
-- When you add blink.cmp, luasnip, etc. Neovim now has *more* capabilities.

0 commit comments

Comments
 (0)