Skip to content

Commit f6a66a1

Browse files
authored
Fix cursor position after completion (#998)
* Fix cursor position after completion * Calculate end position * Add comments
1 parent 0985092 commit f6a66a1

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

autoload/lsp/ui/vim/completion.vim

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,26 @@ function! s:on_complete_done_after() abort
122122
\ 'character': l:position['character'] + l:overflow_after,
123123
\ }
124124
\ }
125+
125126
if get(l:completion_item, 'insertTextFormat', 1) == 2
127+
" insert Snippet.
126128
call lsp#utils#text_edit#apply_text_edits('%', [{ 'range': l:range, 'newText': '' }])
127129
if exists('g:lsp_snippet_expand') && len(g:lsp_snippet_expand) > 0
128130
call g:lsp_snippet_expand[0]({ 'snippet': l:text })
129131
else
130132
call s:simple_expand_text(l:text)
131133
endif
132134
else
135+
" apply TextEdit.
133136
call lsp#utils#text_edit#apply_text_edits('%', [{ 'range': l:range, 'newText': l:text }])
137+
138+
" The VSCode always apply completion word as snippet.
139+
" It means we should place cursor to end of new inserted text as snippet does.
140+
let l:lines = lsp#utils#_split_by_eol(l:text)
141+
let l:end = l:range.end
142+
let l:end.line += len(l:lines) - 1
143+
let l:end.character = strchars(l:text[-1]) + (len(l:lines) > 1 ? 0 : l:end.character)
144+
call cursor(lsp#utils#position#lsp_to_vim('%', l:end))
134145
endif
135146
endif
136147

0 commit comments

Comments
 (0)