- Improved parse of snippets file.

This commit is contained in:
Shougo Matsushita 2012-10-29 11:22:37 +09:00
parent 2b5eb202f7
commit de453e5a16
3 changed files with 71 additions and 66 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: 28 Oct 2012. " Last Modified: 29 Oct 2012.
" 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
@ -170,34 +170,28 @@ function! neosnippet#recaching()"{{{
let s:snippets = {} let s:snippets = {}
endfunction"}}} endfunction"}}}
function! s:set_snippet_dict(snippet_pattern, snippet_dict, dup_check, snippets_file)"{{{ function! s:set_snippet_dict(snippet_dict, snippets, dup_check, snippets_file)"{{{
if !has_key(a:snippet_pattern, 'name') if empty(a:snippet_dict)
return return
endif endif
let pattern = s:set_snippet_pattern(a:snippet_pattern) let action_pattern = '^snippet\s\+' . a:snippet_dict.name . '$'
let action_pattern = '^snippet\s\+' . a:snippet_pattern.name . '$' let snippet = s:initialize_snippet(
let a:snippet_dict[a:snippet_pattern.name] = pattern \ a:snippet_dict, a:snippets_file,
let a:dup_check[a:snippet_pattern.name] = 1 \ a:snippet_dict.linenr, action_pattern,
\ a:snippet_dict.name)
let a:snippets[a:snippet_dict.name] = snippet
let a:dup_check[a:snippet_dict.name] = snippet
for alias in get(a:snippet_pattern, 'alias', []) for alias in get(a:snippet_dict, 'alias', [])
let alias_pattern = copy(pattern) let alias_snippet = copy(snippet)
let alias_pattern.word = alias let alias_snippet.word = alias
let alias_pattern.action__path = a:snippets_file let a:snippet_dict[alias] = alias_snippet
let alias_pattern.action__pattern = action_pattern let a:dup_check[alias] = alias_snippet
let alias_pattern.real_name = a:snippet_pattern.name
let a:snippet_dict[alias] = alias_pattern
let a:dup_check[alias] = 1
endfor 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
endfunction"}}} endfunction"}}}
function! s:set_snippet_pattern(dict)"{{{ function! s:initialize_snippet(dict, path, line, pattern, name)"{{{
let a:dict.word = substitute(a:dict.word, '\n$', '', '') let a:dict.word = substitute(a:dict.word, '\n$', '', '')
let menu_pattern = (a:dict.word =~ let menu_pattern = (a:dict.word =~
\ s:get_placeholder_marker_substitute_pattern() \ s:get_placeholder_marker_substitute_pattern()
@ -219,6 +213,8 @@ function! s:set_snippet_pattern(dict)"{{{
\ 'description' : a:dict.word, \ 'description' : a:dict.word,
\ 'menu' : menu_pattern.abbr, \ 'menu' : menu_pattern.abbr,
\ 'dup' : 1, 'options' : a:dict.options, \ 'dup' : 1, 'options' : a:dict.options,
\ 'action__path' : a:path, 'action__line' : a:line,
\ 'action__pattern' : a:pattern, 'real_name' : a:name,
\} \}
return dict return dict
endfunction"}}} endfunction"}}}
@ -304,16 +300,15 @@ function! neosnippet#make_cache(filetype)"{{{
\ + split(globpath(join(snippets_dir, ','), \ + split(globpath(join(snippets_dir, ','),
\ filetype . '/**/*.snip*'), '\n') \ filetype . '/**/*.snip*'), '\n')
for snippets_file in reverse(snippets_files) for snippets_file in reverse(snippets_files)
call s:load_snippets(snippet, snippets_file) call s:parse_snippets_file(snippet, snippets_file)
endfor endfor
let s:snippets[filetype] = snippet let s:snippets[filetype] = snippet
endfunction"}}} endfunction"}}}
function! s:load_snippets(snippet, snippets_file)"{{{ function! s:parse_snippets_file(snippets, snippets_file)"{{{
let dup_check = {} let dup_check = {}
let snippet_pattern = { 'word' : '', let snippet_dict = {}
\ 'options' : { 'head' : 0, 'word' : 0 } }
let linenr = 1 let linenr = 1
@ -330,64 +325,68 @@ function! s:load_snippets(snippet, snippets_file)"{{{
for snippets_file in split(globpath(join( for snippets_file in split(globpath(join(
\ neosnippet#get_snippets_directory(), ','), \ neosnippet#get_snippets_directory(), ','),
\ snippet_file), '\n') \ snippet_file), '\n')
call s:load_snippets(a:snippet, snippets_file) call s:parse_snippets_file(a:snippets, snippets_file)
endfor endfor
elseif line =~ '^delete\s' elseif line =~ '^delete\s'
let name = matchstr(line, '^delete\s\+\zs.*$') let name = matchstr(line, '^delete\s\+\zs.*$')
if name != '' && has_key(a:snippet, name) if name != '' && has_key(a:snippets, name)
call filter(a:snippet, 'v:val.real_name !=# name') call filter(a:snippets, 'v:val.real_name !=# name')
endif endif
elseif line =~ '^snippet\s' elseif line =~ '^snippet\s'
if has_key(snippet_pattern, 'name') if !empty(snippet_dict)
" Set previous snippet. " Set previous snippet.
call s:set_snippet_dict(snippet_pattern, call s:set_snippet_dict(snippet_dict,
\ a:snippet, dup_check, a:snippets_file) \ a:snippets, dup_check, a:snippets_file)
let snippet_pattern = { 'word' : '',
\ 'options' : { 'head' : 0, 'word' : 0 } }
endif endif
" Initialize snippet dict.
let snippet_dict = { 'word' : '', 'linenr' : linenr,
\ 'options' : { 'head' : 0, 'word' : 0 } }
" Try using the name without the description (abbr). " Try using the name without the description (abbr).
let snippet_pattern.name = matchstr(line, '^snippet\s\+\zs\S\+') let snippet_dict.name = matchstr(line, '^snippet\s\+\zs\S\+')
" Fall back to using the name and description (abbr) combined. " Fall back to using the name and description (abbr) combined.
" SnipMate snippets may have duplicate names, but different " SnipMate snippets may have duplicate names, but different
" descriptions (abbrs). " descriptions (abbrs).
if has_key(dup_check, snippet_pattern.name) if has_key(dup_check, snippet_dict.name)
let description = matchstr(line, '^snippet\s\+\zs.*$') let description = matchstr(line, '^snippet\s\+\zs.*$')
if description !=# snippet_pattern.name if description !=# snippet_dict.name
" Convert description. " Convert description.
let snippet_pattern.name = let snippet_dict.name =
\ substitute(description, '\W\+', '_', 'g') \ substitute(description, '\W\+', '_', 'g')
endif endif
endif endif
" Collect the description (abbr) of the snippet, if set on snippet line. " Collect the description (abbr) of the snippet, if set on snippet line.
" This is for compatibility with SnipMate-style snippets. " This is for compatibility with SnipMate-style snippets.
let snippet_pattern.abbr = matchstr(line, '^snippet\s\+\S\+\s\+\zs.*$') let snippet_dict.abbr = matchstr(line, '^snippet\s\+\S\+\s\+\zs.*$')
" Check for duplicated names. " Check for duplicated names.
if has_key(dup_check, snippet_pattern.name) if has_key(dup_check, snippet_dict.name)
call neosnippet#util#print_error( let dup = dup_check[snippet_dict.name]
\ 'Warning: ' . a:snippets_file . ':' call neosnippet#util#print_error(printf(
\ . linenr . ': duplicated snippet name `' \ 'Warning: %s:%d is overriding `%s` from %s:%d',
\ . snippet_pattern.name . '`') \ a:snippets_file, linenr, snippet_dict.name,
call neosnippet#util#print_error( \ dup.action__path, dup.action__line))
\ 'Please delete this snippet name before.') call neosnippet#util#print_error(printf(
\ 'Please rename the snippet name or use `delete %s`.',
\ snippet_dict.name))
endif endif
elseif has_key(snippet_pattern, 'name') elseif !empty(snippet_dict)
" Allow overriding/setting of the description (abbr) of the snippet. " Allow overriding/setting of the description (abbr) of the snippet.
" This will override what was set via the snippet line. " This will override what was set via the snippet line.
if line =~ '^abbr\s' if line =~ '^abbr\s'
let snippet_pattern.abbr = matchstr(line, '^abbr\s\+\zs.*$') let snippet_dict.abbr = matchstr(line, '^abbr\s\+\zs.*$')
elseif line =~ '^alias\s' elseif line =~ '^alias\s'
let snippet_pattern.alias = split(matchstr(line, let snippet_dict.alias = split(matchstr(line,
\ '^alias\s\+\zs.*$'), '[,[:space:]]\+') \ '^alias\s\+\zs.*$'), '[,[:space:]]\+')
elseif line =~ '^prev_word\s' elseif line =~ '^prev_word\s'
let prev_word = matchstr(line, let prev_word = matchstr(line,
\ '^prev_word\s\+[''"]\zs.*\ze[''"]$') \ '^prev_word\s\+[''"]\zs.*\ze[''"]$')
if prev_word == '^' if prev_word == '^'
" For backward compatibility. " For backward compatibility.
let snippet_pattern.options.head = 1 let snippet_dict.options.head = 1
else else
call neosnippet#util#print_error( call neosnippet#util#print_error(
\ 'prev_word must be "^" character.') \ 'prev_word must be "^" character.')
@ -395,43 +394,45 @@ function! s:load_snippets(snippet, snippets_file)"{{{
elseif line =~ '^options\s\+' elseif line =~ '^options\s\+'
for option in split(matchstr(line, for option in split(matchstr(line,
\ '^options\s\+\zs.*$'), '[,[:space:]]\+') \ '^options\s\+\zs.*$'), '[,[:space:]]\+')
if !has_key(snippet_pattern.options, option) if !has_key(snippet_dict.options, option)
call neosnippet#util#print_error( call neosnippet#util#print_error(
\ printf('invalid option name : %s is detected.', option)) \ printf('invalid option name : %s is detected.', option))
else else
let snippet_pattern.options[option] = 1 let snippet_dict.options[option] = 1
endif endif
endfor endfor
elseif line =~ '^\s' elseif line =~ '^\s'
if snippet_pattern.word != '' if snippet_dict.word != ''
let snippet_pattern.word .= "\n" let snippet_dict.word .= "\n"
else else
" Substitute Tab character. " Substitute Tab character.
let line = substitute(line, '^\t', '', '') let line = substitute(line, '^\t', '', '')
endif endif
let snippet_pattern.word .= let snippet_dict.word .=
\ matchstr(line, '^ *\zs.*$') \ matchstr(line, '^ *\zs.*$')
elseif line =~ '^$' elseif line =~ '^$'
" Blank line. " Blank line.
let snippet_pattern.word .= "\n" let snippet_dict.word .= "\n"
endif endif
endif endif
let linenr += 1 let linenr += 1
endfor endfor
if snippet_pattern.word !~ if !empty(snippet_dict)
\ s:get_placeholder_marker_substitute_pattern() if snippet_dict.word !~
" Add placeholder. \ s:get_placeholder_marker_substitute_pattern()
let snippet_pattern.word .= '${0}' " Add placeholder.
let snippet_dict.word .= '${0}'
endif
" Set previous snippet.
call s:set_snippet_dict(snippet_dict,
\ a:snippets, dup_check, a:snippets_file)
endif endif
" Set previous snippet. return a:snippets
call s:set_snippet_dict(snippet_pattern,
\ a:snippet, dup_check, a:snippets_file)
return a:snippet
endfunction"}}} endfunction"}}}
function! s:is_beginning_of_line(cur_text)"{{{ function! s:is_beginning_of_line(cur_text)"{{{

View File

@ -64,7 +64,8 @@ snippet catch
options head options head
catch ${1:/${2:pattern: empty, E484, Vim(cmdname):{errmsg\\}\}/} catch ${1:/${2:pattern: empty, E484, Vim(cmdname):{errmsg\\}\}/}
snippet log snippet echomsg
alias log
options head options head
echomsg string(${1}) echomsg string(${1})

View File

@ -454,6 +454,9 @@ snippet *neosnippet-unite-action-snippet*
============================================================================== ==============================================================================
CHANGELOG *neosnippet-changelog* CHANGELOG *neosnippet-changelog*
2012-10-29
- Improved parse of snippets file.
2012-10-28 2012-10-28
- Improved snipMate compatibility. - Improved snipMate compatibility.
- Improved expand behavior. - Improved expand behavior.