- Improved initializer.

This commit is contained in:
Shougo Matsushita 2013-11-19 16:04:32 +09:00
parent 3ff2918433
commit 3f4e7a6ea9
4 changed files with 201 additions and 168 deletions

View File

@ -1,7 +1,7 @@
"============================================================================= "=============================================================================
" FILE: neosnippet.vim " FILE: neosnippet.vim
" AUTHOR: Shougo Matsushita <Shougo.Matsu@gmail.com> " AUTHOR: Shougo Matsushita <Shougo.Matsu@gmail.com>
" Last Modified: 18 Nov 2013. " Last Modified: 19 Nov 2013.
" License: MIT license {{{ " License: MIT license {{{
" Permission is hereby granted, free of charge, to any person obtaining " Permission is hereby granted, free of charge, to any person obtaining
" a copy of this software and associated documentation files (the " a copy of this software and associated documentation files (the
@ -45,20 +45,6 @@ let s:neosnippet_options = [
\] \]
"}}} "}}}
function! neosnippet#initialize() "{{{
let s:is_initialized = 1
call s:initialize_script_variables()
call s:initialize_others()
call s:initialize_cache()
endfunction"}}}
function! neosnippet#_check_initialize() "{{{
if !exists('s:is_initialized')
call neosnippet#initialize()
endif
endfunction"}}}
" For echodoc. "{{{ " For echodoc. "{{{
let s:doc_dict = { let s:doc_dict = {
\ 'name' : 'neosnippet', \ 'name' : 'neosnippet',
@ -106,7 +92,7 @@ function! neosnippet#caching() "{{{
endfunction"}}} endfunction"}}}
function! neosnippet#recaching() "{{{ function! neosnippet#recaching() "{{{
let s:snippets = {} call neosnippet#variables#set_snippets({})
endfunction"}}} endfunction"}}}
function! s:set_snippet_dict(snippet_dict, snippets, dup_check, snippets_file) "{{{ function! s:set_snippet_dict(snippet_dict, snippets, dup_check, snippets_file) "{{{
@ -178,7 +164,7 @@ function! neosnippet#edit_snippets(args) "{{{
return return
endif endif
call neosnippet#_check_initialize() call neosnippet#init#check()
let [args, options] = neosnippet#util#parse_options( let [args, options] = neosnippet#util#parse_options(
\ a:args, s:neosnippet_options) \ a:args, s:neosnippet_options)
@ -242,7 +228,7 @@ function! s:initialize_options(options) "{{{
endfunction"}}} endfunction"}}}
function! neosnippet#make_cache(filetype) "{{{ function! neosnippet#make_cache(filetype) "{{{
call neosnippet#_check_initialize() call neosnippet#init#check()
let filetype = a:filetype == '' ? let filetype = a:filetype == '' ?
\ &filetype : a:filetype \ &filetype : a:filetype
@ -250,7 +236,8 @@ function! neosnippet#make_cache(filetype) "{{{
let filetype = 'nothing' let filetype = 'nothing'
endif endif
if has_key(s:snippets, filetype) let snippets = neosnippet#variables#get_snippets()
if has_key(snippets, filetype)
return return
endif endif
@ -267,11 +254,12 @@ function! neosnippet#make_cache(filetype) "{{{
call s:parse_snippets_file(snippet, snippets_file) call s:parse_snippets_file(snippet, snippets_file)
endfor endfor
let s:snippets[filetype] = snippet let snippets = neosnippet#variables#get_snippets()
let snippets[filetype] = snippet
endfunction"}}} endfunction"}}}
function! neosnippet#source_file(filename) "{{{ function! neosnippet#source_file(filename) "{{{
call neosnippet#_check_initialize() call neosnippet#init#check()
let neosnippet = neosnippet#get_current_neosnippet() let neosnippet = neosnippet#get_current_neosnippet()
call s:parse_snippets_file(neosnippet.snippets, a:filename) call s:parse_snippets_file(neosnippet.snippets, a:filename)
@ -455,31 +443,34 @@ endfunction"}}}
function! neosnippet#jump(cur_text, col) "{{{ function! neosnippet#jump(cur_text, col) "{{{
call s:skip_next_auto_completion() call s:skip_next_auto_completion()
let expand_stack = neosnippet#variables#get_expand_stack()
" Get patterns and count. " Get patterns and count.
if empty(s:snippets_expand_stack) if empty(expand_stack)
return s:search_outof_range(a:col) return s:search_outof_range(a:col)
endif endif
let expand_info = s:snippets_expand_stack[-1] let expand_info = expand_stack[-1]
" Search patterns. " Search patterns.
let [begin, end] = s:get_snippet_range( let [begin, end] = neosnippet#_get_snippet_range(
\ expand_info.begin_line, \ expand_info.begin_line,
\ expand_info.begin_patterns, \ expand_info.begin_patterns,
\ expand_info.end_line, \ expand_info.end_line,
\ expand_info.end_patterns) \ expand_info.end_patterns)
if s:search_snippet_range(begin, end, expand_info.holder_cnt) if neosnippet#_search_snippet_range(begin, end, expand_info.holder_cnt)
" Next count. " Next count.
let expand_info.holder_cnt += 1 let expand_info.holder_cnt += 1
return 1 return 1
endif endif
" Search placeholder 0. " Search placeholder 0.
if s:search_snippet_range(begin, end, 0) if neosnippet#_search_snippet_range(begin, end, 0)
return 1 return 1
endif endif
" Not found. " Not found.
let s:snippets_expand_stack = s:snippets_expand_stack[: -2] let expand_stack = neosnippet#variables#get_expand_stack()
let expand_stack = expand_stack[: -2]
return s:search_outof_range(a:col) return s:search_outof_range(a:col)
endfunction"}}} endfunction"}}}
@ -546,6 +537,8 @@ function! neosnippet#expand(cur_text, col, trigger_name) "{{{
let &l:foldmethod = 'manual' let &l:foldmethod = 'manual'
endif endif
let expand_stack = neosnippet#variables#get_expand_stack()
try try
call setline('.', snippet_lines[0]) call setline('.', snippet_lines[0])
if len(snippet_lines) > 1 if len(snippet_lines) > 1
@ -560,7 +553,7 @@ function! neosnippet#expand(cur_text, col, trigger_name) "{{{
\ [getline(begin_line - 1)] : [] \ [getline(begin_line - 1)] : []
let end_patterns = (end_line < line('$')) ? let end_patterns = (end_line < line('$')) ?
\ [getline(end_line + 1)] : [] \ [getline(end_line + 1)] : []
call add(s:snippets_expand_stack, { call add(expand_stack, {
\ 'begin_line' : begin_line, \ 'begin_line' : begin_line,
\ 'begin_patterns' : begin_patterns, \ 'begin_patterns' : begin_patterns,
\ 'end_line' : end_line, \ 'end_line' : end_line,
@ -684,7 +677,7 @@ function! neosnippet#register_oneshot_snippet() "{{{
echo 'Registered trigger : ' . trigger echo 'Registered trigger : ' . trigger
endfunction"}}} endfunction"}}}
function! s:get_snippet_range(begin_line, begin_patterns, end_line, end_patterns) "{{{ function! neosnippet#_get_snippet_range(begin_line, begin_patterns, end_line, end_patterns) "{{{
let pos = getpos('.') let pos = getpos('.')
call cursor(a:begin_line, 0) call cursor(a:begin_line, 0)
@ -718,7 +711,7 @@ function! s:get_snippet_range(begin_line, begin_patterns, end_line, end_patterns
call setpos('.', pos) call setpos('.', pos)
return [begin, end] return [begin, end]
endfunction"}}} endfunction"}}}
function! s:search_snippet_range(start, end, cnt, ...) "{{{ function! neosnippet#_search_snippet_range(start, end, cnt, ...) "{{{
let is_select = get(a:000, 0, 1) let is_select = get(a:000, 0, 1)
call s:substitute_placeholder_marker(a:start, a:end, a:cnt) call s:substitute_placeholder_marker(a:start, a:end, a:cnt)
@ -970,13 +963,14 @@ function! neosnippet#get_current_neosnippet() "{{{
return b:neosnippet return b:neosnippet
endfunction"}}} endfunction"}}}
function! neosnippet#get_snippets() "{{{ function! neosnippet#get_snippets() "{{{
call neosnippet#_check_initialize() call neosnippet#init#check()
let neosnippet = neosnippet#get_current_neosnippet() let neosnippet = neosnippet#get_current_neosnippet()
let snippets = copy(neosnippet.snippets) let snippets = copy(neosnippet.snippets)
for filetype in s:get_sources_filetypes(neosnippet#get_filetype()) for filetype in s:get_sources_filetypes(neosnippet#get_filetype())
call neosnippet#make_cache(filetype) call neosnippet#make_cache(filetype)
call extend(snippets, s:snippets[filetype], 'keep') call extend(snippets,
\ neosnippet#variables#get_snippets()[filetype], 'keep')
endfor endfor
let cur_text = neosnippet#util#get_cur_text() let cur_text = neosnippet#util#get_cur_text()
@ -993,26 +987,20 @@ function! neosnippet#get_snippets() "{{{
return snippets return snippets
endfunction"}}} endfunction"}}}
function! neosnippet#get_snippets_directory() "{{{ function! neosnippet#get_snippets_directory() "{{{
call neosnippet#_check_initialize() let snippets_dir = copy(neosnippet#variables#get_snippets_dir())
let snippets_dir = copy(s:snippets_dir)
if !get(g:neosnippet#disable_runtime_snippets, if !get(g:neosnippet#disable_runtime_snippets,
\ neosnippet#get_filetype(), \ neosnippet#get_filetype(),
\ get(g:neosnippet#disable_runtime_snippets, '_', 0)) \ get(g:neosnippet#disable_runtime_snippets, '_', 0))
let snippets_dir += s:runtime_dir let snippets_dir += neosnippet#variables#get_runtime_dir()
endif endif
return snippets_dir return snippets_dir
endfunction"}}} endfunction"}}}
function! neosnippet#get_user_snippets_directory() "{{{ function! neosnippet#get_user_snippets_directory() "{{{
call neosnippet#_check_initialize() return copy(neosnippet#variables#get_snippets_dir())
return copy(s:snippets_dir)
endfunction"}}} endfunction"}}}
function! neosnippet#get_runtime_snippets_directory() "{{{ function! neosnippet#get_runtime_snippets_directory() "{{{
call neosnippet#_check_initialize() return copy(neosnippet#variables#get_runtime_dir())
return copy(s:runtime_dir)
endfunction"}}} endfunction"}}}
function! neosnippet#get_filetype() "{{{ function! neosnippet#get_filetype() "{{{
if !exists('s:exists_context_filetype') if !exists('s:exists_context_filetype')
@ -1206,50 +1194,6 @@ function! s:skip_next_auto_completion() "{{{
let neosnippet.trigger = 0 let neosnippet.trigger = 0
endfunction"}}} endfunction"}}}
function! s:on_insert_leave() "{{{
" Get patterns and count.
if empty(s:snippets_expand_stack)
\ || neosnippet#get_current_neosnippet().trigger
return
endif
let expand_info = s:snippets_expand_stack[-1]
if expand_info.begin_line != expand_info.end_line
return
endif
" Search patterns.
let [begin, end] = s:get_snippet_range(
\ expand_info.begin_line,
\ expand_info.begin_patterns,
\ expand_info.end_line,
\ expand_info.end_patterns)
let pos = getpos('.')
" Found snippet.
let found = 0
try
while s:search_snippet_range(begin, end, expand_info.holder_cnt, 0)
" Next count.
let expand_info.holder_cnt += 1
let found = 1
endwhile
" Search placeholder 0.
if s:search_snippet_range(begin, end, 0)
let found = 1
endif
finally
if found
stopinsert
endif
call setpos('.', pos)
endtry
endfunction"}}}
if g:neosnippet#enable_snipmate_compatibility if g:neosnippet#enable_snipmate_compatibility
" For snipMate function. " For snipMate function.
function! Filename(...) function! Filename(...)
@ -1264,84 +1208,6 @@ if g:neosnippet#enable_snipmate_compatibility
endfunction endfunction
endif endif
function! s:initialize_script_variables() "{{{
" Initialize.
let s:snippets_expand_stack = []
let s:snippets = {}
let s:runtime_dir = []
" Set runtime dir.
let s:runtime_dir += split(globpath(&runtimepath,
\ 'autoload/neosnippet/snippets'), '\n')
if g:neosnippet#enable_snipmate_compatibility
" Load snipMate snippet directories.
let s:runtime_dir += split(globpath(&runtimepath,
\ 'snippets'), '\n')
endif
let s:runtime_dir += (exists('g:snippets_dir') ?
\ split(g:snippets_dir, '\s*,\s*')
\ : split(globpath(&runtimepath, 'snippets'), '\n'))
call map(s:runtime_dir, 'substitute(v:val, "[\\\\/]$", "", "")')
" Set snippets_dir.
let s:snippets_dir = []
for dir in neosnippet#util#option2list(g:neosnippet#snippets_directory)
let dir = neosnippet#util#expand(dir)
if !isdirectory(dir) && !neosnippet#util#is_sudo()
call mkdir(dir, 'p')
endif
call add(s:snippets_dir, dir)
endfor
call map(s:snippets_dir, 'substitute(v:val, "[\\\\/]$", "", "")')
endfunction"}}}
function! s:initialize_cache() "{{{
" Caching _ snippets.
call neosnippet#make_cache('_')
" Initialize check.
call neosnippet#caching()
endfunction"}}}
function! s:initialize_others() "{{{
augroup neosnippet "{{{
autocmd!
" Set caching event.
autocmd FileType * call neosnippet#caching()
" Recaching events
autocmd BufWritePost *.snip,*.snippets
\ call neosnippet#recaching()
autocmd BufEnter *
\ call neosnippet#clear_select_mode_mappings()
autocmd InsertLeave * call s:on_insert_leave()
augroup END"}}}
augroup neosnippet
autocmd BufNewFile,BufRead,Syntax *
\ execute 'syntax match neosnippetExpandSnippets'
\ "'".neosnippet#get_placeholder_marker_pattern(). '\|'
\ .neosnippet#get_sync_placeholder_marker_pattern().'\|'
\ .neosnippet#get_mirror_placeholder_marker_pattern()."'"
\ 'containedin=ALL oneline'
if has('conceal')
autocmd BufNewFile,BufRead,Syntax *
\ syntax region neosnippetConcealExpandSnippets
\ matchgroup=neosnippetExpandSnippets
\ start='<`\d\+:\=\|<{\d\+:\=\|<|'
\ end='`>\|}>\||>'
\ containedin=ALL
\ concealends oneline
endif
augroup END
doautocmd neosnippet BufRead
hi def link neosnippetExpandSnippets Special
if get(g:, 'loaded_echodoc', 0)
call echodoc#register('snippets_complete', s:doc_dict)
endif
call neosnippet#clear_select_mode_mappings()
endfunction"}}}
let &cpo = s:save_cpo let &cpo = s:save_cpo
unlet s:save_cpo unlet s:save_cpo

