Skip to content

Commit f02bf20

Browse files
tsufekiprabirshrestha
authored andcommitted
Expose diagnostics count for current buffer. (#376)
* Expose diagnostics count for current buffer. * Add lsp#get_buffer_first_error_line()
1 parent 2b583fe commit f02bf20

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

autoload/lsp.vim

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,3 +783,14 @@ function! s:send_didchange_queue(...) abort
783783
endfor
784784
let s:didchange_queue = []
785785
endfunction
786+
787+
" Return dict with diagnostic counts for current buffer
788+
" { 'error': 1, 'warning': 0, 'information': 0, 'hint': 0 }
789+
function! lsp#get_buffer_diagnostics_counts() abort
790+
return lsp#ui#vim#diagnostics#get_buffer_diagnostics_counts()
791+
endfunction
792+
793+
" Return first error line or v:null if there are no errors
794+
function! lsp#get_buffer_first_error_line() abort
795+
return lsp#ui#vim#diagnostics#get_buffer_first_error_line()
796+
endfunction

autoload/lsp/ui/vim/diagnostics.vim

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ function! s:get_diagnostics(uri) abort
185185
endif
186186
endif
187187
endif
188-
return [0, []]
188+
return [0, {}]
189189
endfunction
190190

191191
" Get diagnostics for the current buffer URI from all servers
@@ -219,4 +219,43 @@ function! s:compare_diagnostics(d1, d2) abort
219219
return l:line1 > l:line2 ? 1 : -1
220220
endif
221221
endfunction
222+
223+
let s:diagnostic_kinds = {
224+
\ 1: 'error',
225+
\ 2: 'warning',
226+
\ 3: 'information',
227+
\ 4: 'hint',
228+
\ }
229+
230+
function! lsp#ui#vim#diagnostics#get_buffer_diagnostics_counts() abort
231+
let l:counts = {
232+
\ 'error': 0,
233+
\ 'warning': 0,
234+
\ 'information': 0,
235+
\ 'hint': 0,
236+
\ }
237+
let l:uri = lsp#utils#get_buffer_uri()
238+
let [l:has_diagnostics, l:diagnostics] = s:get_diagnostics(l:uri)
239+
for [l:server_name, l:data] in items(l:diagnostics)
240+
for l:diag in l:data['response']['params']['diagnostics']
241+
let l:key = get(s:diagnostic_kinds, l:diag['severity'], 'error')
242+
let l:counts[l:key] += 1
243+
endfor
244+
endfor
245+
return l:counts
246+
endfunction
247+
248+
function! lsp#ui#vim#diagnostics#get_buffer_first_error_line() abort
249+
let l:uri = lsp#utils#get_buffer_uri()
250+
let [l:has_diagnostics, l:diagnostics] = s:get_diagnostics(l:uri)
251+
let l:first_error_line = v:null
252+
for [l:server_name, l:data] in items(l:diagnostics)
253+
for l:diag in l:data['response']['params']['diagnostics']
254+
if l:diag['severity'] ==# 1 && (l:first_error_line ==# v:null || l:first_error_line ># l:diag['range']['start']['line'])
255+
let l:first_error_line = l:diag['range']['start']['line']
256+
endif
257+
endfor
258+
endfor
259+
return l:first_error_line ==# v:null ? v:null : l:first_error_line + 1
260+
endfunction
222261
" vim sw=4 ts=4 et

doc/vim-lsp.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ CONTENTS *vim-lsp-contents*
3434
stop_server |vim-lsp-stop_server|
3535
utils#find_nearest_parent_file_directory
3636
|vim-lsp-utils-find_nearest_parent_file_directory|
37+
vim-lsp-get_buffer_diagnostics_counts |vim-lsp-get_buffer_diagnostics_counts|
38+
vim-lsp-get_buffer_first_error_line |vim-lsp-get_buffer_first_error_line|
3739
Commands |vim-lsp-commands|
3840
LspCodeAction |LspCodeAction|
3941
LspDocumentDiagnostics |LspDocumentDiagnostics|
@@ -584,6 +586,19 @@ This method is mainly used to generate 'root_uri' when registering server.
584586
* If there is not directory with the specific files or diretories
585587
found, the method will return an empty string.
586588

589+
lsp#get_buffer_diagnostics_counts *vim-lsp-get_buffer_diagnostics_counts*
590+
591+
Get dict with diagnostic counts for current buffer. Useful e.g. for display
592+
in status line.
593+
594+
Returns dictionary with keys "error", "warning", "information", "hint".
595+
596+
lsp#get_buffer_first_error_line *vim-lsp-get_buffer_first_error_line*
597+
598+
Get line number of first error in current buffer.
599+
600+
Returns |Number| or |v:null| if there are no errors.
601+
587602
===============================================================================
588603
Commands *vim-lsp-commands*
589604

0 commit comments

Comments
 (0)