neosnippet.vim/autoload/unite/sources/neosnippet.vim

139 lines
3.8 KiB
VimL
Raw Normal View History

2012-03-06 23:19:01 +00:00
"=============================================================================
2014-02-10 02:39:54 +00:00
" FILE: neosnippet.vim
2017-06-15 00:11:15 +00:00
" AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>
2017-06-15 00:04:27 +00:00
" License: MIT license
2012-03-06 23:19:01 +00:00
"=============================================================================
2017-10-01 13:40:17 +00:00
function! unite#sources#neosnippet#define() abort
2012-03-06 23:19:01 +00:00
let kind = {
2013-12-30 22:50:41 +00:00
\ 'name' : 'neosnippet',
2012-03-06 23:19:01 +00:00
\ 'default_action' : 'expand',
\ 'action_table': {},
\ 'parents': ['jump_list', 'completion'],
\ 'alias_table' : { 'edit' : 'open' },
\ }
call unite#define_kind(kind)
return s:source
2017-10-01 13:40:17 +00:00
endfunction
2012-03-06 23:19:01 +00:00
2012-09-30 14:16:26 +00:00
" neosnippet source.
2012-03-06 23:19:01 +00:00
let s:source = {
2013-12-30 22:50:41 +00:00
\ 'name': 'neosnippet',
2012-03-06 23:19:01 +00:00
\ 'hooks' : {},
\ 'action_table' : {},
\ }
2017-10-01 13:40:17 +00:00
function! s:source.hooks.on_init(args, context) abort
2012-03-06 23:39:08 +00:00
let a:context.source__cur_keyword_pos =
2012-09-30 14:16:26 +00:00
\ s:get_keyword_pos(neosnippet#util#get_cur_text())
2012-03-06 23:39:08 +00:00
let a:context.source__snippets =
2015-08-21 21:36:11 +00:00
\ sort(values(neosnippet#helpers#get_completion_snippets()))
2017-10-01 13:40:17 +00:00
endfunction
2012-03-06 23:19:01 +00:00
2017-10-01 13:40:17 +00:00
function! s:source.gather_candidates(args, context) abort
2013-12-30 22:37:27 +00:00
return map(copy(a:context.source__snippets), "{
\ 'word' : v:val.word,
\ 'abbr' : printf('%-50s %s', v:val.word, v:val.menu_abbr),
2013-12-30 22:50:41 +00:00
\ 'kind' : 'neosnippet',
2013-12-30 22:37:27 +00:00
\ 'action__complete_word' : v:val.word,
\ 'action__complete_pos' : a:context.source__cur_keyword_pos,
\ 'action__path' : v:val.action__path,
\ 'action__pattern' : v:val.action__pattern,
\ 'source__menu' : v:val.menu_abbr,
\ 'source__snip' : v:val.snip,
\ 'source__snip_ref' : v:val,
2013-12-30 22:50:41 +00:00
\ }")
2017-10-01 13:40:17 +00:00
endfunction
2012-03-06 23:19:01 +00:00
2017-10-01 13:40:17 +00:00
" Actions
2012-03-06 23:19:01 +00:00
let s:action_table = {}
let s:action_table.expand = {
\ 'description' : 'expand snippet',
\ }
2017-10-01 13:40:17 +00:00
function! s:action_table.expand.func(candidate) abort
2012-09-30 14:16:26 +00:00
let cur_text = neosnippet#util#get_cur_text()
let cur_keyword_str = matchstr(cur_text, '\S\+$')
2012-03-06 23:19:01 +00:00
let context = unite#get_context()
2013-11-21 09:54:04 +00:00
call neosnippet#view#_expand(
2012-03-07 01:05:00 +00:00
\ cur_text . a:candidate.action__complete_word[len(cur_keyword_str)],
\ context.col, a:candidate.action__complete_word)
2017-10-01 13:40:17 +00:00
endfunction
2012-03-06 23:19:01 +00:00
let s:action_table.preview = {
\ 'description' : 'preview snippet',
\ 'is_selectable' : 1,
\ 'is_quit' : 0,
\ }
2017-10-01 13:40:17 +00:00
function! s:action_table.preview.func(candidates) abort
2012-03-06 23:19:01 +00:00
for snip in a:candidates
echohl String
echo snip.action__complete_word
echohl None
echo snip.source__snip
echo ' '
endfor
2017-10-01 13:40:17 +00:00
endfunction
2012-03-06 23:19:01 +00:00
let s:action_table.unite__new_candidate = {
\ 'description' : 'add new snippet',
\ 'is_quit' : 1,
\ }
2017-10-01 13:40:17 +00:00
function! s:action_table.unite__new_candidate.func(candidate) abort
let trigger = unite#util#input('Please input snippet trigger: ')
if trigger == ''
echo 'Canceled.'
return
endif
call unite#take_action('open', a:candidate)
if &filetype != 'snippet'
" Open failed.
return
endif
if getline('$') != ''
" Append line.
call append('$', '')
endif
2012-10-21 12:13:26 +00:00
call append('$', [
\ 'snippet ' . trigger,
\ 'abbr ' . trigger,
\ 'options head',
\ ' '
\ ])
call cursor(line('$'), 0)
call cursor(0, col('$'))
2017-10-01 13:40:17 +00:00
endfunction
2012-10-18 02:57:09 +00:00
let s:source.action_table = s:action_table
2012-03-06 23:19:01 +00:00
unlet! s:action_table
2017-10-01 13:40:17 +00:00
function! unite#sources#neosnippet#start_complete() abort
2012-10-14 23:39:34 +00:00
if !exists(':Unite')
call neosnippet#util#print_error(
\ 'unite.vim is not installed.')
call neosnippet#util#print_error(
\ 'Please install unite.vim Ver.1.5 or above.')
return ''
endif
2014-02-10 02:39:54 +00:00
return unite#start_complete(['neosnippet'],
2012-12-13 07:52:58 +00:00
\ { 'input': neosnippet#util#get_cur_text(), 'buffer_name' : '' })
2017-10-01 13:40:17 +00:00
endfunction
2012-03-06 23:19:01 +00:00
2017-10-01 13:40:17 +00:00
function! s:get_keyword_pos(cur_text) abort
2012-09-30 14:16:26 +00:00
let cur_keyword_pos = match(a:cur_text, '\S\+$')
2012-03-06 23:19:01 +00:00
if cur_keyword_pos < 0
" Empty string.
return len(a:cur_text)
endif
return cur_keyword_pos
2017-10-01 13:40:17 +00:00
endfunction