neosnippet.vim/autoload/neocomplcache/sources/snippets_complete.vim

946 lines
29 KiB
VimL
Raw Normal View History

2012-02-02 04:33:35 +00:00
"=============================================================================
" FILE: snippets_complete.vim
" AUTHOR: Shougo Matsushita <Shougo.Matsu@gmail.com>
" Last Modified: 01 Sep 2012.
2012-02-02 04:33:35 +00:00
" 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
if !exists('s:snippets')
let s:snippets = {}
endif
let s:source = {
\ 'name' : 'snippets_complete',
\ 'kind' : 'plugin',
\}
function! s:source.initialize()"{{{
" Initialize.
let s:snippets = {}
let s:snippets_expand_stack = []
2012-02-02 04:33:35 +00:00
if !exists('g:neocomplcache_snippets_disable_runtime_snippets')
let g:neocomplcache_snippets_disable_runtime_snippets = 0
endif
call neocomplcache#set_dictionary_helper(
2012-02-05 01:24:59 +00:00
\ g:neocomplcache_source_rank, 'snippets_complete', 8)
2012-02-02 04:33:35 +00:00
let s:snippets_dir = []
let s:runtime_dir = split(globpath(&runtimepath,
\ 'autoload/neocomplcache/sources/snippets_complete'), '\n')
if !g:neocomplcache_snippets_disable_runtime_snippets
" Set snippets dir.
let s:snippets_dir += (exists('g:snippets_dir') ?
2012-02-25 14:05:10 +00:00
\ split(g:snippets_dir, '\s*,\s*')
2012-02-02 04:33:35 +00:00
\ : split(globpath(&runtimepath, 'snippets'), '\n'))
\ + s:runtime_dir
endif
if exists('g:neocomplcache_snippets_dir')
2012-02-25 14:05:10 +00:00
for dir in split(g:neocomplcache_snippets_dir, '\s*,\s*')
2012-02-02 04:33:35 +00:00
let dir = neocomplcache#util#expand(dir)
if !isdirectory(dir)
call mkdir(dir, 'p')
endif
call add(s:snippets_dir, dir)
endfor
endif
call map(s:snippets_dir, 'substitute(v:val, "[\\\\/]$", "", "")')
augroup neocomplcache"{{{
" Set caching event.
autocmd FileType * call s:caching()
" Recaching events
autocmd BufWritePost *.snip,*.snippets call s:caching_snippets(expand('<afile>:t:r'))
augroup END"}}}
if has('conceal')
" Supported conceal features.
augroup neocomplcache
2012-03-15 14:26:21 +00:00
autocmd BufNewFile,BufRead,ColorScheme *
\ syn match neocomplcacheExpandSnippets
\ '<`\d\+:\|`>\|<{\d\+:\|}>' conceal
2012-02-02 04:33:35 +00:00
autocmd BufNewFile,BufRead,ColorScheme *
2012-03-13 23:49:57 +00:00
\ execute 'syn match neocomplcacheExpandSnippets'
2012-03-15 14:26:21 +00:00
\ '''<`\d\+\\\@<!`>\|<{\d\+\\\@<!}>\|'
2012-03-13 23:49:57 +00:00
\ .s:get_mirror_placeholder_marker_pattern()."'"
\ 'conceal cchar=$'
2012-02-02 04:33:35 +00:00
augroup END
else
augroup neocomplcache
autocmd BufNewFile,BufRead,ColorScheme *
2012-03-13 23:49:57 +00:00
\ execute 'syn match neocomplcacheExpandSnippets'
\ "'".s:get_placeholder_marker_pattern(). '\|'
\ .s:get_sync_placeholder_marker_pattern().'\|'
\ .s:get_mirror_placeholder_marker_pattern()."'"
2012-02-02 04:33:35 +00:00
augroup END
endif
hi def link NeoComplCacheExpandSnippets Special
command! -nargs=? -complete=customlist,neocomplcache#filetype_complete
2012-03-13 23:49:57 +00:00
\ NeoComplCacheEditSnippets
\ call s:edit_snippets(<q-args>, 0)
2012-02-02 04:33:35 +00:00
command! -nargs=? -complete=customlist,neocomplcache#filetype_complete
2012-03-13 23:49:57 +00:00
\ NeoComplCacheEditRuntimeSnippets
\ call s:edit_snippets(<q-args>, 1)
2012-02-02 04:33:35 +00:00
command! -nargs=? -complete=customlist,neocomplcache#filetype_complete
2012-03-13 23:49:57 +00:00
\ NeoComplCacheCachingSnippets
\ call s:caching_snippets(<q-args>)
2012-02-02 04:33:35 +00:00
" Select mode mappings.
if !exists('g:neocomplcache_disable_select_mode_mappings')
snoremap <CR> a<BS>
snoremap <BS> a<BS>
snoremap <right> <ESC>a
snoremap <left> <ESC>bi
snoremap ' a<BS>'
snoremap ` a<BS>`
snoremap % a<BS>%
snoremap U a<BS>U
snoremap ^ a<BS>^
snoremap \ a<BS>\
snoremap <C-x> a<BS><c-x>
endif
" Caching _ snippets.
call s:caching_snippets('_')
" Initialize check.
call s:caching()
if neocomplcache#exists_echodoc()
call echodoc#register('snippets_complete', s:doc_dict)
endif
endfunction"}}}
function! s:source.finalize()"{{{
delcommand NeoComplCacheEditSnippets
delcommand NeoComplCacheEditRuntimeSnippets
delcommand NeoComplCacheCachingSnippets
hi clear NeoComplCacheExpandSnippets
if neocomplcache#exists_echodoc()
call echodoc#unregister('snippets_complete')
endif
endfunction"}}}
function! s:source.get_keyword_list(cur_keyword_str)"{{{
if !has_key(s:snippets, '_')
" Caching _ snippets.
call s:caching_snippets('_')
endif
let snippets = values(s:snippets['_'])
let filetype = neocomplcache#get_context_filetype()
if !has_key(s:snippets, filetype)
" Caching snippets.
call s:caching_snippets(filetype)
endif
for source in neocomplcache#get_sources_list(s:snippets, filetype)
let snippets += values(source)
endfor
2012-03-14 00:35:41 +00:00
return s:keyword_filter(neocomplcache#dup_filter(snippets),
\ a:cur_keyword_str)
2012-02-02 04:33:35 +00:00
endfunction"}}}
function! neocomplcache#sources#snippets_complete#define()"{{{
return s:source
endfunction"}}}
function! s:compare_words(i1, i2)
return a:i1.menu - a:i2.menu
endfunction
" For echodoc."{{{
let s:doc_dict = {
\ 'name' : 'snippets_complete',
\ 'rank' : 100,
\ 'filetypes' : {},
\ }
function! s:doc_dict.search(cur_text)"{{{
if mode() !=# 'i'
return []
endif
let snippets = neocomplcache#sources#snippets_complete#get_snippets()
let cur_word = s:get_cursor_keyword_snippet(snippets, a:cur_text)
if cur_word == ''
return []
endif
let snip = snippets[cur_word]
let ret = []
call add(ret, { 'text' : snip.word, 'highlight' : 'String' })
call add(ret, { 'text' : ' ' })
call add(ret, { 'text' : snip.menu, 'highlight' : 'Special' })
return ret
endfunction"}}}
"}}}
function! s:keyword_filter(list, cur_keyword_str)"{{{
let keyword_escape = neocomplcache#keyword_escape(a:cur_keyword_str)
let prev_word = neocomplcache#get_prev_word(a:cur_keyword_str)
" Keyword filter.
2012-03-14 00:35:41 +00:00
let pattern = printf('v:val.word =~ %s && (!has_key(v:val, "prev_word") || v:val.prev_word == %s)',
2012-02-02 04:33:35 +00:00
\string('^' . keyword_escape), string(prev_word))
let list = filter(a:list, pattern)
" Substitute abbr.
2012-03-14 00:35:41 +00:00
let abbr_pattern = printf('%%.%ds..%%s',
\ g:neocomplcache_max_keyword_width-10)
2012-02-02 04:33:35 +00:00
for snippet in list
if snippet.snip =~ '\\\@<!`=.*\\\@<!`'
let snippet.menu = s:eval_snippet(snippet.snip)
if g:neocomplcache_max_keyword_width >= 0 &&
\ len(snippet.menu) > g:neocomplcache_max_keyword_width
2012-03-14 00:35:41 +00:00
let snippet.menu = printf(abbr_pattern,
\ snippet.menu, snippet.menu[-8:])
2012-02-02 04:33:35 +00:00
endif
let snippet.menu = '`Snip` ' . snippet.menu
endif
endfor
return list
endfunction"}}}
function! neocomplcache#sources#snippets_complete#expandable()"{{{
let ret = 0
if neocomplcache#sources#snippets_complete#force_expandable()
2012-02-02 04:33:35 +00:00
let ret += 1
endif
if neocomplcache#sources#snippets_complete#jumpable()
2012-02-02 04:33:35 +00:00
let ret += 2
endif
return ret
endfunction"}}}
function! neocomplcache#sources#snippets_complete#force_expandable()"{{{
let snippets = neocomplcache#sources#snippets_complete#get_snippets()
let cur_text = neocomplcache#get_cur_text(1)
" Found snippet trigger.
return s:get_cursor_keyword_snippet(snippets, cur_text) != ''
\ || s:get_cursor_snippet(snippets, cur_text) != ''
endfunction"}}}
function! neocomplcache#sources#snippets_complete#jumpable()"{{{
" Found snippet placeholder.
2012-03-14 00:35:41 +00:00
return search(s:get_placeholder_marker_pattern(). '\|'
\ .s:get_sync_placeholder_marker_pattern(), 'nw') > 0
endfunction"}}}
2012-02-02 04:33:35 +00:00
function! s:caching()"{{{
2012-03-14 00:35:41 +00:00
for filetype in neocomplcache#get_source_filetypes(
\ neocomplcache#get_context_filetype(1))
2012-02-02 04:33:35 +00:00
if !has_key(s:snippets, filetype)
call s:caching_snippets(filetype)
endif
endfor
endfunction"}}}
function! s:set_snippet_dict(snippet_pattern, snippet_dict, dup_check, snippets_file)"{{{
2012-03-17 13:53:56 +00:00
if !has_key(a:snippet_pattern, 'name')
return
2012-02-02 04:33:35 +00:00
endif
2012-03-17 13:53:56 +00:00
let pattern = s:set_snippet_pattern(a:snippet_pattern)
let action_pattern = '^snippet\s\+' . a:snippet_pattern.name . '$'
let a:snippet_dict[a:snippet_pattern.name] = pattern
let a:dup_check[a:snippet_pattern.name] = 1
for alias in get(a:snippet_pattern, 'alias', [])
let alias_pattern = copy(pattern)
let alias_pattern.word = alias
let abbr = (g:neocomplcache_max_keyword_width >= 0 &&
\ len(alias) > g:neocomplcache_max_keyword_width) ?
\ printf(abbr_pattern, alias, alias[-8:]) : alias
let alias_pattern.abbr = abbr
let alias_pattern.action__path = a:snippets_file
let alias_pattern.action__pattern = action_pattern
let alias_pattern.real_name = a:snippet_pattern.name
let a:snippet_dict[alias] = alias_pattern
let a:dup_check[alias] = 1
endfor
let snippet = a:snippet_dict[a:snippet_pattern.name]
let snippet.action__path = a:snippets_file
let snippet.action__pattern = action_pattern
let snippet.real_name = a:snippet_pattern.name
2012-02-02 04:33:35 +00:00
endfunction"}}}
function! s:set_snippet_pattern(dict)"{{{
2012-02-24 04:42:12 +00:00
let abbr_pattern = printf('%%.%ds..%%s',
\ g:neocomplcache_max_keyword_width-10)
2012-02-02 04:33:35 +00:00
2012-03-13 15:39:45 +00:00
let a:dict.word = substitute(a:dict.word, '\n$', '', '')
let menu_pattern =
\ (a:dict.word =~ '\${\d\+\%(:.\{-}\)\?\\\@<!}') ?
2012-02-24 04:42:12 +00:00
\ '<Snip> ' : '[Snip] '
2012-03-14 00:35:41 +00:00
let abbr = get(a:dict, 'abbr', substitute(a:dict.word,
\ s:get_placeholder_marker_pattern(). '\|'.
\ s:get_mirror_placeholder_marker_pattern().
\ '\|\s\+\|\n', ' ', 'g'))
2012-02-24 04:42:12 +00:00
let abbr = (g:neocomplcache_max_keyword_width >= 0 &&
\ len(abbr) > g:neocomplcache_max_keyword_width)?
2012-02-02 04:33:35 +00:00
\ printf(abbr_pattern, abbr, abbr[-8:]) : abbr
let dict = {
2012-02-24 04:42:12 +00:00
\ 'word' : a:dict.name,
2012-03-13 15:39:45 +00:00
\ 'snip' : a:dict.word, 'abbr' : a:dict.name,
\ 'description' : a:dict.word,
2012-02-02 04:33:35 +00:00
\ 'menu' : menu_pattern . abbr, 'dup' : 1
\}
if has_key(a:dict, 'prev_word')
let dict.prev_word = a:dict.prev_word
endif
return dict
endfunction"}}}
function! s:edit_snippets(filetype, isruntime)"{{{
if a:filetype == ''
let filetype = neocomplcache#get_context_filetype(1)
else
let filetype = a:filetype
endif
" Edit snippet file.
if a:isruntime
if empty(s:runtime_dir)
return
endif
let filename = s:runtime_dir[0].'/'.filetype.'.snip'
else
if empty(s:snippets_dir)
return
endif
let filename = s:snippets_dir[-1].'/'.filetype.'.snip'
endif
if filereadable(filename)
edit `=filename`
else
enew
setfiletype snippet
saveas `=filename`
endif
endfunction"}}}
function! s:caching_snippets(filetype)"{{{
let filetype = a:filetype == '' ?
\ &filetype : a:filetype
let snippet = {}
2012-08-02 02:11:44 +00:00
let snippets_files =
\ split(globpath(join(s:snippets_dir, ','),
\ filetype . '.snip*'), '\n')
2012-03-14 00:35:41 +00:00
\ + split(globpath(join(s:snippets_dir, ','),
2012-08-02 02:11:44 +00:00
\ filetype . '_*.snip*'), '\n')
\ + split(globpath(join(s:snippets_dir, ','),
\ filetype . '/*.snip*'), '\n')
2012-02-02 04:33:35 +00:00
for snippets_file in snippets_files
call s:load_snippets(snippet, snippets_file)
endfor
let s:snippets[filetype] = snippet
endfunction"}}}
function! s:load_snippets(snippet, snippets_file)"{{{
let dup_check = {}
let snippet_pattern = { 'word' : '' }
2012-03-14 00:35:41 +00:00
let abbr_pattern = printf('%%.%ds..%%s',
\ g:neocomplcache_max_keyword_width-10)
2012-02-02 04:33:35 +00:00
let linenr = 1
for line in readfile(a:snippets_file)
if line =~ '^\h\w*.*\s$'
" Delete spaces.
let line = substitute(line, '\s\+$', '', '')
endif
if line =~ '^include'
" Include snippets.
let snippet_file = matchstr(line, '^include\s\+\zs.*$')
2012-03-14 00:35:41 +00:00
for snippets_file in split(globpath(join(s:snippets_dir, ','),
\ snippet_file), '\n')
2012-02-02 04:33:35 +00:00
call s:load_snippets(a:snippet, snippets_file)
endfor
elseif line =~ '^delete\s'
let name = matchstr(line, '^delete\s\+\zs.*$')
if name != '' && has_key(a:snippet, name)
call filter(a:snippet, 'v:val.real_name !=# name')
endif
elseif line =~ '^snippet\s'
if has_key(snippet_pattern, 'name')
" Set previous snippet.
call s:set_snippet_dict(snippet_pattern,
\ a:snippet, dup_check, a:snippets_file)
let snippet_pattern = { 'word' : '' }
endif
let snippet_pattern.name =
2012-03-14 00:35:41 +00:00
\ substitute(matchstr(line, '^snippet\s\+\zs.*$'),
\ '\s', '_', 'g')
2012-02-02 04:33:35 +00:00
" Check for duplicated names.
if has_key(dup_check, snippet_pattern.name)
2012-03-14 00:35:41 +00:00
call neocomplcache#print_error(
\ 'Warning: ' . a:snippets_file . ':'
2012-02-02 04:33:35 +00:00
\ . linenr . ': duplicated snippet name `'
\ . snippet_pattern.name . '`')
2012-03-13 15:39:45 +00:00
call neocomplcache#print_error(
\ 'Please delete this snippet name before.')
2012-02-02 04:33:35 +00:00
endif
elseif has_key(snippet_pattern, 'name')
" Only in snippets.
if line =~ '^abbr\s'
let snippet_pattern.abbr = matchstr(line, '^abbr\s\+\zs.*$')
elseif line =~ '^alias\s'
let snippet_pattern.alias = split(matchstr(line,
\ '^alias\s\+\zs.*$'), '[,[:space:]]\+')
elseif line =~ '^prev_word\s'
let snippet_pattern.prev_word = matchstr(line,
\ '^prev_word\s\+[''"]\zs.*\ze[''"]$')
elseif line =~ '^\s'
2012-06-02 00:53:35 +00:00
if snippet_pattern.word != ''
2012-03-13 15:39:45 +00:00
let snippet_pattern.word .= "\n"
2012-02-02 04:33:35 +00:00
endif
2012-06-02 00:53:35 +00:00
let snippet_pattern.word .=
\ matchstr(line, '^\%(\t\| *\)\zs.*$')
2012-02-02 04:33:35 +00:00
elseif line =~ '^$'
" Blank line.
2012-03-13 15:39:45 +00:00
let snippet_pattern.word .= "\n"
2012-02-02 04:33:35 +00:00
endif
endif
let linenr += 1
endfor
" Set previous snippet.
call s:set_snippet_dict(snippet_pattern,
\ a:snippet, dup_check, a:snippets_file)
return a:snippet
endfunction"}}}
function! s:get_cursor_keyword_snippet(snippets, cur_text)"{{{
2012-02-22 03:59:57 +00:00
let cur_word = matchstr(a:cur_text,
\ neocomplcache#get_keyword_pattern_end().'\|\h\w*\W\+$')
" Check prev_word.
let prev_word = neocomplcache#get_prev_word(cur_word)
let pattern = printf('(!has_key(v:val, "prev_word") || v:val.prev_word ==# %s)',
\ string(prev_word))
let dict = filter(copy(a:snippets), pattern)
if !has_key(dict, cur_word)
2012-02-02 04:33:35 +00:00
let cur_word = ''
endif
return cur_word
endfunction"}}}
function! s:get_cursor_snippet(snippets, cur_text)"{{{
let cur_word = matchstr(a:cur_text, '\S\+$')
while cur_word != '' && !has_key(a:snippets, cur_word)
let cur_word = cur_word[1:]
endwhile
return cur_word
endfunction"}}}
function! s:snippets_force_expand(cur_text, col)"{{{
2012-02-22 03:59:57 +00:00
let cur_word = s:get_cursor_snippet(
2012-03-14 00:35:41 +00:00
\ neocomplcache#sources#snippets_complete#get_snippets(),
\ a:cur_text)
2012-02-02 04:33:35 +00:00
2012-02-22 03:59:57 +00:00
call neocomplcache#sources#snippets_complete#expand(
\ a:cur_text, a:col, cur_word)
2012-02-02 04:33:35 +00:00
endfunction"}}}
function! s:snippets_expand_or_jump(cur_text, col)"{{{
let cur_word = s:get_cursor_keyword_snippet(
2012-03-14 00:35:41 +00:00
\ neocomplcache#sources#snippets_complete#get_snippets(),
\ a:cur_text)
if cur_word == ''
" Check by force_expand.
let cur_word = s:get_cursor_snippet(
\ neocomplcache#sources#snippets_complete#get_snippets(),
\ a:cur_text)
endif
2012-02-02 04:33:35 +00:00
if cur_word != ''
" Found snippet trigger.
2012-02-22 03:59:57 +00:00
call neocomplcache#sources#snippets_complete#expand(
\ a:cur_text, a:col, cur_word)
2012-02-02 04:33:35 +00:00
else
call s:snippets_force_jump(a:cur_text, a:col)
endif
endfunction"}}}
function! s:snippets_jump_or_expand(cur_text, col)"{{{
let cur_word = s:get_cursor_keyword_snippet(
\ neocomplcache#sources#snippets_complete#get_snippets(), a:cur_text)
2012-03-14 00:35:41 +00:00
if search(s:get_placeholder_marker_pattern(). '\|'
\ .s:get_sync_placeholder_marker_pattern(), 'nw') > 0
2012-02-02 04:33:35 +00:00
" Found snippet placeholder.
call s:snippets_force_jump(a:cur_text, a:col)
else
2012-02-22 03:59:57 +00:00
call neocomplcache#sources#snippets_complete#expand(
\ a:cur_text, a:col, cur_word)
2012-02-02 04:33:35 +00:00
endif
endfunction"}}}
2012-03-13 15:39:45 +00:00
2012-02-02 04:33:35 +00:00
function! neocomplcache#sources#snippets_complete#expand(cur_text, col, trigger_name)"{{{
if a:trigger_name == ''
let pos = getpos('.')
let pos[2] = len(a:cur_text)+1
call setpos('.', pos)
if pos[2] < col('$')
startinsert
else
startinsert!
endif
return
endif
let snippets = neocomplcache#sources#snippets_complete#get_snippets()
let snippet = snippets[a:trigger_name]
let cur_text = a:cur_text[: -1-len(a:trigger_name)]
let snip_word = snippet.snip
if snip_word =~ '\\\@<!`.*\\\@<!`'
let snip_word = s:eval_snippet(snip_word)
endif
" Substitute escaped `.
let snip_word = substitute(snip_word, '\\`', '`', 'g')
2012-03-14 00:43:33 +00:00
" Substitute markers.
let snip_word = substitute(snip_word,
\ s:get_placeholder_marker_substitute_pattern(),
\ '<`\1`>', 'g')
let snip_word = substitute(snip_word,
\ s:get_mirror_placeholder_marker_substitute_pattern(),
\ '<|\1|>', 'g')
2012-02-02 04:33:35 +00:00
" Insert snippets.
let next_line = getline('.')[a:col-1 :]
2012-03-13 15:39:45 +00:00
let snippet_lines = split(snip_word, '\n', 1)
if empty(snippet_lines)
return
endif
let begin_line = line('.')
let end_line = line('.') + len(snippet_lines) - 1
let snippet_lines[0] = cur_text . snippet_lines[0]
let next_col = len(snippet_lines[-1]) + 1
let snippet_lines[-1] = snippet_lines[-1] . next_line
2012-06-02 00:53:35 +00:00
call setline('.', snippet_lines[0])
2012-03-13 23:49:57 +00:00
if len(snippet_lines) > 1
2012-06-02 00:53:35 +00:00
call append('.', snippet_lines[1:])
2012-03-13 23:49:57 +00:00
endif
2012-03-13 15:39:45 +00:00
call s:indent_snippet(begin_line, end_line)
let begin_patterns = (begin_line > 1) ?
\ [getline(begin_line - 1)] : []
let end_patterns = (end_line < line('$')) ?
\ [getline(end_line + 1)] : []
call add(s:snippets_expand_stack, {
2012-03-15 11:25:16 +00:00
\ 'begin_line' : begin_line,
\ 'begin_patterns' : begin_patterns,
2012-03-15 11:25:16 +00:00
\ 'end_line' : end_line,
\ 'end_patterns' : end_patterns,
\ 'holder_cnt' : 1,
\ })
2012-02-02 04:33:35 +00:00
if has('folding') && foldclosed(line('.'))
" Open fold.
silent! normal! zO
endif
if next_col < col('$')
startinsert
else
startinsert!
endif
2012-03-14 00:35:41 +00:00
if snip_word =~ s:get_placeholder_marker_pattern()
2012-02-02 04:33:35 +00:00
call s:snippets_force_jump(a:cur_text, a:col)
endif
let &l:iminsert = 0
let &l:imsearch = 0
endfunction"}}}
2012-03-13 15:39:45 +00:00
function! s:indent_snippet(begin, end)"{{{
2012-03-08 09:38:58 +00:00
let equalprg = &l:equalprg
setlocal equalprg=
2012-02-02 04:33:35 +00:00
2012-03-15 15:06:21 +00:00
let pos = getpos('.')
let base_indent = matchstr(getline(a:begin), '^\s\+')
2012-06-02 00:53:35 +00:00
for line_nr in range(a:begin+1, a:end)
2012-03-13 15:39:45 +00:00
call cursor(line_nr, 0)
2012-02-02 04:33:35 +00:00
2012-03-15 15:06:21 +00:00
if getline('.') =~ '^\t\+'
if &l:expandtab
" Expand tab.
cal setline('.', substitute(getline('.'),
2012-03-18 15:39:35 +00:00
\ '^\t\+', base_indent . repeat(' ', &shiftwidth *
2012-03-15 15:06:21 +00:00
\ len(matchstr(getline('.'), '^\t\+'))), ''))
elseif line_nr != a:begin
call setline('.', base_indent . getline('.'))
endif
2012-02-02 04:33:35 +00:00
else
2012-03-14 00:35:41 +00:00
silent normal! ==
2012-02-02 04:33:35 +00:00
endif
endfor
2012-03-15 15:06:21 +00:00
call setpos('.', pos)
2012-03-13 15:39:45 +00:00
let &l:equalprg = equalprg
2012-02-02 04:33:35 +00:00
endfunction"}}}
2012-03-13 15:39:45 +00:00
2012-02-02 04:33:35 +00:00
function! s:snippets_force_jump(cur_text, col)"{{{
" Get patterns and count.
if empty(s:snippets_expand_stack)
return s:search_outof_range(a:col)
endif
2012-02-02 04:33:35 +00:00
let expand_info = s:snippets_expand_stack[-1]
" Search patterns.
let [begin, end] = s:get_snippet_range(
2012-03-15 11:25:16 +00:00
\ expand_info.begin_line,
\ expand_info.begin_patterns,
2012-03-15 11:25:16 +00:00
\ expand_info.end_line,
\ expand_info.end_patterns)
if s:search_snippet_range(begin, end, expand_info.holder_cnt)
" Next count.
let expand_info.holder_cnt += 1
return 1
endif
2012-02-02 04:33:35 +00:00
" Search placeholder 0.
if s:search_snippet_range(begin, end, 0)
return 1
2012-02-02 04:33:35 +00:00
endif
" Not found.
let s:snippets_expand_stack = s:snippets_expand_stack[: -2]
return s:search_outof_range(a:col)
2012-02-02 04:33:35 +00:00
endfunction"}}}
2012-03-15 11:25:16 +00:00
function! s:get_snippet_range(begin_line, begin_patterns, end_line, end_patterns)"{{{
let pos = getpos('.')
call cursor(a:begin_line, 0)
if empty(a:begin_patterns)
let begin = line('.') - 50
else
2012-03-15 12:29:33 +00:00
let [begin, _] = searchpos('^' . neocomplcache#util#escape_pattern(
\ a:begin_patterns[0]) . '$', 'bnW')
if begin <= 0
2012-03-15 11:25:16 +00:00
let begin = line('.') - 50
2012-03-15 10:59:01 +00:00
endif
endif
2012-03-15 11:25:16 +00:00
if begin <= 0
let begin = 1
endif
2012-02-02 04:33:35 +00:00
2012-03-15 11:25:16 +00:00
call cursor(a:end_line, 0)
if empty(a:end_patterns)
let end = line('.') + 50
else
2012-03-15 12:29:33 +00:00
let [end, _] = searchpos('^' . neocomplcache#util#escape_pattern(
\ a:end_patterns[0]) . '$', 'nW')
if end <= 0
2012-03-15 10:59:01 +00:00
let end = line('.') + 50
endif
endif
if end > line('$')
let end = line('$')
endif
2012-03-15 11:25:16 +00:00
call setpos('.', pos)
2012-03-15 11:50:45 +00:00
return [begin, end]
endfunction"}}}
function! s:search_snippet_range(start, end, cnt)"{{{
call s:substitute_placeholder_marker(a:start, a:end, a:cnt)
let pattern = substitute(
\ s:get_placeholder_marker_pattern(), '\\d\\+', a:cnt, '')
2012-02-02 04:33:35 +00:00
let line = a:start
for line in filter(range(a:start, a:end),
\ 'getline(v:val) =~ pattern')
call s:expand_placeholder(a:start, a:end, a:cnt, line)
2012-02-02 04:33:35 +00:00
return 1
endfor
return 0
endfunction"}}}
function! s:search_outof_range(col)"{{{
call s:substitute_placeholder_marker(1, 0, 0)
2012-02-02 04:33:35 +00:00
2012-03-14 00:35:41 +00:00
let pattern = s:get_placeholder_marker_pattern()
2012-02-02 04:33:35 +00:00
if search(pattern, 'w') > 0
call s:expand_placeholder(line('.'), 0, '\d\+', line('.'))
return 1
endif
let pos = getpos('.')
if a:col == 1
let pos[2] = 1
call setpos('.', pos)
startinsert
elseif a:col == col('$')
startinsert!
else
let pos[2] = a:col+1
call setpos('.', pos)
startinsert
endif
" Not found.
return 0
endfunction"}}}
function! s:expand_placeholder(start, end, holder_cnt, line)"{{{
2012-03-14 00:35:41 +00:00
let pattern = substitute(s:get_placeholder_marker_pattern(),
\ '\\d\\+', a:holder_cnt, '')
2012-02-02 04:33:35 +00:00
let current_line = getline(a:line)
let match = match(current_line, pattern)
2012-03-14 00:35:41 +00:00
let default_pattern = substitute(
\ s:get_placeholder_marker_default_pattern(),
\ '\\d\\+', a:holder_cnt, '')
2012-02-02 04:33:35 +00:00
let default = substitute(
\ matchstr(current_line, default_pattern), '\\\ze.', '', 'g')
2012-04-05 00:53:52 +00:00
" Substitute marker.
let default = substitute(default,
\ s:get_placeholder_marker_substitute_pattern(),
\ '<`\1`>', 'g')
let default = substitute(default,
\ s:get_mirror_placeholder_marker_substitute_pattern(),
\ '<|\1|>', 'g')
2012-02-02 04:33:35 +00:00
let default_len = len(default)
let pos = getpos('.')
let pos[1] = a:line
let pos[2] = match+1
let cnt = s:search_sync_placeholder(a:start, a:end, a:holder_cnt)
if cnt > 0
2012-03-14 00:35:41 +00:00
let pattern = substitute(s:get_placeholder_marker_pattern(),
\ '\\d\\+', cnt, '')
2012-02-02 04:33:35 +00:00
call setline(a:line, substitute(current_line, pattern,
2012-03-14 01:13:13 +00:00
\ '<{'.cnt.':'.escape(default, '\').'}>', ''))
let pos[2] += len('<{'.cnt.':')
2012-02-02 04:33:35 +00:00
else
" Substitute holder.
call setline(a:line,
\ substitute(current_line, pattern, escape(default, '\'), ''))
endif
call setpos('.', pos)
if default_len > 0
" Select default value.
let len = default_len-1
2012-03-14 00:35:41 +00:00
if &l:selection == 'exclusive'
2012-02-02 04:33:35 +00:00
let len += 1
endif
stopinsert
2012-03-14 00:35:41 +00:00
execute 'normal! v'. repeat('l', len) . "\<C-g>"
2012-02-02 04:33:35 +00:00
elseif pos[2] < col('$')
startinsert
else
startinsert!
endif
endfunction"}}}
function! s:search_sync_placeholder(start, end, number)"{{{
if a:end == 0
" Search in current buffer.
2012-03-14 00:35:41 +00:00
let cnt = matchstr(getline('.'),
\ substitute(s:get_placeholder_marker_pattern(),
\ '\\d\\+', '\\zs\\d\\+\\ze', ''))
return search(substitute(
2012-03-15 11:06:20 +00:00
\ s:get_mirror_placeholder_marker_pattern(),
2012-03-14 00:35:41 +00:00
\ '\\d\\+', cnt, ''), 'nw') > 0 ? cnt : 0
2012-02-02 04:33:35 +00:00
endif
2012-03-14 00:35:41 +00:00
let pattern = substitute(
\ s:get_mirror_placeholder_marker_pattern(),
\ '\\d\\+', a:number, '')
2012-02-02 04:33:35 +00:00
for line in filter(range(a:start, a:end),
\ 'getline(v:val) =~ pattern')
2012-03-23 03:14:13 +00:00
return a:number
2012-02-02 04:33:35 +00:00
endfor
return 0
endfunction"}}}
function! s:substitute_placeholder_marker(start, end, snippet_holder_cnt)"{{{
if a:snippet_holder_cnt > 1
let cnt = a:snippet_holder_cnt-1
2012-03-18 15:56:24 +00:00
let sync_marker = substitute(s:get_sync_placeholder_marker_pattern(),
2012-03-14 00:35:41 +00:00
\ '\\d\\+', cnt, '')
2012-03-18 15:56:24 +00:00
let mirror_marker = substitute(
\ s:get_mirror_placeholder_marker_pattern(),
\ '\\d\\+', cnt, '')
2012-02-02 04:33:35 +00:00
let line = a:start
2012-03-14 01:13:13 +00:00
for line in range(a:start, a:end)
2012-03-18 15:56:24 +00:00
if getline(line) =~ sync_marker
2012-03-14 00:35:41 +00:00
let sub = escape(matchstr(getline(line),
\ substitute(s:get_sync_placeholder_marker_default_pattern(),
\ '\\d\\+', cnt, '')), '/\')
2012-03-18 15:56:24 +00:00
silent execute printf('%d,%ds/' . mirror_marker . '/%s/'
\ . (&gdefault ? '' : 'g'), a:start, a:end, sub)
call setline(line, substitute(getline(line), sync_marker, sub, ''))
2012-02-02 04:33:35 +00:00
endif
2012-03-14 01:13:13 +00:00
endfor
2012-03-14 00:35:41 +00:00
elseif search(s:get_sync_placeholder_marker_pattern(), 'wb') > 0
let sub = escape(matchstr(getline('.'),
\ s:get_sync_placeholder_marker_default_pattern()), '/\')
let cnt = matchstr(getline('.'),
\ substitute(s:get_sync_placeholder_marker_pattern(),
\ '\\d\\+', '\\zs\\d\\+\\ze', ''))
2012-03-18 15:56:24 +00:00
silent execute printf('%%s/' . mirror_marker . '/%s/'
2012-03-14 00:35:41 +00:00
\ . (&gdefault ? 'g' : ''), sub)
2012-03-18 15:56:24 +00:00
let sync_marker = substitute(s:get_sync_placeholder_marker_pattern(),
2012-03-14 00:35:41 +00:00
\ '\\d\\+', cnt, '')
2012-03-18 15:56:24 +00:00
let mirror_marker = substitute(
\ s:get_mirror_placeholder_marker_pattern(),
\ '\\d\\+', cnt, '')
call setline('.', substitute(getline('.'), sync_marker, sub, ''))
2012-02-02 04:33:35 +00:00
endif
endfunction"}}}
function! s:trigger(function)"{{{
let cur_text = neocomplcache#get_cur_text(1)
2012-03-14 00:35:41 +00:00
return printf("\<ESC>:call %s(%s,%d)\<CR>",
\ a:function, string(cur_text), col('.'))
2012-02-02 04:33:35 +00:00
endfunction"}}}
function! s:eval_snippet(snippet_text)"{{{
let snip_word = ''
let prev_match = 0
let match = match(a:snippet_text, '\\\@<!`.\{-}\\\@<!`')
while match >= 0
if match - prev_match > 0
let snip_word .= a:snippet_text[prev_match : match - 1]
endif
2012-03-14 00:35:41 +00:00
let prev_match = matchend(a:snippet_text,
\ '\\\@<!`.\{-}\\\@<!`', match)
2012-02-02 04:33:35 +00:00
let snip_word .= eval(a:snippet_text[match+1 : prev_match - 2])
let match = match(a:snippet_text, '\\\@<!`.\{-}\\\@<!`', prev_match)
endwhile
if prev_match >= 0
let snip_word .= a:snippet_text[prev_match :]
endif
return snip_word
endfunction"}}}
function! neocomplcache#sources#snippets_complete#get_snippets()"{{{
" Get buffer filetype.
let filetype = neocomplcache#get_context_filetype(1)
let snippets = {}
for source in neocomplcache#get_sources_list(s:snippets, filetype)
call extend(snippets, source, 'keep')
endfor
call extend(snippets, copy(s:snippets['_']), 'keep')
return snippets
endfunction"}}}
function! neocomplcache#sources#snippets_complete#get_snippets_dir()"{{{
return s:snippets_dir
endfunction"}}}
2012-02-02 04:33:35 +00:00
2012-03-13 23:49:57 +00:00
function! s:get_placeholder_marker_pattern()"{{{
2012-03-14 00:43:33 +00:00
return '<`\d\+\%(:.\{-}\)\?\\\@<!`>'
2012-03-13 23:49:57 +00:00
endfunction"}}}
2012-03-14 00:35:41 +00:00
function! s:get_placeholder_marker_substitute_pattern()"{{{
2012-03-14 00:43:33 +00:00
return '\${\(\d\+\%(:.\{-}\)\?\\\@<!\)}'
2012-03-14 00:35:41 +00:00
endfunction"}}}
function! s:get_placeholder_marker_default_pattern()"{{{
2012-03-14 00:43:33 +00:00
return '<`\d\+:\zs.\{-}\ze\\\@<!`>'
2012-03-14 00:35:41 +00:00
endfunction"}}}
2012-03-13 23:49:57 +00:00
function! s:get_sync_placeholder_marker_pattern()"{{{
2012-03-14 00:43:33 +00:00
return '<{\d\+\%(:.\{-}\)\?\\\@<!}>'
2012-03-13 23:49:57 +00:00
endfunction"}}}
2012-03-14 00:35:41 +00:00
function! s:get_sync_placeholder_marker_default_pattern()"{{{
2012-03-14 00:43:33 +00:00
return '<{\d\+:\zs.\{-}\ze\\\@<!}>'
2012-03-14 00:35:41 +00:00
endfunction"}}}
2012-03-13 23:49:57 +00:00
function! s:get_mirror_placeholder_marker_pattern()"{{{
2012-03-14 00:43:33 +00:00
return '<|\d\+|>'
2012-03-13 23:49:57 +00:00
endfunction"}}}
2012-03-14 00:35:41 +00:00
function! s:get_mirror_placeholder_marker_substitute_pattern()"{{{
2012-03-14 00:43:33 +00:00
return '\$\(\d\+\)'
2012-03-14 00:35:41 +00:00
endfunction"}}}
2012-03-13 23:49:57 +00:00
function! s:SID_PREFIX()"{{{
2012-02-02 04:33:35 +00:00
return matchstr(expand('<sfile>'), '<SNR>\d\+_')
2012-03-13 23:49:57 +00:00
endfunction"}}}
2012-02-02 04:33:35 +00:00
" Plugin key-mappings.
inoremap <silent><expr> <Plug>(neocomplcache_snippets_expand)
\ <SID>trigger(<SID>SID_PREFIX().'snippets_expand_or_jump')
snoremap <silent><expr> <Plug>(neocomplcache_snippets_expand)
\ <SID>trigger(<SID>SID_PREFIX().'snippets_expand_or_jump')
inoremap <silent><expr> <Plug>(neocomplcache_snippets_jump)
\ <SID>trigger(<SID>SID_PREFIX().'snippets_jump_or_expand')
snoremap <silent><expr> <Plug>(neocomplcache_snippets_jump)
\ <SID>trigger(<SID>SID_PREFIX().'snippets_jump_or_expand')
inoremap <silent><expr> <Plug>(neocomplcache_snippets_force_expand)
\ <SID>trigger(<SID>SID_PREFIX().'snippets_force_expand')
snoremap <silent><expr> <Plug>(neocomplcache_snippets_force_expand)
\ <SID>trigger(<SID>SID_PREFIX().'snippets_force_expand')
inoremap <silent><expr> <Plug>(neocomplcache_snippets_force_jump)
\ <SID>trigger(<SID>SID_PREFIX().'snippets_force_jump')
snoremap <silent><expr> <Plug>(neocomplcache_snippets_force_jump)
\ <SID>trigger(<SID>SID_PREFIX().'snippets_force_jump')
let &cpo = s:save_cpo
unlet s:save_cpo
" vim: foldmethod=marker