neosnippet.vim/autoload/neosnippet/helpers.vim

210 lines
6.5 KiB
VimL
Raw Normal View History

2013-11-21 09:25:37 +00:00
"=============================================================================
" FILE: helpers.vim
" AUTHOR: Shougo Matsushita <Shougo.Matsu@gmail.com>
" License: MIT license {{{
" Permission is hereby granted, free of charge, to any person obtaining
" a copy of this software and associated documentation files (the
" "Software"), to deal in the Software without restriction, including
" without limitation the rights to use, copy, modify, merge, publish,
" distribute, sublicense, and/or sell copies of the Software, and to
" permit persons to whom the Software is furnished to do so, subject to
" the following conditions:
"
" The above copyright notice and this permission notice shall be included
" in all copies or substantial portions of the Software.
"
" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
" OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
" IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
" CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
" TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
" SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
" }}}
"=============================================================================
let s:save_cpo = &cpo
set cpo&vim
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 "{{{
return has('patch-7.4.1498') ? js_encode(a:expr) : string(a:expr)
endfunction "}}}
function! neosnippet#helpers#json2vim(expr) abort "{{{
sandbox return has('patch-7.4.1498') ? js_decode(a:expr) : eval(a:expr)
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
let &cpo = s:save_cpo
unlet s:save_cpo
" vim: foldmethod=marker