View File

@ -0,0 +1,167 @@
"=============================================================================
" FILE: init.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
function! neosnippet#init#_initialize() "{{{
let s:is_initialized = 1
call s:initialize_script_variables()
call s:initialize_others()
call s:initialize_cache()
endfunction"}}}
function! neosnippet#init#check() "{{{
if !exists('s:is_initialized')
call neosnippet#init#_initialize()
endif
endfunction"}}}
function! s:initialize_script_variables() "{{{
" Set runtime dir.
let runtime_dir = neosnippet#variables#get_runtime_dir()
let runtime_dir += split(globpath(&runtimepath,
\ 'autoload/neosnippet/snippets'), '\n')
if g:neosnippet#enable_snipmate_compatibility
" Load snipMate snippet directories.
let runtime_dir += split(globpath(&runtimepath,
\ 'snippets'), '\n')
endif
let runtime_dir += (exists('g:snippets_dir') ?
\ split(g:snippets_dir, '\s*,\s*')
\ : split(globpath(&runtimepath, 'snippets'), '\n'))
call map(runtime_dir, 'substitute(v:val, "[\\\\/]$", "", "")')
" Set snippets_dir.
let snippets_dir = neosnippet#variables#get_snippets_dir()
for dir in neosnippet#util#option2list(g:neosnippet#snippets_directory)
let dir = neosnippet#util#expand(dir)
if !isdirectory(dir) && !neosnippet#util#is_sudo()
call mkdir(dir, 'p')
endif
call add(snippets_dir, dir)
endfor
call map(snippets_dir, 'substitute(v:val, "[\\\\/]$", "", "")')
endfunction"}}}
function! s:initialize_cache() "{{{
" Caching _ snippets.
call neosnippet#make_cache('_')
" Initialize check.
call neosnippet#caching()
endfunction"}}}
function! s:initialize_others() "{{{
augroup neosnippet "{{{
autocmd!
" Set caching event.
autocmd FileType * call neosnippet#caching()
" Recaching events
autocmd BufWritePost *.snip,*.snippets
\ call neosnippet#recaching()
autocmd BufEnter *
\ call neosnippet#clear_select_mode_mappings()
autocmd InsertLeave * call s:on_insert_leave()
augroup END"}}}
augroup neosnippet
autocmd BufNewFile,BufRead,Syntax *
\ execute 'syntax match neosnippetExpandSnippets'
\ "'".neosnippet#get_placeholder_marker_pattern(). '\|'
\ .neosnippet#get_sync_placeholder_marker_pattern().'\|'
\ .neosnippet#get_mirror_placeholder_marker_pattern()."'"
\ 'containedin=ALL oneline'
if has('conceal')
autocmd BufNewFile,BufRead,Syntax *
\ syntax region neosnippetConcealExpandSnippets
\ matchgroup=neosnippetExpandSnippets
\ start='<`\d\+:\=\|<{\d\+:\=\|<|'
\ end='`>\|}>\||>'
\ containedin=ALL
\ concealends oneline
endif
augroup END
doautocmd neosnippet BufRead
hi def link neosnippetExpandSnippets Special
if get(g:, 'loaded_echodoc', 0)
call echodoc#register('snippets_complete', s:doc_dict)
endif
call neosnippet#clear_select_mode_mappings()
endfunction"}}}
function! s:on_insert_leave() "{{{
let expand_stack = neosnippet#variables#get_expand_stack()
" Get patterns and count.
if empty(expand_stack)
\ || neosnippet#get_current_neosnippet().trigger
return
endif
let expand_info = expand_stack[-1]
if expand_info.begin_line != expand_info.end_line
return
endif
" Search patterns.
let [begin, end] = neosnippet#_get_snippet_range(
\ expand_info.begin_line,
\ expand_info.begin_patterns,
\ expand_info.end_line,
\ expand_info.end_patterns)
let pos = getpos('.')
" Found snippet.
let found = 0
try
while neosnippet#_search_snippet_range(begin, end, expand_info.holder_cnt, 0)
" Next count.
let expand_info.holder_cnt += 1
let found = 1
endwhile
" Search placeholder 0.
if neosnippet#_search_snippet_range(begin, end, 0)
let found = 1
endif
finally
if found
stopinsert
endif
call setpos('.', pos)
endtry
endfunction"}}}
let &cpo = s:save_cpo
unlet s:save_cpo
" vim: foldmethod=marker

View File

@ -1,7 +1,7 @@
"============================================================================= "=============================================================================
" FILE: mappings.vim " FILE: mappings.vim
" AUTHOR: Shougo Matsushita <Shougo.Matsu@gmail.com> " AUTHOR: Shougo Matsushita <Shougo.Matsu@gmail.com>
" Last Modified: 18 Nov 2013. " Last Modified: 19 Nov 2013.
" License: MIT license {{{ " License: MIT license {{{
" Permission is hereby granted, free of charge, to any person obtaining " Permission is hereby granted, free of charge, to any person obtaining
" a copy of this software and associated documentation files (the " a copy of this software and associated documentation files (the
@ -85,7 +85,7 @@ function! s:SID_PREFIX() "{{{
endfunction"}}} endfunction"}}}
function! s:trigger(function) "{{{ function! s:trigger(function) "{{{
call neosnippet#_check_initialize() call neosnippet#init#check()
let cur_text = neosnippet#util#get_cur_text() let cur_text = neosnippet#util#get_cur_text()

View File

@ -1,7 +1,7 @@
"============================================================================= "=============================================================================
" FILE: neosnippet.vim " FILE: neosnippet.vim
" AUTHOR: Shougo Matsushita <Shougo.Matsu@gmail.com> " AUTHOR: Shougo Matsushita <Shougo.Matsu@gmail.com>
" Last Modified: 18 Nov 2013. " Last Modified: 19 Nov 2013.
" License: MIT license {{{ " License: MIT license {{{
" Permission is hereby granted, free of charge, to any person obtaining " Permission is hereby granted, free of charge, to any person obtaining
" a copy of this software and associated documentation files (the " a copy of this software and associated documentation files (the
@ -70,7 +70,7 @@ inoremap <expr><silent> <Plug>(neosnippet_start_unite_snippet)
"}}} "}}}
augroup neosnippet "{{{ augroup neosnippet "{{{
autocmd InsertEnter * call neosnippet#initialize() autocmd InsertEnter * call neosnippet#init#_initialize()
augroup END"}}} augroup END"}}}
" Commands. "{{{ " Commands. "{{{