Skip to content

Commit 96579bd

Browse files
mattnprabirshrestha
authored andcommitted
Signature help (#479)
* Add signatureHelp * Use <buffer> * Check lsp_use_trigger_characters_for_signature_help * Rename to lsp_signature_help_enabled * Use InsertCharPre * Add abort * Check documentation exists
1 parent 63b9bd6 commit 96579bd

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed

autoload/lsp.vim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,9 @@ function! s:handle_initialize(server_name, data) abort
659659
for l:Init_callback in l:init_callbacks
660660
call l:Init_callback(a:data)
661661
endfor
662+
if g:lsp_signature_help_enabled
663+
call lsp#ui#vim#signature_help#setup()
664+
endif
662665

663666
doautocmd User lsp_server_init
664667
endfunction

autoload/lsp/capabilities.vim

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,23 @@ function! lsp#capabilities#get_text_document_change_sync_kind(server_name) abort
103103
endif
104104
return 1
105105
endfunction
106+
107+
function! lsp#capabilities#has_signature_help_provider(server_name) abort
108+
let l:capabilities = lsp#get_server_capabilities(a:server_name)
109+
if !empty(l:capabilities) && has_key(l:capabilities, 'signatureHelpProvider')
110+
return 1
111+
endif
112+
return 0
113+
endfunction
114+
115+
function! lsp#capabilities#get_signature_help_trigger_characters(server_name) abort
116+
let l:capabilities = lsp#get_server_capabilities(a:server_name)
117+
if !empty(l:capabilities) && has_key(l:capabilities, 'signatureHelpProvider')
118+
if type(l:capabilities['signatureHelpProvider']) == type({})
119+
if has_key(l:capabilities['signatureHelpProvider'], 'triggerCharacters')
120+
return l:capabilities['signatureHelpProvider']['triggerCharacters']
121+
endif
122+
endif
123+
endif
124+
return []
125+
endfunction
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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

doc/vim-lsp.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ CONTENTS *vim-lsp-contents*
3131
g:lsp_peek_alignment |g:lsp_peek_alignment|
3232
g:lsp_preview_max_width |g:lsp_preview_max_width|
3333
g:lsp_preview_max_height |g:lsp_preview_max_height|
34+
g:lsp_signature_help_enabled |g:lsp_signature_help_enabled|
3435
Functions |vim-lsp-functions|
3536
enable |vim-lsp-enable|
3637
disable |vim-lsp-disable|
@@ -454,6 +455,17 @@ g:lsp_preview_max_height *g:lsp_preview_max_height*
454455
If positive, determines the maximum height of the preview window in
455456
characters. Use a value of `-1` to disable setting a maximum height.
456457

458+
g:lsp_signature_help_enabled *g:lsp_signature_help_enabled*
459+
Type: |Number|
460+
Default: `1`
461+
462+
Enable support for signature help. Set to `0` to disable.
463+
464+
Example:
465+
>
466+
let g:lsp_signature_help_enabled = 1
467+
let g:lsp_signature_help_enabled = 0
468+
457469
===============================================================================
458470
FUNCTIONS *vim-lsp-functions*
459471

plugin/lsp.vim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ let g:lsp_preview_doubletap = get(g:, 'lsp_preview_doubletap', [function('lsp#ui
3232
let g:lsp_peek_alignment = get(g:, 'lsp_peek_alignment', 'center')
3333
let g:lsp_preview_max_width = get(g:, 'lsp_preview_max_width', -1)
3434
let g:lsp_preview_max_height = get(g:, 'lsp_preview_max_height', -1)
35+
let g:lsp_signature_help_enabled = get(g:, 'lsp_signature_help_enabled', 1)
3536

3637
let g:lsp_get_vim_completion_item = get(g:, 'lsp_get_vim_completion_item', [function('lsp#omni#default_get_vim_completion_item')])
3738
let g:lsp_get_supported_capabilities = get(g:, 'lsp_get_supported_capabilities', [function('lsp#default_get_supported_capabilities')])
@@ -68,6 +69,7 @@ command! LspTypeDefinition call lsp#ui#vim#type_definition()
6869
command! -nargs=0 LspStatus echo lsp#get_server_status()
6970
command! LspNextReference call lsp#ui#vim#references#jump(+1)
7071
command! LspPreviousReference call lsp#ui#vim#references#jump(-1)
72+
command! -nargs=? -complete=customlist,lsp#utils#empty_complete LspSignatureHelp call lsp#ui#vim#signature_help#get_signature_help_under_cursor()
7173

7274
nnoremap <plug>(lsp-code-action) :<c-u>call lsp#ui#vim#code_action()<cr>
7375
nnoremap <plug>(lsp-declaration) :<c-u>call lsp#ui#vim#declaration(0)<cr>
@@ -93,3 +95,4 @@ nnoremap <plug>(lsp-peek-implementation) :<c-u>call lsp#ui#vim#implementation(1)
9395
nnoremap <plug>(lsp-status) :<c-u>echo lsp#get_server_status()<cr>
9496
nnoremap <plug>(lsp-next-reference) :<c-u>call lsp#ui#vim#references#jump(+1)<cr>
9597
nnoremap <plug>(lsp-previous-reference) :<c-u>call lsp#ui#vim#references#jump(-1)<cr>
98+
nnoremap <plug>(lsp-signature-help) :<c-u>call lsp#ui#vim#signature_help#get_signature_help_under_cursor()<cr>

0 commit comments

Comments
 (0)