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.
 
 

141 lines
3.9 KiB

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