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.
 
 

191 lines
5.4 KiB

  1. "=============================================================================
  2. " FILE: helpers.vim
  3. " AUTHOR: Shougo Matsushita <Shougo.Matsu@gmail.com>
  4. " License: MIT license
  5. "=============================================================================
  6. let s:save_cpo = &cpo
  7. set cpo&vim
  8. function! neosnippet#helpers#get_cursor_snippet(snippets, cur_text) abort "{{{
  9. let cur_word = matchstr(a:cur_text, '\S\+$')
  10. if cur_word != '' && has_key(a:snippets, cur_word)
  11. return cur_word
  12. endif
  13. while cur_word != ''
  14. if has_key(a:snippets, cur_word) &&
  15. \ a:snippets[cur_word].options.word
  16. return cur_word
  17. endif
  18. let cur_word = substitute(cur_word, '^\%(\w\+\|\W\)', '', '')
  19. endwhile
  20. return cur_word
  21. endfunction"}}}
  22. function! neosnippet#helpers#get_snippets(...) abort "{{{
  23. let mode = get(a:000, 0, mode())
  24. call neosnippet#init#check()
  25. let neosnippet = neosnippet#variables#current_neosnippet()
  26. let snippets = copy(neosnippet.snippets)
  27. for filetype in s:get_sources_filetypes(neosnippet#helpers#get_filetype())
  28. call neosnippet#commands#_make_cache(filetype)
  29. call extend(snippets, neosnippet#variables#snippets()[filetype])
  30. endfor
  31. let cur_text = neosnippet#util#get_cur_text()
  32. if mode ==# 'i' || mode ==# 's'
  33. " Special filters.
  34. if !s:is_beginning_of_line(cur_text)
  35. call filter(snippets, '!v:val.options.head')
  36. endif
  37. endif
  38. call filter(snippets, "cur_text =~# get(v:val, 'regexp', '')")
  39. if exists('b:neosnippet_disable_snippet_triggers')
  40. call filter(snippets,
  41. \ "index(b:neosnippet_disable_snippet_triggers, v:val.word) < 0")
  42. endif
  43. return snippets
  44. endfunction"}}}
  45. function! neosnippet#helpers#get_completion_snippets() abort "{{{
  46. return filter(neosnippet#helpers#get_snippets(),
  47. \ "!get(v:val.options, 'oneshot', 0)")
  48. endfunction"}}}
  49. function! neosnippet#helpers#get_snippets_directory() abort "{{{
  50. let snippets_dir = copy(neosnippet#variables#snippets_dir())
  51. if !get(g:neosnippet#disable_runtime_snippets,
  52. \ neosnippet#helpers#get_filetype(),
  53. \ get(g:neosnippet#disable_runtime_snippets, '_', 0))
  54. let snippets_dir += neosnippet#variables#runtime_dir()
  55. endif
  56. return snippets_dir
  57. endfunction"}}}
  58. function! neosnippet#helpers#get_filetype() abort "{{{
  59. " context_filetype.vim installation check.
  60. if !exists('s:exists_context_filetype')
  61. silent! call context_filetype#version()
  62. let s:exists_context_filetype = exists('*context_filetype#version')
  63. endif
  64. let context_filetype =
  65. \ s:exists_context_filetype ?
  66. \ context_filetype#get_filetype() : &filetype
  67. if context_filetype == ''
  68. let context_filetype = 'nothing'
  69. endif
  70. return context_filetype
  71. endfunction"}}}
  72. function! neosnippet#helpers#get_selected_text(type, ...) abort "{{{
  73. let sel_save = &selection
  74. let &selection = 'inclusive'
  75. let reg_save = @@
  76. let pos = getpos('.')
  77. try
  78. " Invoked from Visual mode, use '< and '> marks.
  79. if a:0
  80. silent exe "normal! `<" . a:type . "`>y"
  81. elseif a:type == 'line'
  82. silent exe "normal! '[V']y"
  83. elseif a:type == 'block'
  84. silent exe "normal! `[\<C-v>`]y"
  85. else
  86. silent exe "normal! `[v`]y"
  87. endif
  88. return @@
  89. finally
  90. let &selection = sel_save
  91. let @@ = reg_save
  92. call setpos('.', pos)
  93. endtry
  94. endfunction"}}}
  95. function! neosnippet#helpers#delete_selected_text(type, ...) abort "{{{
  96. let sel_save = &selection
  97. let &selection = 'inclusive'
  98. let reg_save = @@
  99. let pos = getpos('.')
  100. try
  101. " Invoked from Visual mode, use '< and '> marks.
  102. if a:0
  103. silent exe "normal! `<" . a:type . "`>d"
  104. elseif a:type ==# 'V'
  105. silent exe "normal! `[V`]s"
  106. elseif a:type ==# "\<C-v>"
  107. silent exe "normal! `[\<C-v>`]d"
  108. else
  109. silent exe "normal! `[v`]d"
  110. endif
  111. finally
  112. let &selection = sel_save
  113. let @@ = reg_save
  114. call setpos('.', pos)
  115. endtry
  116. endfunction"}}}
  117. function! neosnippet#helpers#substitute_selected_text(type, text) abort "{{{
  118. let sel_save = &selection
  119. let &selection = 'inclusive'
  120. let reg_save = @@
  121. let pos = getpos('.')
  122. try
  123. " Invoked from Visual mode, use '< and '> marks.
  124. if a:0
  125. silent exe "normal! `<" . a:type . "`>s" . a:text
  126. elseif a:type ==# 'V'
  127. silent exe "normal! '[V']hs" . a:text
  128. elseif a:type ==# "\<C-v>"
  129. silent exe "normal! `[\<C-v>`]s" . a:text
  130. else
  131. silent exe "normal! `[v`]s" . a:text
  132. endif
  133. finally
  134. let &selection = sel_save
  135. let @@ = reg_save
  136. call setpos('.', pos)
  137. endtry
  138. endfunction"}}}
  139. function! neosnippet#helpers#vim2json(expr) abort "{{{
  140. return has('patch-7.4.1498') ? js_encode(a:expr) : string(a:expr)
  141. endfunction "}}}
  142. function! neosnippet#helpers#json2vim(expr) abort "{{{
  143. sandbox return has('patch-7.4.1498') ? js_decode(a:expr) : eval(a:expr)
  144. endfunction "}}}
  145. function! s:is_beginning_of_line(cur_text) abort "{{{
  146. let keyword_pattern = '\S\+'
  147. let cur_keyword_str = matchstr(a:cur_text, keyword_pattern.'$')
  148. let line_part = a:cur_text[: -1-len(cur_keyword_str)]
  149. let prev_word_end = matchend(line_part, keyword_pattern)
  150. return prev_word_end <= 0
  151. endfunction"}}}
  152. function! s:get_sources_filetypes(filetype) abort "{{{
  153. let filetypes =
  154. \ exists('*context_filetype#get_filetypes') ?
  155. \ context_filetype#get_filetypes(a:filetype) :
  156. \ split(((a:filetype == '') ? 'nothing' : a:filetype), '\.')
  157. return ['_'] + filetypes
  158. endfunction"}}}
  159. let &cpo = s:save_cpo
  160. unlet s:save_cpo
  161. " vim: foldmethod=marker