added support for basic textDocument/publishDiagnostics via :LspDocumententDiagnostics

This commit is contained in:
Prabir Shrestha
2017-09-17 23:41:58 -07:00
parent bae35d03d3
commit 358e1eec44
5 changed files with 85 additions and 3 deletions

View File

@@ -70,6 +70,7 @@ Plug 'prabirshrestha/asyncomplete-lsp.vim'
| Command | Description|
|--|--|
|`:LspDocumentDiagnostics`| Get current document diagnostics information |
|`:LspDefinition`| Go to definition |
|`:LspDocumentFormat`| Format entire document |
|`:LspDocumentRangeFormat`| Format document selection |

View File

@@ -423,7 +423,7 @@ 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 l:response['method'] == 'textDocument/publishDiagnostics'
call lsp#ui#vim#handle_text_document_publish_diagnostics(a:server_name, l:response)
call lsp#ui#vim#handle_text_document_publish_diagnostics(a:server_name, a:data)
endif
endif
else

View File

@@ -1,4 +1,5 @@
let s:last_req_id = 0
let s:diagnostics = {} " { uri: { 'server_name': response } }
function! lsp#ui#vim#definition() abort
let l:servers = filter(lsp#get_whitelisted_servers(), 'lsp#capabilities#has_definition_provider(v:val)')
@@ -233,10 +234,39 @@ function! lsp#ui#vim#document_symbol() abort
endfunction
function! lsp#ui#vim#handle_text_document_publish_diagnostics(server_name, data) abort
if lsp#client#is_error(a:data)
if lsp#client#is_error(a:data['response'])
return
endif
" TODO: handle textDocument/publishDiagnostics
let l:uri = a:data['response']['params']['uri']
if !has_key(s:diagnostics, l:uri)
let s:diagnostics[l:uri] = {}
endif
let s:diagnostics[l:uri][a:server_name] = a:data
endfunction
function! lsp#ui#vim#document_diagnostics() abort
let l:uri = lsp#utils#get_buffer_uri()
if !has_key(s:diagnostics, l:uri)
echom 'No diagnostics results'
return
endif
let l:diagnostics = s:diagnostics[l:uri]
let l:result = []
for [l:server_name, l:data] in items(l:diagnostics)
let l:result += lsp#ui#vim#utils#diagnostics_to_loc_list(l:data)
endfor
call setqflist(l:result)
" autocmd FileType qf setlocal wrap
if empty(l:result)
echom 'No diagnostics results found'
else
echom 'Retrieved diagnostics results'
copen
endif
endfunction
function! s:handle_symbol(server, last_req_id, type, data) abort

View File

@@ -47,6 +47,13 @@ let s:symbol_kinds = {
\ '18': 'array',
\ }
let s:diagnostic_severity = {
\ 1: 'Error',
\ 2: 'Warning',
\ 3: 'Information',
\ 4: 'Hint',
\ }
function! lsp#ui#vim#utils#symbols_to_loc_list(result) abort
if !has_key(a:result['response'], 'result')
return []
@@ -77,6 +84,45 @@ function! lsp#ui#vim#utils#symbols_to_loc_list(result) abort
return l:list
endfunction
function! lsp#ui#vim#utils#diagnostics_to_loc_list(result) abort
if !has_key(a:result['response'], 'params')
return
endif
let l:uri = a:result['response']['params']['uri']
let l:diagnostics = a:result['response']['params']['diagnostics']
let l:list = []
if !empty(l:diagnostics) && s:is_file_uri(l:uri)
let l:path = lsp#utils#uri_to_path(l:uri)
let l:bufnr = bufnr(l:path)
for l:item in l:diagnostics
let l:text = ''
if has_key(l:item, 'source') && !empty(l:item['source'])
let l:text .= l:item['source'] . ':'
endif
if has_key(l:item, 'severity') && !empty(l:item['severity'])
let l:text .= s:get_diagnostic_severity_text(l:item['severity']) . ':'
endif
if has_key(l:item, 'code') && !empty(l:item['code'])
let l:text .= l:item['code'] . ':'
endif
let l:text .= l:item['message']
let l:line = l:item['range']['start']['line'] + 1
let l:col = l:item['range']['start']['character'] + 1
call add(l:list, {
\ 'filename': l:path,
\ 'lnum': l:line,
\ 'col': l:col,
\ 'text': l:text,
\ })
endfor
endif
return l:list
endfunction
function! s:is_file_uri(uri) abort
return stridx(a:uri, 'file:///') == 0
endfunction
@@ -85,3 +131,7 @@ endfunction
function! s:get_symbol_text_from_kind(kind)
return has_key(s:symbol_kinds, a:kind) ? s:symbol_kinds[a:kind] : 'unknown symbol ' . a:kind
endfunction
function! s:get_diagnostic_severity_text(severity)
return s:diagnostic_severity[a:severity]
endfunction

View File

@@ -15,6 +15,7 @@ endif
command! LspDefinition call lsp#ui#vim#definition()
command! LspDocumentSymbol call lsp#ui#vim#document_symbol()
command! LspDocumentDiagnostics call lsp#ui#vim#document_diagnostics()
command! LspHover call lsp#ui#vim#hover()
command! LspReferences call lsp#ui#vim#references()
command! LspRename call lsp#ui#vim#rename()