Skip to content

Commit ffe2ab0

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 ffe2ab0

File tree

1 file changed

+48
-26
lines changed

1 file changed

+48
-26
lines changed

init.lua

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -628,32 +628,54 @@ require('lazy').setup({
628628

629629
-- Diagnostic Config
630630
-- See :help vim.diagnostic.Opts
631-
vim.diagnostic.config {
632-
severity_sort = true,
633-
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-
},
656-
}
631+
-- Diagnostic configuration similar to VS Code's Error Lens.
632+
-- In insert mode, diagnostics are displayed as inline virtual text.
633+
-- In normal mode, diagnostics are shown as virtual lines below the affected lines.
634+
635+
local function set_virtual_text(enable)
636+
local diagnostic_icons = {
637+
[vim.diagnostic.severity.ERROR] = '󰅚 ',
638+
[vim.diagnostic.severity.WARN] = '󰀪 ',
639+
[vim.diagnostic.severity.INFO] = '󰋽 ',
640+
[vim.diagnostic.severity.HINT] = '󰌶 ',
641+
}
642+
643+
vim.diagnostic.config {
644+
update_in_insert = true, -- error messages in insert mode
645+
severity_sort = true,
646+
float = { border = 'rounded', source = 'if_many' },
647+
underline = { severity = vim.diagnostic.severity.ERROR },
648+
signs = vim.g.have_nerd_font and {
649+
text = diagnostic_icons,
650+
} or {},
651+
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+
658+
virtual_text = enable and {
659+
source = 'if_many',
660+
spacing = 2,
661+
format = function(diagnostic)
662+
return (diagnostic_icons[diagnostic.severity] or '') .. diagnostic.message
663+
end,
664+
} or false,
665+
}
666+
end
667+
668+
vim.api.nvim_create_autocmd('InsertEnter', {
669+
callback = function()
670+
set_virtual_text(true)
671+
end,
672+
})
673+
674+
vim.api.nvim_create_autocmd('InsertLeave', {
675+
callback = function()
676+
set_virtual_text(false)
677+
end,
678+
})
657679

658680
-- LSP servers and clients are able to communicate to each other what features they support.
659681
-- By default, Neovim doesn't support everything that is in the LSP specification.

0 commit comments

Comments
 (0)