roxma / nvim-completion-manager

:warning: PLEASE USE https://github.com/ncm2/ncm2 INSTEAD
MIT License
917 stars 49 forks source link

Ultisnips stopped working in visual mode #38

Closed rafaeln closed 7 years ago

rafaeln commented 7 years ago

I have the following LaTeX snipped:

snippet it "Italics" i
\\textit{${1:${VISUAL}}}${0}
endsnippet

Which allows me to visually select some text I want to italicize, say, the word text, press <tab>, which makes the text text disappear and puts me in insert mode, type it and press <tab> again, which then gives me \textit{text}. Somehow, if I'm using nvim-completion-manager, that stops working.

roxma commented 7 years ago

I guess this is due to the <Tab> mapping recommended by NCM conflicts your ultisnips mapping? If you remove this mapping from your vimrc, it should work.

inoremap <expr> <Tab> pumvisible() ? "\<C-n>" : "\<Tab>"
inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"
roxma commented 7 years ago

It's kind of tricky to integrated these mappings. Currently I'm using <c-j> as next (normal j is down key, easier to remember for me) and <c-k> as prev for jumping around snippet field, instead of <Tab>, to avoid mapping conflicts.

roxma commented 7 years ago

Just read Ultisnips documentation, here's what I've tried to integrate the mappings without conflict:

vnoremap <expr> <Plug>(snips_result_check) g:ulti_expand_or_jump_res?'':"\<Tab>"
inoremap <expr> <Plug>(snips_result_check) g:ulti_expand_or_jump_res?'':"\<Tab>"
imap <expr> <Tab> (pumvisible() ? "\<C-n>" : "\<C-r>=UltiSnips#ExpandSnippetOrJump()\<cr>\<Plug>(snips_result_check)")

And I think it could be easy to extend to use <s-tab>.

roxma commented 7 years ago

Just tweaked a complete version, this works on my neovim:

    let g:UltiSnipsExpandTrigger        = "<Plug>(ultisnips_expand)"
    let g:UltiSnipsJumpForwardTrigger   = "<Plug>(ultisnips_expand)"
    let g:UltiSnipsJumpBackwardTrigger  = "<Plug>(ultisnips_backward)"
    let g:UltiSnipsListSnippets         = "<Plug>(ultisnips_list)"
    let g:UltiSnipsRemoveSelectModeMappings = 0 
    vnoremap <expr> <Plug>(ultisnip_expand_or_jump_result) g:ulti_expand_or_jump_res?'':"\<Tab>"
    inoremap <expr> <Plug>(ultisnip_expand_or_jump_result) g:ulti_expand_or_jump_res?'':"\<Tab>"
    imap <expr> <Tab> (pumvisible() ? "\<C-n>" : "\<C-r>=UltiSnips#ExpandSnippetOrJump()\<cr>\<Plug>(ultisnip_expand_or_jump_result)")
    xmap <Tab> <Plug>(ultisnips_expand)
    smap <Tab> <Plug>(ultisnips_expand)
    autocmd! User UltiSnipsEnterFirstSnippet xmap <Tab> <Plug>(ultisnips_expand)
    autocmd! User UltiSnipsEnterFirstSnippet smap <Tab> <Plug>(ultisnips_expand)

    vnoremap <expr> <Plug>(ultisnips_backwards_result) g:ulti_jump_backwards_res?'':"\<S-Tab>"
    inoremap <expr> <Plug>(ultisnips_backwards_result) g:ulti_jump_backwards_res?'':"\<S-Tab>"
    imap <expr> <S-Tab> (pumvisible() ? "\<C-p>" : "\<C-r>=UltiSnips#JumpBackwards()\<cr>\<Plug>(ultisnips_backwards_result)")
    xmap <S-Tab> <Plug>(ultisnips_backward)
    smap <S-Tab> <Plug>(ultisnips_backward)
roxma commented 7 years ago

Remove the <Tab> mapping provided by NCM, and the trigger keys configuration for ultisnips,

Add the following code to your vimrc, this version should work better for both plugins.

And if there's an openning popup menu get in the way, use <c-y> to close the popup menu. Or simple map another key, <c-u> in this example, to trigger ultisnips.

    let g:UltiSnipsExpandTrigger        = "<Plug>(ultisnips_expand)"
    let g:UltiSnipsJumpForwardTrigger   = "<Plug>(ultisnips_expand)"
    let g:UltiSnipsJumpBackwardTrigger  = "<Plug>(ultisnips_backward)"
    let g:UltiSnipsListSnippets         = "<Plug>(ultisnips_list)"
    let g:UltiSnipsRemoveSelectModeMappings = 0 

    vnoremap <expr> <Plug>(ultisnip_expand_or_jump_result) g:ulti_expand_or_jump_res?'':"\<Tab>"
    inoremap <expr> <Plug>(ultisnip_expand_or_jump_result) g:ulti_expand_or_jump_res?'':"\<Tab>"
    imap <silent> <expr> <Tab> (pumvisible() ? "\<C-n>" : "\<C-r>=UltiSnips#ExpandSnippetOrJump()\<cr>\<Plug>(ultisnip_expand_or_jump_result)")
    xmap <Tab> <Plug>(ultisnips_expand)
    smap <Tab> <Plug>(ultisnips_expand)

    vnoremap <expr> <Plug>(ultisnips_backwards_result) g:ulti_jump_backwards_res?'':"\<S-Tab>"
    inoremap <expr> <Plug>(ultisnips_backwards_result) g:ulti_jump_backwards_res?'':"\<S-Tab>"
    imap <silent> <expr> <S-Tab> (pumvisible() ? "\<C-p>" : "\<C-r>=UltiSnips#JumpBackwards()\<cr>\<Plug>(ultisnips_backwards_result)")
    xmap <S-Tab> <Plug>(ultisnips_backward)
    smap <S-Tab> <Plug>(ultisnips_backward)

    " optional
    inoremap <silent> <c-u> <c-r>=cm#sources#ultisnips#trigger_or_popup("\<Plug>(ultisnips_expand)")<cr>
roxma commented 7 years ago

neosnippet version:

    imap <expr> <Tab> (pumvisible() ? "\<C-n>" : (neosnippet#mappings#expand_or_jump_impl()!=''?neosnippet#mappings#expand_or_jump_impl():"\<Tab>"))
    smap <Tab>     <Plug>(neosnippet_expand_or_jump)  
    xmap <Tab>     <Plug>(neosnippet_expand_target)   
    " neosnippet doesn't have jump back key
    imap <expr> <S-Tab> (pumvisible() ? "\<C-p>" : "\<S-Tab>")

@Shougo I found this is also useful for deoplete.nvim with ultisnips or neosnippet

Shougo commented 7 years ago

@Shougo I found this is also useful for deoplete.nvim with ultisnips or neosnippet

It is already explained in neosnippet examples.

rafaeln commented 7 years ago

Now this works perfectly for me! Thanks!

partounian commented 7 years ago

I just moved all of my config from deoplete and more to NCM and neosnippet.

It was pretty smooth but I can't figure out how to expand snippets. (I am using the code you have shown above). At this moment tab also goes through the list of autocomplete suggestions. Also, I seem to be getting snippets as part of the menu, but autocompletion of words in the buffer only at times.

Thank you for this awesome plugin, and your work with neovim in general. @roxma and @Shougo

roxma commented 7 years ago

@partounian

Could you provide detailed description of how to reproduce the problem and your minimal vimrc?

partounian commented 7 years ago

@roxma

Well before I break up my vim, my question is mainly about this snippet.

imap <expr> <Tab> (pumvisible() ? "\<C-n>" : (neosnippet#mappings#expand_or_jump_impl()!=''?neosnippet#mappings#expand_or_jump_impl():"\<Tab>"))
smap <Tab> <Plug>(neosnippet_expand_or_jump)  
xmap <Tab> <Plug>(neosnippet_expand_target)   
" neosnippet doesn't have jump back key
imap <expr> <S-Tab> (pumvisible() ? "\<C-p>" : "\<S-Tab>")

It seems that Tab is what is used to expand a Snippet? I am confused on how it is supposed to expand when it only goes through the list of autocomplete options.

roxma commented 7 years ago
imap <expr> <Tab> (pumvisible() ? "\<C-n>" : (neosnippet#mappings#expand_or_jump_impl()!=''?neosnippet#mappings#expand_or_jump_impl():"\<Tab>"))

This mappings use <tab> to move over the completion items when popup menu is visible.

If you want to expand a snippet, you need to close the popup menu first, with a <c-y> key for example.

This mapping is just a trick but I personally don't use this mapping, it is not as handy as it seems. I prefer to use a separate key for expanding snippet, and going through placeholders.