Skip to content

Instantly share code, notes, and snippets.

@bbtdev
Created August 17, 2018 18:56
Show Gist options
  • Save bbtdev/16c335f67f294d155350051db7f76f21 to your computer and use it in GitHub Desktop.
Save bbtdev/16c335f67f294d155350051db7f76f21 to your computer and use it in GitHub Desktop.
vim-nvim-lsp-snippets-fixes
link https://github.com/autozimu/LanguageClient-neovim/issues/379
function! ExpandLspSnippet()
call UltiSnips#ExpandSnippetOrJump()
if !pumvisible() || empty(v:completed_item)
return ''
endif
" only expand Lsp if UltiSnips#ExpandSnippetOrJump not effect.
let l:value = v:completed_item['word']
let l:matched = len(l:value)
if l:matched <= 0
return ''
endif
" remove inserted chars before expand snippet
if col('.') == col('$')
let l:matched -= 1
exec 'normal! ' . l:matched . 'Xx'
else
exec 'normal! ' . l:matched . 'X'
endif
if col('.') == col('$') - 1
" move to $ if at the end of line.
call cursor(line('.'), col('$'))
endif
" expand snippet now.
call UltiSnips#Anon(l:value)
return ''
endfunction
imap <C-k> <C-R>=ExpandLspSnippet()<CR>
------------------------------
Here's a slight tweak to @lisongmin's tweak to allow the use of the Enter key:
Change the initial return to return a newline:
if !pumvisible() || empty(v:completed_item)
return '^M'
endif
where ^M is the output of <C-v><C-m>. Then you can map <CR>:
imap <silent> <CR> <C-r>=ExpandLspSnippet()<CR>
And after selecting a completion you can hit Enter to start the expansion.
link https://github.com/autozimu/LanguageClient-neovim/issues/379
let g:ulti_expand_res = 0 "default value, just set once
function! CompleteSnippet()
if empty(v:completed_item)
return
endif
call UltiSnips#ExpandSnippet()
if g:ulti_expand_res > 0
return
endif
let l:complete = type(v:completed_item) == v:t_dict ? v:completed_item.word : v:completed_item
let l:comp_len = len(l:complete)
let l:cur_col = mode() == 'i' ? col('.') - 2 : col('.') - 1
let l:cur_line = getline('.')
let l:start = l:comp_len <= l:cur_col ? l:cur_line[:l:cur_col - l:comp_len] : ''
let l:end = l:cur_col < len(l:cur_line) ? l:cur_line[l:cur_col + 1 :] : ''
call setline('.', l:start . l:end)
call cursor('.', l:cur_col - l:comp_len + 2)
call UltiSnips#Anon(l:complete)
endfunction
autocmd CompleteDone * call CompleteSnippet()
These are the mappings I use, although any mappings should work with the autocommand:
imap <silent><expr> <tab> pumvisible() ? "\<c-y>" : "\<tab>"
let g:UltiSnipsExpandTrigger="<NUL>"
let g:UltiSnipsListSnippets="<NUL>"
let g:UltiSnipsJumpForwardTrigger="<tab>"
let g:UltiSnipsJumpBackwardTrigger="<s-tab>"
needs ncm2/ncm2-ultisnips
link: https://github.com/ncm2/ncm2-ultisnips/issues/6
"" UltiSnips+NCM function parameter expansion UltiSnips+NCM function parame
" We don't really want UltiSnips to map these two, but there's no option for
" that so just make it map them to a <Plug> key.
let g:UltiSnipsExpandTrigger = "<Plug>(ultisnips_expand_or_jump)"
let g:UltiSnipsJumpForwardTrigger = "<Plug>(ultisnips_expand_or_jump)"
" Let UltiSnips bind the jump backward trigger as there's nothing special
" about it.
let g:UltiSnipsJumpBackwardTrigger = "<S-Tab>"
" Try expanding snippet or jumping with UltiSnips and return <Tab> if nothing
" worked.
function! UltiSnipsExpandOrJumpOrTab()
call UltiSnips#ExpandSnippetOrJump()
if g:ulti_expand_or_jump_res > 0
return ""
else
return "\<Tab>"
endif
endfunction
" First try expanding with ncm2_ultisnips. This does both LSP snippets and
" normal snippets when there's a completion popup visible.
inoremap <silent> <expr> <Tab> ncm2_ultisnips#expand_or("\<Plug>(ultisnips_try_expand)")
" If that failed, try the UltiSnips expand or jump function. This handles
" short snippets when the completion popup isn't visible yet as well as
" jumping forward from the insert mode. Writes <Tab> if there is no special
" action taken.
inoremap <silent> <Plug>(ultisnips_try_expand) <C-R>=UltiSnipsExpandOrJumpOrTab()<CR>
" Select mode mapping for jumping forward with <Tab>.
snoremap <silent> <Tab> <Esc>:call UltiSnips#ExpandSnippetOrJump()<cr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment