-
I'm using Vim 9.1 from MacPorts, and this seems like a thing that should be possible with an LSP plugin. But is it actually doable with Vim 9.x, without a lot of crazy configuration? I didn't notice any mention of it in the vim-lsp docs, or in the discussions, and the only examples I've seen in the wild are people using NeoVim or LunarVim (which is just NeoVim). The vim-lsp docs say this: g:lsp_diagnostics_virtual_text_enabled
Type: Number
Default: 1 for neovim 0.3+
Enables virtual text to be shown next to diagnostic errors. Requires
NeoVim with version 0.3 or newer or Vim with virtual-text and
patch 9.0.0178, and g:lsp_diagnostics_enabled set to 1.
Virtual text uses the same highlight groups used for signs (eg LspErrorText),
but can be uniquely defined if you want to have different highlight groups
for signs and virtual text. To set unique virtual text highlighting, you
can set or link LspErrorVirtualText, LspWarningVirtualText,
LspInformationVirtualText and LspHintVirtualText highlight groups. …and my Vim seems to meet all those criteria, but I still get diagnostics below the errors, not to the right of them. Even with all the work that I presume went into debouncing, this just isn't a good experience when you're editing lines below some active errors. I did a decent amount of using my human brain and looking around before resorting to asking a GPT for help, and here's what it came up with. autocmd User lsp_setup call lsp#register_server({
\ 'name': 'your_language_server',
\ 'cmd': {server_info->['path/to/your/language-server']},
" ⋮
\ 'on_diagnostics': function('s:on_diagnostics'),
\ })
function! s:on_diagnostics(_, params)
" Get the diagnostics
let l:diagnostics = params['diagnostics']
for l:diagnostic in l:diagnostics
" Adjust the position to the end of the line
let l:line = l:diagnostic['range']['end']['line']
let l:char = l:diagnostic['range']['end']['character']
let l:message = l:diagnostic['message']
" Display virtual text at the end of the line
call nvim_buf_set_virtual_text(0, -1, l:line, [[l:message]], {})
endfor
endfunction Seems plausible, and it appears to be error-free (at least my Vim doesn't crash on startup). However, the question I'm left with is how do I apply that |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I even read vim/vim#7553 as far as I could, but it got over my head pretty quick. |
Beta Was this translation helpful? Give feedback.
-
The solution is—you guessed it—in the help. The default alignment for virtual text is g:lsp_diagnostics_virtual_text_align g:lsp_diagnostics_virtual_text_align
Type: String
Default: "below"
Determines the align of the diagnostics virtual text. Requires
g:lsp_diagnostics_virtual_text_enabled.
Possible values are:
after after the end of the line
right right aligned in the window (unless the text wraps to the next
screen line)
below in the next screen line
above just above the line
Only one "right" property can fit in each line, if there are two or more
these will go in a separate line (still right aligned).
This value is passed as the "text_align" property in a prop_add() call.
Example:
let g:lsp_diagnostics_virtual_text_align = "right" SolutionAdd this to your augroup lsp_install
au!
" call s:on_lsp_buffer_enabled only for languages that has the server registered.
autocmd User lsp_buffer_enabled call s:on_lsp_buffer_enabled()
" see: https://github.com/prabirshrestha/vim-lsp/discussions/1582
" options are: after, right, below(*), above (*default)
let g:lsp_diagnostics_virtual_text_align = 'right'
augroup END |
Beta Was this translation helpful? Give feedback.
The solution is—you guessed it—in the help. The default alignment for virtual text is
below
. From the docs as of this writing: