Shougo / neosnippet.vim

neo-snippet plugin
Other
1.12k stars 108 forks source link

neosnippet#expand does nothing #433

Closed naquad closed 6 years ago

naquad commented 6 years ago

Hi.

I'm trying to implement a command that will expand given snippet (need it for Denite menu). Currently it looks like this:

command! -nargs=1 ExpandSnippet :call neosnippet#expand(<f-args>)

I try to invoke it as :ExpandSnippet date_english and nothing happens. But typing it in manually and pressing tab works fine.

Neosnippet is current master NeoVIM is 0.3.0

P. S. I've tried to let g:neosnippet#enable_completed_snippet=1 but that didn't help.

Shougo commented 6 years ago

You cannot use it from command. You need to call neosnippet#expand() as mapping-<expr>.

It is invalid usage. Closing.

Shougo commented 6 years ago

I don't want to support it from Denite. It is complex.

Shougo commented 6 years ago

P. S. I've tried to let g:neosnippet#enable_completed_snippet=1 but that didn't help.

It is different feature.

naquad commented 6 years ago

Ok do you have any ideas on how to invoke neosnippet from outside of mapping? I've tried feedkeys() and norm command, but no luck.

naquad commented 6 years ago

I've came up with this:

function! s:InvokeSnippet(snip) abort
  exec "nmap <buffer><silent><expr> \\tmp_expand neosnippet#expand('" . escape(a:snip, '\\''') . "')"
  norm \tmp_expand
  nunmap <buffer> \tmp_expand
  call cursor('.', col('.') + 1)
  startinsert
endfunction

command! -nargs=1 InvokeSnippet :call <SID>InvokeSnippet(<f-args>)

Super ugly, but works.

naquad commented 6 years ago

Even better:

function! s:InvokeSnippet(trigger) abort
  let snippets = neosnippet#helpers#get_snippets()
  if ! has_key(snippets, a:trigger)
    return
  endif

  let col = col('.')
  let pre = getline('.')[:col - 1]
  let snippet = snippets[a:trigger]

  call neosnippet#view#_insert(snippet.snip, snippet.options, pre, col + 1)
endfunction

command! -nargs=1 InvokeSnippet :call <SID>InvokeSnippet(<f-args>)