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.
 
 

147 lines
4.0 KiB

  1. "=============================================================================
  2. " FILE: neosnippet.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! unite#sources#neosnippet#define() abort "{{{
  9. let kind = {
  10. \ 'name' : 'neosnippet',
  11. \ 'default_action' : 'expand',
  12. \ 'action_table': {},
  13. \ 'parents': ['jump_list', 'completion'],
  14. \ 'alias_table' : { 'edit' : 'open' },
  15. \ }
  16. call unite#define_kind(kind)
  17. return s:source
  18. endfunction "}}}
  19. " neosnippet source.
  20. let s:source = {
  21. \ 'name': 'neosnippet',
  22. \ 'hooks' : {},
  23. \ 'action_table' : {},
  24. \ }
  25. function! s:source.hooks.on_init(args, context) abort "{{{
  26. let a:context.source__cur_keyword_pos =
  27. \ s:get_keyword_pos(neosnippet#util#get_cur_text())
  28. let a:context.source__snippets =
  29. \ sort(values(neosnippet#helpers#get_completion_snippets()))
  30. endfunction"}}}
  31. function! s:source.gather_candidates(args, context) abort "{{{
  32. return map(copy(a:context.source__snippets), "{
  33. \ 'word' : v:val.word,
  34. \ 'abbr' : printf('%-50s %s', v:val.word, v:val.menu_abbr),
  35. \ 'kind' : 'neosnippet',
  36. \ 'action__complete_word' : v:val.word,
  37. \ 'action__complete_pos' : a:context.source__cur_keyword_pos,
  38. \ 'action__path' : v:val.action__path,
  39. \ 'action__pattern' : v:val.action__pattern,
  40. \ 'source__menu' : v:val.menu_abbr,
  41. \ 'source__snip' : v:val.snip,
  42. \ 'source__snip_ref' : v:val,
  43. \ }")
  44. endfunction "}}}
  45. " Actions "{{{
  46. let s:action_table = {}
  47. let s:action_table.expand = {
  48. \ 'description' : 'expand snippet',
  49. \ }
  50. function! s:action_table.expand.func(candidate) abort "{{{
  51. let cur_text = neosnippet#util#get_cur_text()
  52. let cur_keyword_str = matchstr(cur_text, '\S\+$')
  53. let context = unite#get_context()
  54. call neosnippet#view#_expand(
  55. \ cur_text . a:candidate.action__complete_word[len(cur_keyword_str)],
  56. \ context.col, a:candidate.action__complete_word)
  57. endfunction"}}}
  58. let s:action_table.preview = {
  59. \ 'description' : 'preview snippet',
  60. \ 'is_selectable' : 1,
  61. \ 'is_quit' : 0,
  62. \ }
  63. function! s:action_table.preview.func(candidates) abort "{{{
  64. for snip in a:candidates
  65. echohl String
  66. echo snip.action__complete_word
  67. echohl None
  68. echo snip.source__snip
  69. echo ' '
  70. endfor
  71. endfunction"}}}
  72. let s:action_table.unite__new_candidate = {
  73. \ 'description' : 'add new snippet',
  74. \ 'is_quit' : 1,
  75. \ }
  76. function! s:action_table.unite__new_candidate.func(candidate) abort "{{{
  77. let trigger = unite#util#input('Please input snippet trigger: ')
  78. if trigger == ''
  79. echo 'Canceled.'
  80. return
  81. endif
  82. call unite#take_action('open', a:candidate)
  83. if &filetype != 'snippet'
  84. " Open failed.
  85. return
  86. endif
  87. if getline('$') != ''
  88. " Append line.
  89. call append('$', '')
  90. endif
  91. call append('$', [
  92. \ 'snippet ' . trigger,
  93. \ 'abbr ' . trigger,
  94. \ 'options head',
  95. \ ' '
  96. \ ])
  97. call cursor(line('$'), 0)
  98. call cursor(0, col('$'))
  99. endfunction"}}}
  100. let s:source.action_table = s:action_table
  101. unlet! s:action_table
  102. "}}}
  103. function! unite#sources#neosnippet#start_complete() abort "{{{
  104. if !exists(':Unite')
  105. call neosnippet#util#print_error(
  106. \ 'unite.vim is not installed.')
  107. call neosnippet#util#print_error(
  108. \ 'Please install unite.vim Ver.1.5 or above.')
  109. return ''
  110. endif
  111. return unite#start_complete(['neosnippet'],
  112. \ { 'input': neosnippet#util#get_cur_text(), 'buffer_name' : '' })
  113. endfunction "}}}
  114. function! s:get_keyword_pos(cur_text) abort "{{{
  115. let cur_keyword_pos = match(a:cur_text, '\S\+$')
  116. if cur_keyword_pos < 0
  117. " Empty string.
  118. return len(a:cur_text)
  119. endif
  120. return cur_keyword_pos
  121. endfunction"}}}
  122. let &cpo = s:save_cpo
  123. unlet s:save_cpo
  124. " vim: foldmethod=marker