Skip to content

Commit 2c9a2c6

Browse files
mattnhrsh7th
andcommitted
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 <[email protected]>
1 parent bfca7a9 commit 2c9a2c6

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

autoload/lsp/omni.vim

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -237,17 +237,21 @@ endfunction
237237
function! lsp#omni#default_get_vim_completion_item(item, ...) abort
238238
let l:server_name = get(a:, 1, '')
239239

240-
if g:lsp_insert_text_enabled && has_key(a:item, 'insertText') && !empty(a:item['insertText'])
241-
if has_key(a:item, 'insertTextFormat') && a:item['insertTextFormat'] != 1
242-
let l:word = a:item['label']
243-
else
244-
let l:word = a:item['insertText']
245-
endif
246-
let l:abbr = a:item['label']
247-
else
240+
let l:word = ''
241+
if get(a:item, 'insertTextFormat', -1) == 2 && !empty(get(a:item, 'insertText', ''))
242+
" if candidate is snippet, use insertText. But it may include
243+
" placeholder.
244+
let l:word = lsp#utils#make_valid_word(a:item['insertText'])
245+
elseif !empty(get(a:item, 'insertText', ''))
246+
" if plain-text insertText, use it.
247+
let l:word = a:item['insertText']
248+
elseif has_key(a:item, 'textEdit')
249+
let l:word = lsp#utils#make_valid_word(a:item['label'])
250+
endif
251+
if empty(l:word)
248252
let l:word = a:item['label']
249-
let l:abbr = a:item['label']
250253
endif
254+
let l:abbr = a:item['label']
251255

252256
if has_key(a:item, 'insertTextFormat') && a:item['insertTextFormat'] == 2
253257
let l:word = substitute(l:word, '\<\$[0-9]\+\|\${[^}]\+}\>', '', 'g')

autoload/lsp/utils.vim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,11 @@ function! lsp#utils#base64_decode(data) abort
311311

312312
return l:ret
313313
endfunction
314+
315+
function! lsp#utils#make_valid_word(str) abort
316+
let l:str = matchstr(a:str, '^[^ (<{\[\t\r\n]\+')
317+
if l:str =~# ':$'
318+
return l:str[:-2]
319+
endif
320+
return l:str
321+
endfunction

test/lsp/utils.vimspec

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,25 @@ Describe lsp#utils
172172
Assert Equals(lsp#utils#base64_decode('AAAAEgAJABYAAAAIAAQAFw=='), [0, 0, 0, 18, 0, 9, 0, 22, 0, 0, 0, 8, 0, 4, 0, 23])
173173
End
174174
End
175+
176+
Describe lsp#utils#make_valid_word
177+
It should make valid word
178+
Assert Equals(lsp#utils#make_valid_word('my-word'), 'my-word')
179+
Assert Equals(lsp#utils#make_valid_word(' my-word'), '')
180+
Assert Equals(lsp#utils#make_valid_word("my\nword"), 'my')
181+
Assert Equals(lsp#utils#make_valid_word('my-word: description'), 'my-word')
182+
Assert Equals(lsp#utils#make_valid_word('my-word : description'), 'my-word')
183+
Assert Equals(lsp#utils#make_valid_word('my-word is word'), 'my-word')
184+
Assert Equals(lsp#utils#make_valid_word('my-func()'), 'my-func')
185+
Assert Equals(lsp#utils#make_valid_word('my-name::space'), 'my-name::space')
186+
Assert Equals(lsp#utils#make_valid_word('my-name#space'), 'my-name#space')
187+
Assert Equals(lsp#utils#make_valid_word('my-name.space'), 'my-name.space')
188+
Assert Equals(lsp#utils#make_valid_word('my-name.space: foo'), 'my-name.space')
189+
Assert Equals(lsp#utils#make_valid_word('my-name%space: foo'), 'my-name%space')
190+
Assert Equals(lsp#utils#make_valid_word('my-name&space: foo'), 'my-name&space')
191+
Assert Equals(lsp#utils#make_valid_word('my-array[0]'), 'my-array')
192+
Assert Equals(lsp#utils#make_valid_word('my-array<string>'), 'my-array')
193+
Assert Equals(lsp#utils#make_valid_word("my-name\tdescription"), 'my-name')
194+
End
195+
End
175196
End

0 commit comments

Comments
 (0)