neosnippet.vim/autoload/neosnippet/commands.vim

204 lines
6.0 KiB
VimL
Raw Normal View History

2013-11-19 07:19:33 +00:00
"=============================================================================
" FILE: commands.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-19 07:19:33 +00:00
"=============================================================================
2017-10-01 13:40:17 +00:00
" Variables
2013-11-19 07:19:33 +00:00
let s:edit_options = [
\ '-runtime',
\ '-vertical', '-horizontal', '-direction=', '-split',
\]
2016-03-26 10:46:01 +00:00
let s:Cache = neosnippet#util#get_vital().import('System.Cache.Deprecated')
2013-11-19 07:19:33 +00:00
2017-10-01 13:40:17 +00:00
function! s:get_list() abort
if !exists('s:List')
let s:List = vital#of('neosnippet').import('Data.List')
endif
return s:List
2017-10-01 13:40:17 +00:00
endfunction
2017-10-01 13:40:17 +00:00
function! neosnippet#commands#_edit(args) abort
2013-11-19 07:19:33 +00:00
if neosnippet#util#is_sudo()
call neosnippet#util#print_error(
\ '"sudo vim" is detected. This feature is disabled.')
return
endif
call neosnippet#init#check()
let [args, options] = neosnippet#util#parse_options(
\ a:args, s:edit_options)
let filetype = get(args, 0, '')
if filetype == ''
2013-11-21 09:40:40 +00:00
let filetype = neosnippet#helpers#get_filetype()
2013-11-19 07:19:33 +00:00
endif
let options = s:initialize_options(options)
let snippet_dir = (options.runtime ?
\ get(neosnippet#get_runtime_snippets_directory(), 0, '') :
\ get(neosnippet#get_user_snippets_directory(), -1, ''))
if snippet_dir == ''
call neosnippet#util#print_error('Snippet directory is not found.')
return
endif
if !isdirectory(snippet_dir) && !neosnippet#util#is_sudo()
call mkdir(snippet_dir, 'p')
endif
2013-11-19 07:19:33 +00:00
" Edit snippet file.
let filename = snippet_dir .'/'.filetype
if isdirectory(filename)
" Edit in snippet directory.
let filename .= '/'.filetype
endif
if filename !~ '\.snip*$'
let filename .= '.snip'
endif
if options.split
" Split window.
execute options.direction
\ (options.vertical ? 'vsplit' : 'split')
endif
try
2014-08-06 20:39:12 +00:00
execute 'edit' fnameescape(filename)
2013-11-19 07:19:33 +00:00
catch /^Vim\%((\a\+)\)\=:E749/
endtry
2017-10-01 13:40:17 +00:00
endfunction
2013-11-19 07:19:33 +00:00
2017-10-01 13:40:17 +00:00
function! neosnippet#commands#_make_cache(filetype) abort
2013-11-19 07:19:33 +00:00
call neosnippet#init#check()
let filetype = a:filetype == '' ?
\ &filetype : a:filetype
if filetype ==# ''
let filetype = 'nothing'
endif
2013-11-21 09:20:36 +00:00
let snippets = neosnippet#variables#snippets()
2013-11-19 07:19:33 +00:00
if has_key(snippets, filetype)
return
endif
2014-05-11 08:46:20 +00:00
let snippets[filetype] = {}
2013-11-19 07:19:33 +00:00
2014-01-01 01:22:03 +00:00
let path = join(neosnippet#helpers#get_snippets_directory(), ',')
let cache_dir = neosnippet#variables#data_dir()
2013-11-19 07:19:33 +00:00
for filename in s:get_snippets_files(path, filetype)
" Clear cache file
call s:Cache.deletefile(cache_dir, filename)
2014-05-11 08:46:20 +00:00
let snippets[filetype] = extend(snippets[filetype],
\ neosnippet#parser#_parse_snippets(filename))
2014-05-11 08:46:20 +00:00
endfor
if g:neosnippet#enable_snipmate_compatibility
" Load file snippets
for filename in s:get_snippet_files(path, filetype)
let trigger = fnamemodify(filename, ':t:r')
let snippets[filetype][trigger] =
\ neosnippet#parser#_parse_snippet(filename, trigger)
endfor
endif
2017-10-01 13:40:17 +00:00
endfunction
2013-11-19 07:19:33 +00:00
2017-10-01 13:40:17 +00:00
function! neosnippet#commands#_source(filename) abort
2013-11-19 07:19:33 +00:00
call neosnippet#init#check()
2013-11-21 09:20:36 +00:00
let neosnippet = neosnippet#variables#current_neosnippet()
2014-05-11 08:46:20 +00:00
let neosnippet.snippets = extend(neosnippet.snippets,
2015-10-23 05:23:52 +00:00
\ neosnippet#parser#_parse_snippets(a:filename))
2017-10-01 13:40:17 +00:00
endfunction
2013-11-19 07:19:33 +00:00
2017-10-01 13:40:17 +00:00
function! neosnippet#commands#_clear_markers() abort
let expand_stack = neosnippet#variables#expand_stack()
" Get patterns and count.
2015-08-29 00:33:43 +00:00
if !&l:modifiable || !&l:modified
\ || empty(expand_stack)
\ || neosnippet#variables#current_neosnippet().trigger
return
endif
2015-12-19 02:38:44 +00:00
call neosnippet#view#_clear_markers(expand_stack[-1])
2017-10-01 13:40:17 +00:00
endfunction
2013-11-21 09:40:40 +00:00
" Complete helpers.
2017-10-01 13:40:17 +00:00
function! neosnippet#commands#_edit_complete(arglead, cmdline, cursorpos) abort
2013-11-19 07:19:33 +00:00
return filter(s:edit_options +
2013-11-21 09:40:40 +00:00
\ neosnippet#commands#_filetype_complete(a:arglead, a:cmdline, a:cursorpos),
2013-11-19 07:19:33 +00:00
\ 'stridx(v:val, a:arglead) == 0')
2017-10-01 13:40:17 +00:00
endfunction
function! neosnippet#commands#_filetype_complete(arglead, cmdline, cursorpos) abort
2013-11-21 09:40:40 +00:00
" Dup check.
let ret = {}
for item in map(
\ split(globpath(&runtimepath, 'syntax/*.vim'), '\n') +
\ split(globpath(&runtimepath, 'indent/*.vim'), '\n') +
\ split(globpath(&runtimepath, 'ftplugin/*.vim'), '\n')
\ , 'fnamemodify(v:val, ":t:r")')
if !has_key(ret, item) && item =~ '^'.a:arglead
let ret[item] = 1
endif
endfor
return sort(keys(ret))
2017-10-01 13:40:17 +00:00
endfunction
function! neosnippet#commands#_complete_target_snippets(arglead, cmdline, cursorpos) abort
2013-11-21 09:40:40 +00:00
return map(filter(values(neosnippet#helpers#get_snippets()),
\ "stridx(v:val.word, a:arglead) == 0
\ && v:val.snip =~# neosnippet#get_placeholder_target_marker_pattern()"), 'v:val.word')
2017-10-01 13:40:17 +00:00
endfunction
2013-11-19 07:19:33 +00:00
2017-10-01 13:40:17 +00:00
function! s:initialize_options(options) abort
let default_options = {
\ 'runtime' : 0,
\ 'vertical' : 0,
\ 'direction' : 'below',
\ 'split' : 0,
\ }
let options = extend(default_options, a:options)
" Complex initializer.
if has_key(options, 'horizontal')
" Disable vertically.
let options.vertical = 0
endif
return options
2017-10-01 13:40:17 +00:00
endfunction
2017-10-01 13:40:17 +00:00
function! s:get_snippets_files(path, filetype) abort
let snippets_files = []
for glob in s:get_list().flatten(
\ map(split(get(g:neosnippet#scope_aliases,
\ a:filetype, a:filetype), '\s*,\s*'), "
2015-10-26 13:02:33 +00:00
\ [v:val.'.snip', v:val.'.snippets',
\ v:val.'/**/*.snip', v:val.'/**/*.snippets']
\ + (a:filetype != '_' &&
\ !has_key(g:neosnippet#scope_aliases, a:filetype) ?
2015-10-26 13:02:33 +00:00
\ [v:val . '_*.snip', v:val . '_*.snippets'] : [])"))
let snippets_files += split(globpath(a:path, glob), '\n')
endfor
return reverse(s:get_list().uniq(snippets_files))
2017-10-01 13:40:17 +00:00
endfunction
function! s:get_snippet_files(path, filetype) abort
let snippet_files = []
for glob in s:get_list().flatten(
\ map(split(get(g:neosnippet#scope_aliases,
\ a:filetype, a:filetype), '\s*,\s*'), "
\ [v:val.'/*.snippet']"))
let snippet_files += split(globpath(a:path, glob), '\n')
endfor
return reverse(s:get_list().uniq(snippet_files))
2017-10-01 13:40:17 +00:00
endfunction