mirror of
https://github.com/prabirshrestha/vim-lsp.git
synced 2025-12-14 20:35:59 +01:00
52
autoload/lsp/internal/document_hover/under_cursor.vim
Normal file
52
autoload/lsp/internal/document_hover/under_cursor.vim
Normal file
@@ -0,0 +1,52 @@
|
||||
" https://microsoft.github.io/language-server-protocol/specification#textDocument_hover
|
||||
" options - {
|
||||
" server - 'server_name' " optional
|
||||
" }
|
||||
function! lsp#internal#document_hover#under_cursor#do(options) abort
|
||||
let l:bufnr = bufnr('%')
|
||||
if has_key(a:options, 'server')
|
||||
let l:servers = [a:options['server']]
|
||||
else
|
||||
let l:servers = filter(lsp#get_allowed_servers(), 'lsp#capabilities#has_hover_provider(v:val)')
|
||||
endif
|
||||
|
||||
if len(l:servers) == 0
|
||||
let l:filetype = getbufvar(l:bufnr, '&filetype')
|
||||
call lsp#utils#error('textDocument/hover not supported for ' . l:filetype)
|
||||
return
|
||||
endif
|
||||
|
||||
redraw | echo 'Retrieving hover ...'
|
||||
|
||||
call lsp#_new_command()
|
||||
|
||||
" TODO: ask user to select server for formatting if there are multiple servers
|
||||
let l:request = {
|
||||
\ 'method': 'textDocument/hover',
|
||||
\ 'params': {
|
||||
\ 'textDocument': lsp#get_text_document_identifier(),
|
||||
\ 'position': lsp#get_position(),
|
||||
\ },
|
||||
\ }
|
||||
call lsp#callbag#pipe(
|
||||
\ lsp#callbag#fromList(l:servers),
|
||||
\ lsp#callbag#flatMap({server->
|
||||
\ lsp#request(server, l:request)
|
||||
\ }),
|
||||
\ lsp#callbag#tap({x->s:show_hover(x['server_name'], x['request'], x['response'])}),
|
||||
\ lsp#callbag#takeUntil(lsp#callbag#pipe(
|
||||
\ lsp#stream(),
|
||||
\ lsp#callbag#filter({x->has_key(x, 'command')}),
|
||||
\ )),
|
||||
\ lsp#callbag#subscribe(),
|
||||
\ )
|
||||
endfunction
|
||||
|
||||
function! s:show_hover(server_name, request, response) abort
|
||||
if !has_key(a:response, 'result') || empty(a:response['result']) ||
|
||||
\ empty(a:response['result']['contents'])
|
||||
call lsp#utils#error('No hover information found in server - ' . a:server_name)
|
||||
endif
|
||||
|
||||
call lsp#ui#vim#output#preview(a:server_name, a:response['result']['contents'], {'statusline': ' LSP Hover'})
|
||||
endfunction
|
||||
@@ -1,43 +0,0 @@
|
||||
function! s:not_supported(what) abort
|
||||
return lsp#utils#error(a:what.' not supported for '.&filetype)
|
||||
endfunction
|
||||
|
||||
function! lsp#ui#vim#hover#get_hover_under_cursor() abort
|
||||
let l:servers = filter(lsp#get_allowed_servers(), 'lsp#capabilities#has_hover_provider(v:val)')
|
||||
|
||||
if len(l:servers) == 0
|
||||
call s:not_supported('Retrieving hover')
|
||||
return
|
||||
endif
|
||||
|
||||
for l:server in l:servers
|
||||
call lsp#send_request(l:server, {
|
||||
\ 'method': 'textDocument/hover',
|
||||
\ 'params': {
|
||||
\ 'textDocument': lsp#get_text_document_identifier(),
|
||||
\ 'position': lsp#get_position(),
|
||||
\ },
|
||||
\ 'on_notification': function('s:handle_hover', [l:server]),
|
||||
\ })
|
||||
endfor
|
||||
|
||||
echo 'Retrieving hover ...'
|
||||
endfunction
|
||||
|
||||
function! s:handle_hover(server, data) abort
|
||||
if lsp#client#is_error(a:data['response'])
|
||||
call lsp#utils#error('Failed to retrieve hover information for ' . a:server)
|
||||
return
|
||||
endif
|
||||
|
||||
if !has_key(a:data['response'], 'result')
|
||||
return
|
||||
endif
|
||||
|
||||
if !empty(a:data['response']['result']) && !empty(a:data['response']['result']['contents'])
|
||||
call lsp#ui#vim#output#preview(a:server, a:data['response']['result']['contents'], {'statusline': ' LSP Hover'})
|
||||
return
|
||||
else
|
||||
call lsp#utils#error('No hover information found')
|
||||
endif
|
||||
endfunction
|
||||
@@ -92,7 +92,7 @@ command! -nargs=? LspDocumentDiagnostics call lsp#internal#diagnostics#document_
|
||||
\ 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=? -complete=customlist,lsp#utils#empty_complete LspHover call lsp#internal#document_hover#under_cursor#do({})
|
||||
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>)
|
||||
@@ -140,7 +140,7 @@ 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-symbol-search) :<c-u>call lsp#internal#document_symbol#search#do({})<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-hover) :<c-u>call lsp#internal#document_hover#under_cursor#do({})<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#internal#diagnostics#movement#_next_error()<cr>
|
||||
|
||||
Reference in New Issue
Block a user