{"id":2052,"date":"2022-05-26T20:20:29","date_gmt":"2022-05-26T14:50:29","guid":{"rendered":"https:\/\/smarttech101.com\/?p=2052"},"modified":"2023-03-31T17:35:11","modified_gmt":"2023-03-31T12:05:11","slug":"nvim-lsp-diagnostics-keybindings-signs-virtual-texts","status":"publish","type":"post","link":"https:\/\/smarttech101.com\/nvim-lsp-diagnostics-keybindings-signs-virtual-texts\/","title":{"rendered":"Nvim lsp diagnostics – keybindings, signs, virtual texts"},"content":{"rendered":"\n
Nvim lsp diagnostics (also known as “linting”) enables you to see Errors, Warnings, Hints, and Informations right on your screen while coding<\/strong>. It, therefore, prevents you from having most of the debugging headaches later.<\/p>\n\n\n\n \ud83d\udcd3 Note: Follow this article step by step to avoid any problems.<\/strong><\/p>\n\n\n\n If have not set up basic configuration in Neovim, do that over here<\/a>. This article is part of my series on setting up Nvim Lsp (Neovim Language Server Protocol):<\/p>\n\n\n\n I recommend you to follow all three articles in the above order.<\/p>\n\n\n\n Now, we will set up key bindings to move from one error\/warning to another, and open all errors in a new window.<\/p>\n\n\n\n Create a file Here,<\/p>\n\n\n\n “Severity signs” are signs for severity levels of problems in your code. By default, they are Note: you might not see the above signs in your browser because these signs are taken from the nerd fonts. Therefore, to see them, you need to use the nerd fonts in your terminal.<\/strong><\/p>\n\n\n\n The message area in neovim is the area you see at the bottom<\/strong> (Figure 2). If you want to print error\/warning\/hints\/information in the message area when your cursor is on any line having them, put the following lines in the Personally, I don’t use it because Virtual Text<\/strong>: is the text you see after \u25cf. Two \u25cf means two diagnostics info and so on.<\/p>\n\n\n\n Floating Text<\/strong>: is the text you will see in a floating window after pressing the shortcut Put the following configuration in the file Here,<\/p>\n\n\n\n Now, source this file in your \ud83d\udcd3Note: depending upon your project size and computer’s processing power, you might have to wait for some time before all the virtual texts are printed on your neovim window.<\/p>\n\n\n\n\n
Table of Contents<\/h2>\n\n\n\n
\n
Key bindings in nvim lsp diagnostics<\/h2>\n\n\n\n
~\/.config\/nvim\/plug-config\/diagnostics.lua<\/code> with the following content in it:<\/p>\n\n\n\n
vim.api.nvim_set_keymap('n', '<leader>do', '<cmd>lua vim.diagnostic.open_float()<CR>', { noremap = true, silent = true })\nvim.api.nvim_set_keymap('n', '<leader>d[', '<cmd>lua vim.diagnostic.goto_prev()<CR>', { noremap = true, silent = true })\nvim.api.nvim_set_keymap('n', '<leader>d]', '<cmd>lua vim.diagnostic.goto_next()<CR>', { noremap = true, silent = true })\n-- The following command requires plug-ins \"nvim-telescope\/telescope.nvim\", \"nvim-lua\/plenary.nvim\", and optionally \"kyazdani42\/nvim-web-devicons\" for icon support\nvim.api.nvim_set_keymap('n', '<leader>dd', '<cmd>Telescope diagnostics<CR>', { noremap = true, silent = true })\n-- If you don't want to use the telescope plug-in but still want to see all the errors\/warnings, comment out the telescope line and uncomment this:\n-- vim.api.nvim_set_keymap('n', '<leader>dd', '<cmd>lua vim.diagnostic.setloclist()<CR>', { noremap = true, silent = true })<\/code><\/pre>\n\n\n\n
\n
<leader>d[<\/code> to move to the next warning\/error.<\/li>\n\n\n\n
<leader>d]<\/code> to Go to previous warning\/error.<\/li>\n\n\n\n
<leader>dd<\/code> for the error\/warnings in a telescope window (see figure 1).<\/li>\n\n\n\n
--<\/code>” is used to comment out any line in a Lua file.<\/li>\n<\/ul>\n\n\n\n
Severity signs in nvim lsp diagnostics<\/h2>\n\n\n\n
E<\/code> for Error,
W<\/code> for Warning,
H<\/code> for Hints,
I<\/code> for Informations. <\/strong>They are shown in the sign column on the left-most side<\/strong> (see figure 1). To change them, use the following lines into
~\/.config\/nvim\/plug-config\/diagnostics.lua<\/code>:<\/p>\n\n\n\n
local signs = { Error = \"\uf659 \", Warn = \"\uf529 \", Hint = \"\uf835 \", Info = \"\uf449 \" }\nfor type, icon in pairs(signs) do\n local hl = \"DiagnosticSign\" .. type\n vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = hl })\nend<\/code><\/pre>\n\n\n\n
Print diagnostics in the message area<\/h2>\n\n\n\n
~\/.config\/nvim\/plug-config\/diagnostics.lua<\/code>:<\/p>\n\n\n\n
function PrintDiagnostics(opts, bufnr, line_nr, client_id)\n bufnr = bufnr or 0\n line_nr = line_nr or (vim.api.nvim_win_get_cursor(0)[1] - 1)\n opts = opts or {['lnum'] = line_nr}\n\n local line_diagnostics = vim.diagnostic.get(bufnr, opts)\n if vim.tbl_isempty(line_diagnostics) then return end\n\n local diagnostic_message = \"\"\n for i, diagnostic in ipairs(line_diagnostics) do\n diagnostic_message = diagnostic_message .. string.format(\"%d: %s\", i, diagnostic.message or \"\")\n print(diagnostic_message)\n if i ~= #line_diagnostics then\n diagnostic_message = diagnostic_message .. \"\\n\"\n end\n end\n vim.api.nvim_echo({{diagnostic_message, \"Normal\"}}, false, {})\nend\nvim.cmd [[ autocmd! CursorHold * lua PrintDiagnostics() ]]<\/code><\/pre>\n\n\n\n
<leader>do<\/code> is already doing the same task.<\/p>\n\n\n\n
Configuration of virtual and floating text<\/h2>\n\n\n\n
<leader>do<\/code>.<\/p>\n\n\n\n
~\/.config\/nvim\/plug-config\/diagnostics.lua<\/code>:<\/p>\n\n\n\n
vim.diagnostic.config({\n virtual_text = {\n -- source = \"always\", -- Or \"if_many\"\n prefix = '\u25cf', -- Could be '\u25a0', '\u258e', 'x'\n },\n severity_sort = true,\n float = {\n source = \"always\", -- Or \"if_many\"\n },\n})<\/code><\/pre>\n\n\n\n
\n
severity_sort = true<\/code> sets the order in which signs and virtual text are displayed.<\/li>\n\n\n\n
virtual_text = false<\/code> instead to remove Virtual Text altogether from your Neovim window.<\/li>\n\n\n\n
prefix<\/code> sets character preceding the virtual text.<\/li>\n\n\n\n
source = always<\/code> shows diagnostic-source which can be a language server (like
pyright<\/code> for python) in the following figure (Figure 5). Use,
source = \"if_many\"<\/code> if you want to see the source only when the number of sources is more than one.<\/li>\n<\/ul>\n\n\n\n
mypy<\/code>,
pyright<\/code>, etc).<\/figcaption><\/figure>\n\n\n\n
init.vim<\/code>:<\/p>\n\n\n\n
source $HOME\/.config\/nvim\/plug-config\/diagnostics.lua<\/code><\/pre>\n\n\n\n