Fix cursor position after completion (#998)

* Fix cursor position after completion

* Calculate end position

* Add comments
This commit is contained in:
hrsh7th
2021-01-02 17:38:16 +09:00
committed by GitHub
parent 098509278d
commit f6a66a11b5

View File

@@ -122,7 +122,9 @@ function! s:on_complete_done_after() abort
\ 'character': l:position['character'] + l:overflow_after, \ 'character': l:position['character'] + l:overflow_after,
\ } \ }
\ } \ }
if get(l:completion_item, 'insertTextFormat', 1) == 2 if get(l:completion_item, 'insertTextFormat', 1) == 2
" insert Snippet.
call lsp#utils#text_edit#apply_text_edits('%', [{ 'range': l:range, 'newText': '' }]) call lsp#utils#text_edit#apply_text_edits('%', [{ 'range': l:range, 'newText': '' }])
if exists('g:lsp_snippet_expand') && len(g:lsp_snippet_expand) > 0 if exists('g:lsp_snippet_expand') && len(g:lsp_snippet_expand) > 0
call g:lsp_snippet_expand[0]({ 'snippet': l:text }) call g:lsp_snippet_expand[0]({ 'snippet': l:text })
@@ -130,7 +132,16 @@ function! s:on_complete_done_after() abort
call s:simple_expand_text(l:text) call s:simple_expand_text(l:text)
endif endif
else else
" apply TextEdit.
call lsp#utils#text_edit#apply_text_edits('%', [{ 'range': l:range, 'newText': l:text }]) call lsp#utils#text_edit#apply_text_edits('%', [{ 'range': l:range, 'newText': l:text }])
" The VSCode always apply completion word as snippet.
" It means we should place cursor to end of new inserted text as snippet does.
let l:lines = lsp#utils#_split_by_eol(l:text)
let l:end = l:range.end
let l:end.line += len(l:lines) - 1
let l:end.character = strchars(l:text[-1]) + (len(l:lines) > 1 ? 0 : l:end.character)
call cursor(lsp#utils#position#lsp_to_vim('%', l:end))
endif endif
endif endif