neosnippet.vim/autoload/neosnippet/helpers.vim

185 lines
5.3 KiB
VimL
Raw Normal View History

2013-11-21 09:25:37 +00:00
"=============================================================================
" FILE: helpers.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
2013-11-21 09:25:37 +00:00
"=============================================================================
2016-02-08 12:48:14 +00:00
function! neosnippet#helpers#get_cursor_snippet(snippets, cur_text) abort "{{{
2013-11-21 09:25:37 +00:00
let cur_word = matchstr(a:cur_text, '\S\+$')
if cur_word != '' && has_key(a:snippets, cur_word)
return cur_word
endif
while cur_word != ''
if has_key(a:snippets, cur_word) &&
\ a:snippets[cur_word].options.word
return cur_word
endif
let cur_word = substitute(cur_word, '^\%(\w\+\|\W\)', '', '')
endwhile
return cur_word
endfunction"}}}
2016-07-10 03:21:41 +00:00
function! neosnippet#helpers#get_snippets(...) abort "{{{
let mode = get(a:000, 0, mode())
2013-11-21 09:40:40 +00:00
call neosnippet#init#check()
let neosnippet = neosnippet#variables#current_neosnippet()
let snippets = copy(neosnippet.snippets)
for filetype in s:get_sources_filetypes(neosnippet#helpers#get_filetype())
call neosnippet#commands#_make_cache(filetype)
2015-11-07 01:20:02 +00:00
call extend(snippets, neosnippet#variables#snippets()[filetype])
2013-11-21 09:40:40 +00:00
endfor
let cur_text = neosnippet#util#get_cur_text()
2016-07-10 03:21:41 +00:00
if mode ==# 'i' || mode ==# 's'
2013-11-21 09:40:40 +00:00
" Special filters.
if !s:is_beginning_of_line(cur_text)
call filter(snippets, '!v:val.options.head')
endif
endif
call filter(snippets, "cur_text =~# get(v:val, 'regexp', '')")
if exists('b:neosnippet_disable_snippet_triggers')
call filter(snippets,
\ "index(b:neosnippet_disable_snippet_triggers, v:val.word) < 0")
endif
2013-11-21 09:40:40 +00:00
return snippets
endfunction"}}}
2016-02-08 12:48:14 +00:00
function! neosnippet#helpers#get_completion_snippets() abort "{{{
2015-08-21 21:36:11 +00:00
return filter(neosnippet#helpers#get_snippets(),
\ "!get(v:val.options, 'oneshot', 0)")
endfunction"}}}
2013-11-21 09:40:40 +00:00
2016-02-08 12:48:14 +00:00
function! neosnippet#helpers#get_snippets_directory() abort "{{{
2013-11-21 09:40:40 +00:00
let snippets_dir = copy(neosnippet#variables#snippets_dir())
if !get(g:neosnippet#disable_runtime_snippets,
\ neosnippet#helpers#get_filetype(),
\ get(g:neosnippet#disable_runtime_snippets, '_', 0))
let snippets_dir += neosnippet#variables#runtime_dir()
endif
return snippets_dir
endfunction"}}}
2016-02-08 12:48:14 +00:00
function! neosnippet#helpers#get_filetype() abort "{{{
2016-08-13 00:23:10 +00:00
" context_filetype.vim installation check.
2013-11-21 09:40:40 +00:00
if !exists('s:exists_context_filetype')
2016-08-13 00:23:10 +00:00
silent! call context_filetype#version()
let s:exists_context_filetype = exists('*context_filetype#version')
2013-11-21 09:40:40 +00:00
endif
let context_filetype =
\ s:exists_context_filetype ?
\ context_filetype#get_filetype() : &filetype
if context_filetype == ''
let context_filetype = 'nothing'
endif
return context_filetype
endfunction"}}}
2016-02-08 12:48:14 +00:00
function! neosnippet#helpers#get_selected_text(type, ...) abort "{{{
2013-11-21 09:40:40 +00:00
let sel_save = &selection
let &selection = 'inclusive'
let reg_save = @@
let pos = getpos('.')
try
" Invoked from Visual mode, use '< and '> marks.
if a:0
silent exe "normal! `<" . a:type . "`>y"
elseif a:type == 'line'
silent exe "normal! '[V']y"
elseif a:type == 'block'
silent exe "normal! `[\<C-v>`]y"
else
silent exe "normal! `[v`]y"
endif
return @@
finally
let &selection = sel_save
let @@ = reg_save
call setpos('.', pos)
endtry
endfunction"}}}
2016-02-08 12:48:14 +00:00
function! neosnippet#helpers#delete_selected_text(type, ...) abort "{{{
2013-11-21 09:40:40 +00:00
let sel_save = &selection
let &selection = 'inclusive'
let reg_save = @@
let pos = getpos('.')
try
" Invoked from Visual mode, use '< and '> marks.
if a:0
silent exe "normal! `<" . a:type . "`>d"
elseif a:type ==# 'V'
silent exe "normal! `[V`]s"
elseif a:type ==# "\<C-v>"
silent exe "normal! `[\<C-v>`]d"
else
silent exe "normal! `[v`]d"
endif
finally
let &selection = sel_save
let @@ = reg_save
call setpos('.', pos)
endtry
endfunction"}}}
2016-02-08 12:48:14 +00:00
function! neosnippet#helpers#substitute_selected_text(type, text) abort "{{{
2013-11-21 09:40:40 +00:00
let sel_save = &selection
let &selection = 'inclusive'
let reg_save = @@
let pos = getpos('.')
try
" Invoked from Visual mode, use '< and '> marks.
if a:0
silent exe "normal! `<" . a:type . "`>s" . a:text
elseif a:type ==# 'V'
silent exe "normal! '[V']hs" . a:text
elseif a:type ==# "\<C-v>"
silent exe "normal! `[\<C-v>`]s" . a:text
else
silent exe "normal! `[v`]s" . a:text
endif
finally
let &selection = sel_save
let @@ = reg_save
call setpos('.', pos)
endtry
endfunction"}}}
2016-03-29 22:20:16 +00:00
function! neosnippet#helpers#vim2json(expr) abort "{{{
2017-07-14 23:37:03 +00:00
return has('patch-7.4.1498') ? json_encode(a:expr) : string(a:expr)
2016-03-29 22:20:16 +00:00
endfunction "}}}
function! neosnippet#helpers#json2vim(expr) abort "{{{
2017-07-14 23:37:03 +00:00
sandbox return has('patch-7.4.1498') ? json_decode(a:expr) : eval(a:expr)
2016-03-29 22:20:16 +00:00
endfunction "}}}
2016-02-08 12:48:14 +00:00
function! s:is_beginning_of_line(cur_text) abort "{{{
2013-11-21 09:40:40 +00:00
let keyword_pattern = '\S\+'
let cur_keyword_str = matchstr(a:cur_text, keyword_pattern.'$')
let line_part = a:cur_text[: -1-len(cur_keyword_str)]
let prev_word_end = matchend(line_part, keyword_pattern)
return prev_word_end <= 0
endfunction"}}}
2016-02-08 12:48:14 +00:00
function! s:get_sources_filetypes(filetype) abort "{{{
2013-11-21 09:40:40 +00:00
let filetypes =
\ exists('*context_filetype#get_filetypes') ?
\ context_filetype#get_filetypes(a:filetype) :
\ split(((a:filetype == '') ? 'nothing' : a:filetype), '\.')
2015-11-07 01:20:02 +00:00
return ['_'] + filetypes
2013-11-21 09:40:40 +00:00
endfunction"}}}
2013-11-21 09:25:37 +00:00
" vim: foldmethod=marker