Skip to content

Commit eac60f1

Browse files
committed
Prioritize the most severe message per line
1 parent 04428c9 commit eac60f1

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

autoload/lsp/internal/diagnostics/virtual_text.vim

+19-2
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,13 @@ function! s:set_virtual_text(params) abort
159159
endfunction
160160

161161
function! s:place_virtual_text(server, diagnostics_response, bufnr) abort
162+
let l:line_props = {}
163+
162164
let l:linecount = s:Buffer.get_line_count(a:bufnr)
163165
for l:item in lsp#utils#iteratable(a:diagnostics_response['params']['diagnostics'])
164166
let l:line = lsp#utils#position#lsp_line_to_vim(a:bufnr, l:item['range']['start'])
165-
let l:name = get(s:severity_sign_names_mapping, get(l:item, 'severity', 3), 'LspError')
167+
let l:severity = get(l:item, 'severity', 3)
168+
let l:name = get(s:severity_sign_names_mapping, l:severity, 'LspError')
166169
let l:text = g:lsp_diagnostics_virtual_text_prefix . l:item['message']
167170

168171
" Some language servers report an unexpected EOF one line past the end
@@ -179,16 +182,30 @@ function! s:place_virtual_text(server, diagnostics_response, bufnr) abort
179182
" it's an error to add virtual text on lines that don't exist
180183
" anymore due to async processing, just skip such diagnostics
181184
if l:line <= l:linecount
185+
if g:lsp_diagnostics_virtual_text_tidy && has_key(l:line_props, l:line)
186+
" Replace the existing virtual text with the one that has higher severity
187+
if l:severity <= l:line_props[l:line]['severity']
188+
call prop_remove({'id': l:line_props[l:line]['prop_id']}, l:line)
189+
else
190+
continue
191+
endif
192+
endif
193+
182194
let l:type = 'vim_lsp_' . l:name . '_virtual_text'
183195
call prop_remove({'all': v:true, 'type': l:type, 'bufnr': a:bufnr}, l:line)
184-
call prop_add(
196+
let l:prop_id = prop_add(
185197
\ l:line, 0,
186198
\ {
187199
\ 'type': l:type, 'text': l:text, 'bufnr': a:bufnr,
188200
\ 'text_align': g:lsp_diagnostics_virtual_text_align,
189201
\ 'text_padding_left': g:lsp_diagnostics_virtual_text_padding_left,
190202
\ 'text_wrap': g:lsp_diagnostics_virtual_text_wrap,
191203
\ })
204+
205+
let l:line_props[l:line] = {
206+
\ 'prop_id': l:prop_id,
207+
\ 'severity': l:severity,
208+
\ }
192209
endif
193210
endif
194211
endfor

doc/vim-lsp.txt

+16
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ CONTENTS *vim-lsp-contents*
5757
|g:lsp_diagnostics_virtual_text_padding_left|
5858
g:lsp_diagnostics_virtual_text_wrap
5959
|g:lsp_diagnostics_virtual_text_wrap|
60+
g:lsp_diagnostics_virtual_text_tidy
61+
|g:lsp_diagnostics_virtual_text_tidy|
6062
g:lsp_document_code_action_signs_enabled
6163
|g:lsp_document_code_action_signs_enabled|
6264
g:lsp_document_code_action_signs_delay
@@ -785,6 +787,20 @@ g:lsp_diagnostics_virtual_text_wrap *g:lsp_diagnostics_virtual_text_wrap*
785787
Example: >
786788
let g:lsp_diagnostics_virtual_text_wrap = "truncate"
787789
790+
g:lsp_diagnostics_virtual_text_tidy
791+
*g:lsp_diagnostics_virtual_text_tidy*
792+
Type: |Boolean|
793+
Default: `0`
794+
795+
Determines whether or not to show only the diagnostic virtual text
796+
with the highest severity per line. Requires
797+
|g:lsp_diagnostics_virtual_text_enabled|.
798+
This prevents unintentional newlines in the view inserted by diagnostics
799+
messages.
800+
801+
Example: >
802+
let g:lsp_diagnostics_virtual_text_tidy = 0
803+
788804
g:lsp_document_code_action_signs_enabled
789805
*g:lsp_document_code_action_signs_enabled*
790806
Type: |Number|

plugin/lsp.vim

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ let g:lsp_diagnostics_virtual_text_prefix = get(g:, 'lsp_diagnostics_virtual_tex
4141
let g:lsp_diagnostics_virtual_text_align = get(g:, 'lsp_diagnostics_virtual_text_align', 'below')
4242
let g:lsp_diagnostics_virtual_text_wrap = get(g:, 'lsp_diagnostics_virtual_text_wrap', 'wrap')
4343
let g:lsp_diagnostics_virtual_text_padding_left = get(g:, 'lsp_diagnostics_virtual_text_padding_left', 1)
44+
let g:lsp_diagnostics_virtual_text_tidy = get(g:, 'lsp_diagnostics_virtual_text_tidy', 0)
4445

4546
let g:lsp_document_code_action_signs_enabled = get(g:, 'lsp_document_code_action_signs_enabled', 1)
4647
let g:lsp_document_code_action_signs_delay = get(g:, 'lsp_document_code_action_signs_delay', 500)

0 commit comments

Comments
 (0)