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.
 
 

273 lines
8.3 KiB

  1. "=============================================================================
  2. " FILE: mappings.vim
  3. " AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>
  4. " License: MIT license
  5. "=============================================================================
  6. function! neosnippet#mappings#expandable_or_jumpable() abort
  7. return neosnippet#mappings#expandable() || neosnippet#mappings#jumpable()
  8. endfunction
  9. function! neosnippet#mappings#expandable() abort
  10. " Check snippet trigger.
  11. return neosnippet#mappings#completed_expandable()
  12. \ || neosnippet#helpers#get_cursor_snippet(
  13. \ neosnippet#helpers#get_snippets('i'),
  14. \ neosnippet#util#get_cur_text()) != ''
  15. endfunction
  16. function! neosnippet#mappings#jumpable() abort
  17. " Found snippet placeholder.
  18. return search(neosnippet#get_placeholder_marker_pattern(). '\|'
  19. \ .neosnippet#get_sync_placeholder_marker_pattern(), 'nw') > 0
  20. endfunction
  21. function! neosnippet#mappings#completed_expandable() abort
  22. if !s:enabled_completed_snippet()
  23. return 0
  24. endif
  25. let snippet = neosnippet#parser#_get_completed_snippet(
  26. \ v:completed_item, neosnippet#util#get_cur_text(),
  27. \ neosnippet#util#get_next_text())
  28. return snippet != ''
  29. endfunction
  30. function! s:enabled_completed_snippet() abort
  31. return exists('v:completed_item')
  32. \ && !empty(v:completed_item)
  33. \ && g:neosnippet#enable_completed_snippet
  34. endfunction
  35. function! neosnippet#mappings#_clear_select_mode_mappings() abort
  36. if !g:neosnippet#disable_select_mode_mappings
  37. return
  38. endif
  39. redir => mappings
  40. silent! smap
  41. redir END
  42. for map in map(filter(split(mappings, '\n'),
  43. \ "v:val !~# '^s' && v:val !~ '^\\a*\\s*<\\S\\+>'"),
  44. \ "matchstr(v:val, '^\\a*\\s*\\zs\\S\\+')")
  45. silent! execute 'sunmap' map
  46. silent! execute 'sunmap <buffer>' map
  47. endfor
  48. " Define default select mode mappings.
  49. snoremap <CR> a<BS>
  50. snoremap <BS> a<BS>
  51. snoremap <Del> a<BS>
  52. snoremap <C-h> a<BS>
  53. endfunction
  54. function! neosnippet#mappings#_register_oneshot_snippet() abort
  55. let trigger = input('Please input snippet trigger: ', 'oneshot')
  56. if trigger == ''
  57. return
  58. endif
  59. let selected_text = substitute(
  60. \ neosnippet#helpers#get_selected_text(visualmode(), 1), '\n$', '', '')
  61. call neosnippet#helpers#delete_selected_text(visualmode(), 1)
  62. let base_indent = matchstr(selected_text, '^\s*')
  63. " Delete base_indent.
  64. let selected_text = substitute(selected_text,
  65. \'^' . base_indent, '', 'g')
  66. let neosnippet = neosnippet#variables#current_neosnippet()
  67. let options = neosnippet#parser#_initialize_snippet_options()
  68. let options.word = 1
  69. let options.oneshot = 1
  70. let neosnippet.snippets[trigger] = neosnippet#parser#_initialize_snippet(
  71. \ { 'name' : trigger, 'word' : selected_text, 'options' : options },
  72. \ '', 0, '', trigger)
  73. echo 'Registered trigger : ' . trigger
  74. endfunction
  75. function! neosnippet#mappings#_expand_target() abort
  76. let trigger = input('Please input snippet trigger: ',
  77. \ '', 'customlist,neosnippet#commands#_complete_target_snippets')
  78. let neosnippet = neosnippet#variables#current_neosnippet()
  79. if !has_key(neosnippet#helpers#get_snippets('i'), trigger) ||
  80. \ neosnippet#helpers#get_snippets('i')[trigger].snip !~#
  81. \ neosnippet#get_placeholder_target_marker_pattern()
  82. if trigger != ''
  83. echo 'The trigger is invalid.'
  84. endif
  85. let neosnippet.target = ''
  86. return
  87. endif
  88. call neosnippet#mappings#_expand_target_trigger(trigger)
  89. endfunction
  90. function! neosnippet#mappings#_expand_target_trigger(trigger) abort
  91. let neosnippet = neosnippet#variables#current_neosnippet()
  92. let neosnippet.target = substitute(
  93. \ neosnippet#helpers#get_selected_text(visualmode(), 1), '\n$', '', '')
  94. let line = getpos("'<")[1]
  95. let col = getpos("'<")[2]
  96. call neosnippet#helpers#delete_selected_text(visualmode())
  97. call cursor(line, col)
  98. if col == 1
  99. let cur_text = a:trigger
  100. else
  101. let cur_text = neosnippet#util#get_cur_text()
  102. let cur_text = cur_text[: col-2] . a:trigger . cur_text[col :]
  103. endif
  104. call neosnippet#view#_expand(cur_text, col, a:trigger)
  105. if !neosnippet#mappings#jumpable()
  106. call cursor(0, col('.') - 1)
  107. stopinsert
  108. endif
  109. endfunction
  110. function! neosnippet#mappings#_anonymous(snippet) abort
  111. let [cur_text, col, expr] = neosnippet#mappings#_pre_trigger()
  112. let expr .= printf("\<ESC>:call neosnippet#view#_insert(%s, {}, %s, %d)\<CR>",
  113. \ string(a:snippet), string(cur_text), col)
  114. return expr
  115. endfunction
  116. function! neosnippet#mappings#_expand(trigger) abort
  117. let [cur_text, col, expr] = neosnippet#mappings#_pre_trigger()
  118. let expr .= printf("\<ESC>:call neosnippet#view#_expand(%s, %d, %s)\<CR>",
  119. \ string(cur_text), col, string(a:trigger))
  120. return expr
  121. endfunction
  122. function! s:snippets_expand(cur_text, col) abort
  123. if s:expand_completed_snippets(a:cur_text, a:col)
  124. return 0
  125. endif
  126. let cur_word = neosnippet#helpers#get_cursor_snippet(
  127. \ neosnippet#helpers#get_snippets('i'),
  128. \ a:cur_text)
  129. if cur_word != ''
  130. " Found snippet trigger.
  131. call neosnippet#view#_expand(
  132. \ neosnippet#util#get_cur_text(), a:col, cur_word)
  133. return 0
  134. endif
  135. return 1
  136. endfunction
  137. function! s:expand_completed_snippets(cur_text, col) abort
  138. if !s:enabled_completed_snippet()
  139. return 0
  140. endif
  141. let cur_text = a:cur_text
  142. if !empty(get(g:, 'deoplete#_context', []))
  143. \ && has_key(v:completed_item, 'word')
  144. let completed_candidates = filter(copy(g:deoplete#_context),
  145. \ "has_key(v:val, 'snippet') && has_key(v:val, 'snippet_trigger')
  146. \ && v:val.word ==# v:completed_item.word")
  147. if !empty(completed_candidates)
  148. let v:completed_item.snippet = completed_candidates[0].snippet
  149. let v:completed_item.snippet_trigger =
  150. \ completed_candidates[0].snippet_trigger
  151. endif
  152. endif
  153. let snippet = neosnippet#parser#_get_completed_snippet(
  154. \ v:completed_item, cur_text, neosnippet#util#get_next_text())
  155. if snippet == ''
  156. return 0
  157. endif
  158. if has_key(v:completed_item, 'snippet_trigger')
  159. let cur_text = cur_text[: -1-len(v:completed_item.snippet_trigger)]
  160. endif
  161. call neosnippet#view#_insert(snippet, {}, cur_text, a:col)
  162. return 1
  163. endfunction
  164. function! s:snippets_expand_or_jump(cur_text, col) abort
  165. if s:snippets_expand(a:cur_text, a:col)
  166. call neosnippet#view#_jump('', a:col)
  167. endif
  168. endfunction
  169. function! s:snippets_jump_or_expand(cur_text, col) abort
  170. if search(neosnippet#get_placeholder_marker_pattern(). '\|'
  171. \ .neosnippet#get_sync_placeholder_marker_pattern(), 'nw') > 0
  172. " Found snippet placeholder.
  173. call neosnippet#view#_jump('', a:col)
  174. else
  175. return s:snippets_expand(a:cur_text, a:col)
  176. endif
  177. endfunction
  178. function! s:SID_PREFIX() abort
  179. return matchstr(expand('<sfile>'), '<SNR>\d\+_\ze\w\+$')
  180. endfunction
  181. function! neosnippet#mappings#_trigger(function) abort
  182. let [cur_text, col, expr] = neosnippet#mappings#_pre_trigger()
  183. if !neosnippet#mappings#expandable_or_jumpable()
  184. return ''
  185. endif
  186. let expr .= printf("\<ESC>:call %s(%s,%d)\<CR>",
  187. \ a:function, string(cur_text), col)
  188. return expr
  189. endfunction
  190. function! neosnippet#mappings#_pre_trigger() abort
  191. call neosnippet#init#check()
  192. let cur_text = neosnippet#util#get_cur_text()
  193. let col = col('.')
  194. let expr = ''
  195. if mode() !=# 'i'
  196. " Fix column.
  197. let col += 2
  198. endif
  199. " Get selected text.
  200. let neosnippet = neosnippet#variables#current_neosnippet()
  201. let neosnippet.trigger = 1
  202. if mode() ==# 's' && neosnippet.optional_tabstop
  203. let expr .= "\<C-o>\"_d"
  204. endif
  205. return [cur_text, col, expr]
  206. endfunction
  207. " Plugin key-mappings.
  208. function! neosnippet#mappings#expand_or_jump_impl() abort
  209. return mode() ==# 's' ?
  210. \ neosnippet#mappings#_trigger('neosnippet#view#_jump') :
  211. \ neosnippet#mappings#_trigger(
  212. \ s:SID_PREFIX().'snippets_expand_or_jump')
  213. endfunction
  214. function! neosnippet#mappings#jump_or_expand_impl() abort
  215. return mode() ==# 's' ?
  216. \ neosnippet#mappings#_trigger('neosnippet#view#_jump') :
  217. \ neosnippet#mappings#_trigger(
  218. \ s:SID_PREFIX().'snippets_jump_or_expand')
  219. endfunction
  220. function! neosnippet#mappings#expand_impl() abort
  221. return neosnippet#mappings#_trigger(s:SID_PREFIX().'snippets_expand')
  222. endfunction
  223. function! neosnippet#mappings#jump_impl() abort
  224. return neosnippet#mappings#_trigger('neosnippet#view#_jump')
  225. endfunction