Skip to content

Commit fef6079

Browse files
hrsh7thprabirshrestha
authored andcommitted
Improve signature help with lexima.vim (#638)
* Improve signature help with lexima.vim * Fix buffer change problem * Display signature help after cursor jumped * Fix event * Add _ prefix for inner functions
1 parent 6bb1252 commit fef6079

File tree

5 files changed

+103
-15
lines changed

5 files changed

+103
-15
lines changed

autoload/lsp.vim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ function! lsp#enable() abort
5858
if g:lsp_highlights_enabled | call lsp#ui#vim#highlights#enable() | endif
5959
if g:lsp_textprop_enabled | call lsp#ui#vim#diagnostics#textprop#enable() | endif
6060
endif
61+
if g:lsp_signature_help_enabled
62+
call lsp#ui#vim#signature_help#setup()
63+
endif
6164
call lsp#ui#vim#completion#_setup()
6265
call s:register_events()
6366
endfunction
@@ -744,9 +747,6 @@ function! s:handle_initialize(server_name, data) abort
744747
for l:Init_callback in l:init_callbacks
745748
call l:Init_callback(a:data)
746749
endfor
747-
if g:lsp_signature_help_enabled
748-
call lsp#ui#vim#signature_help#setup()
749-
endif
750750

751751
doautocmd User lsp_server_init
752752
endfunction

autoload/lsp/ui/vim/output.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ function! lsp#ui#vim#output#preview(server, data, options) abort
292292
\ && type(g:lsp_preview_doubletap) == 3
293293
\ && len(g:lsp_preview_doubletap) >= 1
294294
\ && type(g:lsp_preview_doubletap[0]) == 2
295-
\ && mode()[0] !=# 'i'
295+
\ && index(['i', 's'], mode()[0]) == -1
296296
echo ''
297297
return call(g:lsp_preview_doubletap[0], [])
298298
endif

autoload/lsp/ui/vim/signature_help.vim

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
let s:debounce_timer_id = 0
2+
13
function! s:not_supported(what) abort
24
return lsp#utils#error(a:what.' not supported for '.&filetype)
35
endfunction
@@ -11,7 +13,6 @@ function! lsp#ui#vim#signature_help#get_signature_help_under_cursor() abort
1113
endif
1214

1315
let l:position = lsp#get_position()
14-
let l:position.character += 1
1516
for l:server in l:servers
1617
call lsp#send_request(l:server, {
1718
\ 'method': 'textDocument/signatureHelp',
@@ -110,21 +111,37 @@ function! s:get_parameter_doc(parameter) abort
110111
return printf('***%s*** - %s', a:parameter['label'], l:doc)
111112
endfunction
112113

113-
function! s:insert_char_pre() abort
114-
let l:buf = bufnr('%')
115-
for l:server_name in lsp#get_whitelisted_servers(l:buf)
116-
let l:keys = lsp#capabilities#get_signature_help_trigger_characters(l:server_name)
117-
for l:key in l:keys
118-
if l:key ==# v:char
119-
call timer_start(0, {_-> lsp#ui#vim#signature_help#get_signature_help_under_cursor() })
120-
endif
121-
endfor
114+
function! s:on_cursor_moved() abort
115+
let l:bufnr = bufnr('%')
116+
call timer_stop(s:debounce_timer_id)
117+
let s:debounce_timer_id = timer_start(200, { -> s:on_text_changed_after(l:bufnr) }, { 'repeat': 1 })
118+
endfunction
119+
120+
function! s:on_text_changed_after(bufnr) abort
121+
if bufnr('%') != a:bufnr
122+
return
123+
endif
124+
if index(['i', 's'], mode()[0]) == -1
125+
return
126+
endif
127+
if win_id2win(lsp#ui#vim#output#getpreviewwinid()) >= 1
128+
return
129+
endif
130+
131+
let l:chars = []
132+
for l:server_name in lsp#get_whitelisted_servers(a:bufnr)
133+
let l:chars += lsp#capabilities#get_signature_help_trigger_characters(l:server_name)
122134
endfor
135+
136+
if index(l:chars, lsp#utils#_get_before_char_skip_white()) >= 0
137+
call lsp#ui#vim#signature_help#get_signature_help_under_cursor()
138+
endif
123139
endfunction
124140

125141
function! lsp#ui#vim#signature_help#setup() abort
126142
augroup _lsp_signature_help_
127143
autocmd!
128-
autocmd InsertCharPre <buffer> call s:insert_char_pre()
144+
autocmd CursorMoved,CursorMovedI * call s:on_cursor_moved()
129145
augroup END
130146
endfunction
147+

autoload/lsp/utils.vim

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,36 @@ function! s:get_base64_alphabet() abort
232232
return l:alphabet
233233
endfunction
234234

235+
function! lsp#utils#_get_before_line() abort
236+
let l:text = getline('.')
237+
let l:idx = min([strlen(l:text), col('.') - 2])
238+
let l:idx = max([l:idx, -1])
239+
if l:idx == -1
240+
return ''
241+
endif
242+
return l:text[0 : l:idx]
243+
endfunction
244+
245+
function! lsp#utils#_get_before_char_skip_white() abort
246+
let l:current_lnum = line('.')
247+
248+
let l:lnum = l:current_lnum
249+
while l:lnum > 0
250+
if l:lnum == l:current_lnum
251+
let l:text = lsp#utils#_get_before_line()
252+
else
253+
let l:text = getline(l:lnum)
254+
endif
255+
let l:match = matchlist(l:text, '\([^[:blank:]]\)\s*$')
256+
if get(l:match, 1, v:null) isnot v:null
257+
return l:match[1]
258+
endif
259+
let l:lnum -= 1
260+
endwhile
261+
262+
return ''
263+
endfunction
264+
235265
let s:alphabet = s:get_base64_alphabet()
236266

237267
function! lsp#utils#base64_decode(data) abort

test/lsp/utils.vimspec

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,47 @@ Describe lsp#utils
110110
End
111111
End
112112

113+
Describe lsp#utils#_get_before_line
114+
It should return line before cursor on col=1
115+
enew!
116+
call setline(1, ['123456789'])
117+
call cursor(1, 1)
118+
Assert Equals(lsp#utils#_get_before_line(), '')
119+
End
120+
121+
It should return line before cursor on col=$
122+
let l:saved_virtualedit = &virtualedit
123+
let &virtualedit = 'all'
124+
enew!
125+
call setline(1, ['123456789'])
126+
call cursor(1, 10)
127+
Assert Equals(lsp#utils#_get_before_line(), '123456789')
128+
let &virtualedit = l:saved_virtualedit
129+
End
130+
131+
It should return line before cursor with multibyte
132+
enew!
133+
call setline(1, ['あいうえおabc'])
134+
call cursor(1, 18)
135+
Assert Equals(lsp#utils#_get_before_line(), 'あいうえおab')
136+
End
137+
End
138+
139+
Describe lsp#utils#_get_before_char_skip_white
140+
It should return before char in above of line
141+
enew!
142+
call setline(1, ['(', ''])
143+
call cursor(2, 1)
144+
Assert Equals(lsp#utils#_get_before_char_skip_white(), '(')
145+
End
146+
It should return before char with multibyte
147+
enew!
148+
call setline(1, ['あいうえお( '])
149+
call cursor(1, 21)
150+
Assert Equals(lsp#utils#_get_before_char_skip_white(), '(')
151+
End
152+
End
153+
113154
Describe lsp#utils#base64_decode
114155
It should decode basic string correctly
115156
Assert Equals(lsp#utils#base64_decode('TWFu'), [77, 97, 110])

0 commit comments

Comments
 (0)