Skip to content

Commit

Permalink
Fix insert text (prabirshrestha#688)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
mattn and hrsh7th committed Jan 28, 2020
1 parent bfca7a9 commit 2c9a2c6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
22 changes: 13 additions & 9 deletions autoload/lsp/omni.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''
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']
let l:abbr = 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')
Expand Down
8 changes: 8 additions & 0 deletions autoload/lsp/utils.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 21 additions & 0 deletions test/lsp/utils.vimspec
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2c9a2c6

Please sign in to comment.