0

Background

I'm using UltiSnips in conjunction with some autocomplete plugins. I have this line controlling the behavior of enter

inoremap <expr> <cr>
            \ !empty(UltiSnips#SnippetsInCurrentScope()) ? 
            \ "\<C-R>=UltiSnips#ExpandSnippet()\<cr>" :
            \ pumvisible() ? "\<c-y>" : "\<cr>"

The idea is to check for snippets (and expand if necessary) then check for autocomplete suggestions (and select that if it is available) and if nothing is going on, use enter.

The Problem

When I press enter on a blank line I don't get a new line. I figured out that what's happening is UltiSnips#SnippetsInCurrentScope() returns a bunch of my snippets when I'm at the beginning of a blank line.

The idea of using UltiSnips#SnippetsInCurrentScope() is from the UltiSnips docs (part 6, the FAQ) as a stand in for an "IsExpandable" function.

Question

Is there another function that captures the spirit of what I want better? If not what is the best way to achieve what I want (just a case that checks if the lines empty?)?

Thanks in advance

0

UltiSnips#SnippetsInCurrentScope return all snippets whose trigger matches the current word, I think the match here means =~, not ==. You need to check word before cursor and the triggers by yourself:

function! s:is_ultisnips_expandable()

  " get word before cursor
  let word = matchstr(getline('.'), printf('\v\w*%%%dc\w', col('.') - 1))
  if(empty(word))
    return 0
  endif

  " test word against triggers
  return !empty(filter(UltiSnips#SnippetsInCurrentScope(), {k,v -> k ==# word}))

endfunction


inoremap <expr> <cr>
            \ <sid>is_ultisnips_expandable() ?
            \ "<C-R>=UltiSnips#ExpandSnippet()<cr>" :
            \ pumvisible() ? "<c-y>" : "<cr>"

Note that I change your \<xxxx> to <xxxx>, one should not use \<xxxx> in map command, check here for detail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.