neosnippet.vim/autoload/neosnippet/parser.vim

457 lines
13 KiB
VimL
Raw Normal View History

"=============================================================================
" FILE: parser.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
"=============================================================================
2016-03-26 10:46:01 +00:00
let s:Cache = neosnippet#util#get_vital().import('System.Cache.Deprecated')
2017-10-01 13:40:17 +00:00
function! neosnippet#parser#_parse_snippets(filename) abort
2015-10-23 05:23:52 +00:00
if !filereadable(a:filename)
call neosnippet#util#print_error(
2015-10-23 05:23:52 +00:00
\ printf('snippet file "%s" is not found.', a:filename))
2014-05-11 08:46:20 +00:00
return {}
endif
2018-01-25 23:39:05 +00:00
if neosnippet#util#is_sudo()
2018-01-25 23:59:10 +00:00
return s:parse(a:filename)[0]
2018-01-25 23:39:05 +00:00
endif
2014-05-11 08:56:27 +00:00
let cache_dir = neosnippet#variables#data_dir()
2016-03-29 22:20:16 +00:00
let snippets = {}
if !s:Cache.check_old_cache(cache_dir, a:filename)
try
let snippets = neosnippet#helpers#json2vim(
\ s:Cache.readfile(cache_dir, a:filename)[0])
catch
endtry
endif
if empty(snippets) || s:Cache.check_old_cache(cache_dir, a:filename)
2015-12-23 12:17:05 +00:00
let [snippets, sourced] = s:parse(a:filename)
2018-01-25 23:39:05 +00:00
if len(snippets) > 5 && !sourced
2015-10-23 05:11:23 +00:00
call s:Cache.writefile(
2016-03-29 22:20:16 +00:00
\ cache_dir, a:filename,
\ [neosnippet#helpers#vim2json(snippets)])
2014-05-11 08:56:27 +00:00
endif
endif
return snippets
2017-10-01 13:40:17 +00:00
endfunction
function! neosnippet#parser#_parse_snippet(filename, trigger) abort
if !filereadable(a:filename)
call neosnippet#util#print_error(
\ printf('snippet file "%s" is not found.', a:filename))
return {}
endif
let snippet_dict = {
\ 'word' : join(readfile(a:filename), "\n\t"),
\ 'name' : a:trigger,
\ 'options' : neosnippet#parser#_initialize_snippet_options()
\ }
return neosnippet#parser#_initialize_snippet(
\ snippet_dict, a:filename, 1, '', a:trigger)
2017-10-01 13:40:17 +00:00
endfunction
2014-05-11 08:46:20 +00:00
2017-10-01 13:40:17 +00:00
function! s:parse(snippets_file) abort
2014-05-11 08:46:20 +00:00
let dup_check = {}
let snippet_dict = {}
let linenr = 1
let snippets = {}
2015-12-23 12:17:05 +00:00
let sourced = 0
2014-05-11 08:46:20 +00:00
for line in readfile(a:snippets_file)
if line =~ '^\h\w*.*\s$'
" Delete spaces.
let line = substitute(line, '\s\+$', '', '')
endif
if line =~ '^#'
" Ignore.
elseif line =~ '^include'
2015-12-23 12:05:12 +00:00
" Include snippets file.
let snippets = extend(snippets, s:include_snippets(
\ [matchstr(line, '^include\s\+\zs.*$')]))
elseif line =~ '^extends'
" Extend snippets files.
let fts = split(matchstr(line, '^extends\s\+\zs.*$'), '\s*,\s*')
for ft in fts
let snippets = extend(snippets, s:include_snippets(
\ [ft.'.snip', ft.'.snippets', ft.'/*']))
endfor
2015-12-23 12:05:12 +00:00
elseif line =~ '^source'
" Source Vim script file.
for file in split(globpath(join(
\ neosnippet#helpers#get_snippets_directory(), ','),
\ matchstr(line, '^source\s\+\zs.*$')), '\n')
execute 'source' fnameescape(file)
2015-12-23 12:17:05 +00:00
let sourced = 1
2015-12-23 12:05:12 +00:00
endfor
elseif line =~ '^delete\s'
let name = matchstr(line, '^delete\s\+\zs.*$')
2014-05-11 08:46:20 +00:00
if name != '' && has_key(snippets, name)
call filter(snippets, 'v:val.real_name !=# name')
endif
elseif line =~ '^snippet\s'
if !empty(snippet_dict)
" Set previous snippet.
call s:set_snippet_dict(snippet_dict,
\ snippets, dup_check, a:snippets_file)
endif
let snippet_dict = s:parse_snippet_name(
\ a:snippets_file, line, linenr, dup_check)
elseif !empty(snippet_dict)
if line =~ '^\s' || line == ''
if snippet_dict.word == ''
" Substitute head tab character.
let line = substitute(line, '^\t', '', '')
endif
let snippet_dict.word .=
2014-05-11 08:46:20 +00:00
\ substitute(line, '^ *', '', '') . "\n"
else
call s:add_snippet_attribute(
\ a:snippets_file, line, linenr, snippet_dict)
endif
endif
let linenr += 1
endfor
if !empty(snippet_dict)
" Set previous snippet.
call s:set_snippet_dict(snippet_dict,
\ snippets, dup_check, a:snippets_file)
endif
2015-12-23 12:17:05 +00:00
return [snippets, sourced]
2017-10-01 13:40:17 +00:00
endfunction
2017-10-01 13:40:17 +00:00
function! s:parse_snippet_name(snippets_file, line, linenr, dup_check) abort
" Initialize snippet dict.
let snippet_dict = {
\ 'word' : '',
\ 'linenr' : a:linenr,
\ 'options' : neosnippet#parser#_initialize_snippet_options()
\ }
" Try using the name without the description (abbr).
let base_name = matchstr(a:line, '^snippet\s\+\zs\S\+')
let snippet_dict.name = base_name
" Fall back to using the name with integer counter,
" but only if the name is a duplicate.
" SnipMate snippets may have duplicate names, but different
" descriptions (abbrs).
let description = matchstr(a:line, '^snippet\s\+\S\+\s\+\zs.*$')
2018-02-18 09:29:42 +00:00
if g:neosnippet#enable_snipmate_compatibility
\ && description != '' && description !=# snippet_dict.name
\ && has_key(a:dup_check, snippet_dict.name)
" Convert description.
let i = 0
while has_key(a:dup_check, snippet_dict.name)
let snippet_dict.name = base_name . '__' . i
let i += 1
endwhile
endif
" Collect the description (abbr) of the snippet, if set on snippet line.
" This is for compatibility with SnipMate-style snippets.
let snippet_dict.abbr = matchstr(a:line,
\ '^snippet\s\+\S\+\s\+\zs.*$')
" Check for duplicated names.
if has_key(a:dup_check, snippet_dict.name)
let dup = a:dup_check[snippet_dict.name]
call neosnippet#util#print_error(printf(
2015-08-15 00:24:17 +00:00
\ '%s:%d is overriding `%s` from %s:%d',
\ a:snippets_file, a:linenr, snippet_dict.name,
\ dup.action__path, dup.action__line))
call neosnippet#util#print_error(printf(
\ 'Please rename the snippet name or use `delete %s`.',
\ snippet_dict.name))
endif
return snippet_dict
2017-10-01 13:40:17 +00:00
endfunction
2017-10-01 13:40:17 +00:00
function! s:add_snippet_attribute(snippets_file, line, linenr, snippet_dict) abort
" Allow overriding/setting of the description (abbr) of the snippet.
" This will override what was set via the snippet line.
if a:line =~ '^abbr\s'
let a:snippet_dict.abbr = matchstr(a:line, '^abbr\s\+\zs.*$')
elseif a:line =~ '^alias\s'
let a:snippet_dict.alias = split(matchstr(a:line,
\ '^alias\s\+\zs.*$'), '[,[:space:]]\+')
elseif a:line =~ '^prev_word\s'
let prev_word = matchstr(a:line,
\ '^prev_word\s\+[''"]\zs.*\ze[''"]$')
if prev_word == '^'
" For backward compatibility.
let a:snippet_dict.options.head = 1
else
call neosnippet#util#print_error(
\ 'prev_word must be "^" character.')
endif
elseif a:line =~ '^regexp\s'
let a:snippet_dict.regexp = matchstr(a:line,
\ '^regexp\s\+[''"]\zs.*\ze[''"]$')
elseif a:line =~ '^options\s\+'
for option in split(matchstr(a:line,
\ '^options\s\+\zs.*$'), '[,[:space:]]\+')
if !has_key(a:snippet_dict.options, option)
call neosnippet#util#print_error(
\ printf('%s:%d', a:snippets_file, a:linenr))
call neosnippet#util#print_error(
2015-08-15 00:24:17 +00:00
\ printf('Invalid option name : "%s"', option))
else
let a:snippet_dict.options[option] = 1
endif
endfor
else
call neosnippet#util#print_error(
\ printf('%s:%d', a:snippets_file, a:linenr))
call neosnippet#util#print_error(
2015-08-15 00:24:17 +00:00
\ printf('Invalid syntax : "%s"', a:line))
endif
2017-10-01 13:40:17 +00:00
endfunction
2017-10-01 13:40:17 +00:00
function! s:set_snippet_dict(snippet_dict, snippets, dup_check, snippets_file) abort
if empty(a:snippet_dict)
return
endif
let action_pattern = '^snippet\s\+' . a:snippet_dict.name . '$'
let snippet = neosnippet#parser#_initialize_snippet(
\ a:snippet_dict, a:snippets_file,
\ 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_dict, 'alias', [])
let alias_snippet = copy(snippet)
let alias_snippet.word = alias
let a:snippets[alias] = alias_snippet
let a:dup_check[alias] = alias_snippet
endfor
2017-10-01 13:40:17 +00:00
endfunction
2017-10-01 13:40:17 +00:00
function! neosnippet#parser#_initialize_snippet(dict, path, line, pattern, name) abort
let a:dict.word = substitute(a:dict.word, '\n\+$', '', '')
2018-01-12 00:31:51 +00:00
if a:dict.word !~
2015-12-19 03:27:49 +00:00
\ neosnippet#get_placeholder_marker_substitute_pattern().'$'
\ && a:dict.word !~
\ neosnippet#get_placeholder_marker_substitute_zero_pattern()
" Add placeholder.
let a:dict.word .= '${0}'
endif
if !has_key(a:dict, 'abbr') || a:dict.abbr == ''
" Set default abbr.
let abbr = substitute(a:dict.word,
\ neosnippet#get_placeholder_marker_pattern(). '\|'.
\ neosnippet#get_mirror_placeholder_marker_pattern().
\ '\|\s\+\|\n\|TARGET', ' ', 'g')
2017-04-23 09:26:07 +00:00
let abbr = substitute(abbr, '\\\(\\\|`\|\$\)', '\1', 'g')
let a:dict.abbr = a:dict.name
else
let abbr = a:dict.abbr
endif
let snippet = {
2018-02-18 10:33:56 +00:00
\ 'word': a:dict.name, 'snip': a:dict.word,
\ 'description': a:dict.word,
\ 'menu_template': abbr,
\ 'menu_abbr': abbr,
\ 'options': a:dict.options,
\ 'real_name': a:name,
\ 'action__path': a:path,
\ 'action__line': a:line,
\ 'action__pattern': a:pattern,
\}
2018-02-18 10:33:56 +00:00
if exists('*json_encode')
let snippet.user_data = json_encode({
\ 'snippet': a:dict.word,
\ 'snippet_trigger': a:dict.name
\ })
endif
if has_key(a:dict, 'regexp')
let snippet.regexp = a:dict.regexp
endif
return snippet
2017-10-01 13:40:17 +00:00
endfunction
2017-10-01 13:40:17 +00:00
function! neosnippet#parser#_initialize_snippet_options() abort
2015-08-21 21:36:11 +00:00
return {
\ 'head' : 0,
\ 'word' :
\ g:neosnippet#expand_word_boundary,
\ 'indent' : 0,
\ 'oneshot' : 0,
\ }
2017-10-01 13:40:17 +00:00
endfunction
2017-10-01 13:40:17 +00:00
function! neosnippet#parser#_get_completed_snippet(completed_item, cur_text, next_text) abort
let item = a:completed_item
2016-11-21 18:51:38 +00:00
if strridx(a:cur_text, item.word) != len(a:cur_text) - len(item.word)
return ''
endif
2016-01-04 03:13:47 +00:00
" Set abbr
2018-02-18 09:53:18 +00:00
let abbrs = []
2018-02-04 09:11:52 +00:00
if get(item, 'info', '') =~# '^.\+('
2018-02-18 09:53:18 +00:00
call add(abbrs, matchstr(item.info, '^\_s*\zs.*'))
endif
2018-02-18 09:53:18 +00:00
if get(item, 'abbr', '') =~# '^.\+('
call add(abbrs, item.abbr)
endif
if get(item, 'menu', '') =~# '^.\+('
call add(abbrs, item.menu)
endif
if item.word =~# '^.\+(' || get(item, 'kind', '') == 'f'
call add(abbrs, item.word)
endif
call map(abbrs, "escape(v:val, '\')")
2018-02-04 09:11:52 +00:00
2018-02-18 09:53:18 +00:00
" () Only supported
let pairs = {'(': ')'}
let no_key = index(keys(pairs), item.word[-1:]) < 0
2018-02-18 09:26:16 +00:00
let word_pattern = '\<' . neosnippet#util#escape_pattern(item.word)
2016-01-04 03:13:47 +00:00
let angle_pattern = word_pattern . '<.\+>(.*)'
2018-02-18 09:53:18 +00:00
let check_pattern = word_pattern . '\%(<.\+>\)\?(.*)'
let abbrs = filter(abbrs, '!no_key || v:val =~# check_pattern')
if empty(abbrs)
2016-01-04 03:13:47 +00:00
return ''
endif
2018-02-18 09:53:18 +00:00
let abbr = abbrs[0]
2016-01-04 03:13:47 +00:00
let key = no_key ? '(' : item.word[-1:]
if a:next_text[:0] ==# key
" Disable auto pair
return ''
endif
let pair = pairs[key]
" Make snippet arguments
let cnt = 1
let snippet = ''
2015-12-26 03:39:45 +00:00
2016-01-04 03:13:47 +00:00
if no_key && abbr !~# angle_pattern
" Auto key
let snippet .= key
endif
2016-01-01 11:53:21 +00:00
if empty(filter(values(pairs), 'stridx(abbr, v:val) > 0'))
" Pairs not found pattern
let snippet .= '${' . cnt . '}'
let cnt += 1
endif
2016-01-04 03:13:47 +00:00
if abbr =~# angle_pattern
2015-12-26 03:39:45 +00:00
" Add angle analysis
let snippet .= '<'
let args = ''
for arg in split(substitute(
\ neosnippet#parser#_get_in_paren('<', '>', abbr),
\ '<\zs.\{-}\ze>', '', 'g'), '[^[]\zs\s*,\s*')
let args .= neosnippet#parser#_conceal_argument(arg, cnt, args)
2015-12-26 03:39:45 +00:00
let cnt += 1
endfor
let snippet .= args
let snippet .= '>'
2016-01-04 03:13:47 +00:00
if no_key
let snippet .= key
endif
2015-12-26 03:39:45 +00:00
endif
let args = ''
2015-12-26 03:12:08 +00:00
for arg in split(substitute(
\ neosnippet#parser#_get_in_paren(key, pair, abbr),
\ key.'\zs.\{-}\ze'.pair . '\|<\zs.\{-}\ze>', '', 'g'),
\ '[^[]\zs\s*,\s*')
2017-06-20 01:07:05 +00:00
if key ==# '(' && (
\ (&filetype ==# 'python' && arg ==# 'self') ||
\ (&filetype ==# 'rust' && arg =~# '\m^&\?\(mut \)\?self$'))
2015-12-26 03:12:08 +00:00
" Ignore self argument
continue
endif
let args .= neosnippet#parser#_conceal_argument(arg, cnt, args)
let cnt += 1
2015-12-26 03:12:08 +00:00
endfor
2015-12-26 03:39:45 +00:00
let snippet .= args
2015-12-26 03:12:08 +00:00
2016-02-06 02:50:02 +00:00
if key != '(' && snippet == ''
let snippet .= '${' . cnt . '}'
let cnt += 1
endif
2016-01-13 22:27:43 +00:00
let snippet .= pair
let snippet .= '${' . cnt . '}'
return snippet
2017-10-01 13:40:17 +00:00
endfunction
2017-10-01 13:40:17 +00:00
function! neosnippet#parser#_get_in_paren(key, pair, str) abort
2015-12-26 03:12:08 +00:00
let s = ''
let level = 0
for c in split(a:str, '\zs')
if c ==# a:key
let level += 1
if level == 1
continue
endif
elseif c ==# a:pair
2017-01-02 01:41:43 +00:00
if level == 1 && (s != '' || a:str =~ '()\s*(.\{-})')
2015-12-26 03:12:08 +00:00
return s
else
let level -= 1
endif
endif
if level > 0
let s .= c
endif
endfor
return ''
2017-10-01 13:40:17 +00:00
endfunction
2015-12-26 03:12:08 +00:00
2017-10-01 13:40:17 +00:00
function! neosnippet#parser#_conceal_argument(arg, cnt, args) abort
let outside = ''
let inside = ''
2018-02-04 09:12:32 +00:00
if a:args != ''
2017-06-20 01:04:58 +00:00
if g:neosnippet#enable_optional_arguments
let inside = ', '
else
let outside = ', '
endif
endif
return printf('%s${%d:#:%s%s}', outside, a:cnt, inside, escape(a:arg, '{}'))
2017-10-01 13:40:17 +00:00
endfunction
function! s:include_snippets(globs) abort
let snippets = {}
for glob in a:globs
for file in split(globpath(join(
\ neosnippet#helpers#get_snippets_directory(), ','), glob), '\n')
call extend(snippets, neosnippet#parser#_parse_snippets(file))
endfor
endfor
return snippets
endfunction