Skip to content

Commit 0985092

Browse files
refactor diagnostics count to using diagnostics state (#999)
1 parent 2eb8d16 commit 0985092

File tree

4 files changed

+29
-63
lines changed

4 files changed

+29
-63
lines changed

autoload/lsp.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1120,7 +1120,7 @@ endfunction
11201120
" Return dict with diagnostic counts for current buffer
11211121
" { 'error': 1, 'warning': 0, 'information': 0, 'hint': 0 }
11221122
function! lsp#get_buffer_diagnostics_counts() abort
1123-
return lsp#ui#vim#diagnostics#get_buffer_diagnostics_counts()
1123+
return lsp#internal#diagnostics#state#_get_diagnostics_count_for_buffer(bufnr('%'))
11241124
endfunction
11251125

11261126
" Return first error line or v:null if there are no errors

autoload/lsp/internal/diagnostics/state.vim

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ let s:diagnostics_state = {}
2323
" internal state for whether it is enabled or not to avoid multiple subscriptions
2424
let s:enabled = 0
2525

26+
let s:diagnostic_kinds = {
27+
\ 1: 'error',
28+
\ 2: 'warning',
29+
\ 3: 'information',
30+
\ 4: 'hint',
31+
\ }
32+
2633
function! lsp#internal#diagnostics#state#_enable() abort
2734
" don't even bother registering if the feature is disabled
2835
if !g:lsp_diagnostics_enabled | return | endif
@@ -144,3 +151,24 @@ endfunction
144151
function! lsp#internal#diagnostics#state#_is_enabled_for_buffer(bufnr) abort
145152
return getbufvar(a:bufnr, 'lsp_diagnostics_enabled', 1) == 1
146153
endfunction
154+
155+
" Return dict with diagnostic counts for the specified buffer
156+
" { 'error': 1, 'warning': 0, 'information': 0, 'hint': 0 }
157+
function! lsp#internal#diagnostics#state#_get_diagnostics_count_for_buffer(bufnr) abort
158+
let l:counts = {
159+
\ 'error': 0,
160+
\ 'warning': 0,
161+
\ 'information': 0,
162+
\ 'hint': 0,
163+
\ }
164+
if lsp#internal#diagnostics#state#_is_enabled_for_buffer(a:bufnr)
165+
let l:uri = lsp#utils#get_buffer_uri(a:bufnr)
166+
for [l:_, l:response] in items(lsp#internal#diagnostics#state#_get_all_diagnostics_grouped_by_server_for_uri(l:uri))
167+
for l:diagnostic in l:response['params']['diagnostics']
168+
let l:key = get(s:diagnostic_kinds, get(l:diagnostic, 'severity', 1) , 'error')
169+
let l:counts[l:key] += 1
170+
endfor
171+
endfor
172+
end
173+
return l:counts
174+
endfunction

autoload/lsp/ui/vim/diagnostics.vim

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ function! lsp#ui#vim#diagnostics#handle_text_document_publish_diagnostics(server
1515
doautocmd <nomodeline> User lsp_diagnostics_updated
1616
endfunction
1717

18-
function! lsp#ui#vim#diagnostics#get_document_diagnostics(bufnr) abort
19-
return get(s:diagnostics, lsp#utils#get_buffer_uri(a:bufnr), {})
20-
endfunction
21-
2218
function! s:severity_of(diagnostic) abort
2319
return get(a:diagnostic, 'severity', 1)
2420
endfunction
@@ -213,31 +209,6 @@ function! s:compare_diagnostics(d1, d2) abort
213209
endif
214210
endfunction
215211

216-
let s:diagnostic_kinds = {
217-
\ 1: 'error',
218-
\ 2: 'warning',
219-
\ 3: 'information',
220-
\ 4: 'hint',
221-
\ }
222-
223-
function! lsp#ui#vim#diagnostics#get_buffer_diagnostics_counts() abort
224-
let l:counts = {
225-
\ 'error': 0,
226-
\ 'warning': 0,
227-
\ 'information': 0,
228-
\ 'hint': 0,
229-
\ }
230-
let l:uri = lsp#utils#get_buffer_uri()
231-
let [l:has_diagnostics, l:diagnostics] = s:get_diagnostics(l:uri)
232-
for [l:server_name, l:data] in items(l:diagnostics)
233-
for l:diag in l:data['response']['params']['diagnostics']
234-
let l:key = get(s:diagnostic_kinds, s:severity_of(l:diag), 'error')
235-
let l:counts[l:key] += 1
236-
endfor
237-
endfor
238-
return l:counts
239-
endfunction
240-
241212
function! lsp#ui#vim#diagnostics#get_buffer_first_error_line() abort
242213
let l:uri = lsp#utils#get_buffer_uri()
243214
let [l:has_diagnostics, l:diagnostics] = s:get_diagnostics(l:uri)

test/lsp/ui/vim/diagnostics.vimspec

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)