Diagnostics for all buffers (#982)

* render diagnostics in loclist and fix args paser
* document --buffers flag for LspDocumentDiagnostics
* do not show diagnostics if it is disabled for buffer
* remove old code and fix plug mapping for lsp-document-diagnostics
* add unit tests for document diagnostics command
This commit is contained in:
Prabir Shrestha
2020-12-27 22:44:18 -08:00
committed by GitHub
parent 124a0a6ab9
commit 6bb6069f94
8 changed files with 124 additions and 36 deletions

View File

@@ -0,0 +1,40 @@
" options = {
" buffers: '1' " optional string, defaults to current buffer, '*' for all buffers
" }
function! lsp#internal#diagnostics#document_diagnostics_command#do(options) abort
if !g:lsp_diagnostics_enabled
call lsp#utils#error(':LspDocumentDiagnostics', 'g:lsp_diagnostics_enabled must be enabled')
return
endif
let l:buffers = get(a:options, 'buffers', '')
let l:filtered_diagnostics = {}
if l:buffers ==# '*'
let l:filtered_diagnostics = lsp#internal#diagnostics#state#_get_all_diagnostics_grouped_by_uri_and_server()
else
let l:uri = lsp#utils#get_buffer_uri()
if !empty(l:uri)
let l:filtered_diagnostics[l:uri] = lsp#internal#diagnostics#state#_get_all_diagnostics_grouped_by_server_for_uri(l:uri)
endif
endif
let l:result = []
for [l:uri, l:value] in items(l:filtered_diagnostics)
if lsp#internal#diagnostics#state#_is_enabled_for_buffer(bufnr(lsp#utils#uri_to_path(l:uri)))
for l:diagnostics in values(l:value)
let l:result += lsp#ui#vim#utils#diagnostics_to_loc_list({ 'response': l:diagnostics })
endfor
endif
endfor
if empty(l:result)
call lsp#utils#error('No diagnostics results')
return
else
call setloclist(0, l:result)
echo 'Retrieved diagnostics results'
botright lopen
endif
endfunction

View File

@@ -36,37 +36,6 @@ function! lsp#ui#vim#diagnostics#get_document_diagnostics(bufnr) abort
return get(s:diagnostics, lsp#utils#get_buffer_uri(a:bufnr), {})
endfunction
function! lsp#ui#vim#diagnostics#document_diagnostics() abort
if !g:lsp_diagnostics_enabled
call lsp#utils#error('Diagnostics manually disabled -- g:lsp_diagnostics_enabled = 0')
return
endif
let l:uri = lsp#utils#get_buffer_uri()
let [l:has_diagnostics, l:diagnostics] = s:get_diagnostics(l:uri)
if !l:has_diagnostics
call lsp#utils#error('No diagnostics results')
return
endif
let l:result = []
for l:data in values(l:diagnostics)
let l:result += lsp#ui#vim#utils#diagnostics_to_loc_list(l:data)
endfor
call setloclist(0, l:result)
" autocmd FileType qf setlocal wrap
if empty(l:result)
call lsp#utils#error('No diagnostics results found')
else
echo 'Retrieved diagnostics results'
botright lopen
endif
endfunction
" Returns a diagnostic object, or empty dictionary if no diagnostics are available.
"
" Note: Consider renaming this method (s/diagnostics/diagnostic) to make

View File

@@ -131,7 +131,6 @@ function! lsp#ui#vim#utils#diagnostics_to_loc_list(result) abort
endfor
endif
return l:list
endfunction

View File

@@ -1,7 +1,9 @@
function! lsp#utils#args#_parse(args, opt) abort
let l:result = {}
for l:item in split(a:args, ' ')
let [l:key, l:value] = split(l:item, '=')
let l:parts = split(l:item, '=')
let l:key = l:parts[0]
let l:value = get(l:parts, 1, '')
let l:key = l:key[2:]
if has_key(a:opt, l:key)
if has_key(a:opt[l:key], 'type')

View File

@@ -1228,7 +1228,17 @@ Same as |:LspCodeLens| but synchronous.
LspDocumentDiagnostics *:LspDocumentDiagnostics*
Gets the current document diagnostics.
Gets the document diagnostics and opens in |location-list|. By default
diagnostics are filtered for current buffer.
Arguments:
--buffers Defaults to empty string, i.e. shows diagnostics for current
buffer. To show diagnotics for all buffer use `--buffers=*`.
Example: >
:LspDocumentDiagnostics
:LspDocumentDiagnostics --buffers=*
LspDeclaration *:LspDeclaration*

View File

@@ -85,7 +85,10 @@ command! LspPeekDeclaration call lsp#ui#vim#declaration(1)
command! LspDefinition call lsp#ui#vim#definition(0, <q-mods>)
command! LspPeekDefinition call lsp#ui#vim#definition(1)
command! LspDocumentSymbol call lsp#ui#vim#document_symbol()
command! LspDocumentDiagnostics call lsp#ui#vim#diagnostics#document_diagnostics()
command! -nargs=? LspDocumentDiagnostics call lsp#internal#diagnostics#document_diagnostics_command#do(
\ extend({}, lsp#utils#args#_parse(<q-args>, {
\ '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>)
@@ -129,7 +132,7 @@ nnoremap <plug>(lsp-peek-declaration) :<c-u>call lsp#ui#vim#declaration(1)<cr>
nnoremap <plug>(lsp-definition) :<c-u>call lsp#ui#vim#definition(0)<cr>
nnoremap <plug>(lsp-peek-definition) :<c-u>call lsp#ui#vim#definition(1)<cr>
nnoremap <plug>(lsp-document-symbol) :<c-u>call lsp#ui#vim#document_symbol()<cr>
nnoremap <plug>(lsp-document-diagnostics) :<c-u>call lsp#ui#vim#diagnostics#document_diagnostics()<cr>
nnoremap <plug>(lsp-document-diagnostics) :<c-u>call lsp#internal#diagnostics#document_diagnostics_command#do({})<cr>
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>

View File

@@ -0,0 +1,54 @@
Describe lsp#internal#diagnostics#document_diagnostics_command
Before
%bwipeout!
let g:lsp_diagnostics_enabled = 1
call lsp#internal#diagnostics#state#_disable()
call lsp#internal#diagnostics#state#_enable()
End
After all
%bwipeout!
let g:lsp_diagnostics_enabled = 0
call lsp#internal#diagnostics#state#_disable()
End
It should be able to show document diagnostics for a buffer
normal! m'
execute printf('keepalt keepjumps edit %s', lsp#test#projectdir('rust') . '/src/documentdiagnostics.rs')
let l:uri = lsp#utils#get_buffer_uri()
let l:uri2 = l:uri . '2'
let l:bufnr = bufnr('%')
let l:server1_response1 = {'method': 'textDocument/publishDiagnostics', 'params': {'uri': l:uri, 'diagnostics': [
\ {'severity': 1, 'message': 'm1', 'range': { 'start': { 'line': 1, 'character': 1, 'end': { 'line': 1, 'character': 1 } } }},
\ {'severity': 1, 'message': 'm2', 'range': { 'start': { 'line': 1, 'character': 2, 'end': { 'line': 1, 'character': 3 } } }},
\ ]}}
let l:server1_response2 = {'method': 'textDocument/publishDiagnostics', 'params': {'uri': l:uri2, 'diagnostics': [
\ {'severity': 1, 'message': 'm3', 'range': { 'start': { 'line': 2, 'character': 1, 'end': { 'line': 1, 'character': 1 } } }},
\ {'severity': 1, 'message': 'm4', 'range': { 'start': { 'line': 3, 'character': 2, 'end': { 'line': 1, 'character': 3 } } }},
\ ]}}
call lsp#stream(1, { 'server': 'server1', 'response': l:server1_response1 })
call lsp#stream(1, { 'server': 'server1', 'response': l:server1_response2 })
execute ':LspDocumentDiagnostics'
let l:result = getloclist(0)
Assert Equals(l:result, [
\ {'lnum': 2, 'bufnr': l:bufnr, 'col': 2, 'pattern': '', 'valid': 1, 'vcol': 0, 'nr': 0, 'type': '', 'module': '', 'text': 'Error:m1'},
\ {'lnum': 2, 'bufnr': l:bufnr, 'col': 3, 'pattern': '', 'valid': 1, 'vcol': 0, 'nr': 0, 'type': '', 'module': '', 'text': 'Error:m2'},
\ ])
execute ':lclose'
execute ':LspDocumentDiagnostics --buffers=*'
let l:result = getloclist(0)
" +2 for bufnr because original bufnr + loclist + new unopened file
Assert Equals(l:result, [
\ {'lnum': 2, 'bufnr': l:bufnr, 'col': 2, 'pattern': '', 'valid': 1, 'vcol': 0, 'nr': 0, 'type': '', 'module': '', 'text': 'Error:m1'},
\ {'lnum': 2, 'bufnr': l:bufnr, 'col': 3, 'pattern': '', 'valid': 1, 'vcol': 0, 'nr': 0, 'type': '', 'module': '', 'text': 'Error:m2'},
\ {'lnum': 3, 'bufnr': l:bufnr + 2, 'col': 2, 'pattern': '', 'valid': 1, 'vcol': 0, 'nr': 0, 'type': '', 'module': '', 'text': 'Error:m3'},
\ {'lnum': 4, 'bufnr': l:bufnr + 2, 'col': 3, 'pattern': '', 'valid': 1, 'vcol': 0, 'nr': 0, 'type': '', 'module': '', 'text': 'Error:m4'},
\ ])
execute ':lclose'
End
End

View File

@@ -0,0 +1,11 @@
fn document_diagnostics() {
unimplemented!();
}
fn broken1(
print
}
fn broken2()
broken(1);
}