Skip to content

Commit 268341f

Browse files
Diagnostics refactor movements (#1004)
1 parent 7c43b01 commit 268341f

File tree

4 files changed

+45
-58
lines changed

4 files changed

+45
-58
lines changed

autoload/lsp.vim

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ function! s:on_text_document_did_open(...) abort
229229
call lsp#log('s:on_text_document_did_open()', l:buf, &filetype, getcwd(), lsp#utils#get_buffer_uri(l:buf))
230230

231231
" Some language server notify diagnostics to the buffer that has not been loaded yet.
232-
" This diagnostics was stored `autoload/lsp/ui/vim/diagnostics.vim` but not highlighted.
232+
" This diagnostics was stored `autoload/lsp/internal/diagnostics/state.vim` but not highlighted.
233233
" So we should refresh highlights when buffer opened.
234234
call lsp#internal#diagnostics#state#_force_notify_buffer(l:buf)
235235

@@ -776,7 +776,6 @@ function! s:on_notification(server_name, id, data, event) abort
776776
let l:response = a:data['response']
777777
let l:server = s:servers[a:server_name]
778778
let l:server_info = l:server['server_info']
779-
let l:lsp_diagnostics_config_enabled = get(get(l:server_info, 'config', {}), 'diagnostics', v:true)
780779

781780
let l:stream_data = { 'server': a:server_name, 'response': l:response }
782781
if has_key(a:data, 'request')
@@ -786,11 +785,10 @@ function! s:on_notification(server_name, id, data, event) abort
786785

787786
if lsp#client#is_server_instantiated_notification(a:data)
788787
if has_key(l:response, 'method')
789-
if g:lsp_diagnostics_enabled && l:lsp_diagnostics_config_enabled && l:response['method'] ==# 'textDocument/publishDiagnostics'
790-
call lsp#ui#vim#diagnostics#handle_text_document_publish_diagnostics(a:server_name, a:data)
791-
elseif l:response['method'] ==# 'textDocument/semanticHighlighting'
788+
if l:response['method'] ==# 'textDocument/semanticHighlighting'
792789
call lsp#ui#vim#semantic#handle_semantic(a:server_name, a:data)
793790
endif
791+
" NOTE: this is legacy code, use stream instead of handling notifications here
794792
endif
795793
else
796794
let l:request = a:data['request']

autoload/lsp/ui/vim/diagnostics.vim renamed to autoload/lsp/internal/diagnostics/movement.vim

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,22 @@
1-
let s:is_win = has('win32') || has('win64')
2-
let s:diagnostics = {} " { uri: { 'server_name': response } }
3-
4-
function! lsp#ui#vim#diagnostics#handle_text_document_publish_diagnostics(server_name, data) abort
5-
if lsp#client#is_error(a:data['response'])
6-
return
7-
endif
8-
let l:uri = a:data['response']['params']['uri']
9-
let l:uri = lsp#utils#normalize_uri(l:uri)
10-
if !has_key(s:diagnostics, l:uri)
11-
let s:diagnostics[l:uri] = {}
12-
endif
13-
let s:diagnostics[l:uri][a:server_name] = a:data
14-
15-
doautocmd <nomodeline> User lsp_diagnostics_updated
16-
endfunction
17-
181
function! s:severity_of(diagnostic) abort
192
return get(a:diagnostic, 'severity', 1)
203
endfunction
214

22-
function! lsp#ui#vim#diagnostics#next_error(...) abort
5+
function! lsp#internal#diagnostics#movement#_next_error(...) abort
236
let l:diagnostics = filter(s:get_all_buffer_diagnostics(),
247
\ {_, diagnostic -> s:severity_of(diagnostic) ==# 1 })
258
let l:options = lsp#utils#parse_command_options(a:000)
269
call s:next_diagnostic(l:diagnostics, l:options)
2710
endfunction
2811

29-
function! lsp#ui#vim#diagnostics#next_warning(...) abort
12+
function! lsp#internal#diagnostics#movement#_next_warning(...) abort
3013
let l:diagnostics = filter(s:get_all_buffer_diagnostics(),
3114
\ {_, diagnostic -> s:severity_of(diagnostic) ==# 2 })
3215
let l:options = lsp#utils#parse_command_options(a:000)
3316
call s:next_diagnostic(l:diagnostics, l:options)
3417
endfunction
3518

36-
function! lsp#ui#vim#diagnostics#next_diagnostic(...) abort
19+
function! lsp#internal#diagnostics#movement#_next_diagnostics(...) abort
3720
let l:options = lsp#utils#parse_command_options(a:000)
3821
call s:next_diagnostic(s:get_all_buffer_diagnostics(), l:options)
3922
endfunction
@@ -87,21 +70,21 @@ function! s:next_diagnostic(diagnostics, options) abort
8770
call winrestview(l:view)
8871
endfunction
8972

90-
function! lsp#ui#vim#diagnostics#previous_error(...) abort
73+
function! lsp#internal#diagnostics#movement#_previous_error(...) abort
9174
let l:diagnostics = filter(s:get_all_buffer_diagnostics(),
9275
\ {_, diagnostic -> s:severity_of(diagnostic) ==# 1 })
9376
let l:options = lsp#utils#parse_command_options(a:000)
9477
call s:previous_diagnostic(l:diagnostics, l:options)
9578
endfunction
9679

97-
function! lsp#ui#vim#diagnostics#previous_warning(...) abort
80+
function! lsp#internal#diagnostics#movement#_previous_warning(...) abort
9881
let l:options = lsp#utils#parse_command_options(a:000)
9982
let l:diagnostics = filter(s:get_all_buffer_diagnostics(),
10083
\ {_, diagnostic -> s:severity_of(diagnostic) ==# 2 })
10184
call s:previous_diagnostic(l:diagnostics, l:options)
10285
endfunction
10386

104-
function! lsp#ui#vim#diagnostics#previous_diagnostic(...) abort
87+
function! lsp#internal#diagnostics#movement#_previous_diagnostics(...) abort
10588
let l:options = lsp#utils#parse_command_options(a:000)
10689
call s:previous_diagnostic(s:get_all_buffer_diagnostics(), l:options)
10790
endfunction
@@ -175,23 +158,30 @@ endfunction
175158

176159
" Get diagnostics for the current buffer URI from all servers
177160
function! s:get_all_buffer_diagnostics(...) abort
178-
let l:target_server_name = get(a:000, 0, '')
161+
let l:server = get(a:000, 0, '')
179162

180-
let l:uri = lsp#utils#get_buffer_uri()
163+
let l:bufnr = bufnr('%')
164+
let l:uri = lsp#utils#get_buffer_uri(l:bufnr)
181165

182-
let [l:has_diagnostics, l:diagnostics] = s:get_diagnostics(l:uri)
183-
if !l:has_diagnostics
166+
if !lsp#internal#diagnostics#state#_is_enabled_for_buffer(l:bufnr)
184167
return []
185168
endif
186169

187-
let l:all_diagnostics = []
188-
for [l:server_name, l:data] in items(l:diagnostics)
189-
if empty(l:target_server_name) || l:server_name ==# l:target_server_name
190-
call extend(l:all_diagnostics, l:data['response']['params']['diagnostics'])
170+
let l:diagnostics_by_server = lsp#internal#diagnostics#state#_get_all_diagnostics_grouped_by_server_for_uri(l:uri)
171+
if empty(l:server)
172+
let l:diagnostics = []
173+
for l:item in values(l:diagnostics_by_server)
174+
let l:diagnostics += l:item['params']['diagnostics']
175+
endfor
176+
else
177+
if has_key(l:diagnostics_by_server, l:server)
178+
let l:diagnostics = l:diagnostics_by_server[l:server]['params']['diagnostics']
179+
else
180+
let l:diagnostics = []
191181
endif
192-
endfor
182+
endif
193183

194-
return l:all_diagnostics
184+
return l:diagnostics
195185
endfunction
196186

197187
function! s:compare_diagnostics(d1, d2) abort

autoload/lsp/internal/diagnostics/state.vim

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,7 @@ function! s:notify_diagnostics_update(...) abort
130130
" if a:0 > 0 | let l:data['response']['params']['server'] = a:1 | endif
131131
" if a:0 > 1 | let l:data['response']['params']['uri'] = a:2 | endif
132132
call lsp#stream(1, l:data)
133-
" TODO: uncomment doautocmd when all diagnostics moves to using callbag
134-
" doautocmd <nomodeline> User lsp_diagnostics_updated
133+
doautocmd <nomodeline> User lsp_diagnostics_updated
135134
endfunction
136135

137136
function! lsp#internal#diagnostics#state#_enable_for_buffer(bufnr) abort

plugin/lsp.vim

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ command! -nargs=? LspDocumentDiagnostics call lsp#internal#diagnostics#document_
9696
\ 'buffers': {'type': type('')},
9797
\ })))
9898
command! -nargs=? -complete=customlist,lsp#utils#empty_complete LspHover call lsp#ui#vim#hover#get_hover_under_cursor()
99-
command! -nargs=* LspNextError call lsp#ui#vim#diagnostics#next_error(<f-args>)
100-
command! -nargs=* LspPreviousError call lsp#ui#vim#diagnostics#previous_error(<f-args>)
101-
command! -nargs=* LspNextWarning call lsp#ui#vim#diagnostics#next_warning(<f-args>)
102-
command! -nargs=* LspPreviousWarning call lsp#ui#vim#diagnostics#previous_warning(<f-args>)
103-
command! -nargs=* LspNextDiagnostic call lsp#ui#vim#diagnostics#next_diagnostic(<f-args>)
104-
command! -nargs=* LspPreviousDiagnostic call lsp#ui#vim#diagnostics#previous_diagnostic(<f-args>)
99+
command! -nargs=* LspNextError call lsp#internal#diagnostics#movement#_next_error(<f-args>)
100+
command! -nargs=* LspPreviousError call lsp#internal#diagnostics#movement#_previous_error(<f-args>)
101+
command! -nargs=* LspNextWarning call lsp#internal#diagnostics#movement#_next_warning(<f-args>)
102+
command! -nargs=* LspPreviousWarning call lsp#internal#diagnostics#movement#_previous_warning(<f-args>)
103+
command! -nargs=* LspNextDiagnostic call lsp#internal#diagnostics#movement#_next_diagnostics(<f-args>)
104+
command! -nargs=* LspPreviousDiagnostic call lsp#internal#diagnostics#movement#_previous_diagnostics(<f-args>)
105105
command! LspReferences call lsp#ui#vim#references()
106106
command! LspRename call lsp#ui#vim#rename()
107107
command! LspTypeDefinition call lsp#ui#vim#type_definition(0, <q-mods>)
@@ -142,18 +142,18 @@ nnoremap <plug>(lsp-document-diagnostics) :<c-u>call lsp#internal#diagnostics#do
142142
nnoremap <plug>(lsp-hover) :<c-u>call lsp#ui#vim#hover#get_hover_under_cursor()<cr>
143143
nnoremap <plug>(lsp-preview-close) :<c-u>call lsp#ui#vim#output#closepreview()<cr>
144144
nnoremap <plug>(lsp-preview-focus) :<c-u>call lsp#ui#vim#output#focuspreview()<cr>
145-
nnoremap <plug>(lsp-next-error) :<c-u>call lsp#ui#vim#diagnostics#next_error()<cr>
146-
nnoremap <plug>(lsp-next-error-nowrap) :<c-u>call lsp#ui#vim#diagnostics#next_error("--nowrap")<cr>
147-
nnoremap <plug>(lsp-previous-error) :<c-u>call lsp#ui#vim#diagnostics#previous_error()<cr>
148-
nnoremap <plug>(lsp-previous-error-nowrap) :<c-u>call lsp#ui#vim#diagnostics#previous_error("--nowrap")<cr>
149-
nnoremap <plug>(lsp-next-warning) :<c-u>call lsp#ui#vim#diagnostics#next_warning()<cr>
150-
nnoremap <plug>(lsp-next-warning-nowrap) :<c-u>call lsp#ui#vim#diagnostics#next_warning("--nowrap")<cr>
151-
nnoremap <plug>(lsp-previous-warning) :<c-u>call lsp#ui#vim#diagnostics#previous_warning()<cr>
152-
nnoremap <plug>(lsp-previous-warning-nowrap) :<c-u>call lsp#ui#vim#diagnostics#previous_warning("--nowrap")<cr>
153-
nnoremap <plug>(lsp-next-diagnostic) :<c-u>call lsp#ui#vim#diagnostics#next_diagnostic()<cr>
154-
nnoremap <plug>(lsp-next-diagnostic-nowrap) :<c-u>call lsp#ui#vim#diagnostics#next_diagnostic("--nowrap")<cr>
155-
nnoremap <plug>(lsp-previous-diagnostic) :<c-u>call lsp#ui#vim#diagnostics#previous_diagnostic()<cr>
156-
nnoremap <plug>(lsp-previous-diagnostic-nowrap) :<c-u>call lsp#ui#vim#diagnostics#previous_diagnostic("--nowrap")<cr>
145+
nnoremap <plug>(lsp-next-error) :<c-u>call lsp#internal#diagnostics#movement#_next_error()<cr>
146+
nnoremap <plug>(lsp-next-error-nowrap) :<c-u>call lsp#internal#diagnostics#movement#_next_error("--nowrap")<cr>
147+
nnoremap <plug>(lsp-previous-error) :<c-u>call lsp#internal#diagnostics#movement#_previous_error()<cr>
148+
nnoremap <plug>(lsp-previous-error-nowrap) :<c-u>call lsp#internal#diagnostics#movement#_previous_error("--nowrap")<cr>
149+
nnoremap <plug>(lsp-next-warning) :<c-u>call lsp#internal#diagnostics#movement#_next_warning()<cr>
150+
nnoremap <plug>(lsp-next-warning-nowrap) :<c-u>call lsp#internal#diagnostics#movement#_next_warning("--nowrap")<cr>
151+
nnoremap <plug>(lsp-previous-warning) :<c-u>call lsp#internal#diagnostics#movement#_previous_warning()<cr>
152+
nnoremap <plug>(lsp-previous-warning-nowrap) :<c-u>call lsp#internal#diagnostics#movement#_previous_warning("--nowrap")<cr>
153+
nnoremap <plug>(lsp-next-diagnostic) :<c-u>call lsp#internal#diagnostics#movement#_next_diagnostics()<cr>
154+
nnoremap <plug>(lsp-next-diagnostic-nowrap) :<c-u>call lsp#internal#diagnostics#movement#_next_diagnostics("--nowrap")<cr>
155+
nnoremap <plug>(lsp-previous-diagnostic) :<c-u>call lsp#internal#diagnostics#movement#_previous_diagnostics()<cr>
156+
nnoremap <plug>(lsp-previous-diagnostic-nowrap) :<c-u>call lsp#internal#diagnostics#movement#_previous_diagnostics("--nowrap")<cr>
157157
nnoremap <plug>(lsp-references) :<c-u>call lsp#ui#vim#references()<cr>
158158
nnoremap <plug>(lsp-rename) :<c-u>call lsp#ui#vim#rename()<cr>
159159
nnoremap <plug>(lsp-type-definition) :<c-u>call lsp#ui#vim#type_definition(0)<cr>

0 commit comments

Comments
 (0)