Diagnostics refactor movements (#1004)

This commit is contained in:
Prabir Shrestha
2021-01-02 13:59:31 -08:00
committed by GitHub
parent 7c43b01635
commit 268341fe60
4 changed files with 45 additions and 58 deletions

View File

@@ -229,7 +229,7 @@ function! s:on_text_document_did_open(...) abort
call lsp#log('s:on_text_document_did_open()', l:buf, &filetype, getcwd(), lsp#utils#get_buffer_uri(l:buf))
" Some language server notify diagnostics to the buffer that has not been loaded yet.
" This diagnostics was stored `autoload/lsp/ui/vim/diagnostics.vim` but not highlighted.
" This diagnostics was stored `autoload/lsp/internal/diagnostics/state.vim` but not highlighted.
" So we should refresh highlights when buffer opened.
call lsp#internal#diagnostics#state#_force_notify_buffer(l:buf)
@@ -776,7 +776,6 @@ function! s:on_notification(server_name, id, data, event) abort
let l:response = a:data['response']
let l:server = s:servers[a:server_name]
let l:server_info = l:server['server_info']
let l:lsp_diagnostics_config_enabled = get(get(l:server_info, 'config', {}), 'diagnostics', v:true)
let l:stream_data = { 'server': a:server_name, 'response': l:response }
if has_key(a:data, 'request')
@@ -786,11 +785,10 @@ function! s:on_notification(server_name, id, data, event) abort
if lsp#client#is_server_instantiated_notification(a:data)
if has_key(l:response, 'method')
if g:lsp_diagnostics_enabled && l:lsp_diagnostics_config_enabled && l:response['method'] ==# 'textDocument/publishDiagnostics'
call lsp#ui#vim#diagnostics#handle_text_document_publish_diagnostics(a:server_name, a:data)
elseif l:response['method'] ==# 'textDocument/semanticHighlighting'
if l:response['method'] ==# 'textDocument/semanticHighlighting'
call lsp#ui#vim#semantic#handle_semantic(a:server_name, a:data)
endif
" NOTE: this is legacy code, use stream instead of handling notifications here
endif
else
let l:request = a:data['request']

View File

@@ -1,39 +1,22 @@
let s:is_win = has('win32') || has('win64')
let s:diagnostics = {} " { uri: { 'server_name': response } }
function! lsp#ui#vim#diagnostics#handle_text_document_publish_diagnostics(server_name, data) abort
if lsp#client#is_error(a:data['response'])
return
endif
let l:uri = a:data['response']['params']['uri']
let l:uri = lsp#utils#normalize_uri(l:uri)
if !has_key(s:diagnostics, l:uri)
let s:diagnostics[l:uri] = {}
endif
let s:diagnostics[l:uri][a:server_name] = a:data
doautocmd <nomodeline> User lsp_diagnostics_updated
endfunction
function! s:severity_of(diagnostic) abort
return get(a:diagnostic, 'severity', 1)
endfunction
function! lsp#ui#vim#diagnostics#next_error(...) abort
function! lsp#internal#diagnostics#movement#_next_error(...) abort
let l:diagnostics = filter(s:get_all_buffer_diagnostics(),
\ {_, diagnostic -> s:severity_of(diagnostic) ==# 1 })
let l:options = lsp#utils#parse_command_options(a:000)
call s:next_diagnostic(l:diagnostics, l:options)
endfunction
function! lsp#ui#vim#diagnostics#next_warning(...) abort
function! lsp#internal#diagnostics#movement#_next_warning(...) abort
let l:diagnostics = filter(s:get_all_buffer_diagnostics(),
\ {_, diagnostic -> s:severity_of(diagnostic) ==# 2 })
let l:options = lsp#utils#parse_command_options(a:000)
call s:next_diagnostic(l:diagnostics, l:options)
endfunction
function! lsp#ui#vim#diagnostics#next_diagnostic(...) abort
function! lsp#internal#diagnostics#movement#_next_diagnostics(...) abort
let l:options = lsp#utils#parse_command_options(a:000)
call s:next_diagnostic(s:get_all_buffer_diagnostics(), l:options)
endfunction
@@ -87,21 +70,21 @@ function! s:next_diagnostic(diagnostics, options) abort
call winrestview(l:view)
endfunction
function! lsp#ui#vim#diagnostics#previous_error(...) abort
function! lsp#internal#diagnostics#movement#_previous_error(...) abort
let l:diagnostics = filter(s:get_all_buffer_diagnostics(),
\ {_, diagnostic -> s:severity_of(diagnostic) ==# 1 })
let l:options = lsp#utils#parse_command_options(a:000)
call s:previous_diagnostic(l:diagnostics, l:options)
endfunction
function! lsp#ui#vim#diagnostics#previous_warning(...) abort
function! lsp#internal#diagnostics#movement#_previous_warning(...) abort
let l:options = lsp#utils#parse_command_options(a:000)
let l:diagnostics = filter(s:get_all_buffer_diagnostics(),
\ {_, diagnostic -> s:severity_of(diagnostic) ==# 2 })
call s:previous_diagnostic(l:diagnostics, l:options)
endfunction
function! lsp#ui#vim#diagnostics#previous_diagnostic(...) abort
function! lsp#internal#diagnostics#movement#_previous_diagnostics(...) abort
let l:options = lsp#utils#parse_command_options(a:000)
call s:previous_diagnostic(s:get_all_buffer_diagnostics(), l:options)
endfunction
@@ -175,23 +158,30 @@ endfunction
" Get diagnostics for the current buffer URI from all servers
function! s:get_all_buffer_diagnostics(...) abort
let l:target_server_name = get(a:000, 0, '')
let l:server = get(a:000, 0, '')
let l:uri = lsp#utils#get_buffer_uri()
let l:bufnr = bufnr('%')
let l:uri = lsp#utils#get_buffer_uri(l:bufnr)
let [l:has_diagnostics, l:diagnostics] = s:get_diagnostics(l:uri)
if !l:has_diagnostics
if !lsp#internal#diagnostics#state#_is_enabled_for_buffer(l:bufnr)
return []
endif
let l:all_diagnostics = []
for [l:server_name, l:data] in items(l:diagnostics)
if empty(l:target_server_name) || l:server_name ==# l:target_server_name
call extend(l:all_diagnostics, l:data['response']['params']['diagnostics'])
let l:diagnostics_by_server = lsp#internal#diagnostics#state#_get_all_diagnostics_grouped_by_server_for_uri(l:uri)
if empty(l:server)
let l:diagnostics = []
for l:item in values(l:diagnostics_by_server)
let l:diagnostics += l:item['params']['diagnostics']
endfor
else
if has_key(l:diagnostics_by_server, l:server)
let l:diagnostics = l:diagnostics_by_server[l:server]['params']['diagnostics']
else
let l:diagnostics = []
endif
endfor
endif
return l:all_diagnostics
return l:diagnostics
endfunction
function! s:compare_diagnostics(d1, d2) abort

View File

@@ -130,8 +130,7 @@ function! s:notify_diagnostics_update(...) abort
" if a:0 > 0 | let l:data['response']['params']['server'] = a:1 | endif
" if a:0 > 1 | let l:data['response']['params']['uri'] = a:2 | endif
call lsp#stream(1, l:data)
" TODO: uncomment doautocmd when all diagnostics moves to using callbag
" doautocmd <nomodeline> User lsp_diagnostics_updated
doautocmd <nomodeline> User lsp_diagnostics_updated
endfunction
function! lsp#internal#diagnostics#state#_enable_for_buffer(bufnr) abort

View File

@@ -96,12 +96,12 @@ command! -nargs=? LspDocumentDiagnostics call lsp#internal#diagnostics#document_
\ 'buffers': {'type': type('')},
\ })))
command! -nargs=? -complete=customlist,lsp#utils#empty_complete LspHover call lsp#ui#vim#hover#get_hover_under_cursor()
command! -nargs=* LspNextError call lsp#ui#vim#diagnostics#next_error(<f-args>)
command! -nargs=* LspPreviousError call lsp#ui#vim#diagnostics#previous_error(<f-args>)
command! -nargs=* LspNextWarning call lsp#ui#vim#diagnostics#next_warning(<f-args>)
command! -nargs=* LspPreviousWarning call lsp#ui#vim#diagnostics#previous_warning(<f-args>)
command! -nargs=* LspNextDiagnostic call lsp#ui#vim#diagnostics#next_diagnostic(<f-args>)
command! -nargs=* LspPreviousDiagnostic call lsp#ui#vim#diagnostics#previous_diagnostic(<f-args>)
command! -nargs=* LspNextError call lsp#internal#diagnostics#movement#_next_error(<f-args>)
command! -nargs=* LspPreviousError call lsp#internal#diagnostics#movement#_previous_error(<f-args>)
command! -nargs=* LspNextWarning call lsp#internal#diagnostics#movement#_next_warning(<f-args>)
command! -nargs=* LspPreviousWarning call lsp#internal#diagnostics#movement#_previous_warning(<f-args>)
command! -nargs=* LspNextDiagnostic call lsp#internal#diagnostics#movement#_next_diagnostics(<f-args>)
command! -nargs=* LspPreviousDiagnostic call lsp#internal#diagnostics#movement#_previous_diagnostics(<f-args>)
command! LspReferences call lsp#ui#vim#references()
command! LspRename call lsp#ui#vim#rename()
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
nnoremap <plug>(lsp-hover) :<c-u>call lsp#ui#vim#hover#get_hover_under_cursor()<cr>
nnoremap <plug>(lsp-preview-close) :<c-u>call lsp#ui#vim#output#closepreview()<cr>
nnoremap <plug>(lsp-preview-focus) :<c-u>call lsp#ui#vim#output#focuspreview()<cr>
nnoremap <plug>(lsp-next-error) :<c-u>call lsp#ui#vim#diagnostics#next_error()<cr>
nnoremap <plug>(lsp-next-error-nowrap) :<c-u>call lsp#ui#vim#diagnostics#next_error("--nowrap")<cr>
nnoremap <plug>(lsp-previous-error) :<c-u>call lsp#ui#vim#diagnostics#previous_error()<cr>
nnoremap <plug>(lsp-previous-error-nowrap) :<c-u>call lsp#ui#vim#diagnostics#previous_error("--nowrap")<cr>
nnoremap <plug>(lsp-next-warning) :<c-u>call lsp#ui#vim#diagnostics#next_warning()<cr>
nnoremap <plug>(lsp-next-warning-nowrap) :<c-u>call lsp#ui#vim#diagnostics#next_warning("--nowrap")<cr>
nnoremap <plug>(lsp-previous-warning) :<c-u>call lsp#ui#vim#diagnostics#previous_warning()<cr>
nnoremap <plug>(lsp-previous-warning-nowrap) :<c-u>call lsp#ui#vim#diagnostics#previous_warning("--nowrap")<cr>
nnoremap <plug>(lsp-next-diagnostic) :<c-u>call lsp#ui#vim#diagnostics#next_diagnostic()<cr>
nnoremap <plug>(lsp-next-diagnostic-nowrap) :<c-u>call lsp#ui#vim#diagnostics#next_diagnostic("--nowrap")<cr>
nnoremap <plug>(lsp-previous-diagnostic) :<c-u>call lsp#ui#vim#diagnostics#previous_diagnostic()<cr>
nnoremap <plug>(lsp-previous-diagnostic-nowrap) :<c-u>call lsp#ui#vim#diagnostics#previous_diagnostic("--nowrap")<cr>
nnoremap <plug>(lsp-next-error) :<c-u>call lsp#internal#diagnostics#movement#_next_error()<cr>
nnoremap <plug>(lsp-next-error-nowrap) :<c-u>call lsp#internal#diagnostics#movement#_next_error("--nowrap")<cr>
nnoremap <plug>(lsp-previous-error) :<c-u>call lsp#internal#diagnostics#movement#_previous_error()<cr>
nnoremap <plug>(lsp-previous-error-nowrap) :<c-u>call lsp#internal#diagnostics#movement#_previous_error("--nowrap")<cr>
nnoremap <plug>(lsp-next-warning) :<c-u>call lsp#internal#diagnostics#movement#_next_warning()<cr>
nnoremap <plug>(lsp-next-warning-nowrap) :<c-u>call lsp#internal#diagnostics#movement#_next_warning("--nowrap")<cr>
nnoremap <plug>(lsp-previous-warning) :<c-u>call lsp#internal#diagnostics#movement#_previous_warning()<cr>
nnoremap <plug>(lsp-previous-warning-nowrap) :<c-u>call lsp#internal#diagnostics#movement#_previous_warning("--nowrap")<cr>
nnoremap <plug>(lsp-next-diagnostic) :<c-u>call lsp#internal#diagnostics#movement#_next_diagnostics()<cr>
nnoremap <plug>(lsp-next-diagnostic-nowrap) :<c-u>call lsp#internal#diagnostics#movement#_next_diagnostics("--nowrap")<cr>
nnoremap <plug>(lsp-previous-diagnostic) :<c-u>call lsp#internal#diagnostics#movement#_previous_diagnostics()<cr>
nnoremap <plug>(lsp-previous-diagnostic-nowrap) :<c-u>call lsp#internal#diagnostics#movement#_previous_diagnostics("--nowrap")<cr>
nnoremap <plug>(lsp-references) :<c-u>call lsp#ui#vim#references()<cr>
nnoremap <plug>(lsp-rename) :<c-u>call lsp#ui#vim#rename()<cr>
nnoremap <plug>(lsp-type-definition) :<c-u>call lsp#ui#vim#type_definition(0)<cr>