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.
 
 

204 lines
6.0 KiB

  1. "=============================================================================
  2. " FILE: commands.vim
  3. " AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>
  4. " License: MIT license
  5. "=============================================================================
  6. " Variables
  7. let s:edit_options = [
  8. \ '-runtime',
  9. \ '-vertical', '-horizontal', '-direction=', '-split',
  10. \]
  11. let s:Cache = neosnippet#util#get_vital().import('System.Cache.Deprecated')
  12. function! s:get_list() abort
  13. if !exists('s:List')
  14. let s:List = vital#of('neosnippet').import('Data.List')
  15. endif
  16. return s:List
  17. endfunction
  18. function! neosnippet#commands#_edit(args) abort
  19. if neosnippet#util#is_sudo()
  20. call neosnippet#util#print_error(
  21. \ '"sudo vim" is detected. This feature is disabled.')
  22. return
  23. endif
  24. call neosnippet#init#check()
  25. let [args, options] = neosnippet#util#parse_options(
  26. \ a:args, s:edit_options)
  27. let filetype = get(args, 0, '')
  28. if filetype == ''
  29. let filetype = neosnippet#helpers#get_filetype()
  30. endif
  31. let options = s:initialize_options(options)
  32. let snippet_dir = (options.runtime ?
  33. \ get(neosnippet#get_runtime_snippets_directory(), 0, '') :
  34. \ get(neosnippet#get_user_snippets_directory(), -1, ''))
  35. if snippet_dir == ''
  36. call neosnippet#util#print_error('Snippet directory is not found.')
  37. return
  38. endif
  39. if !isdirectory(snippet_dir) && !neosnippet#util#is_sudo()
  40. call mkdir(snippet_dir, 'p')
  41. endif
  42. " Edit snippet file.
  43. let filename = snippet_dir .'/'.filetype
  44. if isdirectory(filename)
  45. " Edit in snippet directory.
  46. let filename .= '/'.filetype
  47. endif
  48. if filename !~ '\.snip*$'
  49. let filename .= '.snip'
  50. endif
  51. if options.split
  52. " Split window.
  53. execute options.direction
  54. \ (options.vertical ? 'vsplit' : 'split')
  55. endif
  56. try
  57. execute 'edit' fnameescape(filename)
  58. catch /^Vim\%((\a\+)\)\=:E749/
  59. endtry
  60. endfunction
  61. function! neosnippet#commands#_make_cache(filetype) abort
  62. call neosnippet#init#check()
  63. let filetype = a:filetype == '' ?
  64. \ &filetype : a:filetype
  65. if filetype ==# ''
  66. let filetype = 'nothing'
  67. endif
  68. let snippets = neosnippet#variables#snippets()
  69. if has_key(snippets, filetype)
  70. return
  71. endif
  72. let snippets[filetype] = {}
  73. let path = join(neosnippet#helpers#get_snippets_directory(), ',')
  74. let cache_dir = neosnippet#variables#data_dir()
  75. for filename in s:get_snippets_files(path, filetype)
  76. " Clear cache file
  77. call s:Cache.deletefile(cache_dir, filename)
  78. let snippets[filetype] = extend(snippets[filetype],
  79. \ neosnippet#parser#_parse_snippets(filename))
  80. endfor
  81. if g:neosnippet#enable_snipmate_compatibility
  82. " Load file snippets
  83. for filename in s:get_snippet_files(path, filetype)
  84. let trigger = fnamemodify(filename, ':t:r')
  85. let snippets[filetype][trigger] =
  86. \ neosnippet#parser#_parse_snippet(filename, trigger)
  87. endfor
  88. endif
  89. endfunction
  90. function! neosnippet#commands#_source(filename) abort
  91. call neosnippet#init#check()
  92. let neosnippet = neosnippet#variables#current_neosnippet()
  93. let neosnippet.snippets = extend(neosnippet.snippets,
  94. \ neosnippet#parser#_parse_snippets(a:filename))
  95. endfunction
  96. function! neosnippet#commands#_clear_markers() abort
  97. let expand_stack = neosnippet#variables#expand_stack()
  98. " Get patterns and count.
  99. if !&l:modifiable || !&l:modified
  100. \ || empty(expand_stack)
  101. \ || neosnippet#variables#current_neosnippet().trigger
  102. return
  103. endif
  104. call neosnippet#view#_clear_markers(expand_stack[-1])
  105. endfunction
  106. " Complete helpers.
  107. function! neosnippet#commands#_edit_complete(arglead, cmdline, cursorpos) abort
  108. return filter(s:edit_options +
  109. \ neosnippet#commands#_filetype_complete(a:arglead, a:cmdline, a:cursorpos),
  110. \ 'stridx(v:val, a:arglead) == 0')
  111. endfunction
  112. function! neosnippet#commands#_filetype_complete(arglead, cmdline, cursorpos) abort
  113. " Dup check.
  114. let ret = {}
  115. for item in map(
  116. \ split(globpath(&runtimepath, 'syntax/*.vim'), '\n') +
  117. \ split(globpath(&runtimepath, 'indent/*.vim'), '\n') +
  118. \ split(globpath(&runtimepath, 'ftplugin/*.vim'), '\n')
  119. \ , 'fnamemodify(v:val, ":t:r")')
  120. if !has_key(ret, item) && item =~ '^'.a:arglead
  121. let ret[item] = 1
  122. endif
  123. endfor
  124. return sort(keys(ret))
  125. endfunction
  126. function! neosnippet#commands#_complete_target_snippets(arglead, cmdline, cursorpos) abort
  127. return map(filter(values(neosnippet#helpers#get_snippets()),
  128. \ "stridx(v:val.word, a:arglead) == 0
  129. \ && v:val.snip =~# neosnippet#get_placeholder_target_marker_pattern()"), 'v:val.word')
  130. endfunction
  131. function! s:initialize_options(options) abort
  132. let default_options = {
  133. \ 'runtime' : 0,
  134. \ 'vertical' : 0,
  135. \ 'direction' : 'below',
  136. \ 'split' : 0,
  137. \ }
  138. let options = extend(default_options, a:options)
  139. " Complex initializer.
  140. if has_key(options, 'horizontal')
  141. " Disable vertically.
  142. let options.vertical = 0
  143. endif
  144. return options
  145. endfunction
  146. function! s:get_snippets_files(path, filetype) abort
  147. let snippets_files = []
  148. for glob in s:get_list().flatten(
  149. \ map(split(get(g:neosnippet#scope_aliases,
  150. \ a:filetype, a:filetype), '\s*,\s*'), "
  151. \ [v:val.'.snip', v:val.'.snippets',
  152. \ v:val.'/**/*.snip', v:val.'/**/*.snippets']
  153. \ + (a:filetype != '_' &&
  154. \ !has_key(g:neosnippet#scope_aliases, a:filetype) ?
  155. \ [v:val . '_*.snip', v:val . '_*.snippets'] : [])"))
  156. let snippets_files += split(globpath(a:path, glob), '\n')
  157. endfor
  158. return reverse(s:get_list().uniq(snippets_files))
  159. endfunction
  160. function! s:get_snippet_files(path, filetype) abort
  161. let snippet_files = []
  162. for glob in s:get_list().flatten(
  163. \ map(split(get(g:neosnippet#scope_aliases,
  164. \ a:filetype, a:filetype), '\s*,\s*'), "
  165. \ [v:val.'/*.snippet']"))
  166. let snippet_files += split(globpath(a:path, glob), '\n')
  167. endfor
  168. return reverse(s:get_list().uniq(snippet_files))
  169. endfunction