Closed devth closed 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.
" 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>")
@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.
@sassanh
My guess, it happens when you replace imap
with inoremap
@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.
running
:imap <plug>(cm_inject_snippet)
:imap <plug>(expand_or_nl)
shows these are defined.
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)
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.
Close. Seems to be fixed.
@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
@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?
@sassanh Thanks!
I found Lexima much more flexible, and very easy to configure. It's a very powerful plugin
It seems to be well implemented but doesn't have an equivalent for <m-e>
in autopairs.
@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()
?
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
.
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.
Using:
After
ctrl-j
ing down to the snip I want to expand, I hit enter, but a newline is created instead of expanding the snippet.