You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

434 lines
13 KiB

  1. "=============================================================================
  2. " FILE: parser.vim
  3. " AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>
  4. " License: MIT license
  5. "=============================================================================
  6. let s:Cache = neosnippet#util#get_vital().import('System.Cache.Deprecated')
  7. function! neosnippet#parser#_parse_snippets(filename) abort
  8. if !filereadable(a:filename)
  9. call neosnippet#util#print_error(
  10. \ printf('snippet file "%s" is not found.', a:filename))
  11. return {}
  12. endif
  13. let cache_dir = neosnippet#variables#data_dir()
  14. let snippets = {}
  15. if !s:Cache.check_old_cache(cache_dir, a:filename)
  16. try
  17. let snippets = neosnippet#helpers#json2vim(
  18. \ s:Cache.readfile(cache_dir, a:filename)[0])
  19. catch
  20. endtry
  21. endif
  22. if empty(snippets) || s:Cache.check_old_cache(cache_dir, a:filename)
  23. let [snippets, sourced] = s:parse(a:filename)
  24. if len(snippets) > 5 && !neosnippet#util#is_sudo() && !sourced
  25. call s:Cache.writefile(
  26. \ cache_dir, a:filename,
  27. \ [neosnippet#helpers#vim2json(snippets)])
  28. endif
  29. endif
  30. return snippets
  31. endfunction
  32. function! neosnippet#parser#_parse_snippet(filename, trigger) abort
  33. if !filereadable(a:filename)
  34. call neosnippet#util#print_error(
  35. \ printf('snippet file "%s" is not found.', a:filename))
  36. return {}
  37. endif
  38. let snippet_dict = {
  39. \ 'word' : join(readfile(a:filename), "\n\t"),
  40. \ 'name' : a:trigger,
  41. \ 'options' : neosnippet#parser#_initialize_snippet_options()
  42. \ }
  43. return neosnippet#parser#_initialize_snippet(
  44. \ snippet_dict, a:filename, 1, '', a:trigger)
  45. endfunction
  46. function! s:parse(snippets_file) abort
  47. let dup_check = {}
  48. let snippet_dict = {}
  49. let linenr = 1
  50. let snippets = {}
  51. let sourced = 0
  52. for line in readfile(a:snippets_file)
  53. if line =~ '^\h\w*.*\s$'
  54. " Delete spaces.
  55. let line = substitute(line, '\s\+$', '', '')
  56. endif
  57. if line =~ '^#'
  58. " Ignore.
  59. elseif line =~ '^include'
  60. " Include snippets file.
  61. let snippets = extend(snippets, s:include_snippets(
  62. \ [matchstr(line, '^include\s\+\zs.*$')]))
  63. elseif line =~ '^extends'
  64. " Extend snippets files.
  65. let fts = split(matchstr(line, '^extends\s\+\zs.*$'), '\s*,\s*')
  66. for ft in fts
  67. let snippets = extend(snippets, s:include_snippets(
  68. \ [ft.'.snip', ft.'.snippets', ft.'/*']))
  69. endfor
  70. elseif line =~ '^source'
  71. " Source Vim script file.
  72. for file in split(globpath(join(
  73. \ neosnippet#helpers#get_snippets_directory(), ','),
  74. \ matchstr(line, '^source\s\+\zs.*$')), '\n')
  75. execute 'source' fnameescape(file)
  76. let sourced = 1
  77. endfor
  78. elseif line =~ '^delete\s'
  79. let name = matchstr(line, '^delete\s\+\zs.*$')
  80. if name != '' && has_key(snippets, name)
  81. call filter(snippets, 'v:val.real_name !=# name')
  82. endif
  83. elseif line =~ '^snippet\s'
  84. if !empty(snippet_dict)
  85. " Set previous snippet.
  86. call s:set_snippet_dict(snippet_dict,
  87. \ snippets, dup_check, a:snippets_file)
  88. endif
  89. let snippet_dict = s:parse_snippet_name(
  90. \ a:snippets_file, line, linenr, dup_check)
  91. elseif !empty(snippet_dict)
  92. if line =~ '^\s' || line == ''
  93. if snippet_dict.word == ''
  94. " Substitute head tab character.
  95. let line = substitute(line, '^\t', '', '')
  96. endif
  97. let snippet_dict.word .=
  98. \ substitute(line, '^ *', '', '') . "\n"
  99. else
  100. call s:add_snippet_attribute(
  101. \ a:snippets_file, line, linenr, snippet_dict)
  102. endif
  103. endif
  104. let linenr += 1
  105. endfor
  106. if !empty(snippet_dict)
  107. " Set previous snippet.
  108. call s:set_snippet_dict(snippet_dict,
  109. \ snippets, dup_check, a:snippets_file)
  110. endif
  111. return [snippets, sourced]
  112. endfunction
  113. function! s:parse_snippet_name(snippets_file, line, linenr, dup_check) abort
  114. " Initialize snippet dict.
  115. let snippet_dict = {
  116. \ 'word' : '',
  117. \ 'linenr' : a:linenr,
  118. \ 'options' : neosnippet#parser#_initialize_snippet_options()
  119. \ }
  120. " Try using the name without the description (abbr).
  121. let snippet_dict.name = matchstr(a:line, '^snippet\s\+\zs\S\+')
  122. " Fall back to using the name and description (abbr) combined.
  123. " SnipMate snippets may have duplicate names, but different
  124. " descriptions (abbrs).
  125. let description = matchstr(a:line, '^snippet\s\+\S\+\s\+\zs.*$')
  126. if description != '' && description !=# snippet_dict.name
  127. " Convert description.
  128. let snippet_dict.name .= '_' .
  129. \ substitute(substitute(
  130. \ description, '\W\+', '_', 'g'), '_\+$', '', '')
  131. endif
  132. " Collect the description (abbr) of the snippet, if set on snippet line.
  133. " This is for compatibility with SnipMate-style snippets.
  134. let snippet_dict.abbr = matchstr(a:line,
  135. \ '^snippet\s\+\S\+\s\+\zs.*$')
  136. " Check for duplicated names.
  137. if has_key(a:dup_check, snippet_dict.name)
  138. let dup = a:dup_check[snippet_dict.name]
  139. call neosnippet#util#print_error(printf(
  140. \ '%s:%d is overriding `%s` from %s:%d',
  141. \ a:snippets_file, a:linenr, snippet_dict.name,
  142. \ dup.action__path, dup.action__line))
  143. call neosnippet#util#print_error(printf(
  144. \ 'Please rename the snippet name or use `delete %s`.',
  145. \ snippet_dict.name))
  146. endif
  147. return snippet_dict
  148. endfunction
  149. function! s:add_snippet_attribute(snippets_file, line, linenr, snippet_dict) abort
  150. " Allow overriding/setting of the description (abbr) of the snippet.
  151. " This will override what was set via the snippet line.
  152. if a:line =~ '^abbr\s'
  153. let a:snippet_dict.abbr = matchstr(a:line, '^abbr\s\+\zs.*$')
  154. elseif a:line =~ '^alias\s'
  155. let a:snippet_dict.alias = split(matchstr(a:line,
  156. \ '^alias\s\+\zs.*$'), '[,[:space:]]\+')
  157. elseif a:line =~ '^prev_word\s'
  158. let prev_word = matchstr(a:line,
  159. \ '^prev_word\s\+[''"]\zs.*\ze[''"]$')
  160. if prev_word == '^'
  161. " For backward compatibility.
  162. let a:snippet_dict.options.head = 1
  163. else
  164. call neosnippet#util#print_error(
  165. \ 'prev_word must be "^" character.')
  166. endif
  167. elseif a:line =~ '^regexp\s'
  168. let a:snippet_dict.regexp = matchstr(a:line,
  169. \ '^regexp\s\+[''"]\zs.*\ze[''"]$')
  170. elseif a:line =~ '^options\s\+'
  171. for option in split(matchstr(a:line,
  172. \ '^options\s\+\zs.*$'), '[,[:space:]]\+')
  173. if !has_key(a:snippet_dict.options, option)
  174. call neosnippet#util#print_error(
  175. \ printf('%s:%d', a:snippets_file, a:linenr))
  176. call neosnippet#util#print_error(
  177. \ printf('Invalid option name : "%s"', option))
  178. else
  179. let a:snippet_dict.options[option] = 1
  180. endif
  181. endfor
  182. else
  183. call neosnippet#util#print_error(
  184. \ printf('%s:%d', a:snippets_file, a:linenr))
  185. call neosnippet#util#print_error(
  186. \ printf('Invalid syntax : "%s"', a:line))
  187. endif
  188. endfunction
  189. function! s:set_snippet_dict(snippet_dict, snippets, dup_check, snippets_file) abort
  190. if empty(a:snippet_dict)
  191. return
  192. endif
  193. let action_pattern = '^snippet\s\+' . a:snippet_dict.name . '$'
  194. let snippet = neosnippet#parser#_initialize_snippet(
  195. \ a:snippet_dict, a:snippets_file,
  196. \ a:snippet_dict.linenr, action_pattern,
  197. \ a:snippet_dict.name)
  198. let a:snippets[a:snippet_dict.name] = snippet
  199. let a:dup_check[a:snippet_dict.name] = snippet
  200. for alias in get(a:snippet_dict, 'alias', [])
  201. let alias_snippet = copy(snippet)
  202. let alias_snippet.word = alias
  203. let a:snippets[alias] = alias_snippet
  204. let a:dup_check[alias] = alias_snippet
  205. endfor
  206. endfunction
  207. function! neosnippet#parser#_initialize_snippet(dict, path, line, pattern, name) abort
  208. let a:dict.word = substitute(a:dict.word, '\n\+$', '', '')
  209. if a:dict.word !~ '\n'
  210. \ && a:dict.word !~
  211. \ neosnippet#get_placeholder_marker_substitute_pattern().'$'
  212. \ && a:dict.word !~
  213. \ neosnippet#get_placeholder_marker_substitute_zero_pattern()
  214. " Add placeholder.
  215. let a:dict.word .= '${0}'
  216. endif
  217. if !has_key(a:dict, 'abbr') || a:dict.abbr == ''
  218. " Set default abbr.
  219. let abbr = substitute(a:dict.word,
  220. \ neosnippet#get_placeholder_marker_pattern(). '\|'.
  221. \ neosnippet#get_mirror_placeholder_marker_pattern().
  222. \ '\|\s\+\|\n\|TARGET', ' ', 'g')
  223. let abbr = substitute(abbr, '\\\(\\\|`\|\$\)', '\1', 'g')
  224. let a:dict.abbr = a:dict.name
  225. else
  226. let abbr = a:dict.abbr
  227. endif
  228. let snippet = {
  229. \ 'word' : a:dict.name, 'snip' : a:dict.word,
  230. \ 'description' : a:dict.word,
  231. \ 'menu_template' : abbr,
  232. \ 'menu_abbr' : abbr,
  233. \ 'options' : a:dict.options,
  234. \ 'action__path' : a:path, 'action__line' : a:line,
  235. \ 'action__pattern' : a:pattern, 'real_name' : a:name,
  236. \}
  237. if has_key(a:dict, 'regexp')
  238. let snippet.regexp = a:dict.regexp
  239. endif
  240. return snippet
  241. endfunction
  242. function! neosnippet#parser#_initialize_snippet_options() abort
  243. return {
  244. \ 'head' : 0,
  245. \ 'word' :
  246. \ g:neosnippet#expand_word_boundary,
  247. \ 'indent' : 0,
  248. \ 'oneshot' : 0,
  249. \ }
  250. endfunction
  251. function! neosnippet#parser#_get_completed_snippet(completed_item, cur_text, next_text) abort
  252. let item = a:completed_item
  253. if strridx(a:cur_text, item.word) != len(a:cur_text) - len(item.word)
  254. return ''
  255. endif
  256. if has_key(item, 'snippet')
  257. return item.snippet
  258. endif
  259. " Set abbr
  260. let abbr = (item.abbr != '') ? item.abbr : item.word
  261. if len(item.menu) > 5
  262. " Combine menu.
  263. let abbr .= ' ' . item.menu
  264. endif
  265. if item.info != ''
  266. let abbr .= split(item.info, '\n')[0]
  267. endif
  268. let abbr = escape(abbr, '\')
  269. let pairs = neosnippet#util#get_buffer_config(
  270. \ &filetype, '',
  271. \ 'g:neosnippet#completed_pairs', 'g:neosnippet#_completed_pairs', {})
  272. let word_pattern = neosnippet#util#escape_pattern(item.word)
  273. let angle_pattern = word_pattern . '<.\+>(.*)'
  274. let no_key = index(keys(pairs), item.word[-1:]) < 0
  275. if no_key && abbr !~# word_pattern . '\%(<.\+>\)\?(.*)'
  276. return ''
  277. endif
  278. let key = no_key ? '(' : item.word[-1:]
  279. if a:next_text[:0] ==# key
  280. " Disable auto pair
  281. return ''
  282. endif
  283. let pair = pairs[key]
  284. " Make snippet arguments
  285. let cnt = 1
  286. let snippet = ''
  287. if no_key && abbr !~# angle_pattern
  288. " Auto key
  289. let snippet .= key
  290. endif
  291. if empty(filter(values(pairs), 'stridx(abbr, v:val) > 0'))
  292. " Pairs not found pattern
  293. let snippet .= '${' . cnt . '}'
  294. let cnt += 1
  295. endif
  296. if abbr =~# angle_pattern
  297. " Add angle analysis
  298. let snippet .= '<'
  299. let args = ''
  300. for arg in split(substitute(
  301. \ neosnippet#parser#_get_in_paren('<', '>', abbr),
  302. \ '<\zs.\{-}\ze>', '', 'g'), '[^[]\zs\s*,\s*')
  303. let args .= neosnippet#parser#_conceal_argument(arg, cnt, args)
  304. let cnt += 1
  305. endfor
  306. let snippet .= args
  307. let snippet .= '>'
  308. if no_key
  309. let snippet .= key
  310. endif
  311. endif
  312. let args = ''
  313. for arg in split(substitute(
  314. \ neosnippet#parser#_get_in_paren(key, pair, abbr),
  315. \ key.'\zs.\{-}\ze'.pair . '\|<\zs.\{-}\ze>', '', 'g'),
  316. \ '[^[]\zs\s*,\s*')
  317. if key ==# '(' && (
  318. \ (&filetype ==# 'python' && arg ==# 'self') ||
  319. \ (&filetype ==# 'rust' && arg =~# '\m^&\?\(mut \)\?self$'))
  320. " Ignore self argument
  321. continue
  322. endif
  323. let args .= neosnippet#parser#_conceal_argument(arg, cnt, args)
  324. let cnt += 1
  325. endfor
  326. let snippet .= args
  327. if key != '(' && snippet == ''
  328. let snippet .= '${' . cnt . '}'
  329. let cnt += 1
  330. endif
  331. let snippet .= pair
  332. let snippet .= '${' . cnt . '}'
  333. return snippet
  334. endfunction
  335. function! neosnippet#parser#_get_in_paren(key, pair, str) abort
  336. let s = ''
  337. let level = 0
  338. for c in split(a:str, '\zs')
  339. if c ==# a:key
  340. let level += 1
  341. if level == 1
  342. continue
  343. endif
  344. elseif c ==# a:pair
  345. if level == 1 && (s != '' || a:str =~ '()\s*(.\{-})')
  346. return s
  347. else
  348. let level -= 1
  349. endif
  350. endif
  351. if level > 0
  352. let s .= c
  353. endif
  354. endfor
  355. return ''
  356. endfunction
  357. function! neosnippet#parser#_conceal_argument(arg, cnt, args) abort
  358. let outside = ''
  359. let inside = ''
  360. if (a:args != '')
  361. if g:neosnippet#enable_optional_arguments
  362. let inside = ', '
  363. else
  364. let outside = ', '
  365. endif
  366. endif
  367. return printf('%s${%d:#:%s%s}', outside, a:cnt, inside, escape(a:arg, '{}'))
  368. endfunction
  369. function! s:include_snippets(globs) abort
  370. let snippets = {}
  371. for glob in a:globs
  372. for file in split(globpath(join(
  373. \ neosnippet#helpers#get_snippets_directory(), ','), glob), '\n')
  374. call extend(snippets, neosnippet#parser#_parse_snippets(file))
  375. endfor
  376. endfor
  377. return snippets
  378. endfunction