Skip to content
\n

…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.

\n

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.

\n
autocmd User lsp_setup call lsp#register_server({\n    \\ 'name': 'your_language_server',\n    \\ 'cmd': {server_info->['path/to/your/language-server']},\n    \"\n    \\ 'on_diagnostics': function('s:on_diagnostics'),\n    \\ })\n\nfunction! s:on_diagnostics(_, params)\n    \" Get the diagnostics\n    let l:diagnostics = params['diagnostics']\n    for l:diagnostic in l:diagnostics\n        \" Adjust the position to the end of the line\n        let l:line = l:diagnostic['range']['end']['line']\n        let l:char = l:diagnostic['range']['end']['character']\n        let l:message = l:diagnostic['message']\n        \" Display virtual text at the end of the line\n        call nvim_buf_set_virtual_text(0, -1, l:line, [[l:message]], {})\n    endfor\nendfunction
\n

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 s:ondiagnostics to every language for which a language server is available.

","upvoteCount":1,"answerCount":2,"acceptedAnswer":{"@type":"Answer","text":"

The solution is—you guessed it—in the help. The default alignment for virtual text is below. From the docs as of this writing:

\n
g:lsp_diagnostics_virtual_text_align    g:lsp_diagnostics_virtual_text_align\n    Type: String\n    Default: \"below\"\n\n    Determines the align of the diagnostics virtual text. Requires\n    g:lsp_diagnostics_virtual_text_enabled.\n\n    Possible values are:\n\n       after   after the end of the line\n       right   right aligned in the window (unless the text wraps to the next\n               screen line)\n       below   in the next screen line\n       above   just above the line\n\n    Only one \"right\" property can fit in each line, if there are two or more\n    these will go in a separate line (still right aligned).\n\n    This value is passed as the \"text_align\" property in a prop_add() call.\n\n    Example:\n        let g:lsp_diagnostics_virtual_text_align = \"right\"
\n

Solution

\n

Add this to your ~/.vimrc, inside the augroup lsp_install that you presumably copied from the README:

\n
augroup lsp_install\n    au!\n    \" call s:on_lsp_buffer_enabled only for languages that has the server registered.\n    autocmd User lsp_buffer_enabled call s:on_lsp_buffer_enabled()\n\n    \" see: https://github.com/prabirshrestha/vim-lsp/discussions/1582\n    \" options are: after, right, below(*), above  (*default)\n    let g:lsp_diagnostics_virtual_text_align = 'right'\naugroup END
","upvoteCount":1,"url":"https://github.com/prabirshrestha/vim-lsp/discussions/1582#discussioncomment-10887587"}}}

Displaying virtual text at the end of the line, rather than below the error #1582

Answered by ernstki
ernstki asked this question in Q&A
Discussion options

You must be logged in to vote

The solution is—you guessed it—in the help. The default alignment for virtual text is below. From the docs as of this writing:

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…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by ernstki
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant