mirror of
https://github.com/prabirshrestha/vim-lsp.git
synced 2025-12-14 20:35:59 +01:00
Fix insert text (#688)
* Fix insert text When insertTextFormat==2, it should prefer insertText. But it may include placeholder. When insertTextFormat!=2, use insertText since it is plain-text. When it have textEdit, the inserted text will be modified in later, so insert only word. Otherwize, insert label since it should be what the server want to insert. * Check with valid word pattern * ":" should be invalid character * Add lsp#utils#make_valid_word * Add l: prefix * Add test * Add test * \t should be ignored Co-authored-by: hrsh7th <hrsh7th@gmail.com>
This commit is contained in:
@@ -237,17 +237,21 @@ endfunction
|
||||
function! lsp#omni#default_get_vim_completion_item(item, ...) abort
|
||||
let l:server_name = get(a:, 1, '')
|
||||
|
||||
if g:lsp_insert_text_enabled && has_key(a:item, 'insertText') && !empty(a:item['insertText'])
|
||||
if has_key(a:item, 'insertTextFormat') && a:item['insertTextFormat'] != 1
|
||||
let l:word = a:item['label']
|
||||
else
|
||||
let l:word = a:item['insertText']
|
||||
endif
|
||||
let l:abbr = a:item['label']
|
||||
else
|
||||
let l:word = a:item['label']
|
||||
let l:abbr = a:item['label']
|
||||
let l:word = ''
|
||||
if get(a:item, 'insertTextFormat', -1) == 2 && !empty(get(a:item, 'insertText', ''))
|
||||
" if candidate is snippet, use insertText. But it may include
|
||||
" placeholder.
|
||||
let l:word = lsp#utils#make_valid_word(a:item['insertText'])
|
||||
elseif !empty(get(a:item, 'insertText', ''))
|
||||
" if plain-text insertText, use it.
|
||||
let l:word = a:item['insertText']
|
||||
elseif has_key(a:item, 'textEdit')
|
||||
let l:word = lsp#utils#make_valid_word(a:item['label'])
|
||||
endif
|
||||
if empty(l:word)
|
||||
let l:word = a:item['label']
|
||||
endif
|
||||
let l:abbr = a:item['label']
|
||||
|
||||
if has_key(a:item, 'insertTextFormat') && a:item['insertTextFormat'] == 2
|
||||
let l:word = substitute(l:word, '\<\$[0-9]\+\|\${[^}]\+}\>', '', 'g')
|
||||
|
||||
@@ -311,3 +311,11 @@ function! lsp#utils#base64_decode(data) abort
|
||||
|
||||
return l:ret
|
||||
endfunction
|
||||
|
||||
function! lsp#utils#make_valid_word(str) abort
|
||||
let l:str = matchstr(a:str, '^[^ (<{\[\t\r\n]\+')
|
||||
if l:str =~# ':$'
|
||||
return l:str[:-2]
|
||||
endif
|
||||
return l:str
|
||||
endfunction
|
||||
|
||||
@@ -172,4 +172,25 @@ Describe lsp#utils
|
||||
Assert Equals(lsp#utils#base64_decode('AAAAEgAJABYAAAAIAAQAFw=='), [0, 0, 0, 18, 0, 9, 0, 22, 0, 0, 0, 8, 0, 4, 0, 23])
|
||||
End
|
||||
End
|
||||
|
||||
Describe lsp#utils#make_valid_word
|
||||
It should make valid word
|
||||
Assert Equals(lsp#utils#make_valid_word('my-word'), 'my-word')
|
||||
Assert Equals(lsp#utils#make_valid_word(' my-word'), '')
|
||||
Assert Equals(lsp#utils#make_valid_word("my\nword"), 'my')
|
||||
Assert Equals(lsp#utils#make_valid_word('my-word: description'), 'my-word')
|
||||
Assert Equals(lsp#utils#make_valid_word('my-word : description'), 'my-word')
|
||||
Assert Equals(lsp#utils#make_valid_word('my-word is word'), 'my-word')
|
||||
Assert Equals(lsp#utils#make_valid_word('my-func()'), 'my-func')
|
||||
Assert Equals(lsp#utils#make_valid_word('my-name::space'), 'my-name::space')
|
||||
Assert Equals(lsp#utils#make_valid_word('my-name#space'), 'my-name#space')
|
||||
Assert Equals(lsp#utils#make_valid_word('my-name.space'), 'my-name.space')
|
||||
Assert Equals(lsp#utils#make_valid_word('my-name.space: foo'), 'my-name.space')
|
||||
Assert Equals(lsp#utils#make_valid_word('my-name%space: foo'), 'my-name%space')
|
||||
Assert Equals(lsp#utils#make_valid_word('my-name&space: foo'), 'my-name&space')
|
||||
Assert Equals(lsp#utils#make_valid_word('my-array[0]'), 'my-array')
|
||||
Assert Equals(lsp#utils#make_valid_word('my-array<string>'), 'my-array')
|
||||
Assert Equals(lsp#utils#make_valid_word("my-name\tdescription"), 'my-name')
|
||||
End
|
||||
End
|
||||
End
|
||||
|
||||
Reference in New Issue
Block a user