|
| 1 | +function! s:not_supported(what) abort |
| 2 | + return lsp#utils#error(a:what.' not supported for '.&filetype) |
| 3 | +endfunction |
| 4 | + |
| 5 | +function! lsp#ui#vim#signature_help#get_signature_help_under_cursor() abort |
| 6 | + let l:servers = filter(lsp#get_whitelisted_servers(), 'lsp#capabilities#has_signature_help_provider(v:val)') |
| 7 | + |
| 8 | + if len(l:servers) == 0 |
| 9 | + call s:not_supported('Retrieving signature help') |
| 10 | + return |
| 11 | + endif |
| 12 | + |
| 13 | + let l:position = lsp#get_position() |
| 14 | + let l:position.character += 1 |
| 15 | + for l:server in l:servers |
| 16 | + call lsp#send_request(l:server, { |
| 17 | + \ 'method': 'textDocument/signatureHelp', |
| 18 | + \ 'params': { |
| 19 | + \ 'textDocument': lsp#get_text_document_identifier(), |
| 20 | + \ 'position': position, |
| 21 | + \ }, |
| 22 | + \ 'on_notification': function('s:handle_signature_help', [l:server]), |
| 23 | + \ }) |
| 24 | + endfor |
| 25 | + |
| 26 | + echo 'Retrieving signature help ...' |
| 27 | + return |
| 28 | +endfunction |
| 29 | + |
| 30 | +function! s:handle_signature_help(server, data) abort |
| 31 | + if lsp#client#is_error(a:data['response']) |
| 32 | + call lsp#utils#error('Failed to retrieve signature help information for ' . a:server) |
| 33 | + return |
| 34 | + endif |
| 35 | + |
| 36 | + if !has_key(a:data['response'], 'result') |
| 37 | + return |
| 38 | + endif |
| 39 | + |
| 40 | + if !empty(a:data['response']['result']) && !empty(a:data['response']['result']['signatures']) |
| 41 | + let l:signature = a:data['response']['result']['signatures'][0] |
| 42 | + let l:contents = [l:signature['label']] |
| 43 | + if has_key(l:signature, 'documentation') |
| 44 | + call add(l:contents, l:signature['documentation']) |
| 45 | + endif |
| 46 | + call lsp#ui#vim#output#preview(l:contents, {'statusline': ' LSP SignatureHelp'}) |
| 47 | + return |
| 48 | + else |
| 49 | + " signature help is used while inserting. So this must be graceful. |
| 50 | + "call lsp#utils#error('No signature help information found') |
| 51 | + endif |
| 52 | +endfunction |
| 53 | + |
| 54 | +function! s:insert_char_pre() abort |
| 55 | + let l:buf = bufnr('%') |
| 56 | + for l:server_name in lsp#get_whitelisted_servers(l:buf) |
| 57 | + let l:keys = lsp#capabilities#get_signature_help_trigger_characters(l:server_name) |
| 58 | + for l:key in l:keys |
| 59 | + if l:key ==# v:char |
| 60 | + call timer_start(0, {_-> lsp#ui#vim#signature_help#get_signature_help_under_cursor() }) |
| 61 | + endif |
| 62 | + endfor |
| 63 | + endfor |
| 64 | +endfunction |
| 65 | + |
| 66 | +function! lsp#ui#vim#signature_help#setup() abort |
| 67 | + augroup _lsp_signature_help_ |
| 68 | + autocmd! |
| 69 | + autocmd InsertCharPre <buffer> call s:insert_char_pre() |
| 70 | + augroup END |
| 71 | +endfunction |
0 commit comments