1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
"=============================================================================
" FILE: parser.vim
" AUTHOR:  Shougo Matsushita <Shougo.Matsu@gmail.com>
" 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

let s:Cache = neosnippet#util#get_vital().import('System.Cache.Deprecated')

function! neosnippet#parser#_parse_snippets(filename) abort "{{{
  if !filereadable(a:filename)
    call neosnippet#util#print_error(
          \ printf('snippet file "%s" is not found.', a:filename))
    return {}
  endif

  let cache_dir = neosnippet#variables#data_dir()
  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)
    let [snippets, sourced] = s:parse(a:filename)
    if len(snippets) > 5 && !neosnippet#util#is_sudo() && !sourced
      call s:Cache.writefile(
            \ cache_dir, a:filename,
            \ [neosnippet#helpers#vim2json(snippets)])
    endif
  endif

  return snippets
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)
endfunction"}}}

function! s:parse(snippets_file) abort "{{{
  let dup_check = {}
  let snippet_dict = {}
  let linenr = 1
  let snippets = {}
  let sourced = 0

  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'
      " Include snippets file.
      for file in split(globpath(join(
            \ neosnippet#helpers#get_snippets_directory(), ','),
            \ matchstr(line, '^include\s\+\zs.*$')), '\n')
        let snippets = extend(snippets,
              \ neosnippet#parser#_parse_snippets(file))
      endfor
    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)
        let sourced = 1
      endfor
    elseif line =~ '^delete\s'
      let name = matchstr(line, '^delete\s\+\zs.*$')
      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 .=
              \ 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

  return [snippets, sourced]
endfunction"}}}

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 snippet_dict.name = matchstr(a:line, '^snippet\s\+\zs\S\+')

  " Fall back to using the name and description (abbr) combined.
  " SnipMate snippets may have duplicate names, but different
  " descriptions (abbrs).
  let description = matchstr(a:line, '^snippet\s\+\S\+\s\+\zs.*$')
  if description != '' && description !=# snippet_dict.name
    " Convert description.
    let snippet_dict.name .= '_' .
          \ substitute(substitute(
          \    description, '\W\+', '_', 'g'), '_\+$', '', '')
  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(
          \ '%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
endfunction"}}}

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(
              \ 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(
          \ printf('Invalid syntax : "%s"', a:line))
  endif
endfunction"}}}

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
endfunction"}}}

function! neosnippet#parser#_initialize_snippet(dict, path, line, pattern, name) abort "{{{
  let a:dict.word = substitute(a:dict.word, '\n\+$', '', '')
  if a:dict.word !~ '\n'
        \ && a:dict.word !~
        \    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')
    let a:dict.abbr = a:dict.name
  else
    let abbr = a:dict.abbr
  endif

  let snippet = {
        \ 'word' : a:dict.name, 'snip' : a:dict.word,
        \ 'description' : a:dict.word,
        \ 'menu_template' : abbr,
        \ 'menu_abbr' : abbr,
        \ 'options' : a:dict.options,
        \ 'action__path' : a:path, 'action__line' : a:line,
        \ 'action__pattern' : a:pattern, 'real_name' : a:name,
        \}

  if has_key(a:dict, 'regexp')
    let snippet.regexp = a:dict.regexp
  endif

  return snippet
endfunction"}}}

function! neosnippet#parser#_initialize_snippet_options() abort "{{{
  return {
        \ 'head' : 0,
        \ 'word' :
        \   g:neosnippet#expand_word_boundary,
        \ 'indent' : 0,
        \ 'oneshot' : 0,
        \ }
endfunction"}}}

function! neosnippet#parser#_get_completed_snippet(completed_item, next_text) abort "{{{
  let item = a:completed_item

  if has_key(item, "snippet")
    return item.snippet
  endif

  " Set abbr
  let abbr = (item.abbr != '') ? item.abbr : item.word
  if len(item.menu) > 5
    " Combine menu.
    let abbr .= ' ' . item.menu
  endif
  if item.info != ''
    let abbr = split(item.info, '\n')[0]
  endif
  let pairs = neosnippet#util#get_buffer_config(
      \ &filetype, '',
      \ 'g:neosnippet#completed_pairs', 'g:neosnippet#_completed_pairs', {})
  let word_pattern = neosnippet#util#escape_pattern(item.word)
  let angle_pattern = word_pattern . '<.\+>(.*)'
  let no_key = index(keys(pairs), item.word[-1:]) < 0
  if no_key && abbr !~# word_pattern . '\%(<.\+>\)\?(.*)'
    return ''
  endif

  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 = ''

  if no_key && abbr !~# angle_pattern
    " Auto key
    let snippet .= key
  endif

  if empty(filter(values(pairs), 'stridx(abbr, v:val) > 0'))
    " Pairs not found pattern
    let snippet .= '${' . cnt . '}'
    let cnt += 1
  endif

  if abbr =~# angle_pattern
    " Add angle analysis
    let snippet .= '<'

    let args = ''
    for arg in split(substitute(
          \ neosnippet#parser#_get_in_paren('<', '>', abbr),
          \ '<\zs.\{-}\ze>', '', 'g'), '[^[]\zs\s*,\s*')
      if args != '' && arg !=# '...'
        let args .= ', '
      endif
      let args .= printf('${%d:#:%s%s}',
            \ cnt, ((args != '' && arg ==# '...') ? ', ' : ''),
            \ escape(arg, '{}'))
      let cnt += 1
    endfor
    let snippet .= args
    let snippet .= '>'

    if no_key
      let snippet .= key
    endif
  endif

  let args = ''
  for arg in split(substitute(
        \ neosnippet#parser#_get_in_paren(key, pair, abbr),
        \ key.'\zs.\{-}\ze'.pair . '\|<\zs.\{-}\ze>', '', 'g'),
        \ '[^[]\zs\s*,\s*')
    if key ==# '(' && arg ==# 'self' && &filetype ==# 'python'
      " Ignore self argument
      continue
    endif

    if args != '' && arg !=# '...'
      let args .= ', '
    endif
    let args .= printf('${%d:#:%s%s}',
          \ cnt, ((args != '' && arg ==# '...') ? ', ' : ''),
          \ escape(arg, '{}'))
    let cnt += 1
  endfor
  let snippet .= args

  if key != '(' && snippet == ''
    let snippet .= '${' . cnt . '}'
    let cnt += 1
  endif

  let snippet .= pair
  let snippet .= '${' . cnt . '}'

  return snippet
endfunction"}}}

function! neosnippet#parser#_get_in_paren(key, pair, str) abort "{{{
  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
      if level == 1 && s != ''
        return s
      else
        let level -= 1
      endif
    endif

    if level > 0
      let s .= c
    endif
  endfor

  return ''
endfunction"}}}


let &cpo = s:save_cpo
unlet s:save_cpo

" vim: foldmethod=marker