roxma / nvim-completion-manager

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

UltiSnips won't expand when pressing enter #89

Closed devth closed 7 years ago

devth commented 7 years ago

Using:

  " When the <Enter> key is pressed while the popup menu is visible, it only
  " hides the menu. Use this mapping to hide the menu and also start a new line.
  inoremap <expr> <CR> (pumvisible() ? "\<c-y>\<cr>" : "\<CR>")

  let g:UltiSnipsSnippetsDir='~/.vim_snippets'
  let g:UltiSnipsSnippetDirectories = ['UltiSnips', $HOME.'/.vim_snippets']

  let g:UltiSnipsExpandTrigger = "<Plug>(ultisnips_expand)"
  let g:UltiSnipsJumpForwardTrigger = "<c-j>"
  let g:UltiSnipsJumpBackwardTrigger    = "<c-k>"
  let g:UltiSnipsRemoveSelectModeMappings = 0
  inoremap <silent> <c-u> <c-r>=cm#sources#ultisnips#trigger_or_popup("\<Plug>(ultisnips_expand)")<cr>

After ctrl-jing down to the snip I want to expand, I hit enter, but a newline is created instead of expanding the snippet.

roxma commented 7 years ago

This has been asked before. https://github.com/roxma/nvim-completion-manager/issues/49#issuecomment-305974834

I've just pushed some updates.

Assuming you're using <c-u> for snippet expansion, try this if you want <Enter> key for expanding the snippet.

imap <expr> <CR>  (pumvisible() ?  "\<c-y>\<Plug>(cm_inject_snippet)\<Plug>(expand_or_nl)" : "\<CR>")
imap <expr> <Plug>(expand_or_nl) (has_key(v:completed_item,'snippet')?"\<C-U>":"\<CR>")

It needs two mapping. Because the second mapping expr should be evaluated after accepting the completion item.

// cc @sassanh


Edit:

Personally I don't enjoy this mapping. I'm using a saparate key for expanding snippet. It's more explicit and feels more under-control. You need to understand how it works and then tweak for what you need. The <Plug>(cm_inject_snippet) is added for supporting this feature at a minimum level.


Use this version instead:

" Assuming you're using `<c-u>` for snippet expansion
imap <expr> <CR>  (pumvisible() ?  "\<c-y>\<Plug>(expand_or_nl)" : "\<CR>")
imap <expr> <Plug>(expand_or_nl) (cm#completed_is_snippet() ? "\<C-U>":"\<CR>")
sassanh commented 7 years ago

@roxma after pressing <cr> when completion list is open, it appends "<Plug>(cm_inject_snippet)<Plug>(expand_or_nl)" to current position of cursor. I'm trying to find out why.

roxma commented 7 years ago

@sassanh

My guess, it happens when you replace imap with inoremap

sassanh commented 7 years ago

@roxma no it's imap, it's the complete line:

imap <expr> <CR>  (pumvisible() ?  "\<c-y>\<Plug>(cm_inject_snippet)\<Plug>(expand_or_nl)\<Plug>DiscretionaryEnd" : "\<CR>\<Plug>DiscretionaryEnd")

The weird thing is it doesn't append "<Plug>DiscretionaryEnd" but it prints first two plugs.

sassanh commented 7 years ago

running

:imap <plug>(cm_inject_snippet)
:imap <plug>(expand_or_nl)

shows these are defined.

sassanh commented 7 years ago

I changed it to

imap <expr> <CR>  (pumvisible() ?  "1" : "\<CR>\<Plug>DiscretionaryEnd")

running imap <cr> shows:

i  <CR>        &@<SNR>131_AutoPairsOldCRWrapper73<SNR>131_AutoPairsReturn
i  <CR>          (pumvisible() ?  "1" : "\<CR>\<Plug>DiscretionaryEnd")

but still when I type ima<tab><tab><cr> for example in a vim file it prints:

imap<Plug>(cm_inject_snippet)<Plug>(expand_or_nl)
sassanh commented 7 years ago

OK, autopairs plugin is ruining things.

For those who are using autopairs: I had to disable autopairss automatic <cr> mapping with:

let g:AutoPairsMapCR = 0

and put \<c-r>=AutoPairsReturn()\<cr> and the end of above mappings (in both sides of ternary if) and silent the mapping (<silent>), now it's working with autopairs.

roxma commented 7 years ago

Close. Seems to be fixed.

TxHawks commented 7 years ago

@sassanh - Can you share your how your ternary ended up? I'm trying to do something similar with lexima, but can't seem to make it work.

Thanks

sassanh commented 7 years ago

@TxHawks sure, this is my final ternary:

imap <expr> <Plug>(expand_or_nl) (has_key(v:completed_item,'snippet')?"\<c-x>" : "")
imap <expr> <silent> <cr>  (pumvisible() ? "\<c-y>\<Plug>(cm_inject_snippet)\<Plug>(expand_or_nl)\<Plug>DiscretionaryEnd\<c-r>=AutoPairsReturn()\<cr>" : "\<cr>\<Plug>DiscretionaryEnd\<c-r>=AutoPairsReturn()\<cr>")

Did you find lexima better than autopairs?

TxHawks commented 7 years ago

@sassanh Thanks!

I found Lexima much more flexible, and very easy to configure. It's a very powerful plugin

sassanh commented 7 years ago

It seems to be well implemented but doesn't have an equivalent for <m-e> in autopairs.

TxHawks commented 7 years ago

@sassanh - I don't think it does, but for me, it was very effective for things like Twig, which has patterns like {{- var -}} and {%- tagName -%}, which I couldn't manage to do with auto-pairs. See #101.

In your ternary, why is it <c-r>=AutoPairsReturn() and not <cr>=AutoPairsReturn()?

sassanh commented 7 years ago

AutoPairsReturn() returns a key sequence that should run in insert mode, so <c-r>=AutoParisReturn() inserts that key sequence in buffer, it's used to accomplish this in autopairs:

{
    |
}

after pressing return in a bracket. But I removed this part for lexima:

imap <expr> <silent> <cr>  (pumvisible() ? "\<c-y>\<Plug>(cm_inject_snippet)\<Plug>(expand_or_nl)\<Plug>DiscretionaryEnd" : "\<cr>\<Plug>DiscretionaryEnd")

Seems like lexima doesn't need it as it doesn't have a mechanism equivalent to AutoParisReturn. If you have problems with above mapping then I guess it should be related to another plugin and not lexima.

roxma commented 7 years ago

Update

Use this version instead:

" Assuming you're using `<c-u>` for snippet expansion
imap <expr> <CR>  (pumvisible() ?  "\<c-y>\<Plug>(expand_or_nl)" : "\<CR>")
imap <expr> <Plug>(expand_or_nl) (cm#completed_is_snippet() ? "\<C-U>":"\<CR>")

The previous version is deprecated. The check has_key(v:completed_item,'snippet') may not be valid in the future.