Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to show the most severe diagnostics per line #1579

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions autoload/lsp/internal/diagnostics/virtual_text.vim
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,13 @@ function! s:set_virtual_text(params) abort
endfunction

function! s:place_virtual_text(server, diagnostics_response, bufnr) abort
let l:line_props = {}

let l:linecount = s:Buffer.get_line_count(a:bufnr)
for l:item in lsp#utils#iteratable(a:diagnostics_response['params']['diagnostics'])
let l:line = lsp#utils#position#lsp_line_to_vim(a:bufnr, l:item['range']['start'])
let l:name = get(s:severity_sign_names_mapping, get(l:item, 'severity', 3), 'LspError')
let l:severity = get(l:item, 'severity', 3)
let l:name = get(s:severity_sign_names_mapping, l:severity, 'LspError')
let l:text = g:lsp_diagnostics_virtual_text_prefix . l:item['message']

" Some language servers report an unexpected EOF one line past the end
Expand All @@ -179,16 +182,30 @@ function! s:place_virtual_text(server, diagnostics_response, bufnr) abort
" it's an error to add virtual text on lines that don't exist
" anymore due to async processing, just skip such diagnostics
if l:line <= l:linecount
if g:lsp_diagnostics_virtual_text_tidy && has_key(l:line_props, l:line)
" Replace the existing virtual text with the one that has higher severity
if l:severity <= l:line_props[l:line]['severity']
call prop_remove({'id': l:line_props[l:line]['prop_id']}, l:line)
else
continue
endif
endif

let l:type = 'vim_lsp_' . l:name . '_virtual_text'
call prop_remove({'all': v:true, 'type': l:type, 'bufnr': a:bufnr}, l:line)
call prop_add(
let l:prop_id = prop_add(
\ l:line, 0,
\ {
\ 'type': l:type, 'text': l:text, 'bufnr': a:bufnr,
\ 'text_align': g:lsp_diagnostics_virtual_text_align,
\ 'text_padding_left': g:lsp_diagnostics_virtual_text_padding_left,
\ 'text_wrap': g:lsp_diagnostics_virtual_text_wrap,
\ })

let l:line_props[l:line] = {
\ 'prop_id': l:prop_id,
\ 'severity': l:severity,
\ }
endif
endif
endfor
Expand Down
16 changes: 16 additions & 0 deletions doc/vim-lsp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ CONTENTS *vim-lsp-contents*
|g:lsp_diagnostics_virtual_text_padding_left|
g:lsp_diagnostics_virtual_text_wrap
|g:lsp_diagnostics_virtual_text_wrap|
g:lsp_diagnostics_virtual_text_tidy
|g:lsp_diagnostics_virtual_text_tidy|
g:lsp_document_code_action_signs_enabled
|g:lsp_document_code_action_signs_enabled|
g:lsp_document_code_action_signs_delay
Expand Down Expand Up @@ -785,6 +787,20 @@ g:lsp_diagnostics_virtual_text_wrap *g:lsp_diagnostics_virtual_text_wrap*
Example: >
let g:lsp_diagnostics_virtual_text_wrap = "truncate"

g:lsp_diagnostics_virtual_text_tidy
*g:lsp_diagnostics_virtual_text_tidy*
Type: |Boolean|
Default: `0`

Determines whether or not to show only the diagnostic virtual text
with the highest severity per line. Requires
|g:lsp_diagnostics_virtual_text_enabled|.
This prevents unintentional newlines in the view inserted by diagnostics
messages.

Example: >
let g:lsp_diagnostics_virtual_text_tidy = 0

g:lsp_document_code_action_signs_enabled
*g:lsp_document_code_action_signs_enabled*
Type: |Number|
Expand Down
1 change: 1 addition & 0 deletions plugin/lsp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ let g:lsp_diagnostics_virtual_text_prefix = get(g:, 'lsp_diagnostics_virtual_tex
let g:lsp_diagnostics_virtual_text_align = get(g:, 'lsp_diagnostics_virtual_text_align', 'below')
let g:lsp_diagnostics_virtual_text_wrap = get(g:, 'lsp_diagnostics_virtual_text_wrap', 'wrap')
let g:lsp_diagnostics_virtual_text_padding_left = get(g:, 'lsp_diagnostics_virtual_text_padding_left', 1)
let g:lsp_diagnostics_virtual_text_tidy = get(g:, 'lsp_diagnostics_virtual_text_tidy', 0)

let g:lsp_document_code_action_signs_enabled = get(g:, 'lsp_document_code_action_signs_enabled', 1)
let g:lsp_document_code_action_signs_delay = get(g:, 'lsp_document_code_action_signs_delay', 500)
Expand Down