- Added commands.

This commit is contained in:
Shougo Matsushita 2013-11-19 16:19:33 +09:00
parent c3ca7a9ae2
commit bee4d73092
5 changed files with 149 additions and 113 deletions

View File

@ -38,13 +38,6 @@ call neosnippet#util#set_default(
\ 'g:neosnippet#enable_snipmate_compatibility', 0)
"}}}
" Variables "{{{
let s:neosnippet_options = [
\ '-runtime',
\ '-vertical', '-horizontal', '-direction=', '-split',
\]
"}}}
" For echodoc. "{{{
let s:doc_dict = {
\ 'name' : 'neosnippet',
@ -88,7 +81,7 @@ function! neosnippet#jumpable() "{{{
endfunction"}}}
function! neosnippet#caching() "{{{
call neosnippet#make_cache(&filetype)
call neosnippet#commands#_make_cache(&filetype)
endfunction"}}}
function! neosnippet#recaching() "{{{
@ -157,56 +150,6 @@ function! s:initialize_snippet_options() "{{{
return { 'head' : 0, 'word' : 0, 'indent' : 0 }
endfunction"}}}
function! neosnippet#edit_snippets(args) "{{{
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:neosnippet_options)
let filetype = get(args, 0, '')
if filetype == ''
let filetype = neosnippet#get_filetype()
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
" 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
edit `=filename`
catch /^Vim\%((\a\+)\)\=:E749/
endtry
endfunction"}}}
function! s:initialize_options(options) "{{{
let default_options = {
@ -227,45 +170,7 @@ function! s:initialize_options(options) "{{{
return options
endfunction"}}}
function! neosnippet#make_cache(filetype) "{{{
call neosnippet#init#check()
let filetype = a:filetype == '' ?
\ &filetype : a:filetype
if filetype ==# ''
let filetype = 'nothing'
endif
let snippets = neosnippet#variables#get_snippets()
if has_key(snippets, filetype)
return
endif
let snippets_dir = neosnippet#get_snippets_directory()
let snippet = {}
let snippets_files =
\ split(globpath(join(snippets_dir, ','),
\ filetype . '.snip*'), '\n')
\ + split(globpath(join(snippets_dir, ','),
\ filetype . '_*.snip*'), '\n')
\ + split(globpath(join(snippets_dir, ','),
\ filetype . '/**/*.snip*'), '\n')
for snippets_file in reverse(snippets_files)
call s:parse_snippets_file(snippet, snippets_file)
endfor
let snippets = neosnippet#variables#get_snippets()
let snippets[filetype] = snippet
endfunction"}}}
function! neosnippet#source_file(filename) "{{{
call neosnippet#init#check()
let neosnippet = neosnippet#get_current_neosnippet()
call s:parse_snippets_file(neosnippet.snippets, a:filename)
endfunction"}}}
function! s:parse_snippets_file(snippets, snippets_file) "{{{
function! neosnippet#_parse_snippets_file(snippets, snippets_file) "{{{
let dup_check = {}
let snippet_dict = {}
@ -292,7 +197,7 @@ function! s:parse_snippets_file(snippets, snippets_file) "{{{
for snippets_file in split(globpath(join(
\ neosnippet#get_snippets_directory(), ','),
\ filename), '\n')
call s:parse_snippets_file(a:snippets, snippets_file)
call neosnippet#_parse_snippets_file(a:snippets, snippets_file)
endfor
elseif line =~ '^delete\s'
let name = matchstr(line, '^delete\s\+\zs.*$')
@ -968,7 +873,7 @@ function! neosnippet#get_snippets() "{{{
let neosnippet = neosnippet#get_current_neosnippet()
let snippets = copy(neosnippet.snippets)
for filetype in s:get_sources_filetypes(neosnippet#get_filetype())
call neosnippet#make_cache(filetype)
call neosnippet#commands#_make_cache(filetype)
call extend(snippets,
\ neosnippet#variables#get_snippets()[filetype], 'keep')
endfor
@ -1032,10 +937,6 @@ function! s:get_sources_filetypes(filetype) "{{{
return filetypes + ['_']
endfunction"}}}
function! neosnippet#edit_complete(arglead, cmdline, cursorpos) "{{{
return filter(s:neosnippet_options + neosnippet#filetype_complete(
\ a:arglead, a:cmdline, a:cursorpos), 'stridx(v:val, a:arglead) == 0')
endfunction"}}}
" Complete filetype helper.
function! neosnippet#filetype_complete(arglead, cmdline, cursorpos) "{{{
" Dup check.

View File

@ -0,0 +1,135 @@
"=============================================================================
" FILE: commands.vim
" AUTHOR: Shougo Matsushita <Shougo.Matsu@gmail.com>
" Last Modified: 19 Nov 2013.
" 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
" Variables "{{{
let s:edit_options = [
\ '-runtime',
\ '-vertical', '-horizontal', '-direction=', '-split',
\]
"}}}
function! neosnippet#commands#_edit(args) "{{{
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 == ''
let filetype = neosnippet#get_filetype()
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
" 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
edit `=filename`
catch /^Vim\%((\a\+)\)\=:E749/
endtry
endfunction"}}}
function! neosnippet#commands#_make_cache(filetype) "{{{
call neosnippet#init#check()
let filetype = a:filetype == '' ?
\ &filetype : a:filetype
if filetype ==# ''
let filetype = 'nothing'
endif
let snippets = neosnippet#variables#get_snippets()
if has_key(snippets, filetype)
return
endif
let snippets_dir = neosnippet#get_snippets_directory()
let snippet = {}
let snippets_files =
\ split(globpath(join(snippets_dir, ','),
\ filetype . '.snip*'), '\n')
\ + split(globpath(join(snippets_dir, ','),
\ filetype . '_*.snip*'), '\n')
\ + split(globpath(join(snippets_dir, ','),
\ filetype . '/**/*.snip*'), '\n')
for snippets_file in reverse(snippets_files)
call neosnippet#_parse_snippets_file(snippet, snippets_file)
endfor
let snippets = neosnippet#variables#get_snippets()
let snippets[filetype] = snippet
endfunction"}}}
function! neosnippet#commands#_source(filename) "{{{
call neosnippet#init#check()
let neosnippet = neosnippet#get_current_neosnippet()
call neosnippet#_parse_snippets_file(neosnippet.snippets, a:filename)
endfunction"}}}
function! neosnippet#commands#_edit_complete(arglead, cmdline, cursorpos) "{{{
return filter(s:edit_options +
\ neosnippet#filetype_complete(a:arglead, a:cmdline, a:cursorpos),
\ 'stridx(v:val, a:arglead) == 0')
endfunction"}}}
let &cpo = s:save_cpo
unlet s:save_cpo
" vim: foldmethod=marker

View File

@ -68,8 +68,8 @@ function! s:initialize_script_variables() "{{{
call map(snippets_dir, 'substitute(v:val, "[\\\\/]$", "", "")')
endfunction"}}}
function! s:initialize_cache() "{{{
" Caching _ snippets.
call neosnippet#make_cache('_')
" Make cache for _ snippets.
call neosnippet#commands#_make_cache('_')
" Initialize check.
call neosnippet#caching()

View File

@ -1,7 +1,7 @@
"=============================================================================
" FILE: neosnippet_file.vim
" AUTHOR: Shougo Matsushita <Shougo.Matsu@gmail.com>
" Last Modified: 17 Feb 2013.
" Last Modified: 19 Nov 2013.
" License: MIT license {{{
" Permission is hereby granted, free of charge, to any person obtaining
" a copy of this software and associated documentation files (the
@ -42,7 +42,7 @@ function! s:action_table.neosnippet_source.func(candidates) "{{{
for candidate in a:candidates
let snippet_name = candidate.action__path
if snippet_name != ''
call neosnippet#source_file(snippet_name)
call neosnippet#commands#_source(snippet_name)
endif
endfor
endfunction"}}}
@ -67,7 +67,7 @@ function! s:source_user.action_table.unite__new_candidate.func(candidate) "{{{
let filename = input(
\ 'New snippet file name: ', neosnippet#get_filetype())
if filename != ''
call neosnippet#edit_snippets(filename)
call neosnippet#commands#_edit(filename)
endif
endfunction"}}}
@ -92,7 +92,7 @@ function! s:source_runtime.action_table.unite__new_candidate.func(candidate) "{{
let filename = input(
\ 'New snippet file name: ', neosnippet#get_filetype())
if filename != ''
call neosnippet#edit_snippets('-runtime ' . filename)
call neosnippet#commands#_edit('-runtime ' . filename)
endif
endfunction"}}}

View File

@ -74,17 +74,17 @@ augroup neosnippet "{{{
augroup END"}}}
" Commands. "{{{
command! -nargs=? -complete=customlist,neosnippet#edit_complete
command! -nargs=? -complete=customlist,neosnippet#commands#_edit_complete
\ NeoSnippetEdit
\ call neosnippet#edit_snippets(<q-args>)
\ call neosnippet#commands#_edit(<q-args>)
command! -nargs=? -complete=customlist,neosnippet#filetype_complete
\ NeoSnippetMakeCache
\ call neosnippet#make_cache(<q-args>)
\ call neosnippet#commands#_make_cache(<q-args>)
command! -nargs=1 -complete=file
\ NeoSnippetSource
\ call neosnippet#source_file(<q-args>)
\ call neosnippet#commands#_source(<q-args>)
"}}}
let g:loaded_neosnippet = 